001    //$HeadURL: svn+ssh://rbezema@svn.wald.intevation.org/deegree/base/tags/2.1/src/org/deegree/ogcwebservices/OWSUtils.java $
002    /*----------------    FILE HEADER  ------------------------------------------
003    
004     This file is part of deegree.
005     Copyright (C) 2001-2006 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: 6692 $
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: 6692 $, $Date: 2007-04-25 14:55:52 +0200 (Mi, 25 Apr 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                dcps = op.getDCP();
147            } else if ( clss.equals( GetLegendGraphic.class ) ) {
148                Operation op = om.getOperation( new QualifiedName( "GetLegendGraphic" ) );
149                dcps = op.getDCP();
150            }
151    
152            // search for the first HTTP Get link
153            if ( dcps == null ) {
154                return null;
155            }
156            for ( DCP dcp : dcps ) {
157                if ( dcp instanceof HTTP ) {
158                    HTTP http = (HTTP) dcp;
159                    List<URL> urls = http.getGetOnlineResources();
160                    if ( urls.size() > 0 ) {
161                        return urls.get( 0 );
162                    }
163                }
164            }
165    
166            return null;
167    
168        }
169    
170        /**
171         * @see #getHTTPGetOperationURL(OGCCapabilities, Class)
172         * @param capabilities
173         * @param clss
174         * @return the first operation URL
175         */
176        private synchronized static URL getHTTPGetOperationURL( WFSCapabilities capabilities, Class clss ) {
177    
178            URL url = null;
179            WFSOperationsMetadata om = (WFSOperationsMetadata) capabilities.getOperationsMetadata();
180            if ( clss.equals( GetCapabilities.class ) ) {
181                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetCapabilitiesOperation().getDCPs()[0].getProtocol();
182                url = http.getGetOnlineResources()[0];
183            } else if ( clss.equals( GetFeature.class ) ) {
184                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeature().getDCPs()[0].getProtocol();
185                url = http.getGetOnlineResources()[0];
186            } else if ( clss.equals( GetFeatureWithLock.class ) ) {
187                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getGetFeatureWithLock().getDCPs()[0].getProtocol();
188                url = http.getGetOnlineResources()[0];
189            } else if ( clss.equals( Lock.class ) ) {
190                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getLockFeature().getDCPs()[0].getProtocol();
191                url = http.getGetOnlineResources()[0];
192            } else if ( clss.equals( Transaction.class ) ) {
193                org.deegree.ogcwebservices.getcapabilities.HTTP http = (org.deegree.ogcwebservices.getcapabilities.HTTP) om.getTransaction().getDCPs()[0].getProtocol();
194                url = http.getGetOnlineResources()[0];
195            }
196            return url;
197        }
198    
199        /**
200         * the method return the first HTTPPost URL for a Operation within the pass capabilities
201         * document. If No URL can be found (e.g. the service does not support the operation via HTTP
202         * Get) <code>null</code> will be returned
203         * 
204         * @param capabilities
205         * @param clss
206         * @return the first HTTPPost URL for a Operation within the pass capabilities document. If No
207         *         URL can be found (e.g. the service does not support the operation via HTTP Get)
208         *         <code>null</code> will be returned
209         */
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        private synchronized static URL getHTTPPostOperationURL( WFSCapabilities capabilities,
229                                                                 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    }
253    
254    /***************************************************************************************************
255     * <code>
256     Changes to this class. What the people have been up to:
257    
258     $Log$
259     Revision 1.2  2007/02/27 13:46:11  wanhoff
260     fixed Javadoc @return tag and footer
261    
262     Revision 1.1  2006/10/17 20:31:19  poth
263     *** empty log message ***
264    
265     Revision 1.5  2006/09/08 16:25:33  poth
266     method for getting post addresses from OWS capabilities initialized / two method that do not has to be public set to private
267    
268     Revision 1.4  2006/08/29 13:02:50  poth
269     code formating
270    
271     Revision 1.3  2006/08/23 07:10:22  schmitz
272     Renamed the owscommon_neu package to owscommon_new.
273    
274     Revision 1.2  2006/08/22 10:25:01  schmitz
275     Updated the WMS to use the new OWS common package.
276     Updated the rest of deegree to use the new data classes returned
277     by the updated WMS methods/capabilities.
278    
279     Revision 1.1  2006/08/20 20:51:05  poth
280     initial check in
281    
282     </code>
283     **************************************************************************************************/