001: /* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
002: * This code is licensed under the GPL 2.0 license, availible at the root
003: * application directory.
004: */
005: package org.vfny.geoserver.wcs.requests.readers;
006:
007: import org.vfny.geoserver.Request;
008: import org.vfny.geoserver.util.requests.CapabilitiesHandler;
009: import org.vfny.geoserver.util.requests.readers.XmlRequestReader;
010: import org.vfny.geoserver.wcs.WcsException;
011: import org.vfny.geoserver.wcs.requests.WCSCapabilitiesRequest;
012: import org.vfny.geoserver.wcs.servlets.WCService;
013: import org.xml.sax.InputSource;
014: import org.xml.sax.SAXException;
015: import org.xml.sax.helpers.ParserAdapter;
016: import java.io.IOException;
017: import java.io.Reader;
018: import javax.servlet.http.HttpServletRequest;
019: import javax.xml.parsers.ParserConfigurationException;
020: import javax.xml.parsers.SAXParser;
021: import javax.xml.parsers.SAXParserFactory;
022:
023: /**
024: * reads a WCS GetCapabilities request from an XML stream
025: *
026: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
027: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
028: * @version $Id: CapabilitiesXmlReader.java 6326 2007-03-15 18:36:40Z jdeolive $
029: */
030: public class CapabilitiesXmlReader extends XmlRequestReader {
031: /**
032: * Constructs a new reader.
033: *
034: * @param service The WFS service handling the request.
035: */
036: public CapabilitiesXmlReader(WCService service) {
037: super (service);
038: }
039:
040: /**
041: * Reads the Capabilities XML request into a CapabilitiesRequest object.
042: *
043: * @param reader The plain POST text from the client.
044: *
045: * @return The read CapabilitiesRequest object.
046: *
047: * @throws WmsException For any problems reading the request
048: */
049: public Request read(Reader reader, HttpServletRequest req)
050: throws WcsException {
051: InputSource requestSource = new InputSource(reader);
052:
053: // instantiante parsers and content handlers
054: CapabilitiesHandler currentRequest = new CapabilitiesHandler(
055: new WCSCapabilitiesRequest(getServiceRef()));
056:
057: // read in XML file and parse to content handler
058: try {
059: SAXParserFactory factory = SAXParserFactory.newInstance();
060: SAXParser parser = factory.newSAXParser();
061: ParserAdapter adapter = new ParserAdapter(parser
062: .getParser());
063: adapter.setContentHandler(currentRequest);
064: adapter.parse(requestSource);
065: LOGGER.fine("just parsed: " + requestSource);
066: } catch (SAXException e) {
067: throw new WcsException(e,
068: "XML capabilities request parsing error",
069: getClass().getName());
070: } catch (IOException e) {
071: throw new WcsException(e,
072: "XML capabilities request input error", getClass()
073: .getName());
074: } catch (ParserConfigurationException e) {
075: throw new WcsException(e,
076: "Some sort of issue creating parser", getClass()
077: .getName());
078: }
079:
080: Request r = currentRequest.getRequest(req);
081:
082: if (r.getService() != null) {
083: final String service = r.getService();
084:
085: if (!service.trim().toUpperCase().startsWith("WCS")) {
086: throw new WcsException("SERVICE parameter is wrong.");
087: }
088: } else {
089: throw new WcsException("SERVICE parameter is mandatory.");
090: }
091:
092: if (r.getVersion() != null) {
093: final String version = r.getVersion();
094:
095: if (!version.equals("1.0.0")) {
096: throw new WcsException("VERSION parameter is wrong.");
097: }
098: } else {
099: throw new WcsException("VERSION parameter is mandatory.");
100: }
101:
102: /*if (r.getRequest() != null) {
103: final String requestType = r.getRequest();
104: if (!requestType.equalsIgnoreCase("GetCapabilities") ) {
105: throw new WcsException("REQUEST parameter is wrong.");
106: }
107: } else {
108: throw new WcsException("REQUEST parameter is mandatory.");
109: }*/
110: return r;
111: }
112: }
|