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.geoserver.ows.adapters;
006:
007: import org.geoserver.ows.HttpServletRequestAware;
008: import org.vfny.geoserver.servlets.AbstractService;
009: import org.vfny.geoserver.util.requests.readers.KvpRequestReader;
010: import java.lang.reflect.Constructor;
011: import java.util.Enumeration;
012: import java.util.HashMap;
013: import java.util.Map;
014: import javax.servlet.http.HttpServletRequest;
015:
016: /**
017: * Wraps an old style {@link KvpRequestReader} in a new
018: * {@link org.geoserver.ows.KvpRequestReader}.
019: * <p>
020: * This class needs to be defined in a spring context like:
021: * <pre>
022: * <code>
023: * <bean id="getMapKvpReader" class="org.geoserver.ows.adapters.KvpRequestReaderAdapter">
024: * <!-- first argument is the request class -->
025: * <constructor-arg index="0" value="org.vfny.geoserver.wms.requests.GetMapRequest" />
026: *
027: * <!-- second argument is the old style kvp reader class -->
028: * <constructor-arg index="1" value="org.vfny.geoserver.wms.requests.GetMapKvpReader" />
029: *
030: * <!-- third argument is the old style service -->
031: * <constructor-arg index="2" ref="wmsService" />
032: * <bean>
033: * </code>
034: * </pre>
035: * </p>
036: *
037: * @author Justin Deoliveira, The Open Planning Project, jdeolive@openplans.org
038: *
039: */
040: public class KvpRequestReaderAdapter extends
041: org.geoserver.ows.KvpRequestReader implements
042: HttpServletRequestAware {
043: Class delegateClass;
044: AbstractService service;
045: HttpServletRequest request;
046:
047: public KvpRequestReaderAdapter(Class requestBean,
048: Class delegateClass, AbstractService service) {
049: super (requestBean);
050: this .delegateClass = delegateClass;
051: this .service = service;
052: }
053:
054: public void setHttpRequest(HttpServletRequest request) {
055: this .request = request;
056: }
057:
058: public Object createRequest() throws Exception {
059: //simulate the old kvp processin
060: Map kvp = new HashMap();
061: String paramName;
062: String paramValue;
063:
064: for (Enumeration pnames = request.getParameterNames(); pnames
065: .hasMoreElements();) {
066: paramName = (String) pnames.nextElement();
067: paramValue = request.getParameter(paramName);
068: kvp.put(paramName.toUpperCase(), paramValue);
069: }
070:
071: //look for a constructor, may have to walk up teh class hierachy
072: Class clazz = service.getClass();
073: Constructor constructor = null;
074:
075: while (clazz != null) {
076: try {
077: constructor = delegateClass.getConstructor(new Class[] {
078: Map.class, clazz });
079:
080: break;
081: } catch (NoSuchMethodException e) {
082: clazz = clazz.getSuperclass();
083: }
084: }
085:
086: if (constructor == null) {
087: throw new IllegalStateException(
088: "No appropriate constructor");
089: }
090:
091: //create an instance of the delegate
092: KvpRequestReader delegate = (KvpRequestReader) constructor
093: .newInstance(new Object[] { kvp, service });
094:
095: //create the request object
096: return delegate.getRequest(request);
097: }
098:
099: public Object read(Object request, Map kvp, Map rawKvp)
100: throws Exception {
101: //request object already initialized, just send it back
102: return request;
103: }
104: }
|