001: /*
002: * uDig - User Friendly Desktop Internet GIS client
003: * http://udig.refractions.net
004: * (C) 2004, Refractions Research Inc.
005: *
006: * This library is free software; you can redistribute it and/or
007: * modify it under the terms of the GNU Lesser General Public
008: * License as published by the Free Software Foundation;
009: * version 2.1 of the License.
010: *
011: * This library is distributed in the hope that it will be useful,
012: * but WITHOUT ANY WARRANTY; without even the implied warranty of
013: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
014: * Lesser General Public License for more details.
015: *
016: */
017: package net.refractions.udig.catalog.internal.wms;
018:
019: import java.io.Serializable;
020: import java.net.MalformedURLException;
021: import java.net.URL;
022: import java.util.HashMap;
023: import java.util.Map;
024:
025: import net.refractions.udig.catalog.IService;
026: import net.refractions.udig.catalog.ServiceExtension2;
027: import net.refractions.udig.catalog.wms.internal.Messages;
028:
029: /**
030: * A service extension for creating WMS Services
031: *
032: * @author David Zwiers, Refractions Research
033: * @since 0.6
034: */
035: public class WMSServiceExtension implements ServiceExtension2 {
036:
037: public IService createService(URL id,
038: Map<String, Serializable> params) {
039: if (params == null)
040: return null;
041:
042: if ((!params.containsKey(WMSServiceImpl.WMS_URL_KEY) && id == null)
043: && !params.containsKey(WMSServiceImpl.WMS_WMS_KEY)) {
044: return null; // nope we don't have a WMS_URL_KEY
045: }
046:
047: URL extractedId = extractId(params);
048: if (extractedId != null) {
049: if (id != null)
050: return new WMSServiceImpl(id, params);
051: else
052: return new WMSServiceImpl(extractedId, params);
053: }
054:
055: return null;
056: }
057:
058: private URL extractId(Map<String, Serializable> params) {
059: if (params.containsKey(WMSServiceImpl.WMS_URL_KEY)) {
060: URL base = null; // base url for service
061:
062: if (params.get(WMSServiceImpl.WMS_URL_KEY) instanceof URL) {
063: base = (URL) params.get(WMSServiceImpl.WMS_URL_KEY); // use provided url for base
064: } else {
065: try {
066: base = new URL((String) params
067: .get(WMSServiceImpl.WMS_URL_KEY)); // upcoverting
068: // string to
069: // url for
070: // base
071: } catch (MalformedURLException e1) {
072: // log this?
073: e1.printStackTrace();
074: return null;
075: }
076: params.remove(params.get(WMSServiceImpl.WMS_URL_KEY));
077: params.put(WMSServiceImpl.WMS_URL_KEY, base);
078: }
079: // params now has a valid url
080:
081: return base;
082: }
083: return null;
084: }
085:
086: public Map<String, Serializable> createParams(URL url) {
087: if (!isWMS(url)) {
088: return null;
089: }
090:
091: // wms check
092: Map<String, Serializable> params2 = new HashMap<String, Serializable>();
093: params2.put(WMSServiceImpl.WMS_URL_KEY, url);
094:
095: return params2;
096: }
097:
098: public static boolean isWMS(URL url) {
099: return processURL(url) == null;
100: }
101:
102: public String reasonForFailure(Map<String, Serializable> params) {
103: URL id = extractId(params);
104: if (id == null)
105: return Messages.WMSServiceExtension_needsKey
106: + WMSServiceImpl.WMS_URL_KEY
107: + Messages.WMSServiceExtension_nullValue;
108: return reasonForFailure(id);
109: }
110:
111: public String reasonForFailure(URL url) {
112: return processURL(url);
113: }
114:
115: private static String processURL(URL url) {
116: if (url == null) {
117: return Messages.WMSServiceExtension_nullURL;
118: }
119:
120: String PATH = url.getPath();
121: String QUERY = url.getQuery();
122: String PROTOCOL = url.getProtocol();
123: if (PROTOCOL == null || PROTOCOL.indexOf("http") == -1) { //$NON-NLS-1$ supports 'https' too.
124: return Messages.WMSServiceExtension_protocol
125: + "'" + PROTOCOL + "'"; //$NON-NLS-1$ //$NON-NLS-2$
126: }
127: if (QUERY != null
128: && QUERY.toUpperCase().indexOf("SERVICE=") != -1) { //$NON-NLS-1$
129: int indexOf = QUERY.toUpperCase().indexOf("SERVICE="); //$NON-NLS-1$
130: // we have a service! it better be wfs
131: if (QUERY.toUpperCase().indexOf("SERVICE=WMS") == -1) { //$NON-NLS-1$
132: int endOfExp = QUERY.indexOf('&', indexOf);
133: if (endOfExp == -1)
134: endOfExp = QUERY.length();
135: if (endOfExp > indexOf + 8)
136: return Messages.WMSServiceExtension_badService
137: + QUERY.substring(indexOf + 8, endOfExp);
138: else {
139: return Messages.WMSServiceExtension_badService + ""; //$NON-NLS-1$
140: }
141: }
142: } else if (PATH != null
143: && PATH.toUpperCase().indexOf("GEOSERVER/WMS") != -1) { //$NON-NLS-1$
144: return null;
145: }
146: return null; // try it anyway
147: }
148: }
|