001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/branches/2.2_testing/src/org/deegree/enterprise/servlet/WMPSHandler.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     Aennchenstraße 19
030     53177 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.PrintWriter;
047    
048    import javax.servlet.http.HttpServletResponse;
049    
050    import org.deegree.framework.log.ILogger;
051    import org.deegree.framework.log.LoggerFactory;
052    import org.deegree.framework.util.NetWorker;
053    import org.deegree.framework.util.StringTools;
054    import org.deegree.framework.xml.DOMPrinter;
055    import org.deegree.framework.xml.XMLFragment;
056    import org.deegree.ogcwebservices.OGCWebService;
057    import org.deegree.ogcwebservices.OGCWebServiceException;
058    import org.deegree.ogcwebservices.OGCWebServiceRequest;
059    import org.deegree.ogcwebservices.OGCWebServiceResponse;
060    import org.deegree.ogcwebservices.wmps.WMPService;
061    import org.deegree.ogcwebservices.wmps.WMPServiceFactory;
062    import org.deegree.ogcwebservices.wmps.XMLFactory;
063    import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilities;
064    import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilitiesDocument;
065    import org.deegree.ogcwebservices.wmps.configuration.WMPSConfiguration;
066    import org.deegree.ogcwebservices.wmps.operation.PrintMapResponseDocument;
067    import org.deegree.ogcwebservices.wmps.operation.WMPSGetCapabilitiesResult;
068    import org.deegree.ogcwebservices.wms.InvalidFormatException;
069    
070    /**
071     * 
072     * Web servlet client for WMPS.
073     * 
074     * @author <a href="mailto:deshmukh@lat-lon.de">Anup Deshmukh</a>
075     * 
076     * @author last edited by: $Author: apoth $
077     * 
078     * @version 2.0, $Revision: 9348 $, $Date: 2007-12-27 17:59:14 +0100 (Do, 27 Dez 2007) $
079     * 
080     * @since 2.0
081     */
082    
083    public class WMPSHandler extends AbstractOWServiceHandler  {
084    
085        private static ILogger LOG = LoggerFactory.getLogger( WMPSHandler.class );
086    
087        private HttpServletResponse resp = null;
088        
089        private WMPSConfiguration configuration = null;
090    
091        WMPSHandler() {
092            LOG.logDebug( "New WMPSHandler instance created: "
093                + this.getClass().getName() );
094        }
095    
096        /**
097         * performs the passed OGCWebServiceRequest by accessing service from the pool and passing the
098         * request to it
099         */
100        public void perform( OGCWebServiceRequest request, HttpServletResponse response )
101            throws OGCWebServiceException {
102    
103            resp = response;
104            OGCWebService service;
105            try {
106                service = WMPServiceFactory.getService();
107            } catch (Exception e) {
108                throw new OGCWebServiceException( "Error performing the WMPService(s)." );
109            }
110            configuration = (WMPSConfiguration) ( (WMPService) service ).getCapabilities();
111            if ( service == null ) {
112                OGCWebServiceException exce = 
113                    new OGCWebServiceException( "WMPS:WMPS", "could not access a WMPService instance" );
114                sendException( response, exce );
115                return;
116            }
117            Object o = service.doService( request );
118            handleResponse( o );
119        }
120    
121        /**
122         * 
123         * 
124         * @param result
125         */
126        private void handleResponse( Object result ) {
127            
128    
129            try {
130                OGCWebServiceResponse response = (OGCWebServiceResponse) result;
131                if ( response.getException() != null ) {
132                    // handle the case that an exception occured during the
133                    // request performance
134                    OGCWebServiceException exce = response.getException();
135                    sendException( resp, exce );
136                } else {
137                    if ( response instanceof WMPSGetCapabilitiesResult ) {
138                        handleGetCapabilitiesResponse( (WMPSGetCapabilitiesResult) response );
139                    } else if ( response instanceof PrintMapResponseDocument ) {
140                        handlePrintMapResponse( (PrintMapResponseDocument) response );
141                    }
142                }
143            } catch (InvalidFormatException ife) {
144                LOG.logError( ife.getMessage(), ife );            
145                OGCWebServiceException exce = 
146                    new OGCWebServiceException( "InvalidFormat", ife.getMessage() );
147                sendException( resp, exce );
148            } catch (Exception e) {
149                LOG.logError( e.getMessage(), e );
150                OGCWebServiceException exce = new OGCWebServiceException( "WMPS:write",e.getMessage() );
151                sendException( resp, exce );
152            }
153    
154            
155        }
156    
157        /**
158         * handles the response to a get capabilities request
159         * 
160         * @param response
161         */
162        private void handleGetCapabilitiesResponse( WMPSGetCapabilitiesResult response )
163            throws Exception {
164            
165            resp.setContentType( "application/vnd.ogc.wms_xml" );
166            WMPSCapabilities capa = response.getCapabilities();
167            WMPSCapabilitiesDocument doc = XMLFactory.export( capa );
168    
169            // XMLFragment frag = doc.transform( url.openStream() , XMLFragment.DEFAULT_URL );
170            String xml = DOMPrinter.nodeToString( doc.getRootElement(), "" );
171    
172            String dtd = NetWorker.url2String( configuration.getDeegreeParams().getDTDLocation() );
173            StringBuffer sb = new StringBuffer();
174            sb.append( "<!DOCTYPE WMT_PS_Capabilities SYSTEM " );
175            sb.append( "'"
176                + dtd + "' \n" );
177            sb.append( "[\n<!ELEMENT VendorSpecificCapabilities EMPTY>\n]>" );
178    
179            xml = StringTools.replace( xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
180                "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
181                    + "\n" + sb.toString(), false );
182            xml = StringTools.replace( xml, "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"",
183                "", false );
184    
185            try {
186                PrintWriter pw = resp.getWriter();
187                pw.print( xml );
188                pw.close();
189            } catch (Exception e) {
190                LOG.logError( "-", e );
191            }
192    
193            
194        }
195    
196        /**
197         * handles the response to a print map request
198         * 
199         * @param response
200         */
201        private void handlePrintMapResponse( PrintMapResponseDocument response ) {
202    
203            resp.setContentType( "application/vnd.ogc.wms_xml" );
204    
205            XMLFragment xml = new XMLFragment( response.getRootElement() );
206            try {
207                PrintWriter pw = resp.getWriter();
208                //pw.append( "id=");
209                //pw.append( response.getRootElement().getAttribute( "id" ) );
210                xml.prettyPrint( pw );
211                pw.close();
212            } catch (Exception e) {
213                LOG.logError( "-", e );
214            }
215    
216            
217    
218        }
219    
220       
221    }