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.responses;
006:
007: import org.springframework.context.ApplicationContext;
008: import org.vfny.geoserver.Request;
009: import org.vfny.geoserver.Response;
010: import org.vfny.geoserver.ServiceException;
011: import org.vfny.geoserver.global.GeoServer;
012: import org.vfny.geoserver.global.Service;
013: import org.vfny.geoserver.util.requests.CapabilitiesRequest;
014: import org.vfny.geoserver.wcs.WcsException;
015: import java.io.ByteArrayOutputStream;
016: import java.io.IOException;
017: import java.io.OutputStream;
018: import java.util.HashMap;
019: import java.util.logging.Logger;
020: import javax.xml.transform.TransformerException;
021:
022: /**
023: * DOCUMENT ME!
024: *
025: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last
026: * modification)
027: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last
028: * modification)
029: */
030: public class WCSCapabilitiesResponse implements Response {
031: /** package's logger */
032: private static final Logger LOGGER = org.geotools.util.logging.Logging
033: .getLogger(WCSCapabilitiesResponse.class.getPackage()
034: .getName());
035:
036: /**
037: * Byte array holding the raw content of the capabilities document,
038: * generated in <code>execute()</code>
039: */
040: private byte[] rawResponse;
041:
042: private ApplicationContext applicationContext;
043:
044: public WCSCapabilitiesResponse(ApplicationContext applicationContext) {
045: this .applicationContext = applicationContext;
046: }
047:
048: /**
049: * Returns any extra headers that this service might want to set in the HTTP response object.
050: * @see org.vfny.geoserver.Response#getResponseHeaders()
051: */
052: public HashMap getResponseHeaders() {
053: return null;
054: }
055:
056: /**
057: * DOCUMENT ME!
058: *
059: * @param request
060: * DOCUMENT ME!
061: *
062: * @throws ServiceException
063: * DOCUMENT ME!
064: * @throws IllegalArgumentException
065: * DOCUMENT ME!
066: * @throws WCSException
067: * DOCUMENT ME!
068: */
069: public void execute(Request request) throws ServiceException {
070: if (!(request instanceof CapabilitiesRequest)) {
071: throw new IllegalArgumentException(
072: "Not a GetCapabilities Request");
073: }
074:
075: CapabilitiesRequest capreq = (CapabilitiesRequest) request;
076: int reqUS = -1;
077: if (capreq.getUpdateSequence() != null) {
078: try {
079: reqUS = Integer.parseInt(capreq.getUpdateSequence());
080: } catch (NumberFormatException nfe) {
081: throw new ServiceException(
082: "GeoServer only accepts numbers in the updateSequence parameter");
083: }
084: }
085: int geoUS = request.getServiceRef().getServiceRef()
086: .getGeoServer().getUpdateSequence();
087: if (reqUS > geoUS) {
088: throw new WcsException(
089: "Client supplied an updateSequence that is greater than the current sever updateSequence",
090: "", "InvalidUpdateSequence");
091: }
092: if (reqUS == geoUS) {
093: throw new WcsException(
094: "WCS capabilities document is current (updateSequence = "
095: + geoUS + ")", null,
096: "CurrentUpdateSequence");
097: }
098: //otherwise it's a normal response...
099:
100: WCSCapsTransformer transformer = new WCSCapsTransformer(request
101: .getBaseUrl(), applicationContext);
102:
103: transformer.setIndentation(2);
104: ByteArrayOutputStream out = new ByteArrayOutputStream();
105:
106: try {
107: transformer.transform(request, out);
108: } catch (TransformerException e) {
109: throw new WcsException(e);
110: }
111:
112: this .rawResponse = out.toByteArray();
113: }
114:
115: /**
116: * Returns the fixed capabilities MIME type (application/vnd.ogc.WCS_xml) as
117: * specified in whe WCS spec, version 1.1.1, section 6.5.3, table 3.
118: *
119: * @param gs
120: * DOCUMENT ME!
121: *
122: * @return the capabilities document MIME type.
123: *
124: * @throws IllegalStateException
125: * if the response was not yet produced.
126: */
127: public String getContentType(GeoServer gs)
128: throws IllegalStateException {
129: if (rawResponse == null) {
130: throw new IllegalStateException(
131: "execute() not called or not succeed.");
132: }
133:
134: return gs.getMimeType();
135: }
136:
137: /**
138: * Just returns <code>null</code>, since no special encoding is applyed
139: * to the output data.
140: *
141: * @return <code>null</code>
142: */
143: public String getContentEncoding() {
144: return null;
145: }
146:
147: /**
148: * Just returns <code>null</code>, since no special encoding is applyed
149: * to the output data.
150: *
151: * @return <code>null</code>
152: */
153: public String getContentDisposition() {
154: return null;
155: }
156:
157: /**
158: * Writes the capabilities document generated in <code>execute()</code> to
159: * the given output stream.
160: *
161: * @param out
162: * the capabilities document destination
163: *
164: * @throws ServiceException
165: * never, since the whole content was aquired in
166: * <code>execute()</code>
167: * @throws IOException
168: * if it is thrown while writing to <code>out</code>
169: * @throws IllegalStateException
170: * if <code>execute()</code> was not called/succeed before
171: * this method is called.
172: */
173: public void writeTo(OutputStream out) throws ServiceException,
174: IOException {
175: if (rawResponse == null) {
176: throw new IllegalStateException(
177: "execute() not called or not succeed.");
178: }
179:
180: out.write(rawResponse);
181: }
182:
183: /**
184: * Does nothing, since no processing is done after <code>execute()</code>
185: * has returned.
186: *
187: * @param gs
188: * the service instance
189: */
190: public void abort(Service gs) {
191: }
192: }
|