001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/enterprise/servlet/WASSHandler.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:
045: package org.deegree.enterprise.servlet;
046:
047: import java.io.BufferedInputStream;
048: import java.io.IOException;
049: import java.io.InputStream;
050: import java.io.OutputStream;
051:
052: import javax.servlet.http.HttpServletResponse;
053:
054: import org.apache.commons.httpclient.Header;
055: import org.deegree.enterprise.ServiceException;
056: import org.deegree.framework.log.ILogger;
057: import org.deegree.framework.log.LoggerFactory;
058: import org.deegree.framework.util.CharsetUtils;
059: import org.deegree.ogcwebservices.OGCWebServiceException;
060: import org.deegree.ogcwebservices.OGCWebServiceRequest;
061: import org.deegree.ogcwebservices.wass.common.WASServiceFactory;
062: import org.deegree.ogcwebservices.wass.common.XMLFactory;
063: import org.deegree.ogcwebservices.wass.was.WAService;
064: import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilities;
065: import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilitiesDocument;
066: import org.deegree.ogcwebservices.wass.was.operation.DescribeUserResponse;
067: import org.deegree.ogcwebservices.wass.wss.WSService;
068: import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilities;
069: import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilitiesDocument;
070: import org.deegree.ogcwebservices.wass.wss.operation.DoServiceResponse;
071:
072: /**
073: * This is the servlet handler class for the WASS services, ie, the Web
074: * Authentication Service and the Web Security Service. Attention: since much of
075: * the WAS/WSS behaviour is specified to be the same, much of the code of this
076: * class is shared, see for example the handleResult method.
077: *
078: * @see #handleResult(Object)
079: *
080: * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a>
081: * @author last edited by: $Author: apoth $
082: *
083: * @version 2.0, $Revision: 9338 $, $Date: 2007-12-27 04:31:31 -0800 (Thu, 27 Dec 2007) $
084: *
085: * @since 2.0
086: */
087:
088: public class WASSHandler extends AbstractOWServiceHandler implements
089: ServiceDispatcher {
090:
091: private static final ILogger LOG = LoggerFactory
092: .getLogger(WASSHandler.class);
093:
094: private HttpServletResponse response = null;
095:
096: /**
097: * Method to handle the various output objects.
098: *
099: * @param result
100: * @throws IOException
101: */
102: private void handleResult(Object result) throws IOException {
103: // if result is null, was possibly a CloseSession request, so return
104: // nothing as specified
105: if (result == null) {
106: response.setContentType("text/plain; charset="
107: + CharsetUtils.getSystemCharset());
108: response.getWriter().println();
109: }
110:
111: if (result instanceof OGCWebServiceException) {
112: sendException(response, (OGCWebServiceException) result);
113: } else if (result instanceof Exception) {
114: sendException(response, (Exception) result);
115: } else if (result instanceof String) {
116: // just write the SessionID result from GetSession request
117: response.setContentType("text/plain; charset="
118: + CharsetUtils.getSystemCharset());
119: response.getWriter().print(result);
120: } else if (result instanceof WASCapabilities) {
121: sendCapabilities((WASCapabilities) result);
122: } else if (result instanceof WSSCapabilities) {
123: sendCapabilities((WSSCapabilities) result);
124: } else if (result instanceof DescribeUserResponse) {
125: sendDescribeUserResponse((DescribeUserResponse) result);
126: } else if (result instanceof DoServiceResponse) {
127: sendOtherServiceResponse((DoServiceResponse) result);
128: }
129: }
130:
131: /**
132: * Sends the XML document contained within the given parameter
133: *
134: * @param result
135: * @throws IOException
136: */
137: private void sendDescribeUserResponse(DescribeUserResponse result)
138: throws IOException {
139: response.setContentType("text/xml; charset="
140: + CharsetUtils.getSystemCharset());
141: result.write(response.getOutputStream());
142: }
143:
144: /**
145: * Method to send the result of another service.
146: *
147: * @param result
148: * @throws IOException
149: */
150: private void sendOtherServiceResponse(DoServiceResponse result)
151: throws IOException {
152: Header[] headers = result.getHeaders();
153: // footers will be ignored for now
154: // Header[] footers = result.getFooters();
155: InputStream in = result.getResponseBody();
156: for (Header h : headers) {
157: // maybe we have to filter some headers here TODO
158: LOG.logDebug(h.toExternalForm());
159: response.setHeader(h.getName(), h.getValue());
160: }
161: // BufferedReader bin = new BufferedReader( new InputStreamReader( in ) );
162: // PrintWriter out = response.getWriter();
163: BufferedInputStream bin = new BufferedInputStream(in);
164: OutputStream out = response.getOutputStream();
165: byte[] buf = new byte[4096];
166: int numberRead = 0;
167: while ((numberRead = bin.read(buf)) != -1) {
168: out.write(buf, 0, numberRead);
169: }
170: bin.close();
171: out.close();
172: }
173:
174: /**
175: * Sends the given capabilities.
176: *
177: * @param capabilities
178: */
179: private void sendCapabilities(WSSCapabilities capabilities) {
180: try {
181: response.setContentType("text/xml; charset="
182: + CharsetUtils.getSystemCharset());
183: WSSCapabilitiesDocument document = XMLFactory
184: .export(capabilities);
185: document.write(response.getOutputStream());
186: } catch (IOException e) {
187: LOG.logError("Error sending GetCapabilities response.", e);
188: }
189: }
190:
191: /**
192: * Sends the given capabilities.
193: *
194: * @param capabilities
195: */
196: private void sendCapabilities(WASCapabilities capabilities) {
197: try {
198: response.setContentType("text/xml; charset="
199: + CharsetUtils.getSystemCharset());
200: WASCapabilitiesDocument document = XMLFactory
201: .export(capabilities);
202: document.write(response.getOutputStream());
203: } catch (IOException e) {
204: LOG.logError("Error sending GetCapabilities response.", e);
205: }
206: }
207:
208: /*
209: * (non-Javadoc)
210: *
211: * @see org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.ogcwebservices.OGCWebServiceRequest,
212: * javax.servlet.http.HttpServletResponse)
213: */
214: public void perform(OGCWebServiceRequest request,
215: HttpServletResponse response) throws ServiceException,
216: OGCWebServiceException {
217: Object result = null;
218: this .response = response;
219: if ("WAS".equals(request.getServiceName())) {
220: WAService service = WASServiceFactory
221: .getUncachedWAService(); // get from factory
222: result = service.doService(request);
223: } else if ("WSS".equals(request.getServiceName())) {
224: WSService service = WASServiceFactory
225: .getUncachedWSService(); // get from factory
226: result = service.doService(request);
227: }
228:
229: try {
230: handleResult(result);
231: } catch (IOException e) {
232: LOG.logError(e.getLocalizedMessage(), e);
233: throw new OGCWebServiceException(
234: "Error while handling request: \n"
235: + e.getLocalizedMessage());
236: }
237: }
238:
239: }
|