001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/enterprise/servlet/CSWHandler.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: EXSE, Department of Geography, University of Bonn
007: http://www.giub.uni-bonn.de/deegree/
008: lat/lon GmbH
009: http://www.lat-lon.de
010:
011: This library is free software; you can redistribute it and/or
012: modify it under the terms of the GNU Lesser General Public
013: License as published by the Free Software Foundation; either
014: version 2.1 of the License, or (at your option) any later version.
015:
016: This library is distributed in the hope that it will be useful,
017: but WITHOUT ANY WARRANTY; without even the implied warranty of
018: MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: Lesser General Public License for more details.
020:
021: You should have received a copy of the GNU Lesser General Public
022: License along with this library; if not, write to the Free Software
023: Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
024:
025: Contact:
026:
027: Andreas Poth
028: lat/lon GmbH
029: Aennchenstr. 19
030: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Prof. Dr. Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: greve@giub.uni-bonn.de
041:
042:
043: ---------------------------------------------------------------------------*/
044: package org.deegree.enterprise.servlet;
045:
046: import java.io.IOException;
047: import java.io.OutputStream;
048: import java.io.PrintWriter;
049: import java.lang.reflect.Method;
050:
051: import javax.servlet.http.HttpServletResponse;
052:
053: import org.deegree.enterprise.ServiceException;
054: import org.deegree.framework.log.ILogger;
055: import org.deegree.framework.log.LoggerFactory;
056: import org.deegree.framework.util.CharsetUtils;
057: import org.deegree.framework.xml.XMLFragment;
058: import org.deegree.framework.xml.XMLParsingException;
059: import org.deegree.ogcbase.ExceptionCode;
060: import org.deegree.ogcwebservices.EchoRequest;
061: import org.deegree.ogcwebservices.InvalidParameterValueException;
062: import org.deegree.ogcwebservices.OGCWebServiceException;
063: import org.deegree.ogcwebservices.OGCWebServiceRequest;
064: import org.deegree.ogcwebservices.csw.CSWFactory;
065: import org.deegree.ogcwebservices.csw.CSWPropertiesAccess;
066: import org.deegree.ogcwebservices.csw.CatalogueService;
067: import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilities;
068: import org.deegree.ogcwebservices.csw.capabilities.CatalogueGetCapabilities;
069: import org.deegree.ogcwebservices.csw.discovery.DescribeRecordResult;
070: import org.deegree.ogcwebservices.csw.discovery.GetRecordByIdResult;
071: import org.deegree.ogcwebservices.csw.discovery.GetRecordsResult;
072: import org.deegree.ogcwebservices.csw.manager.HarvestResult;
073: import org.deegree.ogcwebservices.csw.manager.TransactionResult;
074:
075: /**
076: * Web servlet client for CSW.
077: *
078: * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
079: * @author last edited by: $Author: aschmitz $
080: *
081: * @version $Revision: 9499 $, $Date: 2008-01-09 07:47:04 -0800 (Wed, 09 Jan 2008) $
082: *
083: * @see <a href="http://www.dofactory.com/patterns/PatternChain.aspx">Chain of Responsibility Design
084: * Pattern </a>
085: */
086:
087: public class CSWHandler extends AbstractOWServiceHandler {
088:
089: private static final ILogger LOG = LoggerFactory
090: .getLogger(CSWHandler.class);
091:
092: /**
093: * @param request
094: * @param httpResponse
095: * @throws ServiceException
096: * @throws OGCWebServiceException
097: * @see "org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.services.AbstractOGCWebServiceRequest,javax.servlet.http.HttpServletResponse)"
098: */
099: public void perform(OGCWebServiceRequest request,
100: HttpServletResponse httpResponse) throws ServiceException,
101: OGCWebServiceException {
102:
103: LOG.logDebug("Performing request: " + request.toString());
104:
105: try {
106: CatalogueService service = CSWFactory.getService();
107: Object response = service.doService(request);
108: if (response instanceof OGCWebServiceException) {
109: sendExceptionReport(httpResponse,
110: (OGCWebServiceException) response);
111: } else if (response instanceof Exception) {
112: sendExceptionReport(httpResponse, (Exception) response);
113: } else if (response instanceof CatalogueCapabilities) {
114: sendCapabilities(httpResponse,
115: (CatalogueGetCapabilities) request,
116: (CatalogueCapabilities) response);
117: } else if (response instanceof GetRecordsResult) {
118: sendGetRecord(httpResponse, (GetRecordsResult) response);
119: } else if (response instanceof GetRecordByIdResult) {
120: sendGetRecordById(httpResponse,
121: (GetRecordByIdResult) response);
122: } else if (response instanceof DescribeRecordResult) {
123: sendDescribeRecord(httpResponse,
124: (DescribeRecordResult) response);
125: } else if (response instanceof TransactionResult) {
126: sendTransactionResult(httpResponse,
127: (TransactionResult) response);
128: } else if (response instanceof HarvestResult) {
129: sendHarvestResult(httpResponse,
130: (HarvestResult) response);
131: } else if (response instanceof EchoRequest) {
132: sendHarvestResult(httpResponse);
133: } else {
134: OGCWebServiceException e = new OGCWebServiceException(
135: this .getClass().getName(),
136: "Unknown response class: "
137: + (response == null ? "null response object"
138: : response.getClass().getName())
139: + ".");
140: sendExceptionReport(httpResponse, e);
141: }
142: } catch (IOException ex) {
143: throw new ServiceException("Error while sending response: "
144: + ex.getMessage(), ex);
145: } catch (OGCWebServiceException ogc) {
146: sendExceptionReport(httpResponse, ogc);
147: }
148:
149: }
150:
151: /**
152: * Sends the passed <tt>HarvestResult</tt> to the http client.
153: *
154: * @param httpResponse
155: * http connection to the client
156: * @param result
157: * object to send
158: * @throws IOException
159: * @throws XMLParsingException
160: */
161: private void sendHarvestResult(HttpServletResponse httpResponse,
162: HarvestResult result) throws IOException {
163: XMLFragment doc = null;
164: try {
165: doc = org.deegree.ogcwebservices.csw.manager.XMLFactory
166: .export(result);
167: } catch (XMLParsingException e) {
168: throw new IOException(
169: "could not export TransactionResult as XML: "
170: + e.getMessage());
171: }
172: httpResponse.setContentType("text/xml; charset="
173: + CharsetUtils.getSystemCharset());
174: OutputStream os = httpResponse.getOutputStream();
175: doc.write(os);
176: os.close();
177: }
178:
179: /**
180: *
181: * @param httpResponse
182: * @throws IOException
183: */
184: private void sendHarvestResult(HttpServletResponse httpResponse)
185: throws IOException {
186:
187: httpResponse.setContentType("text/xml; charset="
188: + CharsetUtils.getSystemCharset());
189: PrintWriter pw = httpResponse.getWriter();
190: pw.write("<HarvestResponse>Harvest request has been received ");
191: pw.write("and will be performed</HarvestResponse>");
192: pw.close();
193: }
194:
195: /**
196: * Sends the passed <tt>TransactionResult</tt> to the http client.
197: *
198: * @param httpResponse
199: * http connection to the client
200: * @param result
201: * object to send
202: * @throws XMLParsingException
203: */
204: private void sendTransactionResult(
205: HttpServletResponse httpResponse, TransactionResult result)
206: throws IOException {
207: XMLFragment doc = null;
208: try {
209: doc = org.deegree.ogcwebservices.csw.manager.XMLFactory
210: .export(result);
211: } catch (XMLParsingException e) {
212: throw new IOException(
213: "could not export TransactionResult as XML: "
214: + e.getMessage());
215: }
216: httpResponse.setContentType("text/xml; charset="
217: + CharsetUtils.getSystemCharset());
218: OutputStream os = httpResponse.getOutputStream();
219: doc.write(os);
220: os.close();
221: }
222:
223: /**
224: * Sends the passed <tt>CatalogCapabilities</tt> to the http client.
225: *
226: * @param response
227: * http connection to the client
228: * @param capabilities
229: * object to send
230: */
231: private void sendCapabilities(HttpServletResponse response,
232: CatalogueGetCapabilities getCapabilities,
233: CatalogueCapabilities capabilities) throws IOException {
234:
235: boolean xmlOk = false;
236: String[] formats = getCapabilities.getAcceptFormats();
237: if (formats == null || formats.length == 0) {
238: xmlOk = true;
239: } else {
240: for (int i = 0; i < formats.length; i++) {
241: if (formats[i].equals("text/xml")) {
242: xmlOk = true;
243: break;
244: }
245: }
246: }
247: if (!xmlOk) {
248: ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
249: InvalidParameterValueException e = new InvalidParameterValueException(
250: this .getClass().getName(),
251: "OutputFormat must be 'text/xml'.", code);
252: sendExceptionReport(response, e);
253: } else {
254: String version = getCapabilities.getVersion();
255: String className = CSWPropertiesAccess
256: .getString("XMLFactory" + version);
257: XMLFragment doc = null;
258: try {
259: Class<?> clzz = Class.forName(className);
260: Class[] parameterTypes = new Class[] {
261: CatalogueCapabilities.class, String[].class };
262: Object[] parameters = new Object[] { capabilities,
263: getCapabilities.getSections() };
264: Method method = clzz
265: .getMethod("export", parameterTypes);
266: doc = (XMLFragment) method.invoke(null, parameters);
267: } catch (Exception e) {
268: e.printStackTrace();
269: }
270:
271: response.setContentType("text/xml; charset="
272: + CharsetUtils.getSystemCharset());
273: OutputStream os = response.getOutputStream();
274: doc.write(os);
275: os.close();
276: }
277: }
278:
279: /**
280: *
281: * @param response
282: * @param getRecordResponse
283: * @throws IOException
284: */
285: private void sendGetRecord(HttpServletResponse response,
286: GetRecordsResult getRecordResponse) throws IOException {
287: XMLFragment doc = org.deegree.ogcwebservices.csw.discovery.XMLFactory
288: .export(getRecordResponse);
289: if (LOG.isDebug()) {
290: LOG.logDebug("GetRecord response", doc.getAsPrettyString());
291: }
292: response.setContentType("text/xml; charset="
293: + CharsetUtils.getSystemCharset());
294: OutputStream os = response.getOutputStream();
295: doc.write(os);
296: os.close();
297: }
298:
299: /**
300: *
301: * @param response
302: * @param getRecordByResponse
303: * @throws IOException
304: */
305: private void sendGetRecordById(HttpServletResponse response,
306: GetRecordByIdResult getRecordByIdResponse)
307: throws IOException {
308: XMLFragment doc = org.deegree.ogcwebservices.csw.discovery.XMLFactory
309: .export(getRecordByIdResponse);
310: if (LOG.isDebug()) {
311: LOG.logDebug("GetRecordById response", doc
312: .getAsPrettyString());
313: }
314: response.setContentType("text/xml");
315: OutputStream os = response.getOutputStream();
316: doc.write(os);
317: os.close();
318: }
319:
320: /**
321: *
322: * @param response
323: * @param describeRecordRequest
324: * @param describeRecordResponse
325: * @throws IOException
326: */
327: private void sendDescribeRecord(HttpServletResponse response,
328: DescribeRecordResult describeRecordResponse)
329: throws IOException {
330: XMLFragment doc = org.deegree.ogcwebservices.csw.discovery.XMLFactory
331: .export(describeRecordResponse);
332: if (LOG.isDebug()) {
333: LOG.logDebug("DescribeRecord response", doc
334: .getAsPrettyString());
335: }
336: response.setContentType("text/xml; charset="
337: + CharsetUtils.getSystemCharset());
338: OutputStream os = response.getOutputStream();
339: doc.write(os);
340: os.close();
341: }
342: }
|