001: /*
002: * GeoTools - OpenSource mapping toolkit
003: * http://geotools.org
004: * (C) 2004-2006, Geotools Project Managment Committee (PMC)
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; either
009: * version 2.1 of the License, or (at your option) any later version.
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: package org.geotools.data.wms;
017:
018: import java.io.IOException;
019: import java.io.InputStream;
020: import java.net.URL;
021: import java.util.HashMap;
022: import java.util.Iterator;
023: import java.util.Map;
024:
025: import org.geotools.data.ows.AbstractGetCapabilitiesRequest;
026: import org.geotools.data.ows.AbstractRequest;
027: import org.geotools.data.ows.GetCapabilitiesRequest;
028: import org.geotools.data.ows.Response;
029: import org.geotools.data.wms.request.AbstractGetFeatureInfoRequest;
030: import org.geotools.data.wms.request.AbstractGetMapRequest;
031: import org.geotools.data.wms.request.DescribeLayerRequest;
032: import org.geotools.data.wms.request.GetLegendGraphicRequest;
033: import org.geotools.data.wms.request.GetStylesRequest;
034: import org.geotools.data.wms.request.PutStylesRequest;
035: import org.geotools.data.wms.response.GetFeatureInfoResponse;
036: import org.geotools.data.wms.response.GetMapResponse;
037: import org.geotools.data.wms.response.WMSGetCapabilitiesResponse;
038: import org.geotools.ows.ServiceException;
039:
040: /**
041: * Provides support for the Web Map Server 1.0 Specification.
042: * <p>
043: * WMS1_0_0 provides both name and version information that may be checked against a GetCapabilities document during
044: * version negotiation.
045: * </p>
046: *
047: * @author Jody Garnett, Refractions Research
048: * @author rgould
049: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/main/java/org/geotools/data/wms/WMS1_0_0.java $
050: */
051: public class WMS1_0_0 extends WMSSpecification {
052: static final Map formatMimeTypes = new HashMap();
053: static final Map exceptionMimeTypes = new HashMap();
054:
055: static {
056: exceptionMimeTypes.put("WMS_XML", "application/vnd.ogc.se_xml");
057: exceptionMimeTypes.put(
058: "INIMAGE", "application/vnd.ogc.se_inimage"); //$NON-NLS-1$ //$NON-NLS-2$
059: exceptionMimeTypes.put("BLANK", "application/vnd.ogc.se_blank"); //$NON-NLS-1$ //$NON-NLS-2$
060: }
061:
062: static {
063: formatMimeTypes.put("GIF", "image/gif"); //$NON-NLS-1$ //$NON-NLS-2$
064: formatMimeTypes.put("PNG", "image/png"); //$NON-NLS-1$ //$NON-NLS-2$
065: formatMimeTypes.put("JPEG", "image/jpeg"); //$NON-NLS-1$ //$NON-NLS-2$
066: formatMimeTypes.put("BMP", "image/bmp"); //$NON-NLS-1$ //$NON-NLS-2$
067: formatMimeTypes.put(
068: "WebCGM", "image/cgm;Version=4;ProfileId=WebCGM"); //$NON-NLS-1$ //$NON-NLS-2$
069: formatMimeTypes.put("SVG", "image/svg+xml"); //$NON-NLS-1$ //$NON-NLS-2$
070: formatMimeTypes.put("GML.1", "text/xml"); //$NON-NLS-1$ //$NON-NLS-2$
071: formatMimeTypes.put("GML.2", "text/xml"); //$NON-NLS-1$ //$NON-NLS-2$
072: formatMimeTypes.put("GML.3", "text/xml"); //$NON-NLS-1$ //$NON-NLS-2$
073: formatMimeTypes.put("WBMP", "image/vnd.wap.wbmp"); //$NON-NLS-1$ //$NON-NLS-2$
074: formatMimeTypes.put("WMS_XML", "application/vnd.ogc.wms_xml"); //$NON-NLS-1$ //$NON-NLS-2$
075: formatMimeTypes.put("MIME", "mime"); //$NON-NLS-1$ //$NON-NLS-2$
076: formatMimeTypes
077: .put("INIMAGE", "application/vnd.ogc.se_inimage"); //$NON-NLS-1$ //$NON-NLS-2$
078: formatMimeTypes.put("TIFF", "image/tiff"); //$NON-NLS-1$ //$NON-NLS-2$
079: formatMimeTypes.put("GeoTIFF", "image/tiff"); //$NON-NLS-1$ //$NON-NLS-2$
080: formatMimeTypes.put("PPM", "image/x-portable-pixmap"); //$NON-NLS-1$ //$NON-NLS-2$
081: formatMimeTypes.put("BLANK", "application/vnd.ogc.se_blank"); //$NON-NLS-1$ //$NON-NLS-2$
082: formatMimeTypes.put("CW_WKB", "application/x-cubestor-wkb"); //$NON-NLS-1$//$NON-NLS-2$
083: }
084:
085: /**
086: * Public constructor creates the WMS1_0_0 object.
087: */
088: public WMS1_0_0() {
089:
090: }
091:
092: /**
093: * Expected version attribute for root element.
094: *
095: * @return the expect version value for this specification
096: */
097: public String getVersion() {
098: return "1.0.0"; //$NON-NLS-1$
099: }
100:
101: /**
102: * Provides mapping from well known format to MIME type.
103: * <p>
104: * WebMapServer api uses mime type internally for format information (indeed WMS 1.0.0 is the only WMS specifcation
105: * not to use MIME type directly).
106: * </p>
107: * <p>
108: *
109: * @param format
110: * @return MIME type for format
111: */
112: public static final String toFormatMIME(String format) {
113: return getMimeType(format, formatMimeTypes);
114: }
115:
116: /**
117: * The WMS 1.0.0 specification uses a mapping of mimetypes to values to use
118: * as parameter values in requests. This will take a parameter value and
119: * convert it to its according mime type.
120: *
121: * @param exception an exceptions parameter value, such as "WMS_XML"
122: * @return a mimeType, such as "application/vnd.ogc.se_xml"
123: */
124: public static final String toExceptionMimeType(String exception) {
125: return getMimeType(exception, exceptionMimeTypes);
126: }
127:
128: private static final String getMimeType(String key, Map map) {
129: if (map.containsKey(key)) {
130: return (String) map.get(key);
131: }
132:
133: return null;
134: }
135:
136: /**
137: * Provides mapping from MIME type to WMS 1.0.0 Format.
138: * <p>
139: * WebMapServer api uses mime type internally for format information (indeed WMS 1.0.0 is the only WMS specifcation
140: * not to use MIME type directly).
141: * </p>
142: * <p>
143: *
144: * @param mimeType MIME type such as "image/gif"
145: * @return Format well known WMS 1.0.0 format such as "GIF"
146: */
147: public static final String getFormatValue(String mimeType) {
148: return getParameterValue(mimeType, formatMimeTypes);
149: }
150:
151: /**
152: * The WMS 1.0.0 specification uses internal mappings in the parameter value
153: * instead of direct mime types. This will map a given mime type to its
154: * proper parameter value according to the spec.
155: *
156: * @param mimeType the mimeType to use, such as "application/vnd.ogc.se_xml"
157: * @return the proper parameter value, such as "WMS_XML"
158: */
159: public static final String getExceptionValue(String mimeType) {
160: return getParameterValue(mimeType, exceptionMimeTypes);
161: }
162:
163: private static final String getParameterValue(String mimeType,
164: Map map) {
165: for (Iterator i = map.entrySet().iterator(); i.hasNext();) {
166: Map.Entry entry = (Map.Entry) i.next();
167:
168: if (mimeType.equals(entry.getValue())) {
169: return (String) entry.getKey();
170: }
171: }
172:
173: return null;
174: }
175:
176: /**
177: * Create a request for performing GetCapabilities requests on a 1.0.0 server.
178: *
179: * @see org.geotools.data.wms.Specification#createGetCapabilitiesRequest(java.net.URL)
180: * @param server a URL that points to the 1.0.0 server
181: * @return a AbstractGetCapabilitiesRequest object that can provide a valid request
182: */
183: public GetCapabilitiesRequest createGetCapabilitiesRequest(
184: URL server) {
185: return new GetCapsRequest(server);
186: }
187:
188: private static String processKey(String key) {
189: return key.trim().toLowerCase();
190: }
191:
192: /**
193: * We need a custom request object.
194: * <p>
195: * WMS 1.0.0 does requests a bit differently:
196: * <ul>
197: * <li>WMTVER=1.0.0</li>
198: * </p>
199: */
200: static public class GetCapsRequest extends
201: AbstractGetCapabilitiesRequest {
202: /**
203: * Construct a Request compatable with a 1.0.0 Web Feature Server.
204: *
205: * @param urlGetCapabilities URL of GetCapabilities document.
206: */
207: public GetCapsRequest(URL urlGetCapabilities) {
208: super (urlGetCapabilities);
209: }
210:
211: protected void initVersion() {
212: setProperty("WMTVER", "1.0.0"); //$NON-NLS-1$ //$NON-NLS-2$
213: properties.remove("VERSION");
214: }
215:
216: protected void initRequest() {
217: setProperty("REQUEST", "capabilities"); //$NON-NLS-1$ //$NON-NLS-2$
218: }
219:
220: protected void initService() {
221: //The 1.0.0 specification does not use the service property
222: }
223:
224: /*
225: * @see org.geotools.data.wms.request.AbstractRequest#processKey(java.lang.String)
226: */
227: protected String processKey(String key) {
228: return WMS1_0_0.processKey(key);
229: }
230:
231: public Response createResponse(String contentType,
232: InputStream inputStream) throws ServiceException,
233: IOException {
234: return new WMSGetCapabilitiesResponse(contentType,
235: inputStream);
236: }
237: }
238:
239: /**
240: * A GetMapRequest for a 1.0.0 Server
241: */
242: static public class GetMapRequest extends AbstractGetMapRequest {
243: /**
244: * Constructs a GetMapRequest for use with a 1.0.0 server
245: *
246: * @param onlineResource the URL for server's GetMap request
247: */
248: public GetMapRequest(URL onlineResource) {
249: super (onlineResource, null);
250: }
251:
252: protected void initRequest() {
253: setProperty("REQUEST", "map"); //$NON-NLS-1$ //$NON-NLS-2$
254:
255: /*
256: * A 1.0.0 WMS server has been encountered that has EXCEPTIONS as
257: * a required parameter. It does not hurt to explicitly ask for
258: * this, anyway.
259: */
260: setProperty("EXCEPTIONS", AbstractRequest.EXCEPTION_XML);
261: }
262:
263: protected void initVersion() {
264: setProperty(VERSION, "1.0.0");
265: }
266:
267: public void setProperty(String name, String value) {
268: if (name.equals(FORMAT)) {
269: value = getRequestFormat(value);
270: } else if (name.equals(EXCEPTIONS)) {
271: value = getRequestException(value);
272: }
273:
274: super .setProperty(name, value);
275: }
276:
277: public void setFormat(String value) {
278: setProperty(FORMAT, value);
279: }
280:
281: public void setExceptions(String exceptions) {
282: setProperty(EXCEPTIONS, exceptions);
283: }
284:
285: protected String getRequestException(String exception) {
286: return getExceptionValue(exception);
287:
288: }
289:
290: protected String getRequestFormat(String format) {
291: return getFormatValue(format);
292: }
293:
294: protected String processKey(String key) {
295: return WMS1_0_0.processKey(key);
296: }
297:
298: public Response createResponse(String contentType,
299: InputStream inputStream) throws ServiceException,
300: IOException {
301: return new GetMapResponse(contentType, inputStream);
302: }
303: }
304:
305: /**
306: * A GetFeatureInfoRequest for a 1.0.0 server
307: */
308:
309: static public class GetFeatureInfoRequest extends
310: AbstractGetFeatureInfoRequest {
311: /**
312: * @param onlineResource
313: * @param request
314: *
315: */
316: public GetFeatureInfoRequest(URL onlineResource,
317: org.geotools.data.wms.request.GetMapRequest request) {
318: super (onlineResource, request);
319: }
320:
321: protected void initVersion() {
322: setProperty("WMTVER", "1.0.0");
323: }
324:
325: protected String processKey(String key) {
326: return WMS1_0_0.processKey(key);
327: }
328:
329: public Response createResponse(String contentType,
330: InputStream inputStream) throws ServiceException,
331: IOException {
332: return new GetFeatureInfoResponse(contentType, inputStream);
333: }
334: }
335:
336: /**
337: * @see org.geotools.data.wms.Specification#createGetMapRequest(java.net.URL)
338: */
339: public org.geotools.data.wms.request.GetMapRequest createGetMapRequest(
340: URL get) {
341: return new GetMapRequest(get);
342: }
343:
344: /**
345: * @see org.geotools.data.wms.Specification#createGetFeatureInfoRequest(java.net.URL,
346: * org.geotools.data.wms.request.GetMapRequest)
347: */
348: public org.geotools.data.wms.request.GetFeatureInfoRequest createGetFeatureInfoRequest(
349: URL onlineResource,
350: org.geotools.data.wms.request.GetMapRequest getMapRequest) {
351: return new GetFeatureInfoRequest(onlineResource, getMapRequest);
352: }
353:
354: /**
355: * Note that WMS 1.0.0 does not support this method.
356: * @see org.geotools.data.wms.Specification#createDescribeLayerRequest(java.net.URL)
357: */
358: public DescribeLayerRequest createDescribeLayerRequest(
359: URL onlineResource) throws UnsupportedOperationException {
360: throw new UnsupportedOperationException(
361: "WMS 1.0.0 does not support DescribeLayer");
362: }
363:
364: /**
365: * Note that WMS 1.0.0 does not support this method.
366: * @see org.geotools.data.wms.Specification#createGetLegendGraphicRequest(java.net.URL)
367: */
368: public GetLegendGraphicRequest createGetLegendGraphicRequest(
369: URL onlineResource) throws UnsupportedOperationException {
370: throw new UnsupportedOperationException(
371: "WMS 1.0.0 does not support GetLegendGraphic");
372: }
373:
374: /**
375: * Note that WMS 1.0.0 does not support this method
376: * @see org.geotools.data.wms.Specification#createGetStylesRequest(java.net.URL)
377: */
378: public GetStylesRequest createGetStylesRequest(URL onlineResource)
379: throws UnsupportedOperationException {
380: throw new UnsupportedOperationException(
381: "WMS 1.0.0 does not support GetStyles");
382: }
383:
384: /**
385: * Note that WMS 1.0.0 does not support this method
386: * @see org.geotools.data.wms.Specification#createPutStylesRequest(java.net.URL)
387: */
388: public PutStylesRequest createPutStylesRequest(URL onlineResource)
389: throws UnsupportedOperationException {
390: throw new UnsupportedOperationException(
391: "WMS 1.0.0 does not support PutStyles");
392: }
393: }
|