001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2006-2006, Geotools Project Managment Committee (PMC)
005: * (C) 2005, Refractions Research Inc.
006: *
007: * This library is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU Lesser General Public
009: * License as published by the Free Software Foundation; either
010: * version 2.1 of the License, or (at your option) any later version.
011: *
012: * This library is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
015: * Lesser General Public License for more details.
016: */
017: package org.geotools.catalog.wms;
018:
019: import java.net.MalformedURLException;
020: import java.net.URI;
021: import java.net.URISyntaxException;
022: import java.net.URL;
023: import java.util.HashMap;
024: import java.util.Map;
025:
026: import org.geotools.catalog.Catalog;
027: import org.geotools.catalog.Service;
028: import org.geotools.catalog.ServiceFactory;
029:
030: /**
031: * Provides ...TODO summary sentence
032: *
033: * <p>
034: * TODO Description
035: * </p>
036: *
037: * @since 0.6
038: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/main/java/org/geotools/catalog/wms/WMSServiceFactory.java $
039: */
040: public class WMSServiceFactory implements ServiceFactory {
041: /**
042: * TODO summary sentence for createService ...
043: *
044: * @param parent DOCUMENT ME!
045: * @param id
046: * @param params
047: *
048: * @return x
049: */
050: public Service createService(Catalog parent, URI id, Map params) {
051: if (params == null) {
052: return null;
053: }
054:
055: if ((!params.containsKey(WMSService.WMS_URL_KEY) && (id == null))
056: && !params.containsKey(WMSService.WMS_WMS_KEY)) {
057: return null; // nope we don't have a WMS_URL_KEY
058: }
059:
060: if (params.containsKey(WMSService.WMS_URL_KEY)) {
061: URL base = null; // base url for service
062:
063: if (params.get(WMSService.WMS_URL_KEY) instanceof URL) {
064: base = (URL) params.get(WMSService.WMS_URL_KEY); // use provided url for base
065: } else {
066: try {
067: base = new URL((String) params
068: .get(WMSService.WMS_URL_KEY)); // upcoverting string to url for base
069: } catch (MalformedURLException e1) {
070: // log this?
071: e1.printStackTrace();
072:
073: return null;
074: }
075:
076: params.remove(params.get(WMSService.WMS_URL_KEY));
077: params.put(WMSService.WMS_URL_KEY, base);
078: }
079:
080: // params now has a valid url
081: if (id == null) {
082: try {
083: id = new URI(base.toExternalForm());
084: } catch (URISyntaxException e) {
085: return null;
086: }
087: }
088:
089: return new WMSService(parent, id, params);
090: }
091:
092: return null;
093: }
094:
095: public boolean canProcess(URI uri) {
096: try {
097: return isWMS(uri.toURL());
098: } catch (MalformedURLException e) {
099: return false;
100: }
101: }
102:
103: public Map createParams(URI uri) {
104: URL url;
105:
106: try {
107: url = uri.toURL();
108: } catch (MalformedURLException e) {
109: return null;
110: }
111:
112: if (!isWMS(url)) {
113: return null;
114: }
115:
116: // wms check
117: Map params2 = new HashMap();
118: params2.put(WMSService.WMS_URL_KEY, url);
119:
120: return params2;
121: }
122:
123: public static boolean isWMS(URL url) {
124: if (url == null) {
125: return false;
126: }
127:
128: String PATH = url.getPath();
129: String QUERY = url.getQuery();
130: String PROTOCOL = url.getProtocol();
131:
132: if (PROTOCOL.indexOf("http") == -1) { //$NON-NLS-1$ supports 'https' too.
133:
134: return false;
135: }
136:
137: if ((QUERY != null)
138: && (QUERY.toUpperCase().indexOf("SERVICE=") != -1)) { //$NON-NLS-1$
139: // we have a service! It better be WMS
140:
141: return QUERY.toUpperCase().indexOf("SERVICE=WMS") != -1; //$NON-NLS-1$
142: } else if ((PATH != null)
143: && (PATH.toUpperCase().indexOf("GEOSERVER/WMS") != -1)) { //$NON-NLS-1$
144:
145: return true;
146: }
147:
148: return true; // try it anyway
149: }
150: }
|