001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/src/org/deegree/ogcwebservices/wpvs/WMSInvoker.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.ogcwebservices.wpvs;
038    
039    import java.awt.Color;
040    import java.awt.Graphics2D;
041    import java.awt.Image;
042    import java.awt.image.BufferedImage;
043    import java.util.Map;
044    
045    import org.deegree.datatypes.values.Values;
046    import org.deegree.framework.log.ILogger;
047    import org.deegree.framework.log.LoggerFactory;
048    import org.deegree.framework.util.IDGenerator;
049    import org.deegree.framework.util.StringTools;
050    import org.deegree.ogcwebservices.OGCWebService;
051    import org.deegree.ogcwebservices.OGCWebServiceException;
052    import org.deegree.ogcwebservices.wms.operation.GetMap;
053    import org.deegree.ogcwebservices.wms.operation.GetMapResult;
054    import org.deegree.ogcwebservices.wpvs.configuration.AbstractDataSource;
055    import org.deegree.ogcwebservices.wpvs.configuration.LocalWMSDataSource;
056    import org.deegree.ogcwebservices.wpvs.utils.ImageUtils;
057    import org.deegree.ogcwebservices.wpvs.utils.ResolutionStripe;
058    
059    /**
060     * Invoker for a Web Map Service.
061     *
062     * @author <a href="mailto:taddei@lat-lon.de">Ugo Taddei</a>
063     * @author last edited by: $Author: mschneider $
064     *
065     * $Revision: 18195 $, $Date: 2009-06-18 17:55:39 +0200 (Do, 18 Jun 2009) $
066     */
067    public class WMSInvoker extends GetViewServiceInvoker {
068    
069        private static final ILogger LOG = LoggerFactory.getLogger( WMSInvoker.class );
070    
071        private int id;
072    
073        /**
074         * Creates a new instance of this class.
075         *
076         * @param owner
077         *            the handler that owns this invoker
078         * @param id
079         *            which can be used to sort all the request/responses in the resolutionStripe.
080         */
081        public WMSInvoker( ResolutionStripe owner, int id ) {
082            super( owner );
083            this.id = id;
084        }
085    
086        @Override
087        public void invokeService( AbstractDataSource dataSource ) {
088            // int count = 0;
089            if ( !( dataSource != null && ( AbstractDataSource.LOCAL_WMS == dataSource.getServiceType() || AbstractDataSource.REMOTE_WMS == dataSource.getServiceType() ) ) ) {
090                LOG.logError( "The given AbstractDataSource is no LocalWMSDataSource instance. It is needed for a WMSInvoker" );
091                throw new RuntimeException( "DataSource should be a WMS-instance for a WMSInvoker" );
092            }
093            OGCWebService service = null;
094            try {
095                service = dataSource.getOGCWebService();
096            } catch ( OGCWebServiceException ogcwe ) {
097                LOG.logError( ogcwe.getMessage() );
098                throw new RuntimeException( ogcwe );
099            }
100    
101            Object response = null;
102            try {
103                GetMap getMapRequest = createGetMapRequest( (LocalWMSDataSource) dataSource );
104                if ( getMapRequest == null ) {
105                    return;
106                }
107                LOG.logDebug( dataSource.getName() + ": WMS request: " + getMapRequest );
108                /**
109                 * Invoke the wms service.
110                 */
111                response = service.doService( getMapRequest );
112            } catch ( OGCWebServiceException ogcwse ) {
113                if ( !Thread.currentThread().isInterrupted() ) {
114                    StringBuilder msg = new StringBuilder("Error when performing WMS (" );
115                        msg.append( dataSource.getName() );
116                        msg.append( "GetMap: ");
117                        msg.append( ogcwse.getLocalizedMessage() );
118                    LOG.logError( msg.toString(), ogcwse );
119                    resolutionStripe.setTextureRetrievalException( dataSource.getName().getFormattedString() + id,
120                                                                   ogcwse );
121                }
122    
123                return;
124            }
125            if ( response != null ) {
126                if ( response instanceof GetMapResult ) {
127                    BufferedImage responseImage = null;
128                    // the exception may be inside the response
129                    if ( ( (GetMapResult) response ).getException() != null ) {
130                        resolutionStripe.setTextureRetrievalException( dataSource.getName().getFormattedString() + id,
131                                                                       ( (GetMapResult) response ).getException() );
132                    } else {
133                        responseImage = (BufferedImage) ( (GetMapResult) response ).getMap();
134                        // LOG.logDebug( StringTools.concat( 100, "WMS RESULT: ", response ) );
135                        if ( responseImage != null ) {
136                            Color[] colors = ( (LocalWMSDataSource) dataSource ).getTransparentColors();
137                            if ( colors != null && colors.length > 0 ) {
138                                ImageUtils imgUtil = new ImageUtils( colors );
139                                Image img = imgUtil.filterImage( responseImage );
140                                Graphics2D g2d = (Graphics2D) responseImage.getGraphics();
141                                g2d.drawImage( img, 0, 0, null );
142                                g2d.dispose();
143                            }
144                            resolutionStripe.addTexture( ( dataSource.getName().getFormattedString() + id ), responseImage );
145                        }
146                    }
147                }
148            }
149        }
150    
151        /**
152         * Creates a new GetMap request for the given datasource.
153         *
154         * @param ds
155         *            the WMs datasource
156         * @return a new GetMap request
157         */
158        private GetMap createGetMapRequest( LocalWMSDataSource ds ) {
159    
160            GetMap getMapRequest = ds.getPartialGetMapRequest();
161    
162            Values elevation = null;
163            Values time = null;
164            Map<String, Values> sampleDim = null;
165    
166            // int tileSize = owner.getMaxTileSize();
167            int tileWidth = resolutionStripe.getRequestWidthForBBox();
168            int tileHeight = resolutionStripe.getRequestHeightForBBox();
169            if ( tileWidth == -1 || tileHeight == -1 ) {
170                LOG.logError( StringTools.concat( 500,
171                                                  "Not creating WMS-GetMap because the height or the width of the image are -1: " ) );
172                return null;
173            }
174            // GeometryUtils.getImageSizeForSurface( (RankedSurface)box, owner.getMaxTileSize());
175    
176            IDGenerator idg = IDGenerator.getInstance();
177            getMapRequest = GetMap.create( getMapRequest.getVersion(), String.valueOf( idg.generateUniqueID() ),
178                                           getMapRequest.getLayers(), elevation, sampleDim, getMapRequest.getFormat(),
179                                           tileWidth, tileHeight, resolutionStripe.getCRSName().getFormattedString(),
180                                           resolutionStripe.getSurface().getEnvelope(), getMapRequest.getTransparency(),
181                                           getMapRequest.getBGColor(), getMapRequest.getExceptions(), time, null, null,
182                                           null );
183    
184            return getMapRequest;
185        }
186    }