001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/servlet/CSWHandler.java $
002 /*---------------- FILE HEADER ------------------------------------------
003
004 This file is part of deegree.
005 Copyright (C) 2001-2006 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
050 import javax.servlet.http.HttpServletResponse;
051
052 import org.deegree.enterprise.ServiceException;
053 import org.deegree.framework.log.ILogger;
054 import org.deegree.framework.log.LoggerFactory;
055 import org.deegree.framework.util.CharsetUtils;
056 import org.deegree.framework.xml.XMLFragment;
057 import org.deegree.framework.xml.XMLParsingException;
058 import org.deegree.ogcbase.ExceptionCode;
059 import org.deegree.ogcwebservices.EchoRequest;
060 import org.deegree.ogcwebservices.InvalidParameterValueException;
061 import org.deegree.ogcwebservices.OGCWebServiceException;
062 import org.deegree.ogcwebservices.OGCWebServiceRequest;
063 import org.deegree.ogcwebservices.csw.CSWFactory;
064 import org.deegree.ogcwebservices.csw.CatalogueService;
065 import org.deegree.ogcwebservices.csw.capabilities.CatalogueCapabilities;
066 import org.deegree.ogcwebservices.csw.capabilities.CatalogueGetCapabilities;
067 import org.deegree.ogcwebservices.csw.discovery.DescribeRecordResult;
068 import org.deegree.ogcwebservices.csw.discovery.GetRecordByIdResult;
069 import org.deegree.ogcwebservices.csw.discovery.GetRecordsResult;
070 import org.deegree.ogcwebservices.csw.manager.HarvestResult;
071 import org.deegree.ogcwebservices.csw.manager.TransactionResult;
072
073 /**
074 * Web servlet client for CSW.
075 *
076 * @author <a href="mailto:tfr@users.sourceforge.net">Torsten Friebe </A>
077 * @author last edited by: $Author: bezema $
078 *
079 * @version $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $
080 *
081 * @see <a href="http://www.dofactory.com/patterns/PatternChain.aspx">Chain of Responsibility Design
082 * Pattern </a>
083 */
084
085 public class CSWHandler extends AbstractOWServiceHandler {
086
087 private static final ILogger LOG = LoggerFactory.getLogger( CSWHandler.class );
088
089 /**
090 * @param request
091 * @param httpResponse
092 * @throws ServiceException
093 * @throws OGCWebServiceException
094 * @see "org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.services.AbstractOGCWebServiceRequest,javax.servlet.http.HttpServletResponse)"
095 */
096 public void perform( OGCWebServiceRequest request, HttpServletResponse httpResponse )
097 throws ServiceException, OGCWebServiceException {
098
099 LOG.logDebug( "Performing request: " + request.toString() );
100
101 CatalogueService service = CSWFactory.getService();
102 Object response = service.doService( request );
103 try {
104 if ( response instanceof OGCWebServiceException ) {
105 sendException( httpResponse, (OGCWebServiceException) response );
106 } else if ( response instanceof Exception ) {
107 sendException( httpResponse, (Exception) response );
108 } else if ( response instanceof CatalogueCapabilities ) {
109 sendCapabilities( httpResponse, (CatalogueGetCapabilities) request,
110 (CatalogueCapabilities) response );
111 } else if ( response instanceof GetRecordsResult ) {
112 sendGetRecord( httpResponse, (GetRecordsResult) response );
113 } else if ( response instanceof GetRecordByIdResult ) {
114 sendGetRecordById( httpResponse, (GetRecordByIdResult) response );
115 } else if ( response instanceof DescribeRecordResult ) {
116 sendDescribeRecord( httpResponse, (DescribeRecordResult) response );
117 } else if ( response instanceof TransactionResult ) {
118 sendTransactionResult( httpResponse, (TransactionResult) response );
119 } else if ( response instanceof HarvestResult ) {
120 sendHarvestResult( httpResponse, (HarvestResult) response );
121 } else if ( response instanceof EchoRequest ) {
122 sendHarvestResult( httpResponse );
123 } else {
124 OGCWebServiceException e = new OGCWebServiceException(
125 this.getClass().getName(),
126 "Unknown response class: "
127 + ( response == null ? "null response object"
128 : response.getClass().getName() )
129 + "." );
130 sendException( httpResponse, e );
131 }
132 } catch ( IOException ex ) {
133 throw new ServiceException( "Error while sending response: " + ex.getMessage(), ex );
134 }
135
136 }
137
138 /**
139 * Sends the passed <tt>HarvestResult</tt> to the http client.
140 *
141 * @param httpResponse
142 * http connection to the client
143 * @param result
144 * object to send
145 * @throws IOException
146 * @throws XMLParsingException
147 */
148 private void sendHarvestResult( HttpServletResponse httpResponse, HarvestResult result )
149 throws IOException {
150 XMLFragment doc = null;
151 try {
152 doc = org.deegree.ogcwebservices.csw.manager.XMLFactory.export( result );
153 } catch ( XMLParsingException e ) {
154 throw new IOException( "could not export TransactionResult as XML: " + e.getMessage() );
155 }
156 httpResponse.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
157 OutputStream os = httpResponse.getOutputStream();
158 doc.write( os );
159 os.close();
160 }
161
162 /**
163 *
164 * @param httpResponse
165 * @throws IOException
166 */
167 private void sendHarvestResult( HttpServletResponse httpResponse )
168 throws IOException {
169
170 httpResponse.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
171 PrintWriter pw = httpResponse.getWriter();
172 pw.write( "<HarvestResponse>Harvest request has been received " );
173 pw.write( "and will be performed</HarvestResponse>" );
174 pw.close();
175 }
176
177 /**
178 * Sends the passed <tt>TransactionResult</tt> to the http client.
179 *
180 * @param httpResponse
181 * http connection to the client
182 * @param result
183 * object to send
184 * @throws XMLParsingException
185 */
186 private void sendTransactionResult( HttpServletResponse httpResponse, TransactionResult result )
187 throws IOException {
188 XMLFragment doc = null;
189 try {
190 doc = org.deegree.ogcwebservices.csw.manager.XMLFactory.export( result );
191 } catch ( XMLParsingException e ) {
192 throw new IOException( "could not export TransactionResult as XML: " + e.getMessage() );
193 }
194 httpResponse.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
195 OutputStream os = httpResponse.getOutputStream();
196 doc.write( os );
197 os.close();
198 }
199
200 /**
201 * Sends the passed <tt>CatalogCapabilities</tt> to the http client.
202 *
203 * @param response
204 * http connection to the client
205 * @param capabilities
206 * object to send
207 */
208 private void sendCapabilities( HttpServletResponse response,
209 CatalogueGetCapabilities getCapabilities,
210 CatalogueCapabilities capabilities )
211 throws IOException {
212
213 boolean xmlOk = false;
214 String[] formats = getCapabilities.getAcceptFormats();
215 if ( formats == null || formats.length == 0 ) {
216 xmlOk = true;
217 } else {
218 for ( int i = 0; i < formats.length; i++ ) {
219 if ( formats[i].equals( "text/xml" ) ) {
220 xmlOk = true;
221 break;
222 }
223 }
224 }
225 if ( !xmlOk ) {
226 ExceptionCode code = ExceptionCode.INVALIDPARAMETERVALUE;
227 InvalidParameterValueException e = new InvalidParameterValueException(
228 this.getClass().getName(),
229 "OutputFormat must be 'text/xml'.",
230 code );
231 sendException( response, e );
232 } else {
233
234 XMLFragment doc = org.deegree.ogcwebservices.csw.XMLFactory.export(
235 capabilities,
236 getCapabilities.getSections() );
237 response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
238 OutputStream os = response.getOutputStream();
239 doc.write( os );
240 os.close();
241 }
242 }
243
244 /**
245 *
246 * @param response
247 * @param getRecordResponse
248 * @throws IOException
249 */
250 private void sendGetRecord( HttpServletResponse response, GetRecordsResult getRecordResponse )
251 throws IOException {
252 XMLFragment doc = org.deegree.ogcwebservices.csw.discovery.XMLFactory.export( getRecordResponse );
253 response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
254 OutputStream os = response.getOutputStream();
255 doc.write( os );
256 os.close();
257 }
258
259 /**
260 *
261 * @param response
262 * @param getRecordByResponse
263 * @throws IOException
264 */
265 private void sendGetRecordById( HttpServletResponse response,
266 GetRecordByIdResult getRecordByIdResponse )
267 throws IOException {
268 XMLFragment doc = org.deegree.ogcwebservices.csw.discovery.XMLFactory.export( getRecordByIdResponse );
269 response.setContentType( "text/xml" );
270 OutputStream os = response.getOutputStream();
271 doc.write( os );
272 os.close();
273 }
274
275 /**
276 *
277 * @param response
278 * @param describeRecordRequest
279 * @param describeRecordResponse
280 * @throws IOException
281 */
282 private void sendDescribeRecord( HttpServletResponse response,
283 DescribeRecordResult describeRecordResponse )
284 throws IOException {
285 XMLFragment doc = org.deegree.ogcwebservices.csw.discovery.XMLFactory.export( describeRecordResponse );
286 response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() );
287 OutputStream os = response.getOutputStream();
288 doc.write( os );
289 os.close();
290 }
291 }
292 /***************************************************************************************************
293 * <code>
294 Changes to this class. What the people have been up to:
295
296 $Log$
297 Revision 1.23 2007/03/06 10:11:31 wanhoff
298 Fixed Javadoc (@see)
299
300 Revision 1.22 2007/01/14 17:02:20 poth
301 bug fix - harvesting
302 Revision 1.21
303 2006/10/17 20:31:18 poth ** empty log message ***
304
305 Revision 1.20 2006/07/23 10:05:54 poth setting content type for Http responses enhanced by adding
306 charset (for mime types text/plain and text/xml)
307
308 Revision 1.19 2006/07/12 14:46:15 poth comment footer added
309
310 </code>
311 **************************************************************************************************/