001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/standard/wms/control/GetWMSLayerListener.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 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.portal.standard.wms.control;
044:
045: import java.io.IOException;
046: import java.net.MalformedURLException;
047: import java.net.URL;
048: import java.util.List;
049:
050: import org.deegree.datatypes.QualifiedName;
051: import org.deegree.datatypes.values.TypedLiteral;
052: import org.deegree.enterprise.control.AbstractListener;
053: import org.deegree.enterprise.control.FormEvent;
054: import org.deegree.enterprise.control.RPCMember;
055: import org.deegree.enterprise.control.RPCMethodCall;
056: import org.deegree.enterprise.control.RPCParameter;
057: import org.deegree.enterprise.control.RPCStruct;
058: import org.deegree.enterprise.control.RPCUtils;
059: import org.deegree.enterprise.control.RPCWebEvent;
060: import org.deegree.framework.log.ILogger;
061: import org.deegree.framework.log.LoggerFactory;
062: import org.deegree.framework.util.NetWorker;
063: import org.deegree.framework.xml.XMLParsingException;
064: import org.deegree.i18n.Messages;
065: import org.deegree.ogcwebservices.OWSUtils;
066: import org.deegree.ogcwebservices.getcapabilities.InvalidCapabilitiesException;
067: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
068: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocument;
069: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocumentFactory;
070: import org.deegree.owscommon_new.DomainType;
071: import org.deegree.owscommon_new.Operation;
072: import org.deegree.owscommon_new.OperationsMetadata;
073: import org.deegree.portal.PortalException;
074: import org.deegree.portal.standard.wms.util.ClientHelper;
075: import org.xml.sax.SAXException;
076:
077: /**
078: * Lister class for accessing the layers of WMS and return it to the client.
079: *
080: * @version $Revision: 9873 $
081: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth</a>
082: * @author last edited by: $Author: aschmitz $
083: *
084: * @version 1.0. $Revision: 9873 $, $Date: 2008-02-04 04:51:00 -0800 (Mon, 04 Feb 2008) $
085: *
086: * @since 2.0
087: */
088: public class GetWMSLayerListener extends AbstractListener {
089:
090: private static ILogger LOG = LoggerFactory
091: .getLogger(GetWMSLayerListener.class);
092:
093: /**
094: * @see org.deegree.enterprise.control.WebListener#actionPerformed(org.deegree.enterprise.control.FormEvent)
095: */
096: @Override
097: public void actionPerformed(FormEvent event) {
098:
099: RPCWebEvent rpc = (RPCWebEvent) event;
100: try {
101: validate(rpc);
102: } catch (Exception e) {
103: gotoErrorPage("Not a valid RPC for GetWMSLayer\n"
104: + e.getMessage());
105: }
106:
107: WMSCapabilities capabilities = null;
108: URL url = null;
109: try {
110: url = getURL(rpc);
111: capabilities = getWMSCapabilities(url);
112: } catch (MalformedURLException ue) {
113: LOG.logError(ue.getMessage(), ue);
114: gotoErrorPage(Messages.getMessage("IGEO_STD_INVALID_URL",
115: url));
116: return;
117: } catch (PortalException e) {
118: LOG.logError(e.getMessage(), e);
119: gotoErrorPage(Messages
120: .getMessage("IGEO_STD_ADDWMS_INVALID_VERSION"));
121: return;
122: } catch (IOException e) {
123: LOG.logError(e.getMessage(), e);
124: gotoErrorPage(Messages.getMessage(
125: "IGEO_STD_ADDWMS_NO_TARGET", url));
126: return;
127: } catch (SAXException e) {
128: LOG.logError(e.getMessage(), e);
129: gotoErrorPage(Messages.getMessage("IGEO_STD_INVALID_XML",
130: url));
131: return;
132: } catch (InvalidCapabilitiesException e) {
133: LOG.logError(e.getMessage(), e);
134: gotoErrorPage(Messages.getMessage(
135: "IGEO_STD_ADDWMS_INVALID_CAPS", url));
136: return;
137: } catch (XMLParsingException e) {
138: LOG.logError(e.getMessage(), e);
139: gotoErrorPage(Messages.getMessage("IGEO_STD_INVALID_XML",
140: url));
141: return;
142: }
143:
144: String s = ClientHelper
145: .getLayersAsTree(capabilities.getLayer());
146: getRequest().setAttribute("WMSLAYER", s);
147: getRequest().setAttribute("WMSURL", NetWorker.url2String(url));
148: getRequest().setAttribute("WMSVERSION",
149: capabilities.getVersion());
150: s = capabilities.getServiceIdentification().getTitle();
151: s = s.replaceAll("'", "");
152: getRequest().setAttribute("WMSNAME", s);
153:
154: OperationsMetadata om = capabilities.getOperationMetadata();
155: Operation op = om.getOperation(new QualifiedName("GetMap"));
156: if (op == null)
157: om.getOperation(new QualifiedName("map"));
158: DomainType parameter = (DomainType) op
159: .getParameter(new QualifiedName("Format"));
160:
161: // the request needs a String[], not a String
162: List<TypedLiteral> values = parameter.getValues();
163: String[] valueArray = new String[values.size()];
164: for (int i = 0; i < values.size(); ++i)
165: valueArray[i] = values.get(i).getValue();
166: getRequest().setAttribute("FORMATS", valueArray);
167: }
168:
169: /**
170: * @param rpc
171: * @throws PortalException
172: */
173: private void validate(RPCWebEvent rpc) throws PortalException {
174: RPCMethodCall mc = rpc.getRPCMethodCall();
175: RPCParameter param = mc.getParameters()[0];
176: RPCStruct struct = (RPCStruct) param.getValue();
177: RPCMember address = struct.getMember("WMSURL");
178: if (address == null) {
179: throw new PortalException(
180: "missing parameter WMSURL in RPC for GetWMSLayer");
181: }
182: RPCMember version = struct.getMember("VERSION");
183: if (version != null
184: && (((String) version.getValue()).compareTo("1.3.0") > 0 || ((String) version
185: .getValue()).compareTo("1.1.0") < 0)) {
186: throw new PortalException(
187: "VERSION must be >= 1.0.0 and <= 1.3.0 but it is "
188: + version.getValue());
189: }
190: }
191:
192: /**
193: *
194: * @param rpc
195: * @return wms url
196: * @throws MalformedURLException
197: */
198: private URL getURL(RPCWebEvent rpc) throws MalformedURLException {
199: RPCMethodCall mc = rpc.getRPCMethodCall();
200: RPCParameter param = mc.getParameters()[0];
201: RPCStruct struct = (RPCStruct) param.getValue();
202: String address = RPCUtils.getRpcPropertyAsString(struct,
203: "WMSURL");
204:
205: // according to OGC WMS 1.3 Testsuite a URL to a service operation
206: // via HTTPGet must end with '?' or '&'
207: String href = OWSUtils.validateHTTPGetBaseURL(address);
208:
209: StringBuffer sb = new StringBuffer(href);
210: String version = RPCUtils.getRpcPropertyAsString(struct,
211: "VERSION");
212: if ("1.0.0".equals(version)) {
213: sb.append("service=WMS&request=capabilities");
214: } else {
215: sb.append("service=WMS&request=GetCapabilities");
216: }
217: if (version != null) {
218: if ("1.0.0".equals(version)) {
219: sb.append("&WMTVER=").append(version);
220: } else {
221: sb.append("&version=").append(version);
222: }
223: }
224:
225: String sessionid = RPCUtils.getRpcPropertyAsString(struct,
226: "SESSIONID");
227: if (sessionid != null) {
228: sb.append("&sessionID=").append(sessionid);
229: }
230:
231: String useAuthentification = RPCUtils.getRpcPropertyAsString(
232: struct, "useAuthentification");
233: getRequest().setAttribute("USEAUTHENTICATION",
234: "true".equals(useAuthentification));
235: if ("true".equals(useAuthentification)) {
236: String user = RPCUtils.getRpcPropertyAsString(struct,
237: "USER");
238: if (user != null) {
239: String password = RPCUtils.getRpcPropertyAsString(
240: struct, "USER");
241: sb.append("&user=").append(user);
242: if (password != null) {
243: sb.append("&password=").append(password);
244: }
245: }
246: }
247:
248: return new URL(sb.toString());
249: }
250:
251: /**
252: *
253: * @param url
254: * @return the capabilities object
255: * @throws XMLParsingException
256: * @throws PortalException
257: * @throws IOException
258: * @throws SAXException
259: * @throws InvalidCapabilitiesException
260: * @throws XMLParsingException
261: */
262: private WMSCapabilities getWMSCapabilities(URL url)
263: throws PortalException, IOException, SAXException,
264: InvalidCapabilitiesException, XMLParsingException {
265:
266: WMSCapabilitiesDocument doc = WMSCapabilitiesDocumentFactory
267: .getWMSCapabilitiesDocument(url);
268: WMSCapabilities capabilities = (WMSCapabilities) doc
269: .parseCapabilities();
270:
271: if (capabilities.getVersion().compareTo("1.0.0") < 0
272: || capabilities.getVersion().compareTo("1.3.0") > 0) {
273: throw new PortalException(
274: "VERSION must be >= 1.0.0 && <= 1.3.0");
275: }
276:
277: return capabilities;
278: }
279: }
|