org.geoserver.ows.LegacyServiceExceptionHandler Maven / Gradle / Ivy
/* 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.geoserver.ows;
import org.geoserver.ows.util.ResponseUtils;
import org.geoserver.platform.Service;
import org.geoserver.platform.ServiceException;
import org.vfny.geoserver.global.GeoServer;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintStream;
import java.util.List;
import java.util.logging.Level;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
/**
* An implementation of {@link ServiceExceptionHandler} which outputs
* as service exception in a ServiceExceptionReport
document.
*
* This handler is referred to as "legacy" as newer services move to the ows
* style exception report. See {@link DefaultServiceExceptionHandler}.
*
*
*
Version
* By default this exception handler will output a ServiceExceptionReport
* which is of version 1.2.0
. This may be overriden with
* {@link #setVersion(String)}.
*
*
*
DTD and Schema
* By default, no DTD or XML Schema reference will be included in the document.
* The methods {@link #setDTDLocation(String)} and {@link #setSchemaLocation(String)}
* can be used to override this behaviour. Only one of these methods should be
* set per instance of this class.
*
* The supplied value should be relative, and will be appended to the result
* of {@link OWS#getSchemaBaseURL()}.
*
*
*
Content Type
* The default content type for the created document is text/xml
,
* this can be overridden with {@link #setContentType(String)}.
*
*
* @author Justin Deoliveira, The Open Planning Project
*
*/
public class LegacyServiceExceptionHandler extends ServiceExceptionHandler {
/**
* The configuration of hte service.
*/
OWS ows;
/**
* the version of the service exceptoin report.
*/
String version = "1.2.0";
/**
* Location of document type defintion for document
*/
String dtdLocation = null;
/**
* Location of schema for document.
*/
String schemaLocation = null;
/**
* The content type of the produced document
*/
String contentType = "text/xml";
/**
* The central configuration, used to decide whether to dump a verbose stack trace, or not
*/
GeoServer geoServer;
public LegacyServiceExceptionHandler(List services, OWS ows, GeoServer geoServer) {
super(services);
this.ows = ows;
this.geoServer = geoServer;
}
public LegacyServiceExceptionHandler(Service service, OWS ows, GeoServer geoServer) {
super(service);
this.ows = ows;
this.geoServer = geoServer;
}
public void setVersion(String version) {
this.version = version;
}
public void setDTDLocation(String dtd) {
this.dtdLocation = dtd;
}
public void setSchemaLocation(String schemaLocation) {
this.schemaLocation = schemaLocation;
}
public void setContentType(String contentType) {
this.contentType = contentType;
}
public void handleServiceException(ServiceException exception, Service service,
HttpServletRequest request, HttpServletResponse response) {
String tab = " ";
StringBuffer sb = new StringBuffer();
//xml header TODO: should the encoding the server default?
sb.append("");
//dtd location
if (dtdLocation != null) {
String fullDtdLocation = ResponseUtils.appendPath(ows.getSchemaBaseURL(), dtdLocation);
sb.append(" ");
}
//root element
sb.append("");
//write out the service exception
sb.append(tab + "");
//message
if ((exception.getMessage() != null)) {
sb.append("\n" + tab + tab);
dumpExceptionMessages(exception, sb);
if(geoServer.isVerboseExceptions()) {
ByteArrayOutputStream stackTrace = new ByteArrayOutputStream();
exception.printStackTrace(new PrintStream(stackTrace));
sb.append("\nDetails:\n");
sb.append(ResponseUtils.encodeXML(new String(stackTrace.toByteArray())));
}
}
sb.append("\n ");
sb.append(" ");
response.setContentType(contentType);
//TODO: server encoding?
response.setCharacterEncoding("UTF-8");
try {
response.getOutputStream().write(sb.toString().getBytes());
response.getOutputStream().flush();
} catch (IOException e) {
//throw new RuntimeException(e);
// Hmm, not much we can do here. I guess log the fact that we couldn't write out the exception and be done with it...
LOGGER.log(Level.INFO, "Problem writing exception information back to calling client:", e);
}
}
}