001 //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/enterprise/servlet/WASSHandler.java $ 002 /*---------------- FILE HEADER ------------------------------------------ 003 004 This file is part of deegree. 005 Copyright (C) 2001-2004 by: 006 EXSE, Department of Geography, University of Bonn 007 http://www.giub.uni-bonn.de/exse/ 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 Meckenheimer Allee 176 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.BufferedInputStream; 047 import java.io.IOException; 048 import java.io.InputStream; 049 import java.io.OutputStream; 050 051 import javax.servlet.http.HttpServletResponse; 052 053 import org.apache.commons.httpclient.Header; 054 import org.deegree.enterprise.ServiceException; 055 import org.deegree.framework.log.ILogger; 056 import org.deegree.framework.log.LoggerFactory; 057 import org.deegree.framework.util.CharsetUtils; 058 import org.deegree.ogcwebservices.OGCWebServiceException; 059 import org.deegree.ogcwebservices.OGCWebServiceRequest; 060 import org.deegree.ogcwebservices.wass.common.WASServiceFactory; 061 import org.deegree.ogcwebservices.wass.common.XMLFactory; 062 import org.deegree.ogcwebservices.wass.was.WAService; 063 import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilities; 064 import org.deegree.ogcwebservices.wass.was.capabilities.WASCapabilitiesDocument; 065 import org.deegree.ogcwebservices.wass.was.operation.DescribeUserResponse; 066 import org.deegree.ogcwebservices.wass.wss.WSService; 067 import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilities; 068 import org.deegree.ogcwebservices.wass.wss.capabilities.WSSCapabilitiesDocument; 069 import org.deegree.ogcwebservices.wass.wss.operation.DoServiceResponse; 070 071 /** 072 * This is the servlet handler class for the WASS services, ie, the Web 073 * Authentication Service and the Web Security Service. Attention: since much of 074 * the WAS/WSS behaviour is specified to be the same, much of the code of this 075 * class is shared, see for example the handleResult method. 076 * 077 * @see #handleResult(Object) 078 * 079 * @author <a href="mailto:schmitz@lat-lon.de">Andreas Schmitz</a> 080 * @author last edited by: $Author: bezema $ 081 * 082 * @version 2.0, $Revision: 6259 $, $Date: 2007-03-20 10:15:15 +0100 (Di, 20 Mär 2007) $ 083 * 084 * @since 2.0 085 */ 086 087 public class WASSHandler extends AbstractOWServiceHandler implements ServiceDispatcher { 088 089 private static final ILogger LOG = LoggerFactory.getLogger( WASSHandler.class ); 090 091 private HttpServletResponse response = null; 092 093 /** 094 * Method to handle the various output objects. 095 * 096 * @param result 097 * @throws IOException 098 */ 099 private void handleResult( Object result ) 100 throws IOException { 101 // if result is null, was possibly a CloseSession request, so return 102 // nothing as specified 103 if ( result == null ){ 104 response.setContentType( "text/plain; charset=" + CharsetUtils.getSystemCharset() ); 105 response.getWriter().println(); 106 } 107 108 if ( result instanceof OGCWebServiceException ) { 109 sendException( response, (OGCWebServiceException) result ); 110 } else if ( result instanceof Exception ) { 111 sendException( response, (Exception) result ); 112 } else if ( result instanceof String ) { 113 // just write the SessionID result from GetSession request 114 response.setContentType( "text/plain; charset=" + CharsetUtils.getSystemCharset() ); 115 response.getWriter().print( result ); 116 } else if ( result instanceof WASCapabilities ) { 117 sendCapabilities( (WASCapabilities) result ); 118 } else if ( result instanceof WSSCapabilities ) { 119 sendCapabilities( (WSSCapabilities) result ); 120 } else if ( result instanceof DescribeUserResponse ) { 121 sendDescribeUserResponse( (DescribeUserResponse) result ); 122 } else if ( result instanceof DoServiceResponse ) { 123 sendOtherServiceResponse( (DoServiceResponse) result ); 124 } 125 } 126 127 /** 128 * Sends the XML document contained within the given parameter 129 * 130 * @param result 131 * @throws IOException 132 */ 133 private void sendDescribeUserResponse( DescribeUserResponse result ) throws IOException { 134 response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() ); 135 result.write( response.getOutputStream() ); 136 } 137 138 /** 139 * Method to send the result of another service. 140 * 141 * @param result 142 * @throws IOException 143 */ 144 private void sendOtherServiceResponse( DoServiceResponse result ) 145 throws IOException { 146 Header[] headers = result.getHeaders(); 147 // footers will be ignored for now 148 // Header[] footers = result.getFooters(); 149 InputStream in = result.getResponseBody(); 150 for ( Header h : headers ) { 151 // maybe we have to filter some headers here TODO 152 LOG.logDebug( h.toExternalForm() ); 153 response.setHeader( h.getName(), h.getValue() ); 154 } 155 // BufferedReader bin = new BufferedReader( new InputStreamReader( in ) ); 156 // PrintWriter out = response.getWriter(); 157 BufferedInputStream bin = new BufferedInputStream( in ); 158 OutputStream out = response.getOutputStream(); 159 byte[] buf = new byte[4096]; 160 int numberRead = 0; 161 while ( (numberRead = bin.read( buf )) != -1 ){ 162 out.write( buf, 0, numberRead ); 163 } 164 bin.close(); 165 out.close(); 166 } 167 168 /** 169 * Sends the given capabilities. 170 * 171 * @param capabilities 172 */ 173 private void sendCapabilities( WSSCapabilities capabilities ) { 174 try { 175 response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() ); 176 WSSCapabilitiesDocument document = XMLFactory.export( capabilities ); 177 document.write( response.getOutputStream() ); 178 } catch ( IOException e ) { 179 LOG.logError( "Error sending GetCapabilities response.", e ); 180 } 181 } 182 183 /** 184 * Sends the given capabilities. 185 * 186 * @param capabilities 187 */ 188 private void sendCapabilities( WASCapabilities capabilities ) { 189 try { 190 response.setContentType( "text/xml; charset=" + CharsetUtils.getSystemCharset() ); 191 WASCapabilitiesDocument document = XMLFactory.export( capabilities ); 192 document.write( response.getOutputStream() ); 193 } catch ( IOException e ) { 194 LOG.logError( "Error sending GetCapabilities response.", e ); 195 } 196 } 197 198 /* 199 * (non-Javadoc) 200 * 201 * @see org.deegree.enterprise.servlet.ServiceDispatcher#perform(org.deegree.ogcwebservices.OGCWebServiceRequest, 202 * javax.servlet.http.HttpServletResponse) 203 */ 204 public void perform( OGCWebServiceRequest request, HttpServletResponse response ) 205 throws ServiceException, OGCWebServiceException { 206 Object result = null; 207 this.response = response; 208 if ( "WAS".equals( request.getServiceName() ) ) { 209 WAService service = WASServiceFactory.getUncachedWAService(); // get from factory 210 result = service.doService( request ); 211 } else if ( "WSS".equals( request.getServiceName() ) ) { 212 WSService service = WASServiceFactory.getUncachedWSService(); // get from factory 213 result = service.doService( request ); 214 } 215 216 try { 217 handleResult( result ); 218 } catch ( IOException e ) { 219 LOG.logError( e.getLocalizedMessage(), e ); 220 throw new OGCWebServiceException( "Error while handling request: \n" 221 + e.getLocalizedMessage() ); 222 } 223 } 224 225 } 226 227 /* ****************************************************************************** 228 * Changes to this class. What the people have been up to: 229 * $Log$ 230 * Revision 1.11 2006/10/17 20:31:18 poth 231 * *** empty log message *** 232 * 233 * Revision 1.10 2006/08/29 11:41:45 schmitz 234 * Removed the newline when outputting the sessionID. 235 * 236 * Revision 1.9 2006/08/11 08:58:50 schmitz 237 * WAS implements the DescribeUser operation. 238 * 239 * Revision 1.8 2006/07/23 10:05:54 poth 240 * setting content type for Http responses enhanced by adding charset (for mime types text/plain and text/xml) 241 * 242 * Revision 1.7 2006/06/26 15:04:06 bezema 243 * Finished the wass 244 * 245 * Revision 1.6 2006/06/22 06:52:46 poth 246 * footer corrected 247 * 248 * 249 ***************************************************************************** */