WW-1989,WW-2053 Provide "mock" implentation of servlet classes wrapping the portlet classes. Eases reuse of components/interceptors that has references to servlet api objects.

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@564246 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Nils-Helge Garli
2007-08-09 15:36:52 +00:00
parent bdbe0bf7f9
commit 2440ce6bdb
9 changed files with 1627 additions and 0 deletions
@@ -0,0 +1,214 @@
/*
* $Id: $
*
* 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;
}
}
@@ -0,0 +1,81 @@
/*
* $Id: $
*
* 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;
}
}
@@ -0,0 +1,235 @@
/*
* $Id: $
*
* 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;
}
}
@@ -0,0 +1,121 @@
/*
* $Id: $
*
* 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;
}
}
@@ -0,0 +1,88 @@
/*
* $Id: $
*
* 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;
}
}
@@ -0,0 +1,558 @@
/*
* $Id: $
*
* 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.portlet.PortletActionConstants;
/**
* 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;
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(actionPath != null && !actionPath.endsWith(".action")) {
actionPath += ".action";
}
return actionPath;
}
/**
* 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;
}
}
@@ -0,0 +1,70 @@
/*
* $Id: $
*
* 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");
}
}
}
@@ -0,0 +1,233 @@
/*
* $Id: $
*
* 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;
}
}
@@ -0,0 +1,27 @@
<!--
/*
* $Id: package.html 559206 2007-07-24 21:01:18Z 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>