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.vfny.geoserver.ServiceException;
008: import org.vfny.geoserver.servlets.Dispatcher;
009: import java.io.BufferedInputStream;
010: import java.io.BufferedOutputStream;
011: import java.io.BufferedReader;
012: import java.io.File;
013: import java.io.FileInputStream;
014: import java.io.IOException;
015: import java.io.Reader;
016: import java.util.Enumeration;
017: import java.util.HashMap;
018: import java.util.Map;
019: import java.util.logging.Logger;
020: import javax.servlet.http.HttpServletRequest;
021:
022: /**
023: * Reads in a generic request and attempts to determine its type.
024: *
025: * @author Chris Holmes, TOPP
026: * @author Gabriel Rold?n
027: * @version $Id: DispatcherKvpReader.java 7746 2007-11-13 15:38:35Z aaime $
028: */
029: public class DispatcherKvpReader {
030: /** Class logger */
031: private static Logger LOGGER = org.geotools.util.logging.Logging
032: .getLogger("org.vfny.geoserver.requests.readers");
033: private String queryString;
034: private Map requestParams;
035:
036: /**
037: * Constructor with raw request string. Calls parent.
038: *
039: * @param reader A reader of the request from the http client.
040: * @param req The actual request made.
041: *
042: * @throws ServiceException DOCUMENT ME!
043: * @throws IOException
044: */
045: public void read(BufferedReader requestReader,
046: HttpServletRequest req) throws ServiceException,
047: IOException {
048: final StringBuffer output = new StringBuffer();
049: int c;
050:
051: while (-1 != (c = requestReader.read())) {
052: output.append((char) c);
053: }
054:
055: requestReader.close();
056: this .queryString = output.toString();
057:
058: this .requestParams = KvpRequestReader
059: .parseKvpSet(this .queryString);
060: }
061:
062: /**
063: * Returns the request type for a given KVP set.
064: *
065: * @param kvPairs DOCUMENT ME!
066: *
067: * @return Request type.
068: */
069: public static int getRequestType(Map kvPairs) {
070: String responseType = ((String) kvPairs.get("REQUEST"));
071: LOGGER.finer("dispatcher got request " + responseType);
072:
073: if (responseType != null) {
074: responseType = responseType.toUpperCase();
075:
076: if (responseType.equals("GETCAPABILITIES")
077: || responseType.equals("CAPABILITIES")) {
078: return Dispatcher.GET_CAPABILITIES_REQUEST;
079: } else if (responseType.equals("DESCRIBEFEATURETYPE")) {
080: return Dispatcher.DESCRIBE_FEATURE_TYPE_REQUEST;
081: } else if (responseType.equals("GETFEATURE")) {
082: return Dispatcher.GET_FEATURE_REQUEST;
083: } else if (responseType.equals("TRANSACTION")) {
084: return Dispatcher.TRANSACTION_REQUEST;
085: } else if (responseType.equals("GETFEATUREWITHLOCK")) {
086: return Dispatcher.GET_FEATURE_LOCK_REQUEST;
087: } else if (responseType.equals("LOCKFEATURE")) {
088: return Dispatcher.LOCK_REQUEST;
089: } else if (responseType.equals("GETMAP")
090: || responseType.equals("MAP")) {
091: return Dispatcher.GET_MAP_REQUEST;
092: } else if (responseType.equals("GETFEATUREINFO")) {
093: return Dispatcher.GET_FEATURE_INFO_REQUEST;
094: } else if (responseType.equals("DESCRIBELAYER")) {
095: return Dispatcher.DESCRIBE_LAYER_REQUEST;
096: } else if (responseType.equals("GETLEGENDGRAPHIC")) {
097: return Dispatcher.GET_LEGEND_GRAPHIC_REQUEST;
098: } else {
099: return Dispatcher.UNKNOWN;
100: }
101: } else {
102: return Dispatcher.UNKNOWN;
103: }
104: }
105:
106: /**
107: * Returns the request type for a given KVP set.
108: *
109: * @param kvPairs DOCUMENT ME!
110: *
111: * @return Request type.
112: */
113: public static int getServiceType(Map kvPairs) {
114: String serviceType = ((String) kvPairs.get("SERVICE"));
115:
116: if (serviceType != null) {
117: serviceType = serviceType.toUpperCase();
118:
119: if (serviceType.equals("WFS")) {
120: return Dispatcher.WFS_SERVICE;
121: } else if (serviceType.equals("WMS")) {
122: return Dispatcher.WMS_SERVICE;
123: } else {
124: return Dispatcher.UNKNOWN;
125: }
126: } else {
127: return Dispatcher.UNKNOWN;
128: }
129: }
130:
131: /**
132: * @return The service, WFS,WMS,WCS,etc...
133: */
134: public String getService() {
135: if (requestParams.containsKey("SERVICE")) {
136: return (String) requestParams.get("SERVICE");
137: } else {
138: return null;
139: }
140: }
141:
142: /**
143: * @return The request, GetCapabilities,GetMap,etc...
144: */
145: public String getRequest() {
146: if (requestParams.containsKey("REQUEST")) {
147: return (String) requestParams.get("REQUEST");
148: } else {
149: return null;
150: }
151: }
152:
153: public String getQueryString() {
154: return queryString;
155: }
156: }
|