01: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
02: * This code is licensed under the GPL 2.0 license, availible at the root
03: * application directory.
04: */
05: package org.vfny.geoserver.wms.requests;
06:
07: import org.vfny.geoserver.Request;
08: import org.vfny.geoserver.util.requests.CapabilitiesHandler;
09: import org.vfny.geoserver.util.requests.readers.XmlRequestReader;
10: import org.vfny.geoserver.wms.WmsException;
11: import org.vfny.geoserver.wms.servlets.WMService;
12: import org.xml.sax.InputSource;
13: import org.xml.sax.SAXException;
14: import org.xml.sax.helpers.ParserAdapter;
15: import java.io.IOException;
16: import java.io.Reader;
17: import javax.servlet.http.HttpServletRequest;
18: import javax.xml.parsers.ParserConfigurationException;
19: import javax.xml.parsers.SAXParser;
20: import javax.xml.parsers.SAXParserFactory;
21:
22: /**
23: * reads a WMS GetCapabilities request from an XML stream
24: *
25: * @author Gabriel Rold?n
26: * @version $Id: CapabilitiesXmlReader.java 6326 2007-03-15 18:36:40Z jdeolive $
27: *
28: * @task TODO: see if it must be refactored to read WMS GetCapabilities too
29: */
30: public class CapabilitiesXmlReader extends XmlRequestReader {
31: /**
32: * Creates the new reader.
33: *
34: * @param service The WMS service hanlding the request.
35: */
36: public CapabilitiesXmlReader(WMService service) {
37: super (service);
38: }
39:
40: /**
41: * Reads the Capabilities XML request into a CapabilitiesRequest object.
42: *
43: * @param reader The plain POST text from the client.
44: *
45: * @return The read CapabilitiesRequest object.
46: *
47: * @throws WmsException For any problems reading the request
48: */
49: public Request read(Reader reader, HttpServletRequest req)
50: throws WmsException {
51: InputSource requestSource = new InputSource(reader);
52:
53: // instantiante parsers and content handlers
54: CapabilitiesHandler currentRequest = new CapabilitiesHandler(
55: new WMSCapabilitiesRequest(getServiceRef()));
56:
57: // read in XML file and parse to content handler
58: try {
59: SAXParserFactory factory = SAXParserFactory.newInstance();
60: SAXParser parser = factory.newSAXParser();
61: ParserAdapter adapter = new ParserAdapter(parser
62: .getParser());
63: adapter.setContentHandler(currentRequest);
64: adapter.parse(requestSource);
65: LOGGER.fine("just parsed: " + requestSource);
66: } catch (SAXException e) {
67: throw new WmsException(e,
68: "XML capabilities request parsing error",
69: getClass().getName());
70: } catch (IOException e) {
71: throw new WmsException(e,
72: "XML capabilities request input error", getClass()
73: .getName());
74: } catch (ParserConfigurationException e) {
75: throw new WmsException(e,
76: "Some sort of issue creating parser", getClass()
77: .getName());
78: }
79:
80: Request r = currentRequest.getRequest(req);
81:
82: return r;
83: }
84: }
|