001    //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/branches/2.3_testing/src/org/deegree/enterprise/servlet/WMPSHandler.java $
002    /*----------------------------------------------------------------------------
003     This file is part of deegree, http://deegree.org/
004     Copyright (C) 2001-2009 by:
005       Department of Geography, University of Bonn
006     and
007       lat/lon GmbH
008    
009     This library is free software; you can redistribute it and/or modify it under
010     the terms of the GNU Lesser General Public License as published by the Free
011     Software Foundation; either version 2.1 of the License, or (at your option)
012     any later version.
013     This library is distributed in the hope that it will be useful, but WITHOUT
014     ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
015     FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more
016     details.
017     You should have received a copy of the GNU Lesser General Public License
018     along with this library; if not, write to the Free Software Foundation, Inc.,
019     59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
020    
021     Contact information:
022    
023     lat/lon GmbH
024     Aennchenstr. 19, 53177 Bonn
025     Germany
026     http://lat-lon.de/
027    
028     Department of Geography, University of Bonn
029     Prof. Dr. Klaus Greve
030     Postfach 1147, 53001 Bonn
031     Germany
032     http://www.geographie.uni-bonn.de/deegree/
033    
034     e-mail: info@deegree.org
035    ----------------------------------------------------------------------------*/
036    
037    package org.deegree.enterprise.servlet;
038    
039    import java.io.PrintWriter;
040    
041    import javax.servlet.http.HttpServletResponse;
042    
043    import org.deegree.framework.log.ILogger;
044    import org.deegree.framework.log.LoggerFactory;
045    import org.deegree.framework.util.NetWorker;
046    import org.deegree.framework.util.StringTools;
047    import org.deegree.framework.xml.DOMPrinter;
048    import org.deegree.framework.xml.XMLFragment;
049    import org.deegree.ogcwebservices.OGCWebService;
050    import org.deegree.ogcwebservices.OGCWebServiceException;
051    import org.deegree.ogcwebservices.OGCWebServiceRequest;
052    import org.deegree.ogcwebservices.OGCWebServiceResponse;
053    import org.deegree.ogcwebservices.wmps.WMPService;
054    import org.deegree.ogcwebservices.wmps.WMPServiceFactory;
055    import org.deegree.ogcwebservices.wmps.XMLFactory;
056    import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilities;
057    import org.deegree.ogcwebservices.wmps.capabilities.WMPSCapabilitiesDocument;
058    import org.deegree.ogcwebservices.wmps.configuration.WMPSConfiguration;
059    import org.deegree.ogcwebservices.wmps.operation.PrintMapResponseDocument;
060    import org.deegree.ogcwebservices.wmps.operation.WMPSGetCapabilitiesResult;
061    import org.deegree.ogcwebservices.wms.InvalidFormatException;
062    
063    /**
064     *
065     * Web servlet client for WMPS.
066     *
067     * @author <a href="mailto:deshmukh@lat-lon.de">Anup Deshmukh</a>
068     *
069     * @author last edited by: $Author: mschneider $
070     *
071     * @version 2.0, $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18. Jun 2009) $
072     *
073     * @since 2.0
074     */
075    
076    public class WMPSHandler extends AbstractOWServiceHandler {
077    
078        private static ILogger LOG = LoggerFactory.getLogger( WMPSHandler.class );
079    
080        private HttpServletResponse resp = null;
081    
082        private WMPSConfiguration configuration = null;
083    
084        /**
085         * Create an empty WMPSHandler
086         */
087        WMPSHandler() {
088            LOG.logDebug( "New WMPSHandler instance created: " + this.getClass().getName() );
089        }
090    
091        /**
092         * performs the passed OGCWebServiceRequest by accessing service from the pool and passing the
093         * request to it
094         */
095        public void perform( OGCWebServiceRequest request, HttpServletResponse response )
096                                throws OGCWebServiceException {
097    
098            resp = response;
099            OGCWebService service;
100            try {
101                service = WMPServiceFactory.getService();
102            } catch ( Exception e ) {
103                throw new OGCWebServiceException( "Error performing the WMPService(s)." );
104            }
105            configuration = (WMPSConfiguration) ( (WMPService) service ).getCapabilities();
106    //        if ( service == null ) {
107    //            OGCWebServiceException exce = new OGCWebServiceException( "WMPS:WMPS",
108    //                                                                      "could not access a WMPService instance" );
109    //            sendException( response, exce );
110    //            return;
111    //        }
112            Object o = service.doService( request );
113            handleResponse( o );
114        }
115    
116        /**
117         *
118         *
119         * @param result
120         */
121        private void handleResponse( Object result ) {
122    
123            try {
124                OGCWebServiceResponse response = (OGCWebServiceResponse) result;
125                if ( response.getException() != null ) {
126                    // handle the case that an exception occured during the
127                    // request performance
128                    OGCWebServiceException exce = response.getException();
129                    sendException( resp, exce );
130                } else {
131                    if ( response instanceof WMPSGetCapabilitiesResult ) {
132                        handleGetCapabilitiesResponse( (WMPSGetCapabilitiesResult) response );
133                    } else if ( response instanceof PrintMapResponseDocument ) {
134                        handlePrintMapResponse( (PrintMapResponseDocument) response );
135                    }
136                }
137            } catch ( InvalidFormatException ife ) {
138                LOG.logError( ife.getMessage(), ife );
139                OGCWebServiceException exce = new OGCWebServiceException( "InvalidFormat", ife.getMessage() );
140                sendException( resp, exce );
141            } catch ( Exception e ) {
142                LOG.logError( e.getMessage(), e );
143                OGCWebServiceException exce = new OGCWebServiceException( "WMPS:write", e.getMessage() );
144                sendException( resp, exce );
145            }
146    
147        }
148    
149        /**
150         * handles the response to a get capabilities request
151         *
152         * @param response
153         */
154        private void handleGetCapabilitiesResponse( WMPSGetCapabilitiesResult response )
155                                throws Exception {
156    
157            resp.setContentType( "application/vnd.ogc.wms_xml" );
158            WMPSCapabilities capa = response.getCapabilities();
159            WMPSCapabilitiesDocument doc = XMLFactory.export( capa );
160    
161            // XMLFragment frag = doc.transform( url.openStream() , XMLFragment.DEFAULT_URL );
162            String xml = DOMPrinter.nodeToString( doc.getRootElement(), "" );
163    
164            String dtd = NetWorker.url2String( configuration.getDeegreeParams().getDTDLocation() );
165            StringBuffer sb = new StringBuffer();
166            sb.append( "<!DOCTYPE WMT_PS_Capabilities SYSTEM " );
167            sb.append( "'" + dtd + "' \n" );
168            sb.append( "[\n<!ELEMENT VendorSpecificCapabilities EMPTY>\n]>" );
169    
170            xml = StringTools.replace( xml, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>",
171                                       "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" + sb.toString(), false );
172            xml = StringTools.replace( xml, "xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"", "", false );
173    
174            try {
175                PrintWriter pw = resp.getWriter();
176                pw.print( xml );
177                pw.close();
178            } catch ( Exception e ) {
179                LOG.logError( "-", e );
180            }
181    
182        }
183    
184        /**
185         * handles the response to a print map request
186         *
187         * @param response
188         */
189        private void handlePrintMapResponse( PrintMapResponseDocument response ) {
190    
191            resp.setContentType( "application/vnd.ogc.wms_xml" );
192    
193            XMLFragment xml = new XMLFragment( response.getRootElement() );
194            try {
195                PrintWriter pw = resp.getWriter();
196                // pw.append( "id=");
197                // pw.append( response.getRootElement().getAttribute( "id" ) );
198                xml.prettyPrint( pw );
199                pw.close();
200            } catch ( Exception e ) {
201                LOG.logError( "-", e );
202            }
203    
204        }
205    
206    }