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.CoverageHandler;
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 java.util.logging.Level;
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: * DOCUMENT ME!
025: *
026: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
027: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
028: */
029: public class GetCoverageXmlReader extends XmlRequestReader {
030: public GetCoverageXmlReader(WCService service) {
031: super (service);
032: }
033:
034: public Request read(Reader reader, HttpServletRequest req)
035: throws WcsException {
036: // translate string into a proper SAX input source
037: InputSource requestSource = new InputSource(reader);
038:
039: // instantiante parsers and content handlers
040: CoverageHandler contentHandler = new CoverageHandler(
041: (WCService) getServiceRef());
042:
043: // read in XML file and parse to content handler
044: try {
045: if (LOGGER.isLoggable(Level.FINEST)) {
046: LOGGER.finest("about to create parser");
047: }
048:
049: SAXParserFactory factory = SAXParserFactory.newInstance();
050: SAXParser parser = factory.newSAXParser();
051: ParserAdapter adapter = new ParserAdapter(parser
052: .getParser());
053:
054: if (LOGGER.isLoggable(Level.FINEST)) {
055: LOGGER.finest("setting the content handler");
056: }
057:
058: if (LOGGER.isLoggable(Level.FINEST)) {
059: LOGGER.finest("content handler = " + contentHandler);
060: }
061:
062: adapter.setContentHandler(contentHandler);
063:
064: if (LOGGER.isLoggable(Level.FINEST)) {
065: LOGGER.finest("about to parse");
066: }
067:
068: if (LOGGER.isLoggable(Level.FINEST)) {
069: LOGGER.finest("calling parse on " + requestSource);
070: }
071:
072: adapter.parse(requestSource);
073:
074: if (LOGGER.isLoggable(Level.FINEST)) {
075: LOGGER.fine("just parsed: " + requestSource);
076: }
077: } catch (SAXException e) {
078: throw new WcsException(e,
079: "XML getCoverage request SAX parsing error",
080: XmlRequestReader.class.getName());
081: } catch (IOException e) {
082: throw new WcsException(e,
083: "XML get coverage request input error",
084: XmlRequestReader.class.getName());
085: } catch (ParserConfigurationException e) {
086: throw new WcsException(e,
087: "Some sort of issue creating parser",
088: XmlRequestReader.class.getName());
089: }
090:
091: Request r = contentHandler.getRequest(req);
092:
093: if (r.getService() != null) {
094: final String service = r.getService();
095:
096: if (!service.trim().toUpperCase().startsWith("WCS")) {
097: throw new WcsException("SERVICE parameter is wrong.");
098: }
099: } else {
100: throw new WcsException("SERVICE parameter is mandatory.");
101: }
102:
103: if (r.getVersion() != null) {
104: final String version = r.getVersion();
105:
106: if (!version.equals("1.0.0")) {
107: throw new WcsException("VERSION parameter is wrong.");
108: }
109: } else {
110: throw new WcsException("VERSION parameter is mandatory.");
111: }
112:
113: if (r.getRequest() != null) {
114: final String requestType = r.getRequest();
115:
116: if (!requestType.equalsIgnoreCase("GetCoverage")) {
117: throw new WcsException("REQUEST parameter is wrong.");
118: }
119: } else {
120: throw new WcsException("REQUEST parameter is mandatory.");
121: }
122:
123: return r;
124: }
125: }
|