By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,779 Members | 1,101 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,779 IT Pros & Developers. It's quick & easy.

ClassCastException on HttpServletRequest

P: n/a
Hi,

We have a customized HttpServletRequest object, when we run the
application on webshpere 4.0.7,got a ClassCastException on this
object, but was Ok on WAS4.0.1,

Anyone know why, is this something related to the was407 patch?

Here's the code:

public class InfobalHttpServletRequestWrapper
implements HttpServletRequest
{

private String userName = null;
private Principal userPrincipal = null;
private Set roleSet = new HashSet();

private Map parameterMapDecoration = new HashMap();

// ------------------------------------------------------------
Constructor
/**
* Configure a new request wrapper.
*
* @param request The request we are wrapping
* @param remoteUser The name of the user
* @param roles The roles permitted the user, delimited by commas
*/
public ASAHttpServletRequestWrapper (
HttpServletRequest request,
String remoteUser,
String roles) {
super(request);
Assert.assertNotNull ("You must wrap a real request",
request);
Assert.assertNotNull ("You must supply a remote user",
remoteUser);
Assert.assertNotNull ("The user must have roles", roles);
this.userName = remoteUser;
userPrincipal = new Principal ()
{
public boolean equals (Object o) {
if ( o.getClass().equals(this.getClass()) )
return userName.equals( ((Principal)
o).getName() );
else return false;
}
public String getName () { return userName; }
public int hashCode () { return userName.hashCode();
}
public String toString () { return userName; }
};
StringTokenizer st = new StringTokenizer(roles,",");
while ( st.hasMoreTokens() ) {
roleSet.add( st.nextToken() );
}
}

public boolean isUserInRole (String role) {
return roleSet.contains(role);
}

public Principal getUserPrincipal () {
return userPrincipal;
}

public String getRemoteUser () {
return userName;
}

/**
* 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) {
String[] params = (String[]) parameterMapDecoration.get(name);
if ( params == null ) {
return super.getParameter(name);
}
else if ( params.length == 0 ) {
return null;
}
else return params[0];
}

public void setParameter (String name, String value) {
String[] valueArray = { value };
this.setParameterValues(name, valueArray);

}

final class DecoratedEnumeration
implements Enumeration
{
private Iterator iter;
public DecoratedEnumeration (Set inNameSet, Enumeration
nameEnumeration) {
Set nameSet = new HashSet();
nameSet.addAll( inNameSet );
while ( nameEnumeration.hasMoreElements() )
nameSet.add(nameEnumeration.nextElement());
iter = nameSet.iterator();
}
public boolean hasMoreElements () {
return iter.hasNext();
}
public Object nextElement () {
return iter.next();
}
}

/**
* 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() {
return new DecoratedEnumeration(parameterMapDecoration.keySet (),
super.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) {
String[] params = (String[]) parameterMapDecoration.get(name);
if ( params == null ) {
return super.getParameterValues(name);
}
else return params;
}

public void setParameterValues (String name, String[] values) {
parameterMapDecoration.put(name, values);
}

HttpServletRequest req = 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 InfobalHttpServletRequestWrapper(HttpServletReques t ireq) {
req = ireq;
}
//-------------------- 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) { return
req.getParameter(name); }

/**
* 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() { 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) { 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() { 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
*/
protected HttpServletRequest getCoreRequest() {
return req;
}
}
Jul 17 '05 #1
Share this question for a faster answer!
Share on Google+

This discussion thread is closed

Replies have been disabled for this discussion.