struts-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From jo...@apache.org
Subject svn commit: r1139061 [3/5] - in /struts/archive/plugins: ./ src/ src/main/ src/main/java/ src/main/java/org/ src/main/java/org/apache/ src/main/java/org/apache/struts2/ src/main/java/org/apache/struts2/components/ src/main/java/org/apache/struts2/portl...
Date Thu, 23 Jun 2011 20:18:46 GMT
Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletHttpSession.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletHttpSession.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletHttpSession.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletHttpSession.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,215 @@
+/*
+ * $Id: PortletHttpSession.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.util.ArrayList;
+import java.util.Enumeration;
+import java.util.List;
+
+import javax.portlet.PortletSession;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpSession;
+import javax.servlet.http.HttpSessionContext;
+
+/**
+ * Wrapper object exposing a {@link PortletSession} as a {@link HttpSession} instance.
+ * Clients accessing this session object will in fact operate on the
+ * {@link PortletSession} object wrapped by this session object.
+ */
+public class PortletHttpSession implements HttpSession {
+
+	private PortletSession portletSession;
+
+	public PortletHttpSession(PortletSession portletSession) {
+		this.portletSession = portletSession;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getAttribute(java.lang.String)
+	 */
+	public Object getAttribute(String name) {
+		return portletSession.getAttribute(name);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getAttributeNames()
+	 */
+	public Enumeration getAttributeNames() {
+		return portletSession.getAttributeNames();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getCreationTime()
+	 */
+	public long getCreationTime() {
+		return portletSession.getCreationTime();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getId()
+	 */
+	public String getId() {
+		return portletSession.getId();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getLastAccessedTime()
+	 */
+	public long getLastAccessedTime() {
+		return portletSession.getLastAccessedTime();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getMaxInactiveInterval()
+	 */
+	public int getMaxInactiveInterval() {
+		return portletSession.getMaxInactiveInterval();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getServletContext()
+	 */
+	public ServletContext getServletContext() {
+		return new PortletServletContext(portletSession.getPortletContext());
+	}
+
+	/**
+	 * @see javax.servlet.http.HttpSession#getSessionContext()
+	 * @throws IllegalStateException
+	 *             Not supported in a portlet.
+	 */
+	public HttpSessionContext getSessionContext() {
+		throw new IllegalStateException("Not supported in a portlet");
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getValue(java.lang.String)
+	 */
+	public Object getValue(String name) {
+		return getAttribute(name);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#getValueNames()
+	 */
+	public String[] getValueNames() {
+		List<String> names = new ArrayList<String>();
+		Enumeration attrNames = getAttributeNames();
+		while (attrNames.hasMoreElements()) {
+			names.add((String) attrNames.nextElement());
+		}
+		return names.toArray(new String[0]);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#invalidate()
+	 */
+	public void invalidate() {
+		portletSession.invalidate();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#isNew()
+	 */
+	public boolean isNew() {
+		return portletSession.isNew();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#putValue(java.lang.String,
+	 *      java.lang.Object)
+	 */
+	public void putValue(String name, Object value) {
+		setAttribute(name, value);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#removeAttribute(java.lang.String)
+	 */
+	public void removeAttribute(String name) {
+		portletSession.removeAttribute(name);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#removeValue(java.lang.String)
+	 */
+	public void removeValue(String name) {
+		removeAttribute(name);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#setAttribute(java.lang.String,
+	 *      java.lang.Object)
+	 */
+	public void setAttribute(String name, Object value) {
+		portletSession.setAttribute(name, value);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpSession#setMaxInactiveInterval(int)
+	 */
+	public void setMaxInactiveInterval(int interval) {
+		portletSession.setMaxInactiveInterval(interval);
+	}
+
+	/**
+	 * Get the wrapped portlet session.
+	 * 
+	 * @return The wrapped portlet session.
+	 */
+	public PortletSession getPortletSession() {
+		return portletSession;
+	}
+
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletConfig.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletConfig.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletConfig.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletConfig.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,82 @@
+/*
+ * $Id: PortletServletConfig.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.util.Enumeration;
+
+import javax.portlet.PortletConfig;
+import javax.portlet.PortletContext;
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletContext;
+
+/**
+ * Wrapper object exposing a {@link PortletConfig} as a {@link ServletConfig} instance.
+ * Clients accessing this config object will in fact operate on the
+ * {@link PortletConfig} object wrapped by this config object.
+ */
+public class PortletServletConfig implements ServletConfig {
+
+	private PortletConfig portletConfig;
+	
+	public PortletServletConfig(PortletConfig portletConfig) {
+		this.portletConfig = portletConfig;
+	}
+	
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletConfig#getInitParameter(java.lang.String)
+	 */
+	public String getInitParameter(String name) {
+		return portletConfig.getInitParameter(name);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletConfig#getInitParameterNames()
+	 */
+	public Enumeration getInitParameterNames() {
+		return portletConfig.getInitParameterNames();
+	}
+
+	/**
+	 * Get the {@link PortletContext} as a {@link PortletServletContext} instance.
+	 * @see javax.servlet.ServletConfig#getServletContext()
+	 */
+	public ServletContext getServletContext() {
+		return new PortletServletContext(portletConfig.getPortletContext());
+	}
+
+	/**
+	 * Will return the portlet name.
+	 * @see javax.servlet.ServletConfig#getServletName()
+	 */
+	public String getServletName() {
+		return portletConfig.getPortletName();
+	}
+	
+	/**
+	 * Get the wrapped {@link PortletConfig} instance.
+	 * @return The wrapped {@link PortletConfig} instance.
+	 */
+	public PortletConfig getPortletConfig() {
+		return portletConfig;
+	}
+
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletContext.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletContext.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletContext.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletContext.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,236 @@
+/*
+ * $Id: PortletServletContext.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.Set;
+
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequestDispatcher;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.Servlet;
+import javax.servlet.ServletContext;
+import javax.servlet.ServletException;
+
+/**
+ * Wrapper object exposing a {@link PortletContext} as a {@link ServletContext} instance.
+ * Clients accessing this context object will in fact operate on the
+ * {@link PortletContext} object wrapped by this context object.
+ */
+public class PortletServletContext implements ServletContext {
+
+	private PortletContext portletContext;
+	
+	public PortletServletContext(PortletContext portletContext) {
+		this.portletContext = portletContext;
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getAttribute(java.lang.String)
+	 */
+	public Object getAttribute(String name) {
+		return portletContext.getAttribute(name);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getAttributeNames()
+	 */
+	public Enumeration getAttributeNames() {
+		return portletContext.getAttributeNames();
+	}
+
+	/**
+	 * @see javax.servlet.ServletContext#getContext(java.lang.String)
+	 * @throws IllegalStateException Not supported in a portlet.
+	 */
+	public ServletContext getContext(String uripath) {
+		throw new IllegalStateException("Not supported in a portlet");
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getInitParameter(java.lang.String)
+	 */
+	public String getInitParameter(String name) {
+		return portletContext.getInitParameter(name);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getInitParameterNames()
+	 */
+	public Enumeration getInitParameterNames() {
+		return portletContext.getInitParameterNames();
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getMajorVersion()
+	 */
+	public int getMajorVersion() {
+		return portletContext.getMajorVersion();
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getMimeType(java.lang.String)
+	 */
+	public String getMimeType(String file) {
+		return portletContext.getMimeType(file);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getMinorVersion()
+	 */
+	public int getMinorVersion() {
+		return portletContext.getMinorVersion();
+	}
+
+	/**
+	 * Returns a {@link PortletServletRequestDispatcher} wrapping the {@link PortletRequestDispatcher}
+	 * as a {@link RequestDispatcher} instance.
+	 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
+	 * @return PortletServletRequestDispatcher
+	 */
+	public RequestDispatcher getNamedDispatcher(String name) {
+		return new PortletServletRequestDispatcher(portletContext.getNamedDispatcher(name));
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getRealPath(java.lang.String)
+	 */
+	public String getRealPath(String path) {
+		return portletContext.getRealPath(path);
+	}
+
+	/**
+	 * Returns a {@link PortletServletRequestDispatcher} wrapping the {@link PortletRequestDispatcher}
+	 * as a {@link RequestDispatcher} instance.
+	 * @see javax.servlet.ServletContext#getNamedDispatcher(java.lang.String)
+	 * @return PortletServletRequestDispatcher
+	 */
+	public RequestDispatcher getRequestDispatcher(String path) {
+		return new PortletServletRequestDispatcher(portletContext.getRequestDispatcher(path));
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getResource(java.lang.String)
+	 */
+	public URL getResource(String path) throws MalformedURLException {
+		return portletContext.getResource(path);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getResourceAsStream(java.lang.String)
+	 */
+	public InputStream getResourceAsStream(String path) {
+		return portletContext.getResourceAsStream(path);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getResourcePaths(java.lang.String)
+	 */
+	public Set getResourcePaths(String path) {
+		return portletContext.getResourcePaths(path);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getServerInfo()
+	 */
+	public String getServerInfo() {
+		return portletContext.getServerInfo();
+	}
+
+	/**
+	 * @see javax.servlet.ServletContext#getServlet(java.lang.String)
+	 * @throws IllegalStateException Not supported in a portlet.
+	 */
+	public Servlet getServlet(String name) throws ServletException {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#getServletContextName()
+	 */
+	public String getServletContextName() {
+		return portletContext.getPortletContextName();
+	}
+
+	/**
+	 * @see javax.servlet.ServletContext#getServletNames()
+ 	 * @throws IllegalStateException Not supported in a portlet.
+	 */
+	public Enumeration getServletNames() {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * @see javax.servlet.ServletContext#getServlets()
+	 * @throws IllegalStateException Not supported in a portlet.
+	 */
+	public Enumeration getServlets() {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#log(java.lang.String)
+	 */
+	public void log(String msg) {
+		portletContext.log(msg);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#log(java.lang.Exception, java.lang.String)
+	 */
+	public void log(Exception exception, String msg) {
+		log(msg, exception);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#log(java.lang.String, java.lang.Throwable)
+	 */
+	public void log(String message, Throwable throwable) {
+		portletContext.log(message, throwable);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#removeAttribute(java.lang.String)
+	 */
+	public void removeAttribute(String name) {
+		portletContext.removeAttribute(name);
+	}
+
+	/* (non-Javadoc)
+	 * @see javax.servlet.ServletContext#setAttribute(java.lang.String, java.lang.Object)
+	 */
+	public void setAttribute(String name, Object object) {
+		portletContext.setAttribute(name, object);
+	}
+	
+	/**
+	 * Get the wrapped {@link PortletContext} instance.
+	 * @return The wrapped {@link PortletContext} instance.
+	 */
+	public PortletContext getPortletContext() {
+		return portletContext;
+	}
+
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletInputStream.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletInputStream.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletInputStream.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletInputStream.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,122 @@
+/*
+ * $Id: PortletServletInputStream.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.io.IOException;
+import java.io.InputStream;
+
+import javax.servlet.ServletInputStream;
+
+/**
+ * Wrapper object exposing a {@link InputStream} from a portlet as a {@link ServletInputStream} instance.
+ * Clients accessing this stream object will in fact operate on the
+ * {@link InputStream} object wrapped by this stream object.
+ */
+public class PortletServletInputStream extends ServletInputStream {
+
+	private InputStream portletInputStream;
+	
+	public PortletServletInputStream(InputStream portletInputStream) {
+		this.portletInputStream = portletInputStream;
+	}
+	
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#read()
+	 */
+	@Override
+	public int read() throws IOException {
+		return portletInputStream.read();
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#available()
+	 */
+	@Override
+	public int available() throws IOException {
+		return portletInputStream.available();
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#close()
+	 */
+	@Override
+	public void close() throws IOException {
+		portletInputStream.close();
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#mark(int)
+	 */
+	@Override
+	public synchronized void mark(int readlimit) {
+		portletInputStream.mark(readlimit);
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#markSupported()
+	 */
+	@Override
+	public boolean markSupported() {
+		return portletInputStream.markSupported();
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#read(byte[], int, int)
+	 */
+	@Override
+	public int read(byte[] b, int off, int len) throws IOException {
+		return portletInputStream.read(b, off, len);
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#read(byte[])
+	 */
+	@Override
+	public int read(byte[] b) throws IOException {
+		return portletInputStream.read(b);
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#reset()
+	 */
+	@Override
+	public synchronized void reset() throws IOException {
+		portletInputStream.reset();
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.InputStream#skip(long)
+	 */
+	@Override
+	public long skip(long n) throws IOException {
+		return portletInputStream.skip(n);
+	}
+	
+	/**
+	 * Get the wrapped {@link InputStream} instance.
+	 * @return The wrapped {@link InputStream} instance.
+	 */
+	public InputStream getInputStream() {
+		return portletInputStream;
+	}
+
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletOutputStream.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletOutputStream.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletOutputStream.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletOutputStream.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,89 @@
+/*
+ * $Id: PortletServletOutputStream.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.io.IOException;
+import java.io.OutputStream;
+
+import javax.servlet.ServletOutputStream;
+
+/**
+ * Wrapper object exposing a {@link OutputStream} from a portlet as a {@link ServletOutputStream} instance.
+ * Clients accessing this stream object will in fact operate on the
+ * {@link OutputStream} object wrapped by this stream object.
+ */
+public class PortletServletOutputStream extends ServletOutputStream {
+
+	private OutputStream portletOutputStream;
+	
+	public PortletServletOutputStream(OutputStream portletOutputStream) {
+		this.portletOutputStream = portletOutputStream;
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.OutputStream#write(int)
+	 */
+	@Override
+	public void write(int ch) throws IOException {
+		portletOutputStream.write(ch);
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.OutputStream#close()
+	 */
+	@Override
+	public void close() throws IOException {
+		portletOutputStream.close();
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.OutputStream#flush()
+	 */
+	@Override
+	public void flush() throws IOException {
+		portletOutputStream.flush();
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.OutputStream#write(byte[])
+	 */
+	@Override
+	public void write(byte[] b) throws IOException {
+		portletOutputStream.write(b);
+	}
+
+	/* (non-Javadoc)
+	 * @see java.io.OutputStream#write(byte[], int, int)
+	 */
+	@Override
+	public void write(byte[] b, int off, int len) throws IOException {
+		portletOutputStream.write(b, off, len);
+	}
+	
+	/**
+	 * Get the wrapped {@link OutputStream} instance.
+	 * @return The wrapped {@link OutputStream} instance.
+	 */
+	public OutputStream getOutputStream() {
+		return portletOutputStream;
+	}
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequest.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,679 @@
+/*
+ * $Id: PortletServletRequest.java 671156 2008-06-24 12:33:59Z nilsga $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.UnsupportedEncodingException;
+import java.security.Principal;
+import java.util.Enumeration;
+import java.util.Locale;
+import java.util.Map;
+
+import javax.portlet.ActionRequest;
+import javax.portlet.PortletContext;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletRequestDispatcher;
+import javax.portlet.PortletSession;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletInputStream;
+import javax.servlet.ServletRequest;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpSession;
+
+import org.apache.struts2.StrutsConstants;
+import org.apache.struts2.dispatcher.mapper.ActionMapping;
+import org.apache.struts2.portlet.PortletActionConstants;
+import org.apache.struts2.portlet.context.PortletActionContext;
+
+import com.opensymphony.xwork2.inject.Inject;
+
+/**
+ * Wrapper object exposing a {@link PortletRequest} as a
+ * {@link HttpServletRequest} instance. Clients accessing this request object
+ * will in fact operate on the {@link PortletRequest} object wrapped by this
+ * request object.
+ */
+public class PortletServletRequest implements HttpServletRequest, PortletActionConstants {
+
+	private PortletRequest portletRequest;
+
+	private PortletContext portletContext;
+
+	private String extension;
+
+	public PortletServletRequest(PortletRequest portletRequest, PortletContext portletContext) {
+		this.portletRequest = portletRequest;
+		this.portletContext = portletContext;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getAuthType()
+	 */
+	public String getAuthType() {
+		return portletRequest.getAuthType();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getContextPath()
+	 */
+	public String getContextPath() {
+		return portletRequest.getContextPath();
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public Cookie[] getCookies() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getCookies();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public long getDateHeader(String name) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Gets a property from the {@link PortletRequest}. Note that a
+	 * {@link PortletRequest} is not guaranteed to map properties to headers.
+	 * 
+	 * @see PortletRequest#getProperty(String)
+	 * @see javax.servlet.http.HttpServletRequest#getHeader(java.lang.String)
+	 */
+	public String getHeader(String name) {
+		return portletRequest.getProperty(name);
+	}
+
+	/**
+	 * Gets the property names from the {@link PortletRequest}. Note that a
+	 * {@link PortletRequest} is not guaranteed to map properties to headers.
+	 * 
+	 * @see PortletRequest#getPropertyNames()
+	 * @see javax.servlet.http.HttpServletRequest#getHeaderNames()
+	 */
+	public Enumeration getHeaderNames() {
+		return portletRequest.getPropertyNames();
+	}
+
+	/**
+	 * Gets the values for the specified property from the
+	 * {@link PortletRequest}. Note that a {@link PortletRequest} is not
+	 * guaranteed to map properties to headers.
+	 * 
+	 * @see PortletRequest#getProperties(String)
+	 * @see HttpServletRequest#getHeaders(String)
+	 */
+	public Enumeration getHeaders(String name) {
+		return portletRequest.getProperties(name);
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public int getIntHeader(String name) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getMethod()
+	 */
+	public String getMethod() {
+		return null;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getPathInfo()
+	 */
+	public String getPathInfo() {
+		return null;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getPathTranslated()
+	 */
+	public String getPathTranslated() {
+		return null;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getQueryString()
+	 */
+	public String getQueryString() {
+		return null;
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getRemoteUser()
+	 */
+	public String getRemoteUser() {
+		return portletRequest.getRemoteUser();
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public String getRequestURI() {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public StringBuffer getRequestURL() {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getRequestedSessionId()
+	 */
+	public String getRequestedSessionId() {
+		return portletRequest.getRequestedSessionId();
+	}
+
+	/**
+	 * A {@link PortletRequest} has no servlet path. But for compatibility with
+	 * Struts 2 components and interceptors, the action parameter on the request
+	 * is mapped to the servlet path.
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getServletPath()
+	 */
+	public String getServletPath() {
+		String actionPath = getParameter(ACTION_PARAM);
+		if (!hasExtension(actionPath)) {
+			actionPath += "." + extension;
+		}
+		return actionPath;
+	}
+
+	private boolean hasExtension(String actionPath) {
+		return extension == null || "".equals(extension)
+				|| (actionPath != null && actionPath.endsWith("." + extension));
+	}
+
+	/**
+	 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getSession()
+	 */
+	public HttpSession getSession() {
+		return new PortletHttpSession(portletRequest.getPortletSession());
+	}
+
+	/**
+	 * Get the {@link PortletSession} as a {@link PortletHttpSession} instance.
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getSession(boolean)
+	 */
+	public HttpSession getSession(boolean create) {
+		return new PortletHttpSession(portletRequest.getPortletSession(create));
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#getUserPrincipal()
+	 */
+	public Principal getUserPrincipal() {
+		return portletRequest.getUserPrincipal();
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public boolean isRequestedSessionIdFromCookie() {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public boolean isRequestedSessionIdFromURL() {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public boolean isRequestedSessionIdFromUrl() {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#isRequestedSessionIdValid()
+	 */
+	public boolean isRequestedSessionIdValid() {
+		return portletRequest.isRequestedSessionIdValid();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.http.HttpServletRequest#isUserInRole(java.lang.String)
+	 */
+	public boolean isUserInRole(String role) {
+		return portletRequest.isUserInRole(role);
+	}
+
+	/**
+	 * Gets an attribute value on the {@link PortletRequest}. If the attribute
+	 * name is <tt>javax.servlet.include.servlet_path</tt>, it returns the
+	 * same as {@link PortletServletRequest#getServletPath()}
+	 * 
+	 * @see javax.servlet.ServletRequest#getAttribute(java.lang.String)
+	 */
+	public Object getAttribute(String name) {
+		if ("javax.servlet.include.servlet_path".equals(name)) {
+			return getServletPath();
+		} else {
+			return portletRequest.getAttribute(name);
+		}
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getAttributeNames()
+	 */
+	public Enumeration getAttributeNames() {
+		return portletRequest.getAttributeNames();
+	}
+
+	/**
+	 * Can only be invoked in the event phase.
+	 * 
+	 * @see ServletRequest#getCharacterEncoding()
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
+	 */
+	public String getCharacterEncoding() {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getCharacterEncoding();
+		} else {
+			throw new IllegalStateException("Not allowed in render phase");
+		}
+	}
+
+	/**
+	 * Can only be invoked in the event phase.
+	 * 
+	 * @see ServletRequest#getContentLength()
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
+	 */
+	public int getContentLength() {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getContentLength();
+		} else {
+			throw new IllegalStateException("Not allowed in render phase");
+		}
+	}
+
+	/**
+	 * Can only be invoked in the event phase.
+	 * 
+	 * @see ServletRequest#getContentType()
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
+	 */
+	public String getContentType() {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getContentType();
+		} else {
+			throw new IllegalStateException("Not allowed in render phase");
+		}
+	}
+
+	/**
+	 * Can only be invoked in the event phase. When invoked in the event phase,
+	 * it will wrap the portlet's {@link InputStream} as a
+	 * {@link PortletServletInputStream}.
+	 * 
+	 * @see ServletRequest#getInputStream()
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
+	 */
+	public ServletInputStream getInputStream() throws IOException {
+		if (portletRequest instanceof ActionRequest) {
+			return new PortletServletInputStream(((ActionRequest) portletRequest).getPortletInputStream());
+		} else {
+			throw new IllegalStateException("Not allowed in render phase");
+		}
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public String getLocalAddr() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getLocalAddr();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public String getLocalName() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getLocalName();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public int getLocalPort() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getLocalPort();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getLocale()
+	 */
+	public Locale getLocale() {
+		return portletRequest.getLocale();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getLocales()
+	 */
+	public Enumeration getLocales() {
+		return portletRequest.getLocales();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getParameter(java.lang.String)
+	 */
+	public String getParameter(String name) {
+		return portletRequest.getParameter(name);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getParameterMap()
+	 */
+	public Map getParameterMap() {
+		return portletRequest.getParameterMap();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getParameterNames()
+	 */
+	public Enumeration getParameterNames() {
+		return portletRequest.getParameterNames();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getParameterValues(java.lang.String)
+	 */
+	public String[] getParameterValues(String name) {
+		return portletRequest.getParameterValues(name);
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public String getProtocol() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getProtocol();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Can only be invoked in the event phase.
+	 * 
+	 * @see ServletRequest#getReader()
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
+	 */
+	public BufferedReader getReader() throws IOException {
+		if (portletRequest instanceof ActionRequest) {
+			return ((ActionRequest) portletRequest).getReader();
+		} else {
+			throw new IllegalStateException("Not allowed in render phase");
+		}
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getRealPath(java.lang.String)
+	 */
+	public String getRealPath(String path) {
+		return portletContext.getRealPath(path);
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public String getRemoteAddr() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getRemoteAddr();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public String getRemoteHost() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getRemoteHost();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public int getRemotePort() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getRemotePort();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/**
+	 * Get the {@link PortletRequestDispatcher} as a
+	 * {@link PortletServletRequestDispatcher} instance.
+	 * 
+	 * @see javax.servlet.ServletRequest#getRequestDispatcher(java.lang.String)
+	 */
+	public RequestDispatcher getRequestDispatcher(String path) {
+		return new PortletServletRequestDispatcher(portletContext.getRequestDispatcher(path));
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getScheme()
+	 */
+	public String getScheme() {
+		return portletRequest.getScheme();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#getServerName()
+	 */
+	public String getServerName() {
+		return portletRequest.getServerName();
+	}
+
+	/**
+	 * Not allowed in a portlet.
+	 * 
+	 * @throws IllegalStateException
+	 *             Not allowed in a portlet.
+	 */
+	public int getServerPort() {
+		if (portletRequest instanceof HttpServletRequest) {
+			return ((HttpServletRequest) portletRequest).getServerPort();
+		}
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#isSecure()
+	 */
+	public boolean isSecure() {
+		return portletRequest.isSecure();
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#removeAttribute(java.lang.String)
+	 */
+	public void removeAttribute(String name) {
+		portletRequest.removeAttribute(name);
+	}
+
+	/*
+	 * (non-Javadoc)
+	 * 
+	 * @see javax.servlet.ServletRequest#setAttribute(java.lang.String,
+	 *      java.lang.Object)
+	 */
+	public void setAttribute(String name, Object o) {
+		portletRequest.setAttribute(name, o);
+	}
+
+	/**
+	 * Can only be invoked in the event phase.
+	 * 
+	 * @see ServletRequest#setCharacterEncoding(String)
+	 * @throws IllegalStateException
+	 *             If the portlet is not in the event phase.
+	 */
+	public void setCharacterEncoding(String env) throws UnsupportedEncodingException {
+		if (portletRequest instanceof ActionRequest) {
+			((ActionRequest) portletRequest).setCharacterEncoding(env);
+		} else {
+			throw new IllegalStateException("Not allowed in render phase");
+		}
+	}
+
+	/**
+	 * Get the wrapped {@link PortletRequest} instance.
+	 * 
+	 * @return The wrapped {@link PortletRequest} instance.
+	 */
+	public PortletRequest getPortletRequest() {
+		return portletRequest;
+	}
+
+	@Inject(StrutsConstants.STRUTS_ACTION_EXTENSION)
+	public void setExtension(String extension) {
+		if (extension != null) {
+			this.extension = extension.split(",")[0];
+		}
+	}
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequestDispatcher.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequestDispatcher.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequestDispatcher.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletRequestDispatcher.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,71 @@
+/*
+ * $Id: PortletServletRequestDispatcher.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.io.IOException;
+
+import javax.portlet.PortletException;
+import javax.portlet.PortletRequest;
+import javax.portlet.PortletRequestDispatcher;
+import javax.portlet.PortletResponse;
+import javax.portlet.RenderRequest;
+import javax.portlet.RenderResponse;
+import javax.servlet.RequestDispatcher;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+
+public class PortletServletRequestDispatcher implements RequestDispatcher {
+
+	private PortletRequestDispatcher portletRequestDispatcher;
+	
+	public PortletServletRequestDispatcher(PortletRequestDispatcher portletRequestDispatcher) {
+		this.portletRequestDispatcher = portletRequestDispatcher;
+	}
+
+	public void forward(ServletRequest request, ServletResponse response) throws ServletException, IOException {
+		throw new IllegalStateException("Not allowed in a portlet");
+		
+	}
+
+	public void include(ServletRequest request, ServletResponse response) throws ServletException, IOException {
+		if(request instanceof PortletServletRequest && response instanceof PortletServletResponse) {
+			PortletRequest req = ((PortletServletRequest)request).getPortletRequest();
+			PortletResponse resp = ((PortletServletResponse)response).getPortletResponse();
+			if(req instanceof RenderRequest && resp instanceof RenderResponse) {
+				try {
+					portletRequestDispatcher.include((RenderRequest)req, (RenderResponse)resp);
+				}
+				catch(PortletException e) {
+					throw new ServletException(e);
+				}
+			}
+			else {
+				throw new IllegalStateException("Can only be invoked in the render phase");
+			}
+		}
+		else {
+			throw new IllegalStateException("Can only be invoked in a portlet");
+		}
+	}
+
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletResponse.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletResponse.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletResponse.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/PortletServletResponse.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,234 @@
+/*
+ * $Id: PortletServletResponse.java 651946 2008-04-27 13:41:38Z apetrelli $
+ *
+ * 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.struts2.portlet.servlet;
+
+import java.io.IOException;
+import java.io.PrintWriter;
+import java.util.Locale;
+
+import javax.portlet.PortletResponse;
+import javax.portlet.RenderResponse;
+import javax.servlet.ServletOutputStream;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+
+public class PortletServletResponse implements HttpServletResponse {
+
+	private PortletResponse portletResponse;
+	
+	public PortletServletResponse(PortletResponse portletResponse) {
+		this.portletResponse = portletResponse;
+	}
+	
+	public void addCookie(Cookie cookie) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void addDateHeader(String name, long date) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void addHeader(String name, String value) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void addIntHeader(String name, int value) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public boolean containsHeader(String name) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public String encodeRedirectURL(String url) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public String encodeRedirectUrl(String url) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public String encodeURL(String url) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public String encodeUrl(String url) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void sendError(int sc) throws IOException {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void sendError(int sc, String msg) throws IOException {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void sendRedirect(String location) throws IOException {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void setDateHeader(String name, long date) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void setHeader(String name, String value) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void setIntHeader(String name, int value) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void setStatus(int sc) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void setStatus(int sc, String sm) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void flushBuffer() throws IOException {
+		if(portletResponse instanceof RenderResponse) {
+			((RenderResponse)portletResponse).flushBuffer();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public int getBufferSize() {
+		if(portletResponse instanceof RenderResponse) {
+			return ((RenderResponse)portletResponse).getBufferSize();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public String getCharacterEncoding() {
+		if(portletResponse instanceof RenderResponse) {
+			return ((RenderResponse)portletResponse).getCharacterEncoding();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public String getContentType() {
+		if(portletResponse instanceof RenderResponse) {
+			return ((RenderResponse)portletResponse).getContentType();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public Locale getLocale() {
+		if(portletResponse instanceof RenderResponse) {
+			return ((RenderResponse)portletResponse).getLocale();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public ServletOutputStream getOutputStream() throws IOException {
+		if(portletResponse instanceof RenderResponse) {
+			return new PortletServletOutputStream(((RenderResponse)portletResponse).getPortletOutputStream());
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public PrintWriter getWriter() throws IOException {
+		if(portletResponse instanceof RenderResponse) {
+			return ((RenderResponse)portletResponse).getWriter();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public boolean isCommitted() {
+		if(portletResponse instanceof RenderResponse) {
+			return ((RenderResponse)portletResponse).isCommitted();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public void reset() {
+		if(portletResponse instanceof RenderResponse) {
+			((RenderResponse)portletResponse).reset();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public void resetBuffer() {
+		if(portletResponse instanceof RenderResponse) {
+			((RenderResponse)portletResponse).resetBuffer();
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public void setBufferSize(int size) {
+		if(portletResponse instanceof RenderResponse) {
+			((RenderResponse)portletResponse).setBufferSize(size);
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public void setCharacterEncoding(String charset) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void setContentLength(int len) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public void setContentType(String type) {
+		if(portletResponse instanceof RenderResponse) {
+			((RenderResponse)portletResponse).setContentType(type);
+		}
+		else {
+			throw new IllegalStateException("Not allowed in event phase");
+		}
+	}
+
+	public void setLocale(Locale loc) {
+		throw new IllegalStateException("Not allowed in a portlet");
+	}
+
+	public PortletResponse getPortletResponse() {
+		return portletResponse;
+	}
+
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/package.html
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/package.html?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/package.html (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/servlet/package.html Thu Jun 23 20:18:43 2011
@@ -0,0 +1,27 @@
+<!--
+/*
+ * $Id: package.html 590812 2007-10-31 20:32:54Z apetrelli $
+ *
+ * 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.
+ */
+-->
+<body>
+	Portlet wrapper objects for some of the servlet api objects. The wrappers simply delegate to the 
+	underlying, corresponding portlet objects. The portlet wrapper objects makes it easier to reuse 
+	interceptors and components from struts2 core.
+</body>
\ No newline at end of file

Added: struts/archive/plugins/src/main/java/org/apache/struts2/portlet/util/PortletUrlHelper.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/portlet/util/PortletUrlHelper.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/portlet/util/PortletUrlHelper.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/portlet/util/PortletUrlHelper.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,300 @@
+/*
+ * $Id: PortletUrlHelper.java 1099157 2011-05-03 17:53:55Z jogep $
+ *
+ * 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.struts2.portlet.util;
+
+import com.opensymphony.xwork2.util.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import org.apache.commons.lang.StringUtils;
+import org.apache.struts2.StrutsException;
+import org.apache.struts2.portlet.PortletActionConstants;
+import org.apache.struts2.portlet.context.PortletActionContext;
+
+import javax.portlet.*;
+import java.io.UnsupportedEncodingException;
+import java.net.URLEncoder;
+import java.util.Iterator;
+import java.util.LinkedHashMap;
+import java.util.Map;
+import java.util.StringTokenizer;
+
+/**
+ * Helper class for creating Portlet URLs. Portlet URLs are fundamentally different from regular
+ * servlet URLs since they never target the application itself; all requests go through the portlet
+ * container and must therefore be programatically constructed using the
+ * {@link javax.portlet.RenderResponse#createActionURL()} and
+ * {@link javax.portlet.RenderResponse#createRenderURL()} APIs.
+ *
+ */
+public class PortletUrlHelper {
+    public static final String ENCODING = "UTF-8";
+
+    private static final Logger LOG = LoggerFactory.getLogger(PortletUrlHelper.class);
+
+    /**
+     * Create a portlet URL with for the specified action and namespace.
+     *
+     * @param action The action the URL should invoke.
+     * @param namespace The namespace of the action to invoke.
+     * @param method The method of the action to invoke.
+     * @param params The parameters of the URL.
+     * @param type The type of the url, either <tt>action</tt> or <tt>render</tt>
+     * @param mode The PortletMode of the URL.
+     * @param state The WindowState of the URL.
+     * @return The URL String.
+     */
+    public static String buildUrl(String action, String namespace, String method, Map params,
+            String type, String mode, String state) {
+        return buildUrl(action, namespace, method, params, null, type, mode, state,
+                true, true);
+    }
+
+    /**
+     * Create a portlet URL with for the specified action and namespace.
+     *
+     * @see #buildUrl(String, String, Map, String, String, String)
+     */
+    public static String buildUrl(String action, String namespace, String method, Map params,
+            String scheme, String type, String portletMode, String windowState,
+            boolean includeContext, boolean encodeResult) {
+    	StringBuffer resultingAction = new StringBuffer();
+        RenderRequest request = PortletActionContext.getRenderRequest();
+        RenderResponse response = PortletActionContext.getRenderResponse();
+        if (LOG.isDebugEnabled()) {
+            LOG.debug("Creating url. Action = " + action + ", Namespace = "
+                + namespace + ", Type = " + type);
+        }
+        namespace = prependNamespace(namespace, portletMode);
+        if (StringUtils.isEmpty(portletMode)) {
+            portletMode = PortletActionContext.getRenderRequest().getPortletMode().toString();
+        }
+        String result = null;
+        int paramStartIndex = action.indexOf('?');
+        if (paramStartIndex > 0) {
+            String value = action;
+            action = value.substring(0, value.indexOf('?'));
+            String queryStr = value.substring(paramStartIndex + 1);
+            StringTokenizer tok = new StringTokenizer(queryStr, "&");
+            while (tok.hasMoreTokens()) {
+                String paramVal = tok.nextToken();
+                String key = paramVal.substring(0, paramVal.indexOf('='));
+                String val = paramVal.substring(paramVal.indexOf('=') + 1);
+                params.put(key, new String[] { val });
+            }
+        }
+        if (StringUtils.isNotEmpty(namespace)) {
+            resultingAction.append(namespace);
+            if(!action.startsWith("/") && !namespace.endsWith("/")) {
+                resultingAction.append("/");
+            }
+        }
+        resultingAction.append(action);
+        if(StringUtils.isNotEmpty(method)) {
+        	resultingAction.append("!").append(method);
+        }
+        if (LOG.isDebugEnabled()) LOG.debug("Resulting actionPath: " + resultingAction);
+        params.put(PortletActionConstants.ACTION_PARAM, new String[] { resultingAction.toString() });
+
+        PortletURL url = null;
+        if ("action".equalsIgnoreCase(type)) {
+            if (LOG.isDebugEnabled()) LOG.debug("Creating action url");
+            url = response.createActionURL();
+        } else {
+            if (LOG.isDebugEnabled()) LOG.debug("Creating render url");
+            url = response.createRenderURL();
+        }
+
+        params.put(PortletActionConstants.MODE_PARAM, portletMode);
+        url.setParameters(ensureParamsAreStringArrays(params));
+
+        if ("HTTPS".equalsIgnoreCase(scheme)) {
+            try {
+                url.setSecure(true);
+            } catch (PortletSecurityException e) {
+                LOG.error("Cannot set scheme to https", e);
+            }
+        }
+        try {
+            url.setPortletMode(getPortletMode(request, portletMode));
+            url.setWindowState(getWindowState(request, windowState));
+        } catch (Exception e) {
+            LOG.error("Unable to set mode or state:" + e.getMessage(), e);
+        }
+        result = url.toString();
+        // TEMP BUG-WORKAROUND FOR DOUBLE ESCAPING OF AMPERSAND
+        if(result.indexOf("&amp;") >= 0) {
+            result = result.replace("&amp;", "&");
+        }
+        return result;
+
+    }
+
+    /**
+     *
+     * Prepend the namespace configuration for the specified namespace and PortletMode.
+     *
+     * @param namespace The base namespace.
+     * @param portletMode The PortletMode.
+     *
+     * @return prepended namespace.
+     */
+    private static String prependNamespace(String namespace, String portletMode) {
+        StringBuffer sb = new StringBuffer();
+        PortletMode mode = PortletActionContext.getRenderRequest().getPortletMode();
+        if(StringUtils.isNotEmpty(portletMode)) {
+            mode = new PortletMode(portletMode);
+        }
+        String portletNamespace = PortletActionContext.getPortletNamespace();
+        String modeNamespace = (String)PortletActionContext.getModeNamespaceMap().get(mode);
+        if (LOG.isDebugEnabled()) LOG.debug("PortletNamespace: " + portletNamespace + ", modeNamespace: " + modeNamespace);
+        if(StringUtils.isNotEmpty(portletNamespace)) {
+            sb.append(portletNamespace);
+        }
+        if(StringUtils.isNotEmpty(modeNamespace)) {
+            if(!modeNamespace.startsWith("/")) {
+                sb.append("/");
+            }
+            sb.append(modeNamespace);
+        }
+        if(StringUtils.isNotEmpty(namespace)) {
+            if(!namespace.startsWith("/")) {
+                sb.append("/");
+            }
+            sb.append(namespace);
+        }
+        if (LOG.isDebugEnabled()) LOG.debug("Resulting namespace: " + sb);
+        return sb.toString();
+    }
+
+    /**
+     * Encode an url to a non Struts action resource, like stylesheet, image or
+     * servlet.
+     *
+     * @param value
+     * @return encoded url to non Struts action resources.
+     */
+    public static String buildResourceUrl(String value, Map<String, Object> params) {
+        StringBuffer sb = new StringBuffer();
+        // Relative URLs are not allowed in a portlet
+        if (!value.startsWith("/")) {
+            sb.append("/");
+        }
+        sb.append(value);
+        if(params != null && params.size() > 0) {
+            sb.append("?");
+            Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
+            try {
+            while(it.hasNext()) {
+            	Map.Entry<String, Object> entry = it.next();
+
+                sb.append(URLEncoder.encode(entry.getKey(), ENCODING)).append("=");
+                sb.append(URLEncoder.encode(entry.getValue().toString(), ENCODING));
+                if(it.hasNext()) {
+                    sb.append("&");
+                }
+            }
+            } catch (UnsupportedEncodingException e) {
+                throw new StrutsException("Encoding "+ENCODING+" not found");
+            }
+        }
+        RenderResponse resp = PortletActionContext.getRenderResponse();
+        RenderRequest req = PortletActionContext.getRenderRequest();
+        return resp.encodeURL(req.getContextPath() + sb.toString());
+    }
+
+    /**
+     * Will ensure that all entries in <code>params</code> are String arrays,
+     * as requried by the setParameters on the PortletURL.
+     *
+     * @param params The parameters to the URL.
+     * @return A Map with all parameters as String arrays.
+     */
+    public static Map ensureParamsAreStringArrays(Map<String, Object> params) {
+        Map<String, String[]> result = null;
+        if (params != null) {
+            result = new LinkedHashMap<String, String[]>(params.size());
+            Iterator<Map.Entry<String, Object>> it = params.entrySet().iterator();
+            while (it.hasNext()) {
+            	Map.Entry<String, Object> entry = it.next();
+            	Object val = entry.getValue();
+                if (val instanceof String[]) {
+                    result.put(entry.getKey(), (String[])val);
+                } else {
+                    result.put(entry.getKey(), new String[] { val.toString() });
+                }
+            }
+        }
+        return result;
+    }
+
+    /**
+     * Convert the given String to a WindowState object.
+     *
+     * @param portletReq The RenderRequest.
+     * @param windowState The WindowState as a String.
+     * @return The WindowState that mathces the <tt>windowState</tt> String, or if
+     * the String is blank, the current WindowState.
+     */
+    private static WindowState getWindowState(RenderRequest portletReq,
+            String windowState) {
+        WindowState state = portletReq.getWindowState();
+        if (StringUtils.isNotEmpty(windowState)) {
+            if ("maximized".equalsIgnoreCase(windowState)) {
+                state = WindowState.MAXIMIZED;
+            } else if ("normal".equalsIgnoreCase(windowState)) {
+                state = WindowState.NORMAL;
+            } else if ("minimized".equalsIgnoreCase(windowState)) {
+                state = WindowState.MINIMIZED;
+            }
+        }
+        if(state == null) {
+            state = WindowState.NORMAL;
+        }
+        return state;
+    }
+
+    /**
+     * Convert the given String to a PortletMode object.
+     *
+     * @param portletReq The RenderRequest.
+     * @param portletMode The PortletMode as a String.
+     * @return The PortletMode that mathces the <tt>portletMode</tt> String, or if
+     * the String is blank, the current PortletMode.
+     */
+    private static PortletMode getPortletMode(RenderRequest portletReq,
+            String portletMode) {
+        PortletMode mode = portletReq.getPortletMode();
+
+        if (StringUtils.isNotEmpty(portletMode)) {
+            if ("edit".equalsIgnoreCase(portletMode)) {
+                mode = PortletMode.EDIT;
+            } else if ("view".equalsIgnoreCase(portletMode)) {
+                mode = PortletMode.VIEW;
+            } else if ("help".equalsIgnoreCase(portletMode)) {
+                mode = PortletMode.HELP;
+            }
+        }
+        if(mode == null) {
+            mode = PortletMode.VIEW;
+        }
+        return mode;
+    }
+}

Added: struts/archive/plugins/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java
URL: http://svn.apache.org/viewvc/struts/archive/plugins/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java?rev=1139061&view=auto
==============================================================================
--- struts/archive/plugins/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java (added)
+++ struts/archive/plugins/src/main/java/org/apache/struts2/views/freemarker/PortletFreemarkerResult.java Thu Jun 23 20:18:43 2011
@@ -0,0 +1,282 @@
+/*
+ * $Id: PortletFreemarkerResult.java 768802 2009-04-26 21:34:14Z nilsga $
+ *
+ * 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.struts2.views.freemarker;
+
+import java.io.IOException;
+import java.io.Writer;
+import java.util.Locale;
+
+import javax.portlet.ActionResponse;
+import javax.portlet.PortletException;
+import javax.portlet.PortletRequestDispatcher;
+import javax.servlet.ServletContext;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+import org.apache.struts2.ServletActionContext;
+import org.apache.struts2.dispatcher.StrutsResultSupport;
+import org.apache.struts2.portlet.PortletActionConstants;
+import org.apache.struts2.portlet.context.PortletActionContext;
+import org.apache.struts2.views.util.ResourceUtil;
+
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.inject.Inject;
+import com.opensymphony.xwork2.util.ValueStack;
+
+import freemarker.template.Configuration;
+import freemarker.template.ObjectWrapper;
+import freemarker.template.Template;
+import freemarker.template.TemplateException;
+import freemarker.template.TemplateModel;
+import freemarker.template.TemplateModelException;
+
+/**
+ */
+public class PortletFreemarkerResult extends StrutsResultSupport implements PortletActionConstants{
+
+    private static final long serialVersionUID = -5570612389289887543L;
+
+    protected ActionInvocation invocation;
+
+    protected Configuration configuration;
+
+    protected ObjectWrapper wrapper;
+    protected FreemarkerManager freemarkerManager;
+
+    /*
+     * Struts results are constructed for each result execeution
+     *
+     * the current context is availible to subclasses via these protected fields
+     */
+    protected String location;
+
+    private String pContentType = "text/html";
+
+    public PortletFreemarkerResult() {
+        super();
+    }
+
+    public PortletFreemarkerResult(String location) {
+        super(location);
+    }
+    
+    @Inject
+    public void setFreemarkerManager(FreemarkerManager mgr) {
+        this.freemarkerManager = mgr;
+    }
+
+    public void setContentType(String aContentType) {
+        pContentType = aContentType;
+    }
+
+    /**
+     * allow parameterization of the contentType the default being text/html
+     */
+    public String getContentType() {
+        return pContentType;
+    }
+
+    /**
+     * Execute this result, using the specified template location. <p/>The
+     * template location has already been interoplated for any variable
+     * substitutions <p/>this method obtains the freemarker configuration and
+     * the object wrapper from the provided hooks. It them implements the
+     * template processing workflow by calling the hooks for preTemplateProcess
+     * and postTemplateProcess
+     */
+    public void doExecute(String location, ActionInvocation invocation)
+            throws IOException, TemplateException, PortletException {
+        if (PortletActionContext.isEvent()) {
+            executeActionResult(location, invocation);
+        } else if (PortletActionContext.isRender()) {
+            executeRenderResult(location, invocation);
+        }
+    }
+
+    /**
+     * @param location
+     * @param invocation
+     */
+    private void executeActionResult(String location,
+                                     ActionInvocation invocation) {
+        ActionResponse res = PortletActionContext.getActionResponse();
+        // View is rendered outside an action...uh oh...
+		invocation.getInvocationContext().getSession().put(RENDER_DIRECT_LOCATION, location);
+        res.setRenderParameter(PortletActionConstants.ACTION_PARAM, "freemarkerDirect");
+        res.setRenderParameter(PortletActionConstants.MODE_PARAM, PortletActionContext
+                .getRequest().getPortletMode().toString());
+
+    }
+
+    /**
+     * @param location
+     * @param invocation
+     * @throws TemplateException
+     * @throws IOException
+     * @throws TemplateModelException
+     */
+    private void executeRenderResult(String location,
+                                     ActionInvocation invocation) throws TemplateException, IOException,
+            TemplateModelException, PortletException {
+        this.location = location;
+        this.invocation = invocation;
+        this.configuration = getConfiguration();
+        this.wrapper = getObjectWrapper();
+
+        HttpServletRequest req = ServletActionContext.getRequest();
+
+        if (!location.startsWith("/")) {
+            String base = ResourceUtil.getResourceBase(req);
+            location = base + "/" + location;
+        }
+
+        Template template = configuration.getTemplate(location, deduceLocale());
+        TemplateModel model = createModel();
+        // Give subclasses a chance to hook into preprocessing
+        if (preTemplateProcess(template, model)) {
+            try {
+                // Process the template
+                PortletActionContext.getRenderResponse().setContentType(pContentType);
+                template.process(model, getWriter());
+            } finally {
+                // Give subclasses a chance to hook into postprocessing
+                postTemplateProcess(template, model);
+            }
+        }
+    }
+
+    /**
+     * This method is called from {@link #doExecute(String, ActionInvocation)}
+     * to obtain the FreeMarker configuration object that this result will use
+     * for template loading. This is a hook that allows you to custom-configure
+     * the configuration object in a subclass, or to fetch it from an IoC
+     * container. <p/><b>The default implementation obtains the configuration
+     * from the ConfigurationManager instance. </b>
+     */
+    protected Configuration getConfiguration() throws TemplateException {
+        return freemarkerManager.getConfiguration(
+                ServletActionContext.getServletContext());
+    }
+
+    /**
+     * This method is called from {@link #doExecute(String, ActionInvocation)}
+     * to obtain the FreeMarker object wrapper object that this result will use
+     * for adapting objects into template models. This is a hook that allows you
+     * to custom-configure the wrapper object in a subclass. <p/><b>The default
+     * implementation returns {@link Configuration#getObjectWrapper()}</b>
+     */
+    protected ObjectWrapper getObjectWrapper() {
+        return configuration.getObjectWrapper();
+    }
+
+    /**
+     * The default writer writes directly to the response writer.
+     */
+    protected Writer getWriter() throws IOException {
+        return PortletActionContext.getRenderResponse().getWriter();
+    }
+
+    /**
+     * Build the instance of the ScopesHashModel, including JspTagLib support
+     * <p/>Objects added to the model are <p/>
+     * <ul>
+     * <li>Application - servlet context attributes hash model
+     * <li>JspTaglibs - jsp tag lib factory model
+     * <li>Request - request attributes hash model
+     * <li>Session - session attributes hash model
+     * <li>request - the HttpServletRequst object for direct access
+     * <li>response - the HttpServletResponse object for direct access
+     * <li>stack - the OgnLValueStack instance for direct access
+     * <li>ognl - the instance of the OgnlTool
+     * <li>action - the action itself
+     * <li>exception - optional : the JSP or Servlet exception as per the
+     * servlet spec (for JSP Exception pages)
+     * <li>struts - instance of the StrutsUtil class
+     * </ul>
+     */
+    protected TemplateModel createModel() throws TemplateModelException {
+        ServletContext servletContext = ServletActionContext
+                .getServletContext();
+        HttpServletRequest request = ServletActionContext.getRequest();
+        HttpServletResponse response = ServletActionContext.getResponse();
+        ValueStack stack = ServletActionContext.getContext()
+                .getValueStack();
+        return freemarkerManager.buildTemplateModel(stack,
+                invocation.getAction(), servletContext, request, response,
+                wrapper);
+    }
+
+    /**
+     * Returns the locale used for the
+     * {@link Configuration#getTemplate(String, Locale)}call. The base
+     * implementation simply returns the locale setting of the configuration.
+     * Override this method to provide different behaviour,
+     */
+    protected Locale deduceLocale() {
+        return configuration.getLocale();
+    }
+
+    /**
+     * the default implementation of postTemplateProcess applies the contentType
+     * parameter
+     */
+    protected void postTemplateProcess(Template template, TemplateModel data)
+            throws IOException {
+    }
+
+    /**
+     * Called before the execution is passed to template.process(). This is a
+     * generic hook you might use in subclasses to perform a specific action
+     * before the template is processed. By default does nothing. A typical
+     * action to perform here is to inject application-specific objects into the
+     * model root
+     *
+     * @return true to process the template, false to suppress template
+     *         processing.
+     */
+    protected boolean preTemplateProcess(Template template, TemplateModel model)
+            throws IOException {
+        Object attrContentType = template.getCustomAttribute("content_type");
+
+        if (attrContentType != null) {
+            ServletActionContext.getResponse().setContentType(
+                    attrContentType.toString());
+        } else {
+            String contentType = getContentType();
+
+            if (contentType == null) {
+                contentType = "text/html";
+            }
+
+            String encoding = template.getEncoding();
+
+            if (encoding != null) {
+                contentType = contentType + "; charset=" + encoding;
+            }
+
+            ServletActionContext.getResponse().setContentType(contentType);
+        }
+
+        return true;
+    }
+}
+



Mime
View raw message