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.wfs.response;
006:
007: import org.geoserver.ows.DefaultServiceExceptionHandler;
008: import org.geoserver.ows.util.ResponseUtils;
009: import org.geoserver.platform.Service;
010: import org.geoserver.platform.ServiceException;
011: import org.geoserver.wfs.WFS;
012: import org.geotools.util.Version;
013: import java.io.ByteArrayOutputStream;
014: import java.io.IOException;
015: import java.io.PrintStream;
016: import java.util.List;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.servlet.http.HttpServletResponse;
019:
020: /**
021: * Handles a wfs service exception by producing an exception report.
022: *
023: * @author Justin Deoliveira, The Open Planning Project
024: *
025: */
026: public class WfsExceptionHandler extends DefaultServiceExceptionHandler {
027: /**
028: * WFS configuration
029: */
030: WFS wfs;
031:
032: /**
033: * @param service The wfs service descriptors.
034: */
035: public WfsExceptionHandler(List services, WFS wfs) {
036: super (services);
037: this .wfs = wfs;
038: }
039:
040: /**
041: * Encodes a ogc:ServiceExceptionReport to output.
042: */
043: public void handleServiceException(ServiceException e,
044: Service service, HttpServletRequest request,
045: HttpServletResponse response) {
046: Version version = service.getVersion();
047:
048: verboseExceptions = wfs.getGeoServer().isVerboseExceptions();
049: if (new Version("1.0.0").equals(version)) {
050: handle1_0(e, response);
051: } else {
052: super .handleServiceException(e, service, request, response);
053: }
054: }
055:
056: public void handle1_0(ServiceException e,
057: HttpServletResponse response) {
058: try {
059: String tab = " ";
060:
061: StringBuffer s = new StringBuffer();
062: s.append("<?xml version=\"1.0\" ?>\n");
063: s.append("<ServiceExceptionReport\n");
064: s.append(tab + "version=\"1.2.0\"\n");
065: s.append(tab + "xmlns=\"http://www.opengis.net/ogc\"\n");
066: s
067: .append(tab
068: + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n");
069: s.append(tab);
070: s
071: .append("xsi:schemaLocation=\"http://www.opengis.net/ogc ");
072: s.append(ResponseUtils.appendPath(wfs.getSchemaBaseURL(),
073: "wfs/1.0.0/OGC-exception.xsd")
074: + "\">\n");
075:
076: s.append(tab + "<ServiceException");
077:
078: if ((e.getCode() != null) && !e.getCode().equals("")) {
079: s.append(" code=\"" + e.getCode() + "\"");
080: }
081:
082: if ((e.getLocator() != null) && !e.getLocator().equals("")) {
083: s.append(" locator=\"" + e.getLocator() + "\"");
084: }
085:
086: s.append(">");
087:
088: if (e.getMessage() != null) {
089: s.append("\n" + tab + tab);
090: dumpExceptionMessages(e, s);
091:
092: if (verboseExceptions) {
093: ByteArrayOutputStream stackTrace = new ByteArrayOutputStream();
094: e.printStackTrace(new PrintStream(stackTrace));
095:
096: s.append("\nDetails:\n");
097: s.append(ResponseUtils.encodeXML(new String(
098: stackTrace.toByteArray())));
099: }
100: }
101:
102: s.append("\n</ServiceException>");
103: s.append("</ServiceExceptionReport>");
104:
105: response.setContentType("text/xml");
106: response.setCharacterEncoding("UTF-8");
107: response.getOutputStream().write(s.toString().getBytes());
108: response.getOutputStream().flush();
109: } catch (IOException ioe) {
110: throw new RuntimeException(ioe);
111: }
112: }
113:
114: // public void handle1_1( ServiceException e,
115: // HttpServletResponse response ) {
116: //
117: // try {
118: // String tab = " ";
119: //
120: // StringBuffer s = new StringBuffer();
121: // s.append( "<?xml version=\"1.0\" ?>\n" );
122: // s.append( "<ExceptionReport\n" );
123: // s.append( tab + "version=\"1.1.0\"\n" );
124: // s.append( tab + "xmlns=\"http://www.opengis.net/ows\"\n" );
125: // s.append( tab + "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"\n" );
126: // s.append( tab );
127: // s.append( "xsi:schemaLocation=\"http://www.opengis.net/ows " );
128: // //TODO: dont hardcode schmea location
129: // s.append( "http://schemas.opengis.net/ows/1.0.0/owsExceptionReport.xsd\">\n" );
130: //
131: // s.append( tab + "<Exception" );
132: // s.append( " exceptionCode=\"" + e.getCode() + "\"" );
133: //
134: // if ( e.getLocator() != null && !e.getLocator().equals( "" ) ) {
135: // s.append(" locator=\"" + e.getLocator() + "\"" );
136: // }
137: // s.append( ">");
138: //
139: // if ( e.getMessage() != null && !e.getMessage().equals( "" ) ) {
140: // s.append( "\n" + tab + tab );
141: // s.append( "<ExceptionText>\n");
142: // s.append( ResponseUtils.encodeXML( e.getMessage() ) );
143: //
144: // ByteArrayOutputStream stackTrace = new ByteArrayOutputStream();
145: // e.printStackTrace( new PrintStream( stackTrace ) );
146: //
147: // s.append( ResponseUtils.encodeXML( new String( stackTrace.toByteArray() ) ) );
148: // s.append( "</ExceptionText>\n");
149: // }
150: //
151: //
152: // s.append( "\n</Exception>" );
153: // s.append( "</ExceptionReport>" );
154: //
155: // response.setContentType( "text/xml" );
156: // response.setCharacterEncoding( "UTF-8" );
157: // response.getOutputStream().write( s.toString().getBytes() );
158: // response.getOutputStream().flush();
159: //
160: // }
161: // catch (IOException ioe) {
162: // throw new RuntimeException( ioe );
163: // }
164: // }
165: }
|