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 org.geoserver.platform.Operation;
008: import org.geoserver.platform.ServiceException;
009: import java.io.IOException;
010: import java.io.OutputStream;
011: import java.util.Collections;
012: import java.util.HashMap;
013: import java.util.Set;
014:
015: /**
016: * Response to an operation, which serializes the result of the operation to an
017: * output stream.
018: * <p>
019: * A response must specify the following information:
020: * <ul>
021: * <li>The type of object it is capable of serializing, the class is bound to.
022: * See {@link #getBinding()}.
023: * <li>The mime-type of the resulting response. See {@link #getMimeType()}.
024: * </ul>
025: * </p>
026: * <p>
027: * Optionally, a response may declare a well-known name for it. This well
028: * known name corresponds to the "outputFormat" parameter which is supported
029: * on many types of OWS request.
030: * </p>
031: *
032: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
033: *
034: */
035: public abstract class Response {
036: /**
037: * Class of object to serialize
038: */
039: final Class binding;
040:
041: /**
042: * The well known "outputFormat" of the response
043: */
044: final Set outputFormats;
045:
046: /**
047: * Constructor which specified the class this response is bound to.
048: *
049: * @param binding The class of object the response serializes.
050: */
051: public Response(Class binding) {
052: this (binding, (Set) null);
053: }
054:
055: /**
056: * Constructor which specified the class this response is bound to, and a
057: * common name for the type of response.
058: *
059: * @param binding The class of object the response serializes
060: * @param outputFormat A common name for the response.
061: *
062: */
063: public Response(Class binding, String outputFormat) {
064: this (binding, outputFormat == null ? null : Collections
065: .singleton(outputFormat));
066: }
067:
068: /**
069: * Constructor which specified the class this response is bound to, and a
070: * set of common names for the type of response.
071: *
072: * @param binding The class of object the response serializes
073: * @param outputFormats A set of common names for the response.
074: */
075: public Response(Class binding, Set outputFormats) {
076: if (binding == null) {
077: throw new NullPointerException("binding may not be null");
078: }
079:
080: if (outputFormats == null) {
081: outputFormats = Collections.EMPTY_SET;
082: }
083:
084: this .binding = binding;
085: this .outputFormats = outputFormats;
086: }
087:
088: /**
089: * @return The type of object the response can handle.
090: */
091: public final Class getBinding() {
092: return binding;
093: }
094:
095: /**
096: * @deprecated use {@link #getOutputFormats()}.
097: *
098: * @return A common or well-known name for the response, may be <code>null</code>.
099: */
100: public final String getOutputFormat() {
101: if (outputFormats.isEmpty()) {
102: return null;
103: }
104:
105: return (String) outputFormats.iterator().next();
106: }
107:
108: /**
109: *
110: * @return Set of common or well-known name for the response, may be empty.
111: */
112: public final Set getOutputFormats() {
113: return outputFormats;
114: }
115:
116: /**
117: * Determines if the response can handle the operation being performed.
118: * <p>
119: * This method is called before {@link #write(Object, OutputStream, Operation)}.
120: * </p>
121: * <p>
122: * Subclasses should override this method to perform additional checks
123: * against the operation being performed. Example might be checking the
124: * version of the service.
125: * </p>
126: * @param operation The operation being performed.
127: *
128: * @return <code>true</code> if the response can handle the operation,
129: * otherwise <code>false</code>
130: */
131: public boolean canHandle(Operation operation) {
132: return true;
133: }
134:
135: /**
136: * Returns the mime type to be uses when writing the response.
137: *
138: * @param value The value to serialize
139: * @param operation The operation being performed.
140: *
141: *
142: * @return The mime type of the response, must not be <code>null</code>
143: */
144: public abstract String getMimeType(Object value, Operation operation)
145: throws ServiceException;
146:
147: /**
148: * Returns a 2xn array of Strings, each of which is an HTTP header pair
149: * to be set on the HTTP Response. Can return null if there are
150: * no headers to be set on the response.
151: *
152: * @param value The value to serialize
153: * @param operation The operation being performed.
154: *
155: * @return 2xn string array containing string-pairs of HTTP headers/values
156: *
157: */
158: public String[][] getHeaders(Object value, Operation operation)
159: throws ServiceException {
160: //default implementation returns null = no headers to set
161: return null;
162: }
163:
164: /**
165: * Serializes <code>value</code> to <code>output</code>.
166: * <p>
167: * The <code>operation</code> bean is provided for context.
168: * </p>
169: * @param value The value to serialize.
170: * @param output The output stream.
171: * @param operation The operation which resulted in <code>value</code>
172: *
173: * @throws IOException Any I/O errors that occur
174: * @throws ServiceException Any service errors that occur
175: */
176: public abstract void write(Object value, OutputStream output,
177: Operation operation) throws IOException, ServiceException;
178: }
|