org.geoserver.ows.adapters.KvpRequestReaderAdapter Maven / Gradle / Ivy
/* Copyright (c) 2001 - 2007 TOPP - www.openplans.org. All rights reserved.
* This code is licensed under the GPL 2.0 license, availible at the root
* application directory.
*/
package org.geoserver.ows.adapters;
import org.geoserver.ows.HttpServletRequestAware;
import org.vfny.geoserver.servlets.AbstractService;
import org.vfny.geoserver.util.requests.readers.KvpRequestReader;
import java.lang.reflect.Constructor;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
/**
* Wraps an old style {@link KvpRequestReader} in a new
* {@link org.geoserver.ows.KvpRequestReader}.
*
* This class needs to be defined in a spring context like:
*
*
* <bean id="getMapKvpReader" class="org.geoserver.ows.adapters.KvpRequestReaderAdapter">
* <!-- first argument is the request class -->
* <constructor-arg index="0" value="org.vfny.geoserver.wms.requests.GetMapRequest" />
*
* <!-- second argument is the old style kvp reader class -->
* <constructor-arg index="1" value="org.vfny.geoserver.wms.requests.GetMapKvpReader" />
*
* <!-- third argument is the old style service -->
* <constructor-arg index="2" ref="wmsService" />
* <bean>
*
*
*
*
* @author Justin Deoliveira, The Open Planning Project, [email protected]
*
*/
public class KvpRequestReaderAdapter extends org.geoserver.ows.KvpRequestReader
implements HttpServletRequestAware {
Class delegateClass;
AbstractService service;
HttpServletRequest request;
public KvpRequestReaderAdapter(Class requestBean, Class delegateClass, AbstractService service) {
super(requestBean);
this.delegateClass = delegateClass;
this.service = service;
}
public void setHttpRequest(HttpServletRequest request) {
this.request = request;
}
public Object createRequest() throws Exception {
//simulate the old kvp processin
Map kvp = new HashMap();
String paramName;
String paramValue;
for (Enumeration pnames = request.getParameterNames(); pnames.hasMoreElements();) {
paramName = (String) pnames.nextElement();
paramValue = request.getParameter(paramName);
kvp.put(paramName.toUpperCase(), paramValue);
}
//look for a constructor, may have to walk up teh class hierachy
Class clazz = service.getClass();
Constructor constructor = null;
while (clazz != null) {
try {
constructor = delegateClass.getConstructor(new Class[] { Map.class, clazz });
break;
} catch (NoSuchMethodException e) {
clazz = clazz.getSuperclass();
}
}
if (constructor == null) {
throw new IllegalStateException("No appropriate constructor");
}
//create an instance of the delegate
KvpRequestReader delegate = (KvpRequestReader) constructor.newInstance(new Object[] {
kvp, service
});
//create the request object
return delegate.getRequest(request);
}
public Object read(Object request, Map kvp, Map rawKvp) throws Exception {
//request object already initialized, just send it back
return request;
}
}