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.readers.XmlRequestReader;
009: import org.vfny.geoserver.wcs.WcsException;
010: import org.vfny.geoserver.wcs.requests.DescribeHandler;
011: import org.vfny.geoserver.wcs.servlets.WCService;
012: import org.xml.sax.InputSource;
013: import org.xml.sax.SAXException;
014: import org.xml.sax.helpers.ParserAdapter;
015: import java.io.IOException;
016: import java.io.Reader;
017: import javax.servlet.http.HttpServletRequest;
018: import javax.xml.parsers.ParserConfigurationException;
019: import javax.xml.parsers.SAXParser;
020: import javax.xml.parsers.SAXParserFactory;
021:
022: /**
023: * reads in a DescribeCoverageType WCS request from a XML stream
024: *
025: * @author Rob Hranac, TOPP
026: * @author Chris Holmes, TOPP
027: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
028: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
029: * @version 0.1
030: */
031: public class DescribeXmlReader extends XmlRequestReader {
032: /**
033: * Creates a new DescribeXmlReader object.
034: */
035: public DescribeXmlReader(WCService service) {
036: super (service);
037: }
038:
039: /**
040: * DOCUMENT ME!
041: *
042: * @param reader DOCUMENT ME!
043: *
044: * @return DOCUMENT ME!
045: *
046: * @throws WCSException DOCUMENT ME!
047: */
048: public Request read(Reader reader, HttpServletRequest req)
049: throws WcsException {
050: /** create a describe Coverage type request class to return */
051: InputSource requestSource = new InputSource(reader);
052:
053: // instantiante parsers and content handlers
054: DescribeHandler contentHandler = new DescribeHandler(
055: (WCService) 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:
064: adapter.setContentHandler(contentHandler);
065: adapter.parse(requestSource);
066: LOGGER.finer("just parsed: " + requestSource);
067: } catch (SAXException e) {
068: throw new WcsException(e,
069: "XML describe request parsing error", getClass()
070: .getName());
071: } catch (IOException e) {
072: throw new WcsException(e,
073: "XML describe request input error", getClass()
074: .getName());
075: } catch (ParserConfigurationException e) {
076: throw new WcsException(e,
077: "Some sort of issue creating parser", getClass()
078: .getName());
079: }
080:
081: LOGGER.finer("about to return ");
082: LOGGER.finer("returning " + contentHandler.getRequest(req));
083:
084: Request r = contentHandler.getRequest(req);
085:
086: if (r.getService() != null) {
087: final String service = r.getService();
088:
089: if (!service.trim().toUpperCase().startsWith("WCS")) {
090: throw new WcsException("SERVICE parameter is wrong.");
091: }
092: } else {
093: throw new WcsException("SERVICE parameter is mandatory.");
094: }
095:
096: if (r.getVersion() != null) {
097: final String version = r.getVersion();
098:
099: if (!version.equals("1.0.0")) {
100: throw new WcsException("VERSION parameter is wrong.");
101: }
102: } else {
103: throw new WcsException("VERSION parameter is mandatory.");
104: }
105:
106: if (r.getRequest() != null) {
107: final String requestType = r.getRequest();
108:
109: if (!requestType.equalsIgnoreCase("DescribeCoverage")) {
110: throw new WcsException("REQUEST parameter is wrong.");
111: }
112: } else {
113: throw new WcsException("REQUEST parameter is mandatory.");
114: }
115:
116: return r;
117: }
118: }
|