📄 abstractservice.java
字号:
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.vfny.geoserver.servlets;
import org.geoserver.ows.ServiceStrategy;
import org.geoserver.ows.util.XmlCharsetDetector;
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.web.context.WebApplicationContext;
import org.vfny.geoserver.ExceptionHandler;
import org.vfny.geoserver.Request;
import org.vfny.geoserver.Response;
import org.vfny.geoserver.ServiceException;
import org.vfny.geoserver.global.Data;
import org.vfny.geoserver.global.GeoServer;
import org.vfny.geoserver.global.Service;
import org.vfny.geoserver.util.PartialBufferedOutputStream2;
import org.vfny.geoserver.util.requests.readers.KvpRequestReader;
import org.vfny.geoserver.util.requests.readers.XmlRequestReader;
import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStream;
import java.io.Reader;
import java.net.SocketException;
import java.nio.charset.Charset;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* Represents a service that all others extend from. Subclasses should provide
* response and exception handlers as appropriate.
*
* <p>
* It is <b>really</b> important to adhere to the following workflow:
*
* <ol>
* <li>
* get a Request reader
* </li>
* <li>
* ask the Request Reader for the Request object
* </li>
* <li>
* Provide the resulting Request with the ServletRequest that generated it
* </li>
* <li>
* get the appropiate ResponseHandler
* </li>
* <li>
* ask it to execute the Request
* </li>
* <li>
* set the response content type
* </li>
* <li>
* write to the http response's output stream
* </li>
* <li>
* pending - call Response cleanup
* </li>
* </ol>
* </p>
*
* <p>
* If anything goes wrong a ServiceException can be thrown and will be written
* to the output stream instead.
* </p>
*
* <p>
* This is because we have to be sure that no exception have been produced
* before setting the response's content type, so we can set the exception
* specific content type; and that Response.getContentType is called AFTER
* Response.execute, since the MIME type can depend on any request parameter
* or another kind of desission making during the execute process. (i.e.
* FORMAT in WMS GetMap)
* </p>
*
* <p>
* TODO: We need to call Response.abort() if anything goes wrong to allow the
* Response a chance to cleanup after itself.
* </p>
*
* @author Gabriel Rold?n
* @author Chris Holmes
* @author Jody Garnett, Refractions Research
* @version $Id: AbstractService.java 7746 2007-11-13 15:38:35Z aaime $
*/
public abstract class AbstractService extends HttpServlet implements ApplicationContextAware {
/** Class logger */
protected static Logger LOGGER = org.geotools.util.logging.Logging.getLogger("org.vfny.geoserver.servlets");
/**
* Servivce group (maps to 'SERVICE' parameter in OGC service urls)
*/
String service;
/**
* Request type (maps to 'REQUEST' parameter in OGC service urls)
*/
String request;
/**
* Application context used to look up "Services"
*/
WebApplicationContext context;
/**
* Reference to the global geoserver instnace.
*/
GeoServer geoServer;
/**
* Reference to the catalog.
*/
Data catalog;
/**
* Id of the service strategy to use.
*/
String serviceStrategy;
/**
* buffer size to use when PARTIAL-BUFFER is being used
*/
int partialBufferSize;
/**
* Cached service strategy object
*/
// ServiceStrategy strategy;
/**
* Reference to the service
*/
Service serviceRef;
private String kvpString;
// /** DOCUMENT ME! */
// protected HttpServletRequest curRequest;
/**
* Constructor for abstract service.
*
* @param service The service group the service falls into (WFS,WMS,...)
* @param request The service being requested (GetCapabilities, GetMap, ...)
* @param serviceRef The global service this "servlet" falls into
*/
public AbstractService(String service, String request, Service serviceRef) {
this.service = service;
this.request = request;
this.serviceRef = serviceRef;
}
/**
* @return Returns the "service group" that this service falls into.
*/
public String getService() {
return service;
}
/**
* @return Returns the "request" this service maps to.
*/
public String getRequest() {
return request;
}
/**
* Sets a refeference to the global service instance.
*/
public void setServiceRef(Service serviceRef) {
this.serviceRef = serviceRef;
}
/**
* @return The reference to the global service instance.
*/
public Service getServiceRef() {
return serviceRef;
}
/**
* Sets the application context.
* <p>
* Used to process the {@link Service} extension point.
* </p>
*/
public void setApplicationContext(ApplicationContext context)
throws BeansException {
this.context = (WebApplicationContext) context;
}
/**
* @return The application context.
*/
public WebApplicationContext getApplicationContext() {
return context;
}
/**
* Sets the reference to the global geoserver instance.
*/
public void setGeoServer(GeoServer geoServer) {
this.geoServer = geoServer;
}
/**
* @return the reference to the global geoserver instance.
*/
public GeoServer getGeoServer() {
return geoServer;
}
/**
* @return The reference to the global catalog instance.
*/
public Data getCatalog() {
return catalog;
}
/**
* Sets the reference to the global catalog instance.
*
*/
public void setCatalog(Data catalog) {
this.catalog = catalog;
}
/**
* @return The id used to identify the service strategy to be used.
* @see ServiceStrategy#getId()
*/
public String getServiceStrategy() {
return serviceStrategy;
}
/**
* Sets the id used to identify the service strategy to be used.
*/
public void setServiceStrategy(String serviceStrategy) {
this.serviceStrategy = serviceStrategy;
}
/**
* Determines if the service is enabled.
* <p>
* Subclass should override this method if the service can be turned on/off.
* This implementation returns <code>true</code>
* </p>
*/
protected boolean isServiceEnabled(HttpServletRequest req) {
return true;
}
/**
* Override and use spring set servlet context.
*/
public ServletContext getServletContext() {
//override and use spring
return ((WebApplicationContext) context).getServletContext();
}
/**
* DOCUMENT ME!
*
* @param request DOCUMENT ME!
* @param response DOCUMENT ME!
*
* @throws ServletException DOCUMENT ME!
* @throws IOException DOCUMENT ME!
*/
public void doGet(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
// implements the main request/response logic
// this.curRequest = request;
Request serviceRequest = null;
if (!isServiceEnabled(request)) {
sendDisabledServiceError(response);
return;
}
try {
Map requestParams = new HashMap();
String qString = ((this.kvpString != null) ? this.kvpString : request.getQueryString());
LOGGER.fine("reading request: " + qString);
if (this.kvpString != null) {
requestParams = KvpRequestReader.parseKvpSet(qString);
} else {
String paramName;
String paramValue;
for (Enumeration pnames = request.getParameterNames(); pnames.hasMoreElements();) {
paramName = (String) pnames.nextElement();
paramValue = request.getParameter(paramName);
requestParams.put(paramName.toUpperCase(), paramValue);
}
}
KvpRequestReader requestReader = getKvpReader(requestParams);
serviceRequest = requestReader.getRequest(request);
LOGGER.finer("serviceRequest provided with HttpServletRequest: " + request);
//serviceRequest.setHttpServletRequest(request);
} catch (ServiceException se) {
sendError(request, response, se);
return;
} catch (Throwable e) {
sendError(request, response, e);
return;
} finally {
this.kvpString = null;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -