[Yanel-commits] rev 23794 - in
public/yanel/trunk/src/core/java/org/wyona/yanel/core: . util
josias at wyona.com
josias at wyona.com
Mon Apr 16 20:53:22 CEST 2007
Author: josias
Date: 2007-04-16 20:53:21 +0200 (Mon, 16 Apr 2007)
New Revision: 23794
Modified:
public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java
public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java
Log:
made the ResourceManager pass the request parameters to the resource. this allows to access the parameters inside of a resource independently of the request.
Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java 2007-04-16 16:50:47 UTC (rev 23793)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/Resource.java 2007-04-16 18:53:21 UTC (rev 23794)
@@ -16,6 +16,7 @@
package org.wyona.yanel.core;
+import java.util.HashMap;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
@@ -46,6 +47,7 @@
*/
public Resource() {
rtd = null;
+ this.parameters = new HashMap();
}
/**
@@ -161,12 +163,48 @@
this.response = response;
}
+ /**
+ * Gets the parameter map of this resource.
+ * @return map with parameter names as keys and parameter values as values.
+ */
public Map getParameters() {
return parameters;
}
+ /**
+ * Sets the parameter map of this resource.
+ * If a parameter map already exists, it will be replaced.
+ * @param parameters map with parameter names as keys and parameter values as values.
+ */
public void setParameters(Map parameters) {
this.parameters = parameters;
}
+
+ /**
+ * Gets the parameter with the given name.
+ * @param name
+ * @return parameter object or null if no parameter with this name exists.
+ */
+ public Object getParameter(String name) {
+ return this.parameters.get(name);
+ }
+ /**
+ * Gets the parameter with the given name as a string.
+ * @param name
+ * @return parameter string or null if no parameter with this name exists.
+ */
+ public String getParameterAsString(String name) {
+ return this.parameters.get(name).toString();
+ }
+
+ /**
+ * Sets the parameter with the given name and value.
+ * @param name
+ * @param value
+ */
+ public void setParameter(String name, Object value) {
+ this.parameters.put(name, value);
+ }
+
}
Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java 2007-04-16 16:50:47 UTC (rev 23793)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/ResourceManager.java 2007-04-16 18:53:21 UTC (rev 23794)
@@ -17,6 +17,7 @@
package org.wyona.yanel.core;
import java.io.Reader;
+import java.util.Enumeration;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
@@ -24,6 +25,7 @@
import org.apache.log4j.Category;
import org.wyona.yanel.core.map.Map;
import org.wyona.yanel.core.map.Realm;
+import org.wyona.yanel.core.util.HttpServletRequestHelper;
import org.wyona.yanel.core.util.PathUtil;
import org.wyona.yarep.core.NoSuchNodeException;
@@ -66,6 +68,8 @@
resource.setRequest(request);
resource.setResponse(response);
+ passParameters(resource, request);
+
return resource;
} else {
log.error("No resource registered for rti: " + universalName);
@@ -93,6 +97,8 @@
resource.setRequest(request);
resource.setResponse(response);
+ passParameters(resource, request);
+
return resource;
} else {
log.error("Resource Type Definition cannot be determined for: " + realm + ", " + path + ", " + rc.getUniversalName());
@@ -145,4 +151,32 @@
return new ResourceTypeIdentifier("<{http://www.wyona.org/yanel/resource/1.0}file/>", null);
}
}
+
+ /**
+ * Passes request parameter to a resource.
+ * String parameters will be decoded.
+ * TODO: handle multipart requests.
+ * @param resource
+ * @param request
+ */
+ protected void passParameters(Resource resource, HttpServletRequest request) {
+ if (request != null) {
+ Enumeration paramNames = request.getParameterNames();
+
+ while (paramNames.hasMoreElements()) {
+ String name = (String)paramNames.nextElement();
+ String[] values = request.getParameterValues(name);
+ if (values.length == 1) {
+ resource.setParameter(name, HttpServletRequestHelper.decode(values[0]));
+ } else {
+ String[] stringValues = new String[values.length];
+ for (int i = 0; i < values.length; i++) {
+ stringValues[i] = HttpServletRequestHelper.decode(values[i]);
+ }
+ resource.setParameter(name, stringValues);
+ }
+ }
+ }
+ }
+
}
Modified: public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java
===================================================================
--- public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java 2007-04-16 16:50:47 UTC (rev 23793)
+++ public/yanel/trunk/src/core/java/org/wyona/yanel/core/util/HttpServletRequestHelper.java 2007-04-16 18:53:21 UTC (rev 23794)
@@ -41,7 +41,14 @@
return decode(value);
}
- private static String decode(String str) {
+ /**
+ * Fixes the encoding of a request parameter.
+ * The servlet container normally decodes parameters as iso-8859-1, although they are
+ * actually encoded as utf-8. This is wrong and has to be corrected by this method.
+ * @param str parameter with wrong encoding
+ * @return parameter with fixed encoding.
+ */
+ public static String decode(String str) {
if (str == null) return null;
try {
if (container_encoding == null)
@@ -49,7 +56,7 @@
byte[] bytes = str.getBytes(container_encoding);
return new String(bytes, form_encoding);
} catch (UnsupportedEncodingException uee) {
- throw new RuntimeException("Unsupported Encoding Exception", uee);
+ throw new RuntimeException(uee.toString(), uee);
}
}
}
More information about the Yanel-commits
mailing list