/*
* @see javax.servlet.RequestDispatcher#forward(javax.servlet.ServletRequest, javax.servlet.ServletResponse)
*/
protected void forward(ServletRequest request, ServletResponse response, int dispatch) throws ServletException, IOException
{
Request base_request=(request instanceof Request)?((Request)request):HttpConnection.getCurrentConnection().getRequest();
response.resetBuffer();
request.removeAttribute(__JSP_FILE); // TODO remove when glassfish 1044 is fixed
String old_uri=base_request.getRequestURI();
String old_context_path=base_request.getContextPath();
String old_servlet_path=base_request.getServletPath();
String old_path_info=base_request.getPathInfo();
String old_query=base_request.getQueryString();
Attributes old_attr=base_request.getAttributes();
MultiMap old_params=base_request.getParameters();
try
{
if (_named!=null)
_contextHandler.handle(_named, (HttpServletRequest)request, (HttpServletResponse)response, dispatch);
else
{
String query=_dQuery;
if (query!=null)
{
MultiMap parameters=new MultiMap();
UrlEncoded.decodeTo(query,parameters,request.getCharacterEncoding());
if (old_params!=null && old_params.size()>0)
{
// Merge parameters.
Iterator iter = old_params.entrySet().iterator();
while (iter.hasNext())
{
Map.Entry entry = (Map.Entry)iter.next();
String name=(String)entry.getKey();
Object values=entry.getValue();
for (int i=0;i<LazyList.size(values);i++)
parameters.add(name, LazyList.get(values, i));
}
}
if (old_query!=null && old_query.length()>0)
query=query+"&"+old_query;
base_request.setParameters(parameters);
base_request.setQueryString(query);
}
ForwardAttributes attr = new ForwardAttributes(old_attr);
//If we have already been forwarded previously, then keep using the established
//original value. Otherwise, this is the first forward and we need to establish the values.
//Note: the established value on the original request for pathInfo and
//for queryString is allowed to be null, but cannot be null for the other values.
if ((String)old_attr.getAttribute(__FORWARD_REQUEST_URI) != null)
{
attr._pathInfo=(String)old_attr.getAttribute(__FORWARD_PATH_INFO);
attr._query=(String)old_attr.getAttribute(__FORWARD_QUERY_STRING);
attr._requestURI=(String)old_attr.getAttribute(__FORWARD_REQUEST_URI);
attr._contextPath=(String)old_attr.getAttribute(__FORWARD_CONTEXT_PATH);
attr._servletPath=(String)old_attr.getAttribute(__FORWARD_SERVLET_PATH);
}
else
{
attr._pathInfo=old_path_info;
attr._query=old_query;
attr._requestURI=old_uri;
attr._contextPath=old_context_path;
attr._servletPath=old_servlet_path;
}
base_request.setRequestURI(_uri);
base_request.setContextPath(_contextHandler.getContextPath());
base_request.setAttributes(attr);
base_request.setQueryString(query);
_contextHandler.handle(_path, (HttpServletRequest)request, (HttpServletResponse)response, dispatch);
if (base_request.getConnection().getResponse().isWriting())
{
try {response.getWriter().close();}
catch(IllegalStateException e) { response.getOutputStream().close(); }
}
else
{
try {response.getOutputStream().close();}
catch(IllegalStateException e) { response.getWriter().close(); }
}
}
}
finally
{
base_request.setRequestURI(old_uri);
base_request.setContextPath(old_context_path);
base_request.setServletPath(old_servlet_path);
base_request.setPathInfo(old_path_info);
base_request.setAttributes(old_attr);
base_request.setParameters(old_params);
base_request.setQueryString(old_query);
}
}