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.wms;
006:
007: import org.vfny.geoserver.ServiceException;
008: import org.vfny.geoserver.global.GeoServer;
009: import org.vfny.geoserver.util.Requests;
010: import javax.servlet.http.HttpServletRequest;
011:
012: /**
013: * This defines an exception that can be turned into a valid xml service
014: * exception that wms clients will expect. All errors should be wrapped in
015: * this before returning to clients.
016: *
017: * @author Gabriel Rold?n
018: * @version $Id: WmsException.java 7639 2007-10-22 15:18:58Z aaime $
019: */
020: public class WmsException extends ServiceException {
021: /**
022: * The fixed MIME type of a WMS exception.
023: */
024: private static final String SE_XML = "application/vnd.ogc.se_xml";
025:
026: /**
027: * Enum of exception codes defined in Anex A.3 of WMS 1.1.1 spec
028: public static class ExceptionCodeEnum{
029: private String exceptionCode;
030: private ExceptionCodeEnum(String code){
031: this.exceptionCode = code;
032: }
033: public String toString(){
034: return exceptionCode;
035: }
036: }
037: public static final ExceptionCodeEnum InvalidFormat = new ExceptionCodeEnum("InvalidFormat");
038: public static final ExceptionCodeEnum InvalidSRS = new ExceptionCodeEnum("InvalidSRS");
039: public static final ExceptionCodeEnum LayerNotDefined = new ExceptionCodeEnum("LayerNotDefined");
040: public static final ExceptionCodeEnum StyleNotDefined = new ExceptionCodeEnum("StyleNotDefined");
041: public static final ExceptionCodeEnum LayerNotQueryable = new ExceptionCodeEnum("LayerNotQueryable");
042: public static final ExceptionCodeEnum CurrentUpdateSequence = new ExceptionCodeEnum("CurrentUpdateSequence");
043: public static final ExceptionCodeEnum InvalidUpdateSequence = new ExceptionCodeEnum("InvalidUpdateSequence");
044: public static final ExceptionCodeEnum MissingDimensionValue = new ExceptionCodeEnum("MissingDimensionValue");
045: public static final ExceptionCodeEnum InvalidDimensionValue = new ExceptionCodeEnum("InvalidDimensionValue");
046: */
047:
048: /**
049: * Empty constructor.
050: */
051: public WmsException() {
052: super ();
053: }
054:
055: /**
056: * constructor with exception message
057: *
058: * @param message The message for the exception
059: */
060: public WmsException(String message) {
061: super (message);
062: }
063:
064: public WmsException(ServiceException e) {
065: super (e);
066: }
067:
068: /**
069: * Empty constructor.
070: *
071: * @param e The message for the .
072: */
073: public WmsException(Throwable e) {
074: super (e.getMessage(), e);
075: }
076:
077: /**
078: * Empty constructor.
079: *
080: * @param message The message for the .
081: * @param locator The message for the .
082: */
083: public WmsException(String message, String code) {
084: super (message);
085: setCode(code);
086: }
087:
088: /**
089: * Empty constructor.
090: *
091: * @param message The message for the .
092: * @param locator The message for the .
093: */
094: public WmsException(String message, String code, Exception e) {
095: super (message, e);
096: setCode(code);
097: }
098:
099: /**
100: * DOCUMENT ME!
101: *
102: * @param e The message for the .
103: * @param preMessage The message to tack on the front.
104: * @param locator The message for the .
105: */
106: public WmsException(Throwable e, String preMessage, String locator) {
107: super (e, preMessage, locator);
108: }
109:
110: public WmsException(Throwable e, String preMessage, String locator,
111: String code) {
112: this (e, preMessage, locator);
113: setCode(code);
114: }
115:
116: /**
117: * Return request type.
118: *
119: * @param printStackTrace whether the stack trace should be included.
120: *
121: * @return The ServiceExceptionReport of this error.
122: *
123: * @task REVISIT: adapt it to handle WMS too
124: */
125: public String getXmlResponse(boolean printStackTrace,
126: HttpServletRequest request, GeoServer geoserver) {
127: StringBuffer returnXml = new StringBuffer(
128: "<?xml version=\"1.0\"");
129: returnXml.append(" encoding=\"UTF-8\" standalone=\"no\" ?>");
130:
131: String dtdUrl = Requests.getSchemaBaseUrl(request, geoserver)
132: + "/wms/1.1.1/WMS_exception_1_1_1.dtd";
133: returnXml.append("<!DOCTYPE ServiceExceptionReport SYSTEM \""
134: + dtdUrl + "\"> ");
135: returnXml.append("<ServiceExceptionReport version=\"1.1.1\">");
136:
137: // Write exception code
138: returnXml
139: .append(" <ServiceException"
140: + ((getCode() != null) ? (" code=\""
141: + getCode() + "\"") : "") + ">"
142: + getXmlMessage(printStackTrace)
143: + "</ServiceException>");
144:
145: // Write footer
146: returnXml.append(" </ServiceExceptionReport>");
147:
148: return returnXml.toString();
149: }
150:
151: /**
152: * Returns the MIME type of a WMS exception.
153: *
154: * @return <code>"application/vnd.ogc.se_xml"</code>
155: */
156: public String getMimeType(GeoServer geoserver) {
157: return SE_XML;
158: }
159: }
|