Re: getParameter(name) problem
"Jancsi A. Farkas(fx)" <[email protected]>
| Newsgroups | gmane.comp.java.enhydra.barracuda.general |
|---|---|
| Message-ID | <1062063440.2147.30.camel@delphi> |
I have found the problem, it was in
org.enhydra.barracuda.core.helper.servlet.HttpServletRequestWrapper
The modified (for barracuda 1.2.0) file is attached.
Jancsi
On Thu, 2003-08-28 at 11:43, Jancsi A. Farkas(fx) wrote:
>
> There was an issue some time ago with getParam(), not returning the
> right values. As far as I know, this one was fixed, however I still have
> some problems.
>
> For example, it does not make difference between "id" and "component_id"
>
> public Object getItem(String key)
> {
> ViewContext vc = getViewContext();
> .....
>
> // this will return 466, which is wrong, as it is not "id",
> it just
> ends with "id"
> String s = vc.getRequest().getParameter("id");
>
> // this will also return 466, which is ok
> String s = vc.getRequest().getParameter("component_id");
> }
>
> the url used to call this event is someEvent.event?component_id=466.
> Does anybody have some clue about what is happenning?
>
> Thank you
>
> jancsi
>
>
>
> _______________________________________________
> Barracuda mailing list
> [email protected]
> http://barracudamvc.org/lists/listinfo/barracuda
>
HttpServletRequestWrapper.java
(text/x-java, 23.6 KB)
/*
* Enhydra Java Application Server Project
*
* The contents of this file are subject to the Enhydra Public License
* Version 1.1 (the "License"); you may not use this file except in
* compliance with the License. You may obtain a copy of the License on
* the Enhydra web site (http://www.enhydra.org/).
*
* Software distributed under the License is distributed on an "AS IS"
* basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
* the License for the specific terms governing rights and limitations
* under the License.
*
* The Initial Developer of the Enhydra Application Server is Lutris
* Technologies, Inc. The Enhydra Application Server and portions created
* by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
* All Rights Reserved.
*
* Contributor(s):
*
* $Id: HttpServletRequestWrapper.java,v 1.10 2003/01/21 05:42:53 jacobk Exp $
*/
package org.enhydra.barracuda.core.helper.servlet;
import java.io.*;
import java.util.*;
import java.security.*;
import javax.servlet.*;
import javax.servlet.http.*;
import org.enhydra.barracuda.core.helper.state.*;
import org.enhydra.barracuda.plankton.data.*;
/**
* <p>This class acts as a thin wrapper around a ServletRequest. Most calls
* are simply passed through to the underlying request object. This object
* does however, expose a method which allows you to set parameters in
* the request object. This was necessary for cases where we needed to
* be able to do a POST, save the parameters somewhere, and then do a GET
* and reconstitute the parameters from that.
*
* <p>When you instantiate this object, it will automatically check the
* clients session to see if there are any parameter state information that
* needs to be reconstituted into the current request.
*/
public class HttpServletRequestWrapper implements HttpServletRequest {
HttpServletRequest req = null;
List paramList = null;
/**
* Create an HttpServletRequestWrapper around some other
* HttpServletRequest impl. The wrapper adds the ability to
* add/remove parameter values.
*
* @param req the underlying HttpServletRequest
*/
public HttpServletRequestWrapper(HttpServletRequest ireq) {
req = ireq;
//reconstitute any param values from the user's session
ParamPersister.reconstituteReqParamState(this);
}
//-------------------- HttpServletRequestWrapper -------------
/**
* Set a given parameter (note that this is backed by a hashmap,
* so the structure is slightly different than that of the
* underlying ServletRequest which allows multiple paramters
* with the same name). This means that if you attempt to
* set a parameter whose key already exists you will effectively
* overwrite the existing value.
*
* @param name the key name for the parameter
* @param value the value associated with the given key
*/
public void addParameter(String name, String value) {
//eliminate the obvious
if (name==null) return;
//make sure the paramList is initialized
if (paramList==null) setupParamList();
//finally store the new value
paramList.add(new Param(name, value));
}
/**
* Remove the first parameter whose key matches the specified name
*
* @param name the key name for the parameter
*/
public void removeParameter(String name) {
//eliminate the obvious
if (name==null) return;
//make sure the paramList is initialized
if (paramList==null) setupParamList();
//finally remove the first occurence of the parameter
for (int i=0, max=paramList.size(); i<max; i++) {
Param param = (Param) paramList.get(i);
if (param.getKey().equals(name)) {
paramList.remove(i);
break;
}
}
}
/**
* Remove all parameters for a specified name
*
* @param name the key name for the parameter
*/
public void removeAllParameters(String name) {
//eliminate the obvious
if (name==null) return;
//make sure the paramList is initialized
if (paramList==null) setupParamList();
//finally remove the all occurences of the parameter
for (int i=paramList.size()-1; i>=0; i--) {
Param param = (Param) paramList.get(i);
if (param.getKey().equals(name)) paramList.remove(i);
}
}
/**
* Reset the parameter values to their original state
* (ie. the actual values in the request)
*/
public void resetParameters() {
paramList=null;
}
//-------------------- Utility stuff -------------------------
private void setupParamList() {
//eliminate the obvious (only initialize once!)
if (paramList!=null) return;
//create the param list
paramList = new ArrayList(10);
//now copy in all param values from the underlying servlet
//request. From this point on then, the param values will
//be maintained in the paramList
Enumeration enum = req.getParameterNames();
while (enum.hasMoreElements()) {
//get the key
String key = (String) enum.nextElement();
//find all values associated with the key
String[] vals = req.getParameterValues(key);
for (int i=0, max=vals.length; i<max; i++) {
paramList.add(new Param(key, vals[i]));
}
}
}
/**
* This inner class implements Enumaration. It will effectively
* enumerate over all of the parameter key names.
*/
class LocalEnumerator implements Enumeration {
List keyList = null;
Iterator it = null;
public LocalEnumerator(List iparamList) {
keyList = new ArrayList(iparamList.size());
it = iparamList.iterator();
while (it.hasNext()) {
Param param = (Param) it.next();
if (!keyList.contains(param.getKey())) keyList.add(param.getKey());
}
it = keyList.iterator();
}
public boolean hasMoreElements() {
return (it.hasNext());
}
public Object nextElement() {
return it.next();
}
}
//-------------------- HttpServletRequest --------------------
/**
* Returns the name of the authentication scheme used to protect the servlet, for example, "BASIC" or "SSL," or null if the servlet was not protected.
*/
public String getAuthType() {return req.getAuthType();}
/**
* Returns the portion of the request URI that indicates the context of the request.
*/
public String getContextPath() {return req.getContextPath();}
/**
* Returns an array containing all of the Cookie objects the client sent with this request.
*/
public Cookie[] getCookies() {return req.getCookies();}
/**
* Returns the value of the specified request header as a long value that represents a Date object.
*/
public long getDateHeader(String name) {return req.getDateHeader(name);}
/**
* Returns the value of the specified request header as a String.
*/
public String getHeader(String name) {return req.getHeader(name);}
/**
* Returns an enumeration of all the header names this request contains.
*/
public Enumeration getHeaderNames() {return req.getHeaderNames();}
/**
* Returns all the values of the specified request header as an Enumeration of String objects.
*/
public Enumeration getHeaders(String name) {return req.getHeaders(name);}
/**
* Returns the value of the specified request header as an int.
*/
public int getIntHeader(String name) {return req.getIntHeader(name);}
/**
* Returns the name of the HTTP method with which this request was made, for example, GET, POST, or PUT.
*/
public String getMethod() {return req.getMethod();}
/**
* Returns any extra path information associated with the URL the client sent when it made this request.
*/
public String getPathInfo() {return req.getPathInfo();}
/**
* Returns any extra path information after the servlet name but before the query string, and translates it to a real path.
*/
public String getPathTranslated() {return req.getPathTranslated();}
/**
* Returns the query string that is contained in the request URL after the path.
*/
public String getQueryString() {return req.getQueryString();}
/**
* Returns the login of the user making this request, if the user has been authenticated, or null if the user has not been authenticated.
*/
public String getRemoteUser() {return req.getRemoteUser();}
/**
* Returns the session ID specified by the client.
*/
public String getRequestedSessionId() {return req.getRequestedSessionId();}
/**
* Returns the part of this request's URL from the protocol name up to the query string in the first line of the HTTP request.
*/
public String getRequestURI() {return req.getRequestURI();}
//csc_013102.1_start - added to comply with Servlet 2.3 spec
/**
* Reconstructs the URL the client used to make the request. The returned URL contains a protocol, server name, port number, and server path, but it does not include query string parameters.
*/
public StringBuffer getRequestURL() {return req.getRequestURL();}
//csc_013102.1_end - added to comply with Servlet 2.3 spec
/**
* Returns the part of this request's URL that calls the servlet.
*/
public String getServletPath() {return req.getServletPath();}
/**
* Returns the current session associated with this request, or if the request does not have a session, creates one.
*/
public HttpSession getSession() {return req.getSession();}
/**
* Returns the current HttpSession associated with this request or, if if there is no current session and create is true, returns a new session.
*/
public HttpSession getSession(boolean create) {return req.getSession(create);}
/**
* Returns a java.security.Principal object containing the name of the current authenticated user.
*/
public Principal getUserPrincipal() {return req.getUserPrincipal();}
/**
* Checks whether the requested session ID came in as a cookie.
*/
public boolean isRequestedSessionIdFromCookie() {return req.isRequestedSessionIdFromCookie();}
/**
* Deprecated. As of Version 2.1 of the Java Servlet API, use isRequestedSessionIdFromURL() {return req.();}instead.
*/
public boolean isRequestedSessionIdFromUrl() {return req.isRequestedSessionIdFromUrl();}
/**
* Checks whether the requested session ID came in as part of the request URL.
*/
public boolean isRequestedSessionIdFromURL() {return req.isRequestedSessionIdFromURL();}
/**
* Checks whether the requested session ID is still valid.
*/
public boolean isRequestedSessionIdValid() {return req.isRequestedSessionIdValid();}
/**
* Returns a boolean indicating whether the authenticated user is included in the specified logical "role".
*/
public boolean isUserInRole(String role) {return req.isUserInRole(role);}
//-------------------- ServletRequest ------------------------
/**
* Returns the value of the named attribute as an Object, or null if no attribute of the given name exists.
*/
public Object getAttribute(String name) {return req.getAttribute(name);}
/**
* Returns an Enumeration containing the names of the attributes available to this request.
*/
public Enumeration getAttributeNames() {return req.getAttributeNames();}
/**
* Returns the name of the character encoding used in the body of this request.
*/
public String getCharacterEncoding() {return req.getCharacterEncoding();}
/**
* Returns the length, in bytes, of the request body and made available by the input stream, or -1 if the length is not known.
*/
public int getContentLength() {return req.getContentLength();}
/**
* Returns the MIME type of the body of the request, or null if the type is not known.
*/
public String getContentType() {return req.getContentType();}
/**
* Retrieves the body of the request as binary data using a ServletInputStream.
*/
public ServletInputStream getInputStream() throws IOException {return req.getInputStream();}
/**
* Returns the preferred Locale that the client will accept content in, based on the Accept-Language header.
*/
public Locale getLocale() {return req.getLocale();}
/**
* Returns an Enumeration of Locale objects indicating, in decreasing order starting with the preferred locale, the locales that are acceptable to the client based on the Accept-Language header.
*/
public Enumeration getLocales() {return req.getLocales();}
/**
* Returns the value of a request parameter as a String, or
* null if the parameter does not exist.
*
* @param name the key name for the parameter
* @return the parameter value associated with a key name
*/
public String getParameter(String name) {
//eliminate the obvious
if (name==null) return null;
//if paramList exists, get the value from there
if (paramList!=null) {
Iterator it = paramList.iterator();
while (it.hasNext()) {
Param param = (Param) it.next();
if (param.getKey().equals(name)) return param.getValue();
}
return null;
//otherwise just delegate to the underlying request
} else {
//merg_092901.1_start
//This patch submitted by Merg [[email protected]]. The basic problem
//is that some servlet containers do not accurately report all
//parameters submitted with the request. If the request is a post,
//and there were additional parameters submitted in the url, the
//additional params are sometimes not returned (ie. on ATG Dynamo).
//SO...if the value comes back null, then actually check the
//query string for them.
// return req.getParameter(name);
String s = req.getParameter(name);
if (s==null) {
// Still no parameter found, check the queryString
String queryString = req.getQueryString();
if (queryString!=null) {
//csc_110102.1_start - fix deprecation issues
//csc_110402.1 - revert
queryString = java.net.URLDecoder.decode(queryString);
/*
try {
queryString = java.net.URLDecoder.decode(queryString, "UTF-8");
} catch (UnsupportedEncodingException e) {
System.out.println("Encoding Exception: "+e);
e.printStackTrace();
}
*/
//csc_110102.1_end
// int startPos = queryString.indexOf(name + "="); //need "=" to know it is a parameter name as opposed to a value
// int endPos = -1;
//patch
//need "=" to know it is a parameter name as opposed to a value
// also need '&' to make difference between id and aaa_id for example
// if is first param, will be checked in different place
int startPos = queryString.indexOf("&"+name + "=");
int endPos = -1;
if(startPos == -1)
{
// not found inside parameter list, try find as first
if(queryString.startsWith(name+"="))
{
startPos = 0;
}
}
else
{
// skip '&'
startPos++;
}
//end patch
if (startPos!=-1) {
startPos = startPos + name.length() + 1;
endPos = queryString.indexOf("&", startPos);
if (endPos==-1) {
s = queryString.substring(startPos);
}
else {
s = queryString.substring(startPos, endPos);
}
}
}
}
return s;
//merg_092901.1_end
}
}
/**
* Returns an Enumeration of String objects containing the
* names of the parameters contained in this request.
*
* @return an Enumeration of all the parameter names
*/
public Enumeration getParameterNames() {
//if paramList is not null, get the enum from there
if (paramList!=null) {
return new LocalEnumerator(paramList);
//otherwise just delegate to the underlying request
} else {
return req.getParameterNames();
}
}
/**
* Returns an array of String objects containing all of the
* values the given request parameter has, or null if the
* parameter does not exist.
*
* @param name the key name for the parameter
* @return an array of Strings for the given key name
*/
public String[] getParameterValues(String name) {
//eliminate the obvious
if (name==null) return null;
//if paramList is not null, build the array from there
if (paramList!=null) {
List valueList = new ArrayList(paramList.size());
Iterator it = paramList.iterator();
while (it.hasNext()) {
Param param = (Param) it.next();
if (param.getKey().equals(name)) valueList.add(param.getValue());
}
int idx = -1;
String[] valueArr = new String[valueList.size()];
it = valueList.iterator();
while (it.hasNext()) {
valueArr[++idx] = (String) it.next();
}
if (valueArr.length==0) return null;
else return valueArr;
//otherwise just delegate to the underlying request
} else {
return req.getParameterValues(name);
}
}
/**
* Returns the name and version of the protocol the request uses in the form protocol/majorVersion.minorVersion, for example, HTTP/1.1.
*/
public String getProtocol() {return req.getProtocol();}
/**
* Retrieves the body of the request as character data using a BufferedReader.
*/
public BufferedReader getReader() throws IOException {return req.getReader();}
/**
* Deprecated. As of Version 2.1 of the Java Servlet API, use ServletContext.getRealPath(String) {return req.();}instead.
*/
public String getRealPath(String path) {return req.getRealPath(path);}
/**
* Returns the Internet Protocol (IP) {return req.();}address of the client that sent the request.
*/
public String getRemoteAddr() {return req.getRemoteAddr();}
/**
* Returns the fully qualified name of the client that sent the request, or the IP address of the client if the name cannot be determined.
*/
public String getRemoteHost() {return req.getRemoteHost();}
/**
* Returns a RequestDispatcher object that acts as a wrapper for the resource located at the given path.
*/
public RequestDispatcher getRequestDispatcher(String path) {return req.getRequestDispatcher(path);}
/**
* Returns the name of the scheme used to make this request, for example, http, https, or ftp.
*/
public String getScheme() {return req.getScheme();}
/**
* Returns the host name of the server that received the request.
*/
public String getServerName() {return req.getServerName();}
/**
* Returns the port number on which this request was received.
*/
public int getServerPort() {return req.getServerPort();}
/**
* Returns a boolean indicating whether this request was made using a secure channel, such as HTTPS.
*/
public boolean isSecure() {return req.isSecure();}
/**
* Removes an attribute from this request.
*/
public void removeAttribute(String name) {req.removeAttribute(name);}
/**
* Stores an attribute in this request.
*/
public void setAttribute(String name, Object o) {req.setAttribute(name, o);}
//csc_013102.1_start - added these methods to comply with Servlet 2.3 spec
/**
* Overrides the name of the character encoding used in the body
* of this request. This method must be called prior to reading
* request parameters or reading input using getReader().
*/
public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
req.setCharacterEncoding(env);
}
/**
* Returns a java.util.Map of the parameters of this request.
* Request parameters are extra information sent with the request.
* For HTTP servlets, parameters are contained in the query string
* or posted form data.
*/
public Map getParameterMap() {
//if paramList exists, get the value from there
if (paramList!=null) {
Iterator it = paramList.iterator();
Map paramMap = new HashMap(paramList.size());
//csc_120602.1_start
/*
Ok, so the problem with this is that
a) the values should be stored as a String[], and
b) if you have multiple values for a given its not going to work as implemented
while (it.hasNext()) {
Param param = (Param) it.next();
paramMap.put(param.getKey(), param.getValue());
*/
//populate the paramMap with key/val pairs
while (it.hasNext()) {
Param param = (Param) it.next();
String key = param.getKey();
List valList = (List) paramMap.get(key);
if (valList==null) {
valList = new ArrayList(10);
paramMap.put(key, valList);
}
valList.add(param.getValue());
}
//now run back through the paramMap and convert all the
//List values into String[] (to conform with servlet spec)
it = paramMap.keySet().iterator();
while (it.hasNext()) {
Object key = it.next();
List valList = (List) paramMap.get(key);
paramMap.put(key, (String[]) valList.toArray());
}
//csc_120602.1_end
return paramMap;
//otherwise just delegate to the underlying request
} else {
return req.getParameterMap();
}
}
//csc_013102.1_end
/**
* Get the underlying servlet request. The only reason you
* should ever have to do this is if you are trying to forward
* a request. Some containers check to make sure that the
* request object being forwarded is an instance of their own
* implementation...
*
* @return the underlying servlet request object
*/
public HttpServletRequest getCoreRequest() {
return req;
}
}