📄 geoservertestsupport.java
字号:
request.setContentType( "application/x-www-form-urlencoded" );
MockHttpServletResponse response = dispatch( request );
return new ByteArrayInputStream( response.getOutputStreamContent().getBytes() );
}
/**
* Executes an ows request using the POST method.
* <p>
*
* </p>
* @param path The porition of the request after the context ( no query string ),
* example: 'wms'.
*
* @return An input stream which is the result of the request.
*
* @throws Exception
*/
protected InputStream post( String path , String xml ) throws Exception {
MockHttpServletRequest request = createRequest( path );
request.setMethod( "POST" );
request.setContentType( "application/xml" );
request.setBodyContent(xml);
MockHttpServletResponse response = dispatch( request );
return new ByteArrayInputStream( response.getOutputStreamContent().getBytes() );
}
/**
* Executes an ows request using the GET method and returns the result as an
* xml document.
*
* @param path The porition of the request after hte context,
* example: 'wms?request=GetMap&version=1.1.1&..."
*
* @return A result of the request parsed into a dom.
*
* @throws Exception
*/
protected Document getAsDOM( String path ) throws Exception {
return dom( get( path ) );
}
/**
* Executes an ows request using the POST method with key value pairs
* form encoded, returning the result as a dom.
*
* @param path The porition of the request after hte context,
* example: 'wms?request=GetMap&version=1.1.1&..."
*
* @return An input stream which is the result of the request.
*
* @throws Exception
*/
protected Document postAsDOM( String path ) throws Exception {
return dom( post( path ) );
}
/**
* Executes an ows request using the POST method and returns the result as an
* xml document.
* <p>
*
* </p>
* @param path The porition of the request after the context ( no query string ),
* example: 'wms'.
*
* @return An input stream which is the result of the request.
*
* @throws Exception
*/
protected Document postAsDOM( String path, String xml ) throws Exception {
return dom( post( path, xml ) );
}
protected String getAsString(String path) throws Exception {
return string(get(path));
}
/**
* Parses a stream into a dom.
*/
protected Document dom( InputStream input ) throws Exception {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware( true );
//factory.setValidating( true );
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse( input );
return dom;
}
/**
* Parses a stream into a String
*/
protected String string(InputStream input) throws Exception {
BufferedReader reader = null;
StringBuffer sb = new StringBuffer();
char[] buf = new char[8192];
try {
reader = new BufferedReader(new InputStreamReader(input));
String line = null;
while((line = reader.readLine()) != null)
sb.append(line);
} finally {
if(reader != null)
reader.close();
}
return sb.toString();
}
/**
* Utility method to print out a dom.
*/
protected void print( Document dom ) throws Exception {
TransformerFactory txFactory = TransformerFactory.newInstance();
Transformer tx = txFactory.newTransformer();
tx.setOutputProperty( OutputKeys.INDENT, "yes" );
tx.transform( new DOMSource( dom ), new StreamResult( System.out ) );
}
/**
* Convenience method for element.getElementsByTagName() to return the
* first element in the resulting node list.
*/
protected Element getFirstElementByTagName( Element element, String name ) {
NodeList elements = element.getElementsByTagName(name);
if ( elements.getLength() > 0 ) {
return (Element) elements.item(0);
}
return null;
}
/**
* Convenience method for element.getElementsByTagName() to return the
* first element in the resulting node list.
*/
protected Element getFirstElementByTagName( Document dom, String name ) {
return getFirstElementByTagName( dom.getDocumentElement(), name );
}
/**
* Sets up a template in a feature type directory.
*
* @param featureTypeName The name of the feature type.
* @param template The name of the template.
* @param body The content of the template.
*
* @throws IOException
*/
protected void setupTemplate(QName featureTypeName,String template,String body)
throws IOException {
dataDirectory.copyToFeatureTypeDirectory( new ByteArrayInputStream(body.getBytes()), featureTypeName, template );
}
/*
* Helper method to create the kvp params from the query string.
*/
private void kvp(MockHttpServletRequest request, String path) {
int index = path.indexOf('?');
if (index == -1) {
return;
}
String queryString = path.substring(index + 1);
StringTokenizer st = new StringTokenizer(queryString, "&");
while (st.hasMoreTokens()) {
String token = st.nextToken();
String[] keyValuePair = token.split("=");
//check for any special characters
if ( keyValuePair.length > 1 ) {
//replace any equals or & characters
keyValuePair[1] = keyValuePair[1].replaceAll( "%3D", "=" );
keyValuePair[1] = keyValuePair[1].replaceAll( "%3d", "=" );
keyValuePair[1] = keyValuePair[1].replaceAll( "%23", "&" );
}
request.setupAddParameter(keyValuePair[0], keyValuePair.length > 1 ? keyValuePair[1]: "");
}
}
/*
* Helper method for dispatching an executing an ows request.
*/
private MockHttpServletResponse dispatch( MockHttpServletRequest request ) throws Exception {
//create the response
//final MockServletOutputStream output = new MockServletOutputStream();
MockHttpServletResponse response = new MockHttpServletResponse() {
public void setCharacterEncoding( String encoding ) {
}
// public ServletOutputStream getOutputStream() throws IOException {
// return output;
// }
};
//look up the handler
Dispatcher dispatcher =
(Dispatcher) applicationContext.getBean( "dispatcher" );
//dispatcher.setApplicationContext( getGeoServer().getApplicationContext() );
//excute the pre handler step
Collection interceptors =
GeoServerExtensions.extensions(HandlerInterceptor.class, applicationContext );
for ( Iterator i = interceptors.iterator(); i.hasNext(); ) {
HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
interceptor.preHandle( request, response, dispatcher );
}
//execute
dispatcher.handleRequest( request, response );
//execute the post handler step
for ( Iterator i = interceptors.iterator(); i.hasNext(); ) {
HandlerInterceptor interceptor = (HandlerInterceptor) i.next();
interceptor.postHandle( request, response, dispatcher, null );
}
return response;
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -