001    //$HeadURL: svn+ssh://jwilden@svn.wald.intevation.org/deegree/base/branches/2.5_testing/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.GetCapabilities;
043    import org.deegree.ogcwebservices.getcapabilities.OGCCapabilities;
044    import org.deegree.ogcwebservices.wfs.capabilities.WFSCapabilities;
045    import org.deegree.ogcwebservices.wfs.capabilities.WFSOperationsMetadata;
046    import org.deegree.ogcwebservices.wfs.operation.GetFeature;
047    import org.deegree.ogcwebservices.wfs.operation.GetFeatureWithLock;
048    import org.deegree.ogcwebservices.wfs.operation.Lock;
049    import org.deegree.ogcwebservices.wfs.operation.transaction.Transaction;
050    import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
051    import org.deegree.ogcwebservices.wms.operation.GetFeatureInfo;
052    import org.deegree.ogcwebservices.wms.operation.GetLegendGraphic;
053    import org.deegree.ogcwebservices.wms.operation.GetMap;
054    import org.deegree.owscommon_new.DCP;
055    import org.deegree.owscommon_new.HTTP;
056    import org.deegree.owscommon_new.Operation;
057    import org.deegree.owscommon_new.OperationsMetadata;
058    
059    /**
060     *
061     *
062     *
063     * @version $Revision: 26213 $
064     * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
065     * @author last edited by: $Author: apoth $
066     *
067     * @version 1.0. $Revision: 26213 $, $Date: 2010-08-27 14:01:04 +0200 (Fr, 27 Aug 2010) $
068     *
069     * @since 2.0
070     */
071    public class OWSUtils {
072    
073        /**
074         * according to OGC WMS 1.3 Testsuite a URL to a service operation via HTTPGet must end with '?'
075         * or '&'
076         *
077         * @param href
078         * @return the parameter, changed to fulfill the spec
079         */
080        public static String validateHTTPGetBaseURL( String href ) {
081            if ( !href.endsWith( "/" ) ) {
082                if ( href.indexOf( '?' ) < 0 ) {
083                    href = href + '?';
084                } else if ( !href.endsWith( "&" ) && !href.endsWith( "?" ) ) {
085                    href = href + '&';
086                }
087            }
088            return href;
089        }
090    
091        /**
092         * the method return the first HTTPGet URL for a Operation within the pass capabilities
093         * document. If No URL can be found (e.g. the service does not support the operation via HTTP
094         * Get) <code>null</code> will be returned
095         *
096         * @param capabilities
097         * @param clss
098         * @return see above
099         */
100        @SuppressWarnings("unchecked")
101        public synchronized static URL getHTTPGetOperationURL( OGCCapabilities capabilities, Class clss ) {
102            URL url = null;
103            if ( capabilities instanceof WMSCapabilities ) {
104                url = getHTTPGetOperationURL( (WMSCapabilities) capabilities, clss );
105            } else if ( capabilities instanceof WFSCapabilities ) {
106                url = getHTTPGetOperationURL( (WFSCapabilities) capabilities, clss );
107            } else {
108                // TODO
109                // support more service types
110                // possibly use generic base capabilities to extract it
111            }
112            return url;
113    
114        }
115    
116        /**
117         * @see #getHTTPGetOperationURL(OGCCapabilities, Class)
118         * @param capabilities
119         * @param clss
120         * @return the first operation URL
121         */
122        @SuppressWarnings("unchecked")
123        private synchronized static URL getHTTPGetOperationURL( WMSCapabilities capabilities, Class clss ) {
124    
125            OperationsMetadata om = capabilities.getOperationMetadata();
126            List<DCP> dcps = null;
127            if ( clss.equals( GetMap.class ) ) {
128                Operation op = om.getOperation( new QualifiedName( "GetMap" ) );
129                if ( op == null ) {
130                    op = om.getOperation( new QualifiedName( "map" ) );
131                }
132                dcps = op.getDCP();
133            } else if ( clss.equals( GetCapabilities.class ) ) {
134                Operation op = om.getOperation( new QualifiedName( "GetCapabilities" ) );
135                if ( op == null ) {
136                    op = om.getOperation( new QualifiedName( "capabilities" ) );
137                }
138                dcps = op.getDCP();
139            } else if ( clss.equals( GetFeatureInfo.class ) ) {
140                Operation op = om.getOperation( new QualifiedName( "GetFeatureInfo" ) );
141                if ( op == null ) {
142                    op = om.getOperation( new QualifiedName( "FeatureInfo" ) );
143                }
144                dcps = op.getDCP();
145            } else if ( clss.equals( GetLegendGraphic.class ) ) {
146                Operation op = om.getOperation( new QualifiedName( "GetLegendGraphic" ) );
147                dcps = op.getDCP();
148            }
149    
150            // search for the first HTTP Get link
151            if ( dcps == null ) {
152                return null;
153            }
154            for ( DCP dcp : dcps ) {
155                if ( dcp instanceof HTTP ) {
156                    HTTP http = (HTTP) dcp;
157                    List<URL> urls = http.getGetOnlineResources();
158                    if ( urls.size() > 0 ) {
159                        return urls.get( 0 );
160                    }
161                }
162            }
163    
164            return null;
165    
166        }
167    
168        /**
169         * @see #getHTTPGetOperationURL(OGCCapabilities, Class)
170         * @param capabilities
171         * @param clss
172         * @return the first operation URL
173         */
174        @SuppressWarnings("unchecked")
175        private synchronized static URL getHTTPGetOperationURL( WFSCapabilities capabilities, Class clss ) {
176    
177            URL url = null;
178            WFSOperationsMetadata om = (WFSOperationsMetadata) capabilities.getOperationsMetadata();
179            if ( clss.equals( GetCapabilities.class ) ) {
180                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetCapabilitiesOperation().getDCPs()[0].getProtocol();
181                url = http.getGetOnlineResources()[0];
182            } else if ( clss.equals( GetFeature.class ) ) {
183                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeature().getDCPs()[0].getProtocol();
184                url = http.getGetOnlineResources()[0];
185            } else if ( clss.equals( GetFeatureWithLock.class ) ) {
186                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeatureWithLock().getDCPs()[0].getProtocol();
187                url = http.getGetOnlineResources()[0];
188            } else if ( clss.equals( Lock.class ) ) {
189                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getLockFeature().getDCPs()[0].getProtocol();
190                url = http.getGetOnlineResources()[0];
191            } else if ( clss.equals( Transaction.class ) ) {
192                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getTransaction().getDCPs()[0].getProtocol();
193                url = http.getGetOnlineResources()[0];
194            }
195            return url;
196        }
197    
198        /**
199         * the method return the first HTTPPost URL for a Operation within the pass capabilities
200         * document. If No URL can be found (e.g. the service does not support the operation via HTTP
201         * Get) <code>null</code> will be returned
202         *
203         * @param capabilities
204         * @param clss
205         * @return the first HTTPPost URL for a Operation within the pass capabilities document. If No
206         *         URL can be found (e.g. the service does not support the operation via HTTP Get)
207         *         <code>null</code> will be returned
208         */
209        @SuppressWarnings("unchecked")
210        public synchronized static URL getHTTPPostOperationURL( OGCCapabilities capabilities, Class clss ) {
211            URL url = null;
212            if ( capabilities instanceof WFSCapabilities ) {
213                url = getHTTPPostOperationURL( (WFSCapabilities) capabilities, clss );
214            } else {
215                // TODO
216                // support more service types
217                // possibly use generic base capabilities to extract it
218            }
219            return url;
220        }
221    
222        /**
223         * @see #getHTTPPostOperationURL(OGCCapabilities, Class)
224         * @param capabilities
225         * @param clss
226         * @return the first operation URL
227         */
228        @SuppressWarnings("unchecked")
229        private synchronized static URL getHTTPPostOperationURL( WFSCapabilities capabilities, Class clss ) {
230    
231            URL url = null;
232            WFSOperationsMetadata om = (WFSOperationsMetadata) capabilities.getOperationsMetadata();
233            if ( clss.equals( GetCapabilities.class ) ) {
234                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetCapabilitiesOperation().getDCPs()[0].getProtocol();
235                url = http.getPostOnlineResources()[0];
236            } else if ( clss.equals( GetFeature.class ) ) {
237                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeature().getDCPs()[0].getProtocol();
238                url = http.getPostOnlineResources()[0];
239            } else if ( clss.equals( GetFeatureWithLock.class ) ) {
240                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeatureWithLock().getDCPs()[0].getProtocol();
241                url = http.getPostOnlineResources()[0];
242            } else if ( clss.equals( Lock.class ) ) {
243                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getLockFeature().getDCPs()[0].getProtocol();
244                url = http.getPostOnlineResources()[0];
245            } else if ( clss.equals( Transaction.class ) ) {
246                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getTransaction().getDCPs()[0].getProtocol();
247                url = http.getPostOnlineResources()[0];
248            }
249            return url;
250        }
251    
252    }