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.gce;
017:
018: import java.io.IOException;
019: import java.net.MalformedURLException;
020: import java.net.URISyntaxException;
021: import java.net.URL;
022: import java.security.InvalidParameterException;
023:
024: import org.geotools.data.ows.WMSCapabilities;
025: import org.geotools.data.wms.WebMapServer;
026: import org.opengis.coverage.grid.Format;
027: import org.opengis.coverage.grid.GridCoverageExchange;
028: import org.opengis.coverage.grid.GridCoverageReader;
029: import org.opengis.coverage.grid.GridCoverageWriter;
030: import org.xml.sax.SAXException;
031:
032: /**
033: * DOCUMENT ME!
034: *
035: * @author Richard Gould, Refractions Research
036: * @source $URL: http://svn.geotools.org/geotools/tags/2.4.1/modules/plugin/wms/src/main/java/org/geotools/data/wms/gce/WMSGridCoverageExchange.java $
037: */
038: public class WMSGridCoverageExchange implements GridCoverageExchange {
039: /** Available formats for this Web Map Server */
040: private Format[] formats;
041:
042: /** Parsed WMS Capabilities document */
043: private WMSCapabilities capabilities;
044:
045: /** Web Map Server proxy */
046: private WebMapServer wms;
047:
048: public WMSGridCoverageExchange(Object source) throws SAXException,
049: URISyntaxException, IOException {
050: if (source instanceof String || source instanceof URL) {
051: URL url = null;
052:
053: if (source instanceof String) {
054: url = new URL((String) source);
055: } else {
056: url = (URL) source;
057: }
058:
059: wms = new WebMapServer(url);
060: capabilities = wms.getCapabilities();
061:
062: } else if (source instanceof WMSCapabilities) {
063: capabilities = (WMSCapabilities) source;
064: }
065:
066: WMSFormatFactory factory = new WMSFormatFactory(capabilities);
067:
068: if (!factory.isAvailable()) {
069: throw new RuntimeException("WMS support is not available");
070: }
071:
072: formats = new Format[1];
073: formats[0] = factory.createFormat();
074: }
075:
076: public void dispose() throws IOException {
077: // TODO Auto-generated method stub
078: }
079:
080: public Format[] getFormats() {
081: return formats;
082: }
083:
084: public GridCoverageReader getReader(Object source)
085: throws IOException {
086: if (source instanceof String || source instanceof URL) {
087: if (source instanceof String) {
088: try {
089: new URL((String) source);
090: } catch (MalformedURLException e) {
091: throw new InvalidParameterException(
092: "Unable to convert source to a URL (it is malformed)");
093: }
094: }
095:
096: return new WMSReader(wms);
097:
098: }
099:
100: throw new InvalidParameterException(
101: "Source is not of a support type");
102: }
103:
104: /**
105: * WMS Specification does not permit writing!
106: *
107: * @param destination DOCUMENT ME!
108: * @param format DOCUMENT ME!
109: *
110: * @return DOCUMENT ME!
111: *
112: * @throws IOException DOCUMENT ME!
113: * @throws RuntimeException DOCUMENT ME!
114: */
115: public GridCoverageWriter getWriter(Object destination,
116: Format format) throws IOException {
117: throw new RuntimeException("Writing is not supported for WMSs");
118: }
119:
120: public boolean isAvailable() {
121: return true;
122: }
123:
124: public boolean setDataSource(Object datasource) {
125: if (datasource instanceof String || datasource instanceof URL) {
126: try {
127: new URL((String) datasource);
128:
129: return true;
130: } catch (MalformedURLException e) {
131: return false;
132: }
133: }
134:
135: return false;
136: }
137:
138: public WMSCapabilities getCapabilities() {
139: return capabilities;
140: }
141: }
|