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;
006:
007: import org.vfny.geoserver.servlets.AbstractService;
008: import org.vfny.geoserver.util.Requests;
009: import javax.servlet.http.HttpServletRequest;
010:
011: /**
012: * Defines a general Request type and provides accessor methods for universal
013: * request information.
014: * <p>
015: * Also provides access to the HttpRequest that spawned this GeoServer Request.
016: * This HttpRequest is most often used to lookup information stored in the
017: * Web Container (such as the GeoServer Global information).
018: * </p>
019: *
020: * @author Rob Hranac, TOPP
021: * @author Chris Holmes, TOPP
022: * @author Gabriel Roldan
023: * @author $Author: Alessio Fabiani (alessio.fabiani@gmail.com) $ (last modification)
024: * @author $Author: Simone Giannecchini (simboss1@gmail.com) $ (last modification)
025: * @version $Id: Request.java 7522 2007-09-12 22:00:10Z saul.farber $
026: */
027: abstract public class Request {
028: /**
029: * HttpServletRequest responsible for generating this GeoServer Request.
030: */
031: protected HttpServletRequest httpServletRequest;
032:
033: /**
034: * The service type of the request. In other words, is it a WMS
035: * or a WFS. This is a standard element of a request. It now has
036: * a practical purpose in GeoServer, as a GetCapabilities request
037: * can be WMS or WFS, this element tells which it is.
038: */
039: protected String service;
040:
041: /** Request type */
042: protected String request = "";
043:
044: /** Request version */
045: protected String version = "";
046:
047: /** service reference */
048: protected AbstractService serviceRef;
049:
050: /** reference to the base Url that this request was called with.
051: * Note that this is a complete duplicate of info in the above HttpServletRequest
052: * object, and is mainly a forward-thinking field that's going to stick around when
053: * the above HttpServletRequest goes away.
054: */
055: protected String baseUrl;
056:
057: /**
058: * ServiceType,RequestType,ServiceRef constructor.
059: *
060: * @param serviceType Name of hte service (example, WFS)
061: * @param requestType Name of the request (example, GetCapabilties)
062: * @param serviceRef The servlet for the request.
063: */
064: protected Request(String serviceType, String requestType,
065: AbstractService serviceRef) {
066: this .service = serviceType;
067: this .request = requestType;
068: this .serviceRef = serviceRef;
069: }
070:
071: /**
072: * Set the baseUrl that this request was called with.
073: */
074: public void setBaseUrl(String s) {
075: baseUrl = s;
076: }
077:
078: /**
079: * Gets the base url that made this request. This is used to return the
080: * referenced schemas and whatnot relative to the request.
081: * @return The base portion of the url that the client used to make the request.
082: */
083: public String getBaseUrl() {
084: return baseUrl;
085: }
086:
087: /**
088: * Gets requested service.
089: *
090: * @return The requested service.
091: */
092: public String getService() {
093: return this .service;
094: }
095:
096: /**
097: * Gets requested service.
098: *
099: * @param service The requested service.
100: */
101: public void setService(String service) {
102: this .service = service;
103: }
104:
105: /**
106: * Gets requested request type.
107: * <p>
108: * TODO: Could this bre renamed getType() for clarity?
109: * </p>
110: * <p>
111: * Um, no. getType() is less clear. getRequest makes sense because
112: * this is directly modeled off of the XML and KVP Requests that a
113: * wfs or wms makes, and they all contain an element called Request.
114: *
115: * @return The name of the request.
116: */
117: public String getRequest() {
118: return this .request;
119: }
120:
121: /**
122: * Sets requested request type.
123: *
124: * @param reqeust The type of request.
125: */
126: public void setRequest(String requestType) {
127: this .request = requestType;
128: }
129:
130: /**
131: * Return version type.
132: *
133: * @return The request type version.
134: */
135: public String getVersion() {
136: return this .version;
137: }
138:
139: /**
140: * Sets version type.
141: *
142: * @param version The request type version.
143: */
144: public void setVersion(String version) {
145: this .version = version;
146: }
147:
148: /**
149: * Sets the reference to the service.
150: */
151: public void setServiceRef(AbstractService serviceRef) {
152: this .serviceRef = serviceRef;
153: }
154:
155: /**
156: * @return the reference the service.
157: */
158: public AbstractService getServiceRef() {
159: return serviceRef;
160: }
161:
162: public boolean equals(Object o) {
163: if (!(o instanceof Request)) {
164: return false;
165: }
166:
167: Request req = (Request) o;
168: boolean equals = true;
169: equals = ((request == null) ? (req.getRequest() == null)
170: : request.equals(req.getRequest()))
171: && equals;
172: equals = ((version == null) ? (req.getVersion() == null)
173: : version.equals(req.getVersion()))
174: && equals;
175: equals = ((service == null) ? (req.getService() == null)
176: : service.equals(req.getService()))
177: && equals;
178:
179: return equals;
180: }
181:
182: /**
183: * Generate a hashCode based on this Request Object.
184: */
185: public int hashCode() {
186: int result = 17;
187: result = (23 * result)
188: + ((request == null) ? 0 : request.hashCode());
189: result = (23 * result)
190: + ((request == null) ? 0 : version.hashCode());
191: result = (23 * result)
192: + ((request == null) ? 0 : service.hashCode());
193:
194: return result;
195: }
196:
197: /**
198: * Retrive the ServletRequest that generated this GeoServer request.
199: * <p>
200: * The ServletRequest is often used to:
201: * </p>
202: * <ul>
203: * <li>Access the Sesssion and WebContainer by execute opperations
204: * </li>
205: * <li>Of special importance is the use of the ServletRequest to locate the GeoServer Application
206: * </li>
207: * </p>
208: * <p>
209: * This method is called by AbstractServlet during the processing of a Request.
210: * </p>
211: * @return The HttpServletRequest responsible for generating this SerivceRequest
212: */
213: public HttpServletRequest getHttpServletRequest()
214: throws ClassCastException {
215: return httpServletRequest;
216: }
217:
218: //JD: delete this
219: // public WMS getWMS(){
220: // WMS vp = Requests.getWMS( getHttpServletRequest() );
221: // return vp;
222: // }
223: //
224: // public WFS getWFS(){
225: // WFS vp = Requests.getWFS( getHttpServletRequest() );
226: // return vp;
227: // }
228: public String getRootDir() {
229: throw new IllegalArgumentException(
230: "getRootDir -- functionality removed - please verify that its okay with geoserver_data_dir");
231:
232: //return httpServletRequest.getSession().getServletContext().getRealPath("/");
233: }
234:
235: /**
236: * Gets the url that schemas should be referenced from. For now this will
237: * always be local, if we bring back schemaBaseUrl as a param then that will
238: * be possible too. So it is just baseUrl plus data/capabilities, which
239: * is where we store the schemas now.
240: *
241: * @return the base url of the schemas. Will be getBaseUrl() + data/capabilities.
242: */
243: public String getSchemaBaseUrl() {
244: return Requests.getSchemaBaseUrl(getHttpServletRequest(),
245: serviceRef.getGeoServer());
246: }
247:
248: /**
249: * Whether this request was sent through one of the dispatchers, or if
250: * it went directly through the servlet. This is used by the capabilities
251: * response, since we give back a dispatched capabilities document to clients
252: * who request it with a dispatcher.
253: * @return true if the request came through a dispatcher.
254: */
255: public boolean isDispatchedRequest() {
256: HttpServletRequest hsr = getHttpServletRequest();
257:
258: // will happen if the dispatcher was called, as opposed to using the /wfs url.
259: String uri = hsr.getRequestURI();
260:
261: if (uri != null) {
262: uri = uri.toLowerCase();
263: }
264:
265: // will happen if the dispatcher was called, as opposed to using the /wfs url.
266: if (uri.endsWith("/wcs") || uri.endsWith("/wfs")
267: || uri.endsWith("/wms")) {
268: return true;
269: }
270:
271: return false;
272: }
273:
274: /**
275: * Tests if user is Logged into GeoServer.
276: *
277: * @return <code>true</code> if user is logged in
278: */
279: public boolean isLoggedIn() {
280: return Requests.isLoggedIn(getHttpServletRequest());
281: }
282:
283: /**
284: * Sets the servletRequest that generated this GeoServer request.
285: * <p>
286: * The ServletRequest is often used to:
287: * </p>
288: * <ul>
289: * <li>Access the Sesssion and WebContainer by execute opperations
290: * </li>
291: * <li>Of special importance is the use of the ServletRequest to locate the GeoServer Application
292: * </li>
293: * </p>
294: * @param servletRequest The servletRequest to set.
295: */
296: public void setHttpServletRequest(HttpServletRequest servletRequest) {
297: httpServletRequest = servletRequest;
298: }
299: }
|