001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.geoserver.ows;
006:
007: import net.opengis.ows.ExceptionReportType;
008: import net.opengis.ows.ExceptionType;
009: import net.opengis.ows.OwsFactory;
010: import org.apache.xml.serialize.OutputFormat;
011: import org.geoserver.ows.util.RequestUtils;
012: import org.geoserver.ows.xml.v1_0.OWSConfiguration;
013: import org.geoserver.platform.Service;
014: import org.geoserver.platform.ServiceException;
015: import org.geotools.xml.Encoder;
016: import java.io.ByteArrayOutputStream;
017: import java.io.IOException;
018: import java.io.PrintStream;
019: import java.util.Collections;
020: import java.util.List;
021: import java.util.logging.Level;
022:
023: import javax.servlet.http.HttpServletRequest;
024: import javax.servlet.http.HttpServletResponse;
025:
026: /**
027: * A default implementation of {@link ServiceExceptionHandler} which outputs
028: * as service exception in a <code>ows:ExceptionReport</code> document.
029: * <p>
030: * This service exception handler will generate an OWS exception report,
031: * see {@linkplain http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd}.
032: * </p>
033: *
034: * @author Justin Deoliveira, The Open Planning Project
035: *
036: */
037: public class DefaultServiceExceptionHandler extends
038: ServiceExceptionHandler {
039: protected boolean verboseExceptions = false;
040:
041: /**
042: * Constructor to be called if the exception is not for a particular service.
043: *
044: */
045: public DefaultServiceExceptionHandler() {
046: super (Collections.EMPTY_LIST);
047: }
048:
049: /**
050: * Constructor to be called if the exception is for a particular service.
051: *
052: * @param services List of services this handler handles exceptions for.
053: */
054: public DefaultServiceExceptionHandler(List services) {
055: super (services);
056: }
057:
058: /**
059: * Writes out an OWS ExceptionReport document.
060: */
061: public void handleServiceException(ServiceException exception,
062: Service service, HttpServletRequest request,
063: HttpServletResponse response) {
064: OwsFactory factory = OwsFactory.eINSTANCE;
065:
066: ExceptionType e = factory.createExceptionType();
067:
068: if (exception.getCode() != null) {
069: e.setExceptionCode(exception.getCode());
070: } else {
071: //set a default
072: e.setExceptionCode("NoApplicableCode");
073: }
074:
075: e.setLocator(exception.getLocator());
076:
077: //add the message
078: StringBuffer sb = new StringBuffer();
079: dumpExceptionMessages(exception, sb);
080: e.getExceptionText().add(sb.toString());
081: e.getExceptionText().addAll(exception.getExceptionText());
082:
083: if (verboseExceptions) {
084: //add the entire stack trace
085: //exception.
086: e.getExceptionText().add("Details:");
087: ByteArrayOutputStream trace = new ByteArrayOutputStream();
088: exception.printStackTrace(new PrintStream(trace));
089: e.getExceptionText().add(new String(trace.toByteArray()));
090: }
091:
092: ExceptionReportType report = factory
093: .createExceptionReportType();
094: report.setVersion("1.0.0");
095: report.getException().add(e);
096:
097: response.setContentType("application/xml");
098:
099: //response.setCharacterEncoding( "UTF-8" );
100: OWSConfiguration configuration = new OWSConfiguration();
101:
102: OutputFormat format = new OutputFormat();
103: format.setIndenting(true);
104: format.setIndent(2);
105: format.setLineWidth(60);
106:
107: Encoder encoder = new Encoder(configuration, configuration
108: .schema());
109: encoder.setOutputFormat(format);
110:
111: encoder.setSchemaLocation(
112: org.geoserver.ows.xml.v1_0.OWS.NAMESPACE, RequestUtils
113: .schemaBaseURL(request)
114: + "ows/1.0.0/owsExceptionReport.xsd");
115:
116: try {
117: encoder.encode(report,
118: org.geoserver.ows.xml.v1_0.OWS.EXCEPTIONREPORT,
119: response.getOutputStream());
120: } catch (Exception ex) {
121: //throw new RuntimeException(ex);
122: // 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...
123: LOGGER
124: .log(
125: Level.INFO,
126: "Problem writing exception information back to calling client:",
127: e);
128: } finally {
129: try {
130: response.getOutputStream().flush();
131: } catch (IOException ioe) {
132: }
133: }
134: }
135: }
|