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.vfny.geoserver.wcs;
006:
007: import org.vfny.geoserver.ServiceException;
008: import org.vfny.geoserver.global.GeoServer;
009: import org.vfny.geoserver.util.Requests;
010: import java.util.logging.Logger;
011: import javax.servlet.http.HttpServletRequest;
012:
013: /**
014: * This defines an exception that can be turned into a valid xml service
015: * exception that wcs clients will expect. All errors should be wrapped in this
016: * before returning to clients.
017: *
018: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
019: * modification)
020: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
021: * modification)
022: * @version $Id: WcsException.java 8479 2008-02-28 19:11:04Z aaime $
023: */
024: public class WcsException extends ServiceException {
025: /** Class logger */
026: private static Logger LOGGER = org.geotools.util.logging.Logging
027: .getLogger("org.vfny.geoserver.wcs");
028:
029: /**
030: * The fixed MIME type of a WCS exception.
031: */
032: private static final String SE_XML = "application/vnd.ogc.se_xml";
033:
034: /**
035: * Empty constructor.
036: */
037: public WcsException() {
038: super ();
039: }
040:
041: /**
042: * Message constructor.
043: *
044: * @param message The message for the .
045: */
046: public WcsException(String message) {
047: super (message);
048: }
049:
050: /**
051: * Throwable constructor.
052: *
053: * @param e The message for the .
054: */
055: public WcsException(Throwable e) {
056: super (e);
057: }
058:
059: /**
060: * Message Locator constructor.
061: *
062: * @param message The message for the .
063: * @param locator The java class that caused the problem
064: */
065: public WcsException(String message, String locator) {
066: super (message, locator);
067: }
068:
069: /**
070: * Message code and locator constructor.
071: *
072: * @param message The message for the .
073: * @param locator The java class that caused the problem
074: */
075: public WcsException(String message, String locator, String code) {
076: super (message, locator);
077: setCode(code);
078: }
079:
080: public WcsException(String message, Throwable cause) {
081: super (message, cause);
082: }
083:
084: /**
085: * DOCUMENT ME!
086: *
087: * @param e The cause of failure
088: * @param preMessage The message to tack on the front.
089: * @param locator The java class that caused the problem
090: */
091: public WcsException(Throwable e, String preMessage, String locator) {
092: super (e, preMessage, locator);
093: }
094:
095: /**
096: * Return request type.
097: *
098: * @param printStackTrace whether the stack trace should be included.
099: * @param request DOCUMENT ME!
100: *
101: * @return The ServiceExceptionReport of this error.
102: *
103: * @task REVISIT: Our error handling should actually have knowledge of the
104: * app configuration, so that we can set the ogc error report to
105: * validate right (reference our own schema), and to put the correct
106: * mime type here.
107: */
108: public String getXmlResponse(boolean printStackTrace,
109: HttpServletRequest request, GeoServer geoserver) {
110: //Perhaps not the best place to do this, but it's by far the best place to ensure
111: //that all logged errors get recorded in the same way, as there all must return
112: //xml responses.
113: LOGGER.warning("encountered error: " + getMessage());
114:
115: String indent = " ";
116:
117: StringBuffer returnXml = new StringBuffer(
118: "<?xml version=\"1.0\" ?>\n");
119:
120: returnXml.append("<ServiceExceptionReport\n");
121:
122: returnXml.append(indent + "version=\"1.2.0\"\n");
123:
124: returnXml.append(indent
125: + "xmlns=\"http://www.opengis.net/ogc\"\n");
126:
127: returnXml.append(indent + "xmlns:xsi=\"http://www.w3.org/2001/"
128: + "XMLSchema-instance\"\n");
129:
130: returnXml.append(indent);
131:
132: returnXml
133: .append("xsi:schemaLocation=\"http://www.opengis.net/ogc ");
134:
135: returnXml.append(Requests.getSchemaBaseUrl(request, geoserver)
136: + "wcs/1.0.0/OGC-exception.xsd\">\n");
137:
138: //REVISIT: handle multiple service exceptions? must refactor class.
139: returnXml.append(indent + "<ServiceException");
140:
141: if (!isEmpty(getCode())) {
142: returnXml.append(" code=\"" + getCode() + "\"");
143: }
144:
145: if (!isEmpty(this .locator)) {
146: returnXml.append(" locator=\"" + this .locator + "\"");
147: }
148:
149: returnXml.append(">\n" + indent + indent);
150: returnXml.append(getXmlMessage(printStackTrace));
151:
152: returnXml.append(indent + "</ServiceException>\n");
153:
154: returnXml.append("</ServiceExceptionReport>");
155:
156: LOGGER.fine("return wfs exception is " + returnXml);
157:
158: return returnXml.toString();
159: }
160:
161: /**
162: * Returns the mime type that should be exposed to the client
163: * when sending the exception message.
164: *
165: * <p>
166: * Defaults to <code>geoserver.getMimeType()</code>
167: * </p>
168: *
169: * @return
170: */
171: public String getMimeType(GeoServer geoserver) {
172: return SE_XML + "; charset=" + geoserver.getCharSet().name();
173: }
174: }
|