001    //$HeadURL: http://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/ogcwebservices/OWSUtils.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    package org.deegree.ogcwebservices;
037    
038    import java.net.URL;
039    import java.util.List;
040    
041    import org.deegree.datatypes.QualifiedName;
042    import org.deegree.ogcwebservices.getcapabilities.DCPType;
043    import org.deegree.ogcwebservices.getcapabilities.GetCapabilities;
044    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
045    import org.deegree.ogcwebservices.wcs.describecoverage.DescribeCoverage;
046    import org.deegree.ogcwebservices.wcs.getcapabilities.WCSCapabilities;
047    import org.deegree.ogcwebservices.wcs.getcoverage.GetCoverage;
048    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
049    import org.deegree.ogcwebservices.wfs.capabilities.WFSOperationsMetadata;
050    import org.deegree.ogcwebservices.wfs.operation.DescribeFeatureType;
051    import org.deegree.ogcwebservices.wfs.operation.GetFeature;
052    import org.deegree.ogcwebservices.wfs.operation.GetFeatureWithLock;
053    import org.deegree.ogcwebservices.wfs.operation.Lock;
054    import org.deegree.ogcwebservices.wfs.operation.transaction.Transaction;
055    import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
056    import org.deegree.ogcwebservices.wms.operation.GetFeatureInfo;
057    import org.deegree.ogcwebservices.wms.operation.GetLegendGraphic;
058    import org.deegree.ogcwebservices.wms.operation.GetMap;
059    import org.deegree.owscommon_new.DCP;
060    import org.deegree.owscommon_new.HTTP;
061    import org.deegree.owscommon_new.Operation;
062    import org.deegree.owscommon_new.OperationsMetadata;
063    
064    /**
065     * 
066     * 
067     * 
068     * @version $Revision: 31194 $
069     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
070     * @author last edited by: $Author: apoth $
071     * 
072     * @version 1.0. $Revision: 31194 $, $Date: 2011-07-04 09:26:48 +0200 (Mo, 04 Jul 2011) $
073     * 
074     * @since 2.0
075     */
076    public class OWSUtils {
077    
078        /**
079         * according to OGC WMS 1.3 Testsuite a URL to a service operation via HTTPGet must end with '?' or '&'
080         * 
081         * @param href
082         * @return the parameter, changed to fulfill the spec
083         */
084        public static String validateHTTPGetBaseURL( String href ) {
085            if ( !href.toLowerCase().startsWith( "file:" ) ) {
086                if ( !href.endsWith( "/" ) ) {
087                    if ( href.indexOf( '?' ) < 0 ) {
088                        href = href + '?';
089                    } else if ( !href.endsWith( "&" ) && !href.endsWith( "?" ) ) {
090                        href = href + '&';
091                    }
092                }
093            }
094            return href;
095        }
096    
097        /**
098         * the method return the first HTTPGet URL for a Operation within the pass capabilities document. If No URL can be
099         * found (e.g. the service does not support the operation via HTTP Get) <code>null</code> will be returned
100         * 
101         * @param capabilities
102         * @param clss
103         * @return see above
104         */
105        @SuppressWarnings("unchecked")
106        public synchronized static URL getHTTPGetOperationURL( OGCCapabilities capabilities, Class clss ) {
107            URL url = null;
108            if ( capabilities instanceof WMSCapabilities ) {
109                url = getHTTPGetOperationURL( (WMSCapabilities) capabilities, clss );
110            } else if ( capabilities instanceof WFSCapabilities ) {
111                url = getHTTPGetOperationURL( (WFSCapabilities) capabilities, clss );
112            } else if ( capabilities instanceof WCSCapabilities ) {
113                url = getHTTPGetOperationURL( (WCSCapabilities) capabilities, clss );
114            } else {
115                // TODO
116                // support more service types
117                // possibly use generic base capabilities to extract it
118            }
119            return url;
120    
121        }
122    
123        /**
124         * @see #getHTTPGetOperationURL(OGCCapabilities, Class)
125         * @param capabilities
126         * @param clss
127         * @return the first operation URL
128         */
129        @SuppressWarnings("unchecked")
130        private synchronized static URL getHTTPGetOperationURL( WMSCapabilities capabilities, Class clss ) {
131    
132            OperationsMetadata om = capabilities.getOperationMetadata();
133            List<DCP> dcps = null;
134            if ( clss.equals( GetMap.class ) ) {
135                Operation op = om.getOperation( new QualifiedName( "GetMap" ) );
136                if ( op == null ) {
137                    op = om.getOperation( new QualifiedName( "map" ) );
138                }
139                dcps = op.getDCP();
140            } else if ( clss.equals( GetCapabilities.class ) ) {
141                Operation op = om.getOperation( new QualifiedName( "GetCapabilities" ) );
142                if ( op == null ) {
143                    op = om.getOperation( new QualifiedName( "capabilities" ) );
144                }
145                dcps = op.getDCP();
146            } else if ( clss.equals( GetFeatureInfo.class ) ) {
147                Operation op = om.getOperation( new QualifiedName( "GetFeatureInfo" ) );
148                if ( op == null ) {
149                    op = om.getOperation( new QualifiedName( "FeatureInfo" ) );
150                }
151                dcps = op.getDCP();
152            } else if ( clss.equals( GetLegendGraphic.class ) ) {
153                Operation op = om.getOperation( new QualifiedName( "GetLegendGraphic" ) );
154                dcps = op.getDCP();
155            }
156    
157            // search for the first HTTP Get link
158            if ( dcps == null ) {
159                return null;
160            }
161            for ( DCP dcp : dcps ) {
162                if ( dcp instanceof HTTP ) {
163                    HTTP http = (HTTP) dcp;
164                    List<URL> urls = http.getGetOnlineResources();
165                    if ( urls.size() > 0 ) {
166                        return urls.get( 0 );
167                    }
168                }
169            }
170    
171            return null;
172    
173        }
174    
175        /**
176         * @see #getHTTPGetOperationURL(OGCCapabilities, Class)
177         * @param capabilities
178         * @param clss
179         * @return the first operation URL
180         */
181        @SuppressWarnings("unchecked")
182        private synchronized static URL getHTTPGetOperationURL( WFSCapabilities capabilities, Class clss ) {
183    
184            URL url = null;
185            WFSOperationsMetadata om = (WFSOperationsMetadata) capabilities.getOperationsMetadata();
186            if ( clss.equals( GetCapabilities.class ) ) {
187                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetCapabilitiesOperation().getDCPs()[0].getProtocol();
188                url = http.getGetOnlineResources()[0];
189            } else if ( clss.equals( DescribeFeatureType.class ) ) {
190                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getDescribeFeatureType().getDCPs()[0].getProtocol();
191                url = http.getGetOnlineResources()[0];
192            } else if ( clss.equals( GetFeature.class ) ) {
193                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeature().getDCPs()[0].getProtocol();
194                url = http.getGetOnlineResources()[0];
195            } else if ( clss.equals( GetFeatureWithLock.class ) ) {
196                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeatureWithLock().getDCPs()[0].getProtocol();
197                url = http.getGetOnlineResources()[0];
198            } else if ( clss.equals( Lock.class ) ) {
199                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getLockFeature().getDCPs()[0].getProtocol();
200                url = http.getGetOnlineResources()[0];
201            } else if ( clss.equals( Transaction.class ) ) {
202                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getTransaction().getDCPs()[0].getProtocol();
203                url = http.getGetOnlineResources()[0];
204            }
205            return url;
206        }
207    
208        /**
209         * @see #getHTTPGetOperationURL(OGCCapabilities, Class)
210         * @param capabilities
211         * @param clss
212         * @return the first operation URL
213         */
214        @SuppressWarnings("unchecked")
215        private synchronized static URL getHTTPGetOperationURL( WCSCapabilities capabilities, Class clss ) {
216    
217            URL url = null;
218            org.deegree.ogcwebservices.getcapabilities.OperationsMetadata om = capabilities.getCapabilitiy().getOperations();
219            if ( clss.equals( GetCapabilities.class ) ) {
220                DCPType[] dcps = om.getGetCapabilitiesOperation().getDCPs();
221                for ( DCPType dcpType : dcps ) {
222                    url = ( (org.deegree.ogcwebservices.getcapabilities.HTTP) dcpType.getProtocol() ).getGetOnlineResources()[0];
223                    break;
224                }
225            } else if ( clss.equals( GetCoverage.class ) ) {
226                DCPType[] dcps = null;
227                org.deegree.ogcwebservices.getcapabilities.Operation[] ops = om.getOperations();
228                for ( org.deegree.ogcwebservices.getcapabilities.Operation operation : ops ) {
229                    if ( operation.getName().equals( "GetCoverage" ) ) {
230                        dcps = operation.getDCPs();
231                        break;
232                    }
233                }
234                url = ( (org.deegree.ogcwebservices.getcapabilities.HTTP) dcps[0].getProtocol() ).getGetOnlineResources()[0];
235            } else if ( clss.equals( DescribeCoverage.class ) ) {
236                DCPType[] dcps = null;
237                org.deegree.ogcwebservices.getcapabilities.Operation[] ops = om.getOperations();
238                for ( org.deegree.ogcwebservices.getcapabilities.Operation operation : ops ) {
239                    if ( operation.getName().equals( "DescribeCoverage" ) ) {
240                        dcps = operation.getDCPs();
241                        break;
242                    }
243                }
244                url = ( (org.deegree.ogcwebservices.getcapabilities.HTTP) dcps[0].getProtocol() ).getGetOnlineResources()[0];
245            }
246            return url;
247        }
248    
249        /**
250         * the method return the first HTTPPost URL for a Operation within the pass capabilities document. If No URL can be
251         * found (e.g. the service does not support the operation via HTTP Get) <code>null</code> will be returned
252         * 
253         * @param capabilities
254         * @param clss
255         * @return the first HTTPPost URL for a Operation within the pass capabilities document. If No URL can be found
256         *         (e.g. the service does not support the operation via HTTP Get) <code>null</code> will be returned
257         */
258        @SuppressWarnings("unchecked")
259        public synchronized static URL getHTTPPostOperationURL( OGCCapabilities capabilities, Class clss ) {
260            URL url = null;
261            if ( capabilities instanceof WFSCapabilities ) {
262                url = getHTTPPostOperationURL( (WFSCapabilities) capabilities, clss );
263            } else {
264                // TODO
265                // support more service types
266                // possibly use generic base capabilities to extract it
267            }
268            return url;
269        }
270    
271        /**
272         * @see #getHTTPPostOperationURL(OGCCapabilities, Class)
273         * @param capabilities
274         * @param clss
275         * @return the first operation URL
276         */
277        @SuppressWarnings("unchecked")
278        private synchronized static URL getHTTPPostOperationURL( WFSCapabilities capabilities, Class clss ) {
279    
280            URL url = null;
281            WFSOperationsMetadata om = (WFSOperationsMetadata) capabilities.getOperationsMetadata();
282            if ( clss.equals( GetCapabilities.class ) ) {
283                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetCapabilitiesOperation().getDCPs()[0].getProtocol();
284                url = http.getPostOnlineResources()[0];
285            } else if ( clss.equals( DescribeFeatureType.class ) ) {
286                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getDescribeFeatureType().getDCPs()[0].getProtocol();
287                url = http.getPostOnlineResources()[0];
288            } else if ( clss.equals( GetFeature.class ) ) {
289                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeature().getDCPs()[0].getProtocol();
290                url = http.getPostOnlineResources()[0];
291            } else if ( clss.equals( GetFeatureWithLock.class ) ) {
292                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeatureWithLock().getDCPs()[0].getProtocol();
293                url = http.getPostOnlineResources()[0];
294            } else if ( clss.equals( Lock.class ) ) {
295                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getLockFeature().getDCPs()[0].getProtocol();
296                url = http.getPostOnlineResources()[0];
297            } else if ( clss.equals( Transaction.class ) ) {
298                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getTransaction().getDCPs()[0].getProtocol();
299                url = http.getPostOnlineResources()[0];
300            }
301            return url;
302        }
303    
304    }