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.util.requests.readers;
006:
007: import org.apache.xerces.parsers.SAXParser;
008: import org.vfny.geoserver.ServiceException;
009: import org.vfny.geoserver.util.requests.DispatcherHandler;
010: import org.xml.sax.InputSource;
011: import org.xml.sax.SAXException;
012: import org.xml.sax.XMLReader;
013: import java.io.IOException;
014: import java.io.Reader;
015: import java.util.logging.Logger;
016: import javax.servlet.http.HttpServletRequest;
017:
018: /**
019: * This reads in a request and figures out what servlet to dispatch it to.
020: * This does not extend XmlRequestReader, since it does not actually create
021: * the Request, it is just used to pass on the request to a service that can
022: * make the request. The Reader passed in should never be the one directly
023: * from the HttpServletRequest, if that is used then the next xml reader to
024: * get the request will not work. A new BufferedReader should be constructed
025: * from the reader of HttpServletRequest, and its mark should be set and then
026: * reset after this reader is done with it. Nothing else seems to work, for
027: * some reason.
028: *
029: * <p>
030: * In an ideal, refactored world we would implement our handlers better, and
031: * the xml reader could dynamically figure out which handler to pass it to.
032: * But we have no time for that now, so we'll just live with this.
033: * </p>
034: *
035: * @author Chris Holmes
036: * @version $Id: DispatcherXmlReader.java 8179 2008-01-16 16:56:48Z groldan $
037: *
038: * @task REVISIT: This might be better implemented to extend XmlRequestReader,
039: * and to actually construct the requests with the DispatcherHandler.
040: * This way it could really handle WMS dispatching as well, since it
041: * could figure out the service as well as the request. The
042: * getRequestType would be complemented by getServiceType, and each
043: * would just extract the information from the Request that was made.
044: * But right now we don't have much in the way of
045: */
046: public class DispatcherXmlReader {
047: /** Class logger */
048: private static Logger LOGGER = org.geotools.util.logging.Logging
049: .getLogger("org.vfny.geoserver.requests.readers");
050:
051: /** Handler for request interpretation duties. */
052: private DispatcherHandler currentRequest;
053:
054: public DispatcherXmlReader() {
055: }
056:
057: /**
058: * Constructor with raw request string. Calls parent.
059: *
060: * @param reader A reader of the request from the http client.
061: * @param req The actual request made.
062: *
063: * @throws ServiceException DOCUMENT ME!
064: */
065: public void read(Reader reader, HttpServletRequest req)
066: throws ServiceException {
067: //InputSource requestSource = new InputSource((Reader) tempReader);
068: InputSource requestSource = new InputSource(reader);
069:
070: // instantiante parsers and content handlers
071: XMLReader parser = new SAXParser();
072: this .currentRequest = new DispatcherHandler();
073:
074: // read in XML file and parse to content handler
075: try {
076: parser.setContentHandler(currentRequest);
077: parser.parse(requestSource);
078: } catch (SAXException e) {
079: //SAXException does not sets initCause(). Instead, it holds its own "exception" field.
080: if (e.getException() != null && e.getCause() == null) {
081: e.initCause(e.getException());
082: }
083: throw new ServiceException(e, "XML request parsing error",
084: DispatcherXmlReader.class.getName());
085: } catch (IOException e) {
086: throw new ServiceException(e, "XML request input error",
087: DispatcherXmlReader.class.getName());
088: }
089: }
090:
091: /**
092: * @return The service, WFS,WMS,WCS,etc...
093: */
094: public String getService() {
095: return currentRequest.getService();
096: }
097:
098: /**
099: * @return The request, GetCapabilities,GetMap,etc...
100: */
101: public String getRequest() {
102: LOGGER.info("getting request type from " + currentRequest);
103:
104: return currentRequest.getRequest();
105: }
106:
107: //JD: kill these
108: // /**
109: // * Returns the guessed request type..
110: // *
111: // * @return Request type.
112: // */
113: // public int getRequestType() {
114: //
115: //
116: // return currentRequest.getRequestType();
117: // }
118: //
119: // public int getServiceType() {
120: // return currentRequest.getServiceType();
121: // }
122: }
|