You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@myfaces.apache.org by sk...@apache.org on 2007/10/05 10:34:26 UTC

svn commit: r582123 - in /myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic: ./ BasicFrameworkAdapter.java BasicFrameworkAdapterFilter.java

Author: skitching
Date: Fri Oct  5 01:34:25 2007
New Revision: 582123

URL: http://svn.apache.org/viewvc?rev=582123&view=rev
Log:
Add FrameworkAdapter for non-jsf environments. Formerly, non-jsf environments just used the jsf adapter, and
relied on the fact that it always falls back to the standard req/rsp/session stuff if the FacesContext cannot
be found. That isn't very elegant though; a separate class is nicer.

Added:
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapter.java
    myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapterFilter.java

Added: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapter.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapter.java?rev=582123&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapter.java (added)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapter.java Fri Oct  5 01:34:25 2007
@@ -0,0 +1,240 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.myfaces.orchestra.frameworkAdapter.basic;
+
+import java.io.IOException;
+
+import javax.faces.context.FacesContext;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapter;
+import org.apache.myfaces.orchestra.frameworkAdapter.FrameworkAdapterInterface;
+import org.springframework.context.ConfigurableApplicationContext;
+import org.springframework.web.context.support.WebApplicationContextUtils;
+
+/**
+ * An implementation of the FrameworkAdapter for plain servlet environments.
+ * <p>
+ * This class requires the OrchestraServletFilter to be configured to run for every
+ * JSF request.
+ */
+public class BasicFrameworkAdapter implements FrameworkAdapterInterface
+{
+	private final static String ISE_MESSAGE="No request/response data available"; // NON-NLS
+
+	private final static ThreadLocal httpServletRequest = new ThreadLocal();
+	private final static ThreadLocal httpServletResponse = new ThreadLocal();
+
+	private final Log log = LogFactory.getLog(BasicFrameworkAdapter.class);
+
+	private final ServletContext servletContext;
+
+	public BasicFrameworkAdapter(ServletContext context)
+	{
+		servletContext = context;
+	}
+
+	private HttpServletRequest getRequest()
+	{
+		return (HttpServletRequest) httpServletRequest.get();
+	}
+
+	private HttpServletResponse getResponse()
+	{
+		return (HttpServletResponse) httpServletResponse.get();
+	}
+
+	public void beginRequest(ServletRequest req, ServletResponse rsp)
+	{
+		log.debug("Beginning request");
+		if (req instanceof HttpServletRequest)
+		{
+			httpServletRequest.set(req);
+		}
+		if (rsp instanceof HttpServletResponse)
+		{
+			httpServletResponse.set(rsp);
+		}
+
+		FrameworkAdapter.setInstance(this);
+	}
+	
+	public void endRequest()
+	{
+		log.debug("Ending request");
+		FrameworkAdapter.setInstance(null);
+		httpServletRequest.set(null);
+		httpServletResponse.set(null);
+	}
+	
+	public String getInitParameter(String key)
+	{
+		return servletContext.getInitParameter(key);
+	}
+
+	public Object getRequestParameterAttribute(String key)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null)
+		{
+			return request.getParameter(key);
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	public boolean containsRequestParameterAttribute(String key)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null)
+		{
+			return request.getParameter(key) != null;
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	public Object getRequestAttribute(String key)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null)
+		{
+			return request.getAttribute(key);
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	public void setRequestAttribute(String key, Object value)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null)
+		{
+			request.setAttribute(key, value);
+			return;
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	public boolean containsRequestAttribute(String key)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null)
+		{
+			return request.getAttribute(key) != null;
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	public Object getSessionAttribute(String key)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null && request.getSession(true) != null)
+		{
+			return request.getSession(true).getAttribute(key);
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	public void setSessionAttribute(String key, Object value)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null && request.getSession(true) != null)
+		{
+			request.getSession(true).setAttribute(key, value);
+			return;
+		}
+
+	}
+
+	public boolean containsSessionAttribute(String key)
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null && request.getSession(true) != null)
+		{
+			return request.getSession(true).getAttribute(key) != null;
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	protected String getRequestContextPath()
+	{
+		HttpServletRequest request = getRequest();
+		if (request != null)
+		{
+			return request.getContextPath();
+		}
+
+		throw new IllegalStateException(ISE_MESSAGE);
+	}
+
+	// TODO: this is spring-specific. And exposed via the protected API. Neither is good.
+	private ConfigurableApplicationContext getApplicationContext()
+	{
+		// TODO: What about portlets ?
+		ServletContext servletContext = (ServletContext) FacesContext.getCurrentInstance().getExternalContext().getContext();
+
+		return (ConfigurableApplicationContext) WebApplicationContextUtils.getWebApplicationContext(servletContext);
+	}
+
+	public void redirect(String url) throws IOException
+	{
+		StringBuffer redir = new StringBuffer();
+		if (url.startsWith("/"))
+		{
+			redir.append(getRequestContextPath());
+		}
+		redir.append(url);
+		
+		HttpServletResponse rsp = getResponse();
+		String dstUrl = rsp.encodeRedirectURL(redir.toString());
+		rsp.sendRedirect(dstUrl);
+	}
+
+	public Object getBean(String name)
+	{
+		if (!getApplicationContext().containsBean(name))
+		{
+			return null;
+		}
+		return getApplicationContext().getBean(name);
+	}
+
+	/**
+	 * Perform a redirect to the specified url.
+	 * <p>
+	 * A redirect is done rather than a forward so that the remote browser has its
+	 * current url updated appropriately. Note that a redirect does cause any
+	 * request-scoped variables to be discarded.
+	 */
+	public void invokeNavigation(String navigationName) throws IOException
+	{
+		redirect(navigationName);
+	}
+}
\ No newline at end of file

Added: myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapterFilter.java
URL: http://svn.apache.org/viewvc/myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapterFilter.java?rev=582123&view=auto
==============================================================================
--- myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapterFilter.java (added)
+++ myfaces/orchestra/trunk/core/src/main/java/org/apache/myfaces/orchestra/frameworkAdapter/basic/BasicFrameworkAdapterFilter.java Fri Oct  5 01:34:25 2007
@@ -0,0 +1,73 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.myfaces.orchestra.frameworkAdapter.basic;
+
+import java.io.IOException;
+
+import javax.servlet.Filter;
+import javax.servlet.FilterChain;
+import javax.servlet.FilterConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+/**
+ * Configures the JsfFrameworkAdapter.
+ * <p> 
+ * Orchestra accesses information about the request, response, session, etc via a
+ * FrameworkAdapter so that it can be used with multiple web tier frameworks. This
+ * class selects and configures the JSF version of this adapter.
+ * <p>
+ * Note that the JsfOrchestraServletFilter class combines the functionality of this
+ * class and the OrchestraServletFilter class together for convenience (allows just
+ * one filter to be defined rather than two). Of course this is only useful if you
+ * actually want the functionality of the OrchestraServletFilter.
+ */
+public class BasicFrameworkAdapterFilter implements Filter
+{
+	private final Log log = LogFactory.getLog(BasicFrameworkAdapterFilter.class);
+	private BasicFrameworkAdapter adapter;
+
+	public void init(FilterConfig filterConfig) throws ServletException
+	{
+		 adapter = new BasicFrameworkAdapter(filterConfig.getServletContext());
+	}
+
+	public void doFilter(ServletRequest req, ServletResponse rsp, FilterChain filterChain) throws IOException, ServletException
+	{
+		log.debug("doFilter");
+		try
+		{
+			adapter.beginRequest(req, rsp);
+			filterChain.doFilter(req, rsp);
+		}
+		finally
+		{
+			adapter.endRequest();
+		}
+	}
+
+	public void destroy()
+	{
+	}
+}
\ No newline at end of file