/*
* This file is part of the WfMOpen project.
* Copyright (C) 2001-2006 Danet GmbH (www.danet.de), BU BTS.
* All rights reserved.
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*
* $Id: AjaxExternalContext.java 2326 2007-03-27 21:59:44Z mlipp $
*
* $Log$
* Revision 1.2 2006/12/12 09:53:20 drmlipp
* Added Ajax support.
*
* Revision 1.1.2.3 2006/11/27 14:28:33 drmlipp
* Continuing.
*
* Revision 1.1.2.2 2006/11/25 22:45:56 mlipp
* Got request through to JSF.
*
* Revision 1.1.2.1 2006/11/24 16:15:31 drmlipp
* Continuing Ajax support.
*
*/
package de.danet.an.util.jsf;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.security.Principal;
import java.util.AbstractMap;
import java.util.Collections;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Locale;
import java.util.Map;
import java.util.Set;
import javax.faces.context.ExternalContext;
import javax.portlet.PortalContext;
import javax.portlet.PortletMode;
import javax.portlet.PortletPreferences;
import javax.portlet.PortletRequest;
import javax.portlet.PortletSession;
import javax.portlet.WindowState;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.myfaces.context.ReleaseableExternalContext;
import org.apache.myfaces.context.servlet.ApplicationMap;
/**
* This class provides an ExternalContext for use with Ajax requests.
*
* @author Michael Lipp
*/
public final class AjaxExternalContext extends ExternalContext
implements ReleaseableExternalContext {
private static final org.apache.commons.logging.Log logger
= org.apache.commons.logging.LogFactory.getLog(AjaxExternalContext.class);
private HttpServletRequest request;
private HttpServletResponse response;
private String sessionPrefix = null;
private PortletRequest wrappedRequest = null;
private Map applicationMap = null;
private Map initParamMap = null;
private Map requestHeaderMap = null;
private Map requestHeaderValuesMap = null;
private Map requestMap = null;
private Map requestParameterMap = null;
private Map sessionMap = null;
/**
* Create a new instance with all attributes initialized
* to defaults or the given values.
*
* @param request
* @param response
*/
private AjaxExternalContext
(HttpServletRequest request, HttpServletResponse response,
String sessionPrefix) {
this.request = request;
this.response = response;
this.sessionPrefix = sessionPrefix;
}
public static AjaxExternalContext makeContext
(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
String namespace
= request.getParameter(MyFacesAjaxServlet.RENDER_NAMESPACE);
if (namespace == null) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST);
throw new ServletException
("Invalid request: " + request.getRequestURL());
}
HttpSession session = request.getSession();
String sessionPrefix = null;
for (Enumeration e = session.getAttributeNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
if (key.endsWith("?" + MyFacesAjaxServlet.RENDER_NAMESPACE)
&& ((String)session.getAttribute(key)).equals(namespace)) {
sessionPrefix = key.substring
(0, key.indexOf(MyFacesAjaxServlet.RENDER_NAMESPACE));
if (logger.isDebugEnabled()) {
logger.debug("Using session prefix " + sessionPrefix);
}
break;
}
}
if (sessionPrefix == null) {
response.sendError(HttpServletResponse.SC_NOT_FOUND);
throw new ServletException
("No portlet session with namespace " + namespace);
}
return new AjaxExternalContext(request, response, sessionPrefix);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#dispatch(java.lang.String)
*/
public void dispatch(String path) throws IOException {
RequestDispatcher disp = request
.getSession().getServletContext().getRequestDispatcher(path);
if (disp == null) {
throw new IllegalArgumentException ("Not found: " + path);
}
try {
disp.forward(request, response);
} catch (ServletException e) {
throw (IOException)
(new IOException(e.getMessage())).initCause(e);
}
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#encodeActionURL(java.lang.String)
*/
public String encodeActionURL(String url) {
return response.encodeURL(url);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#encodeNamespace(java.lang.String)
*/
public String encodeNamespace(String name) {
String prefix = (String)
getSessionMap().get(MyFacesAdaptedPortlet.NAMESPACE_PREFIX);
return prefix + name;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#encodeResourceURL(java.lang.String)
*/
public String encodeResourceURL(String url) {
return response.encodeURL(url);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getApplicationMap()
*/
public Map getApplicationMap() {
if (applicationMap == null) {
applicationMap = new ApplicationMap();
}
return applicationMap;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getAuthType()
*/
public String getAuthType() {
return request.getAuthType();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getContext()
*/
public Object getContext() {
return request.getSession().getServletContext();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getInitParameter
*/
public String getInitParameter(String param) {
return request.getSession().getServletContext().getInitParameter(param);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getInitParameterMap()
*/
public Map getInitParameterMap() {
if (initParamMap == null) {
initParamMap = new InitParametersMap ();
}
return initParamMap;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRemoteUser()
*/
public String getRemoteUser() {
return request.getRemoteUser();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequest()
*/
public Object getRequest() {
if (wrappedRequest == null) {
wrappedRequest = new RequestWrapper ();
}
return wrappedRequest;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestContextPath()
*/
public String getRequestContextPath() {
return request.getContextPath();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestCookieMap()
*/
public Map getRequestCookieMap() {
return Collections.EMPTY_MAP;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestHeaderMap()
*/
public Map getRequestHeaderMap() {
if (requestHeaderMap == null) {
requestHeaderMap = new RequestHeaderMap ();
}
return requestHeaderMap;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestHeaderValuesMap()
*/
public Map getRequestHeaderValuesMap() {
if (requestHeaderValuesMap == null) {
requestHeaderValuesMap = new RequestHeaderValuesMap ();
}
return requestHeaderValuesMap;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestLocale()
*/
public Locale getRequestLocale() {
return request.getLocale();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestLocales()
*/
public Iterator getRequestLocales() {
return Collections.list(request.getLocales()).iterator();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestMap()
*/
public Map getRequestMap() {
if (requestMap == null) {
requestMap = new RequestMap ();
}
return requestMap;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestParameterMap()
*/
public Map getRequestParameterMap() {
if (requestParameterMap == null) {
requestParameterMap = new RequestParameterMap();
}
return requestParameterMap;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestParameterNames()
*/
public Iterator getRequestParameterNames() {
return Collections.list(request.getParameterNames()).iterator();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestParameterValuesMap()
*/
public Map getRequestParameterValuesMap() {
return request.getParameterMap();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestPathInfo()
*/
public String getRequestPathInfo() {
return null;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getRequestServletPath()
*/
public String getRequestServletPath() {
return null;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getResource(java.lang.String)
*/
public URL getResource(String path) throws MalformedURLException {
return request.getSession().getServletContext().getResource(path);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getResourceAsStream(java.lang.String)
*/
public InputStream getResourceAsStream(String path) {
return request.getSession()
.getServletContext().getResourceAsStream(path);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getResourcePaths(java.lang.String)
*/
public Set getResourcePaths(String path) {
return request.getSession().getServletContext().getResourcePaths(path);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getResponse()
*/
public Object getResponse() {
return response;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getSession(boolean)
*/
public Object getSession(boolean arg0) {
return request.getSession();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getSessionMap()
*/
public Map getSessionMap() {
if (sessionMap == null) {
sessionMap = new SessionMap ();
}
return sessionMap;
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#getUserPrincipal()
*/
public Principal getUserPrincipal() {
return request.getUserPrincipal();
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#isUserInRole(java.lang.String)
*/
public boolean isUserInRole(String role) {
return request.isUserInRole(role);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#log(java.lang.String)
*/
public void log(String arg0) {
request.getSession().getServletContext().log (arg0);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#log(java.lang.String, java.lang.Throwable)
*/
public void log(String arg0, Throwable arg1) {
request.getSession().getServletContext().log (arg0, arg1);
}
/* (non-Javadoc)
* @see javax.faces.context.ExternalContext#redirect(java.lang.String)
*/
public void redirect(String arg0) throws IOException {
response.sendRedirect(arg0);
}
public class ApplicationMap extends AbstractMap {
private Map backupCache = null;
private Map backup() {
if (backupCache == null) {
backupCache = new HashMap ();
ServletContext context
= request.getSession().getServletContext();
for (Enumeration e = context.getAttributeNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
backupCache.put(key, context.getAttribute(key));
}
}
return backupCache;
}
/* (non-Javadoc)
* @see java.util.AbstractMap#entrySet()
*/
public Set entrySet() {
return backup().entrySet();
}
/* (non-Javadoc)
* @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
*/
public Object put(Object key, Object value) {
ServletContext context
= request.getSession().getServletContext();
context.setAttribute((String)key, value);
return backup().put(key, value);
}
/* (non-Javadoc)
* @see java.util.AbstractMap#remove(java.lang.Object)
*/
public Object remove(Object key) {
ServletContext context
= request.getSession().getServletContext();
context.removeAttribute((String)key);
return backup().remove(key);
}
}
public class InitParametersMap extends AbstractMap {
private Map backup = null;
/* (non-Javadoc)
* @see java.util.AbstractMap#entrySet()
*/
public Set entrySet() {
if (backup == null) {
backup = new HashMap ();
ServletContext context
= request.getSession().getServletContext();
for (Enumeration e = context.getInitParameterNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
backup.put(key, context.getInitParameter(key));
}
}
return backup.entrySet();
}
}
public class RequestHeaderMap extends AbstractMap {
private Map backup = null;
/* (non-Javadoc)
* @see java.util.AbstractMap#entrySet()
*/
public Set entrySet() {
if (backup == null) {
backup = new HashMap ();
for (Enumeration e = request.getHeaderNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
backup.put(key, request.getHeader(key));
}
}
return backup.entrySet();
}
}
public class RequestHeaderValuesMap extends AbstractMap {
private Map backup = null;
/* (non-Javadoc)
* @see java.util.AbstractMap#entrySet()
*/
public Set entrySet() {
if (backup == null) {
backup = new HashMap ();
for (Enumeration e = request.getHeaderNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
backup.put
(key, Collections.list
(request.getHeaders(key)).toArray(new String[0]));
}
}
return backup.entrySet();
}
}
public class RequestMap extends AbstractMap {
private Map backupCache = null;
private Map backup () {
if (backupCache == null) {
backupCache = new HashMap ();
for (Enumeration e = request.getAttributeNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
backupCache.put(key, request.getAttribute(key));
}
}
return backupCache;
}
/* (non-Javadoc)
* @see java.util.AbstractMap#entrySet()
*/
public Set entrySet() {
return backup().entrySet();
}
/* (non-Javadoc)
* @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
*/
public Object put(Object key, Object value) {
request.setAttribute((String)key, value);
return backup().put(key, value);
}
/* (non-Javadoc)
* @see java.util.AbstractMap#remove(java.lang.Object)
*/
public Object remove(Object key) {
request.removeAttribute((String)key);
return backup().remove(key);
}
}
public class RequestParameterMap extends AbstractMap {
private Map backup = null;
/* (non-Javadoc)
* @see java.util.AbstractMap#entrySet()
*/
public Set entrySet() {
if (backup == null) {
backup = new HashMap ();
for (Enumeration e = request.getParameterNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
backup.put(key, request.getParameter(key));
}
}
return backup.entrySet();
}
}
public class SessionMap extends AbstractMap {
private Map backupCache = null;
private Map backup() {
if (backupCache == null) {
backupCache = new HashMap ();
HttpSession session = request.getSession();
for (Enumeration e = session.getAttributeNames();
e.hasMoreElements();) {
String key = (String)e.nextElement();
if (key.startsWith(sessionPrefix)) {
backupCache.put(key.substring(sessionPrefix.length()),
session.getAttribute(key));
}
}
}
return backupCache;
}
/* (non-Javadoc)
* @see java.util.AbstractMap#entrySet()
*/
public Set entrySet() {
return backup().entrySet();
}
/* (non-Javadoc)
* @see java.util.AbstractMap#put(java.lang.Object, java.lang.Object)
*/
public Object put(Object key, Object value) {
HttpSession session = request.getSession();
session.setAttribute(sessionPrefix + (String)key, value);
return backup().put(key, value);
}
/* (non-Javadoc)
* @see java.util.AbstractMap#remove(java.lang.Object)
*/
public Object remove(Object key) {
HttpSession session = request.getSession();
session.removeAttribute(sessionPrefix + (String)key);
return backup().remove(key);
}
}
/**
* Make the servlet request look like a portlet request as much as
* possible.
*
* @author Michael Lipp
*/
public class RequestWrapper implements PortletRequest {
public Object getAttribute(String arg0) {
return request.getAttribute(arg0);
}
public Enumeration getAttributeNames() {
return request.getAttributeNames();
}
public String getAuthType() {
return request.getAuthType();
}
public String getContextPath() {
return request.getContextPath();
}
public Locale getLocale() {
return request.getLocale();
}
public Enumeration getLocales() {
return request.getLocales();
}
public String getParameter(String arg0) {
return request.getParameter(arg0);
}
public Map getParameterMap() {
return request.getParameterMap();
}
public Enumeration getParameterNames() {
return request.getParameterNames();
}
public String[] getParameterValues(String arg0) {
return request.getParameterValues(arg0);
}
public PortalContext getPortalContext() {
throw new UnsupportedOperationException();
}
public PortletMode getPortletMode() {
throw new UnsupportedOperationException();
}
public PortletSession getPortletSession() {
throw new UnsupportedOperationException();
}
public PortletSession getPortletSession(boolean arg0) {
throw new UnsupportedOperationException();
}
public PortletPreferences getPreferences() {
throw new UnsupportedOperationException();
}
public Enumeration getProperties(String arg0) {
throw new UnsupportedOperationException();
}
public String getProperty(String arg0) {
throw new UnsupportedOperationException();
}
public Enumeration getPropertyNames() {
throw new UnsupportedOperationException();
}
public String getRemoteUser() {
return request.getRemoteUser();
}
public String getRequestedSessionId() {
return request.getRequestedSessionId();
}
public String getResponseContentType() {
throw new UnsupportedOperationException();
}
public Enumeration getResponseContentTypes() {
throw new UnsupportedOperationException();
}
public String getScheme() {
return request.getScheme();
}
public String getServerName() {
return request.getServerName();
}
public int getServerPort() {
return request.getServerPort();
}
public Principal getUserPrincipal() {
return request.getUserPrincipal();
}
public WindowState getWindowState() {
throw new UnsupportedOperationException();
}
public boolean isPortletModeAllowed(PortletMode arg0) {
throw new UnsupportedOperationException();
}
public boolean isRequestedSessionIdValid() {
return request.isRequestedSessionIdValid();
}
public boolean isSecure() {
return request.isSecure();
}
public boolean isUserInRole(String arg0) {
return request.isUserInRole(arg0);
}
public boolean isWindowStateAllowed(WindowState arg0) {
throw new UnsupportedOperationException();
}
public void removeAttribute(String arg0) {
request.removeAttribute(arg0);
}
public void setAttribute(String arg0, Object arg1) {
request.setAttribute(arg0, arg1);
}
}
/* (non-Javadoc)
* @see org.apache.myfaces.context.ReleaseableExternalContext#release()
*/
public void release() {
}
}