001: //$HeadURL: https://svn.wald.intevation.org/svn/deegree/base/trunk/src/org/deegree/portal/wac/WACServlet.java $
002: /*---------------- FILE HEADER ------------------------------------------
003:
004: This file is part of deegree.
005: Copyright (C) 2001-2008 by:
006: 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: 53115 Bonn
031: Germany
032: E-Mail: poth@lat-lon.de
033:
034: Klaus Greve
035: Department of Geography
036: University of Bonn
037: Meckenheimer Allee 166
038: 53115 Bonn
039: Germany
040: E-Mail: klaus.greve@uni-bonn.de
041:
042: ---------------------------------------------------------------------------*/
043: package org.deegree.portal.wac;
044:
045: import java.io.IOException;
046: import java.io.InputStream;
047: import java.io.OutputStream;
048: import java.io.PrintWriter;
049: import java.net.MalformedURLException;
050: import java.net.URL;
051: import java.util.Collections;
052: import java.util.HashMap;
053: import java.util.List;
054: import java.util.Map;
055:
056: import javax.servlet.ServletException;
057: import javax.servlet.http.HttpServlet;
058: import javax.servlet.http.HttpServletRequest;
059: import javax.servlet.http.HttpServletResponse;
060:
061: import org.deegree.framework.log.ILogger;
062: import org.deegree.framework.log.LoggerFactory;
063: import org.deegree.framework.util.KVP2Map;
064: import org.deegree.framework.util.StringTools;
065: import org.deegree.framework.xml.XMLFragment;
066: import org.deegree.model.metadata.iso19115.Linkage;
067: import org.deegree.model.metadata.iso19115.OnlineResource;
068: import org.deegree.ogcwebservices.InvalidParameterValueException;
069: import org.deegree.ogcwebservices.wms.XMLFactory;
070: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilities;
071: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocument;
072: import org.deegree.ogcwebservices.wms.capabilities.WMSCapabilitiesDocumentFactory;
073: import org.deegree.owscommon_new.DCP;
074: import org.deegree.owscommon_new.HTTP;
075: import org.deegree.owscommon_new.Operation;
076: import org.deegree.owscommon_new.OperationsMetadata;
077: import org.w3c.dom.Document;
078:
079: /**
080: *
081: *
082: * @author <a href="mailto:poth@lat-lon.de">Andreas Poth </a>
083: * @author last edited by: $Author: apoth $
084: *
085: * @version $Revision: 9346 $, $Date: 2007-12-27 08:39:07 -0800 (Thu, 27 Dec 2007) $
086: */
087: public class WACServlet extends HttpServlet {
088:
089: private Map<String, String> users = new HashMap<String, String>();
090:
091: private Map<String, String> passwords = new HashMap<String, String>();
092:
093: private Map<String, String> sessionIDs = Collections
094: .synchronizedMap(new HashMap<String, String>());
095:
096: private String host = null;
097:
098: private int port = 443;
099:
100: private String path = null;
101:
102: private String certificate = null;
103:
104: private String wacURL = null;
105:
106: private static final ILogger LOG = LoggerFactory
107: .getLogger(WACServlet.class);
108:
109: @Override
110: public void init() throws ServletException {
111: super .init();
112: String user = getInitParameter("USER");
113: if (user == null) {
114: throw new ServletException("user must be set!");
115: }
116: users.put("*", user);
117: String password = getInitParameter("PASSWORD");
118: if (password == null) {
119: throw new ServletException("password must be set!");
120: }
121: passwords.put("*", password);
122: host = getInitParameter("HOST");
123: if (host == null) {
124: throw new ServletException("WSS host must be set!");
125: }
126: try {
127: port = Integer.parseInt(getInitParameter("PORT"));
128: } catch (NumberFormatException e) {
129: getServletContext().log("-> using default SSL port 443");
130: }
131: path = getInitParameter("PATH");
132: if (path == null) {
133: throw new ServletException(
134: "path to web application on host must be set!");
135: }
136: certificate = getInitParameter("CERTIFICATE");
137: if (certificate == null) {
138: getServletContext().log("no certificate defined");
139: }
140:
141: }
142:
143: /**
144: * @param request
145: * @param response
146: * @throws IOException
147: */
148: @Override
149: protected void doGet(HttpServletRequest request,
150: HttpServletResponse response) throws IOException {
151:
152: wacURL = "http://" + request.getServerName() + ":"
153: + request.getServerPort() + request.getContextPath()
154: + request.getServletPath();
155:
156: String user = users.get("*");
157: WAClient wac = new WAClient(host, path, port, certificate);
158: if (sessionIDs.get(user) == null) {
159: if (!accessSessionID(wac, response)) {
160: return;
161: }
162: }
163: // get sessionID assigned to the user
164: String sessionID = sessionIDs.get(user);
165: InputStream is = null;
166: try {
167: StringBuffer sb = new StringBuffer(2000);
168: sb.append(request.getQueryString()).append("&sessionID=")
169: .append(sessionID);
170: is = wac.performDoService(request.getQueryString(),
171: sessionID);
172: } catch (Exception e) {
173: e.printStackTrace();
174: sendException(response, "GetSession",
175: "could not perform DoService", StringTools
176: .stackTraceToString(e));
177: }
178:
179: OutputStream os = null;
180: try {
181: os = response.getOutputStream();
182: postProcess(request.getQueryString(), is, os);
183: } catch (Exception e) {
184: sendException(response, "GetSession",
185: "could not post process capabilities", StringTools
186: .stackTraceToString(e));
187: } finally {
188: os.flush();
189: os.close();
190: is.close();
191: }
192: }
193:
194: /**
195: * access a sessionID from a WSS and stores it into an internal Map to use for DoService calls
196: * against a WSS
197: *
198: * @param wac
199: * @param response
200: * @return
201: */
202: private boolean accessSessionID(WAClient wac,
203: HttpServletResponse response) {
204: String user = users.get("*");
205: String password = passwords.get("*");
206: try {
207: String sessionID = wac.performGetSession(user, password);
208: sessionIDs.put(user, sessionID);
209: } catch (WACException e) {
210: e.printStackTrace();
211: sendException(response, "GetSession",
212: "could not perform GetSession", StringTools
213: .stackTraceToString(e));
214: return false;
215: } catch (Exception e) {
216: e.printStackTrace();
217: sendException(response, "GetSession",
218: "could not evaluate GetSession result", StringTools
219: .stackTraceToString(e));
220: return false;
221: }
222: return true;
223: }
224:
225: private void sendException(HttpServletResponse response,
226: String req, String message, String stacktrace) {
227: this .getServletContext().log(message);
228: this .getServletContext().log(stacktrace);
229: response.setContentType("text/xml");
230: if (req == null)
231: req = "";
232: if (message == null)
233: message = "";
234: try {
235: PrintWriter pw = response.getWriter();
236: pw.write("<OGCWebServiceException>");
237: pw.write("<Message>");
238: pw.write(req);
239: pw.write(": failed! ");
240: pw.write(message);
241: pw.write("</Message>");
242: pw.write("<Locator>");
243: pw.write(stacktrace);
244: pw.write("</Locator>");
245: pw.write("</OGCWebServiceException>");
246: pw.close();
247: } catch (Exception ee) {
248: ee.printStackTrace();
249: }
250: }
251:
252: /**
253: * forces a post processing of the wss response if a GetCapabilities request has been performed
254: * by replacing contained the online resources
255: *
256: * @param request
257: * @param is
258: * @param os
259: * stream to write the result too
260: * @return
261: * @throws Exception
262: */
263: private void postProcess(String request, InputStream is,
264: OutputStream os) throws Exception {
265: Map map = KVP2Map.toMap(request);
266: if (map.get("REQUEST").equals("GetCapabilities")) {
267: XMLFragment xml = new XMLFragment();
268: xml.load(is, XMLFragment.DEFAULT_URL);
269: is.close();
270: Document doc = xml.getRootElement().getOwnerDocument();
271: if (map.get("SERVICE").equals("WMS")) {
272: adjustWMSCapabilities(doc, os);
273: } else if (map.get("SERVICE").equals("WFS")) {
274: // TODO
275: } else if (map.get("SERVICE").equals("WCS")) {
276: // TODO
277: } else if (map.get("SERVICE").equals("CSW")) {
278: // TODO
279: } else if (map.get("SERVICE").equals("SCS")) {
280: // TODO
281: }
282: if (map.get("SERVICE").equals("WFS-G")) {
283: // TODO
284: }
285: if (map.get("SERVICE").equals("WTS")) {
286: // TODO
287: }
288: } else {
289: byte[] b = new byte[1024];
290: int c = 0;
291: while ((c = is.read(b)) > 0) {
292: os.write(b, 0, c);
293: }
294: os.close();
295: is.close();
296: }
297: }
298:
299: /**
300: * adjusts the passed WMS capabilities document by replacing the contained online resources with
301: * the address of the WAC
302: *
303: * @param xml
304: * @param user
305: * @throws InvalidParameterValueException
306: */
307: private void adjustWMSCapabilities(Document doc, OutputStream os)
308: throws InvalidParameterValueException, IOException {
309:
310: WMSCapabilities capa = null;
311: try {
312: WMSCapabilitiesDocument cdoc = WMSCapabilitiesDocumentFactory
313: .getWMSCapabilitiesDocument(doc
314: .getDocumentElement());
315: capa = (WMSCapabilities) cdoc.parseCapabilities();
316: } catch (Exception e) {
317: e.printStackTrace();
318: throw new InvalidParameterValueException(
319: "no valid wms capabilities\n"
320: + StringTools.stackTraceToString(e));
321: }
322:
323: OperationsMetadata om = capa.getOperationMetadata();
324:
325: List<Operation> ops = om.getOperations();
326: for (Operation operation : ops) {
327: setNewOnlineResource(operation);
328: }
329:
330: WMSCapabilitiesDocument cdoc = XMLFactory.export(capa);
331: cdoc.write(os);
332: }
333:
334: /**
335: * sets a new online resource for the passed <tt>Operation</tt>
336: *
337: * @param op
338: */
339: private void setNewOnlineResource(Operation op) {
340: List<DCP> dcps = op.getDCP();
341: if (dcps != null) {
342: for (DCP dcp : dcps) {
343: HTTP http = (HTTP) dcp;
344: try {
345: URL url = new URL(wacURL);
346: OnlineResource link = new OnlineResource(
347: new Linkage(url));
348: List<OnlineResource> resources = http.getLinks();
349: int size = resources.size();
350: resources.clear();
351: for (int i = 0; i < size; ++i)
352: resources.add(link);
353: } catch (MalformedURLException e1) {
354: LOG.logError(e1.getLocalizedMessage(), e1);
355: }
356: }
357: }
358: }
359:
360: /**
361: * @param request
362: * @param response
363: * @throws ServletException
364: * @throws IOException
365: */
366: @Override
367: protected void doPost(HttpServletRequest request,
368: HttpServletResponse response) throws ServletException,
369: IOException {
370: doGet(request, response);
371: }
372:
373: }
|