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