From 9c04958fdda8f490fc91aef19819ef9965c36e2e Mon Sep 17 00:00:00 2001 From: =?utf8?q?Leif=20=C3=85strand?= Date: Tue, 20 Nov 2012 14:36:12 +0200 Subject: [PATCH] Add a bunch of methods to request, response and session wrappers (#9749) * Make VaadinPortletRequest extend PortletRequestWrapper to remove some boilerplate Change-Id: Ibfc0f18d85cf77e17de6d6ce561c44677958e3bd --- .../src/com/vaadin/server/VaadinPortlet.java | 28 ++ .../vaadin/server/VaadinPortletRequest.java | 103 ++++--- .../vaadin/server/VaadinPortletResponse.java | 8 +- .../src/com/vaadin/server/VaadinRequest.java | 252 ++++++++++++++++++ .../src/com/vaadin/server/VaadinResponse.java | 13 + .../com/vaadin/server/WrappedHttpSession.java | 25 ++ .../vaadin/server/WrappedPortletSession.java | 25 ++ .../src/com/vaadin/server/WrappedSession.java | 79 ++++++ 8 files changed, 497 insertions(+), 36 deletions(-) diff --git a/server/src/com/vaadin/server/VaadinPortlet.java b/server/src/com/vaadin/server/VaadinPortlet.java index b1843ce9e5..b3ec6ba312 100644 --- a/server/src/com/vaadin/server/VaadinPortlet.java +++ b/server/src/com/vaadin/server/VaadinPortlet.java @@ -96,6 +96,16 @@ public class VaadinPortlet extends GenericPortlet implements Constants { return originalRequest.getRemoteAddr(); } + @Override + public String getRemoteHost() { + return originalRequest.getRemoteHost(); + } + + @Override + public int getRemotePort() { + return originalRequest.getRemotePort(); + } + @Override public String getHeader(String name) { String header = super.getHeader(name); @@ -105,6 +115,24 @@ public class VaadinPortlet extends GenericPortlet implements Constants { return header; } + @Override + public Enumeration getHeaderNames() { + Enumeration headerNames = super.getHeaderNames(); + if (headerNames == null) { + headerNames = originalRequest.getHeaderNames(); + } + return headerNames; + } + + @Override + public Enumeration getHeaders(String name) { + Enumeration headers = super.getHeaders(name); + if (headers == null) { + headers = originalRequest.getHeaders(name); + } + return headers; + } + @Override public Map getParameterMap() { Map parameterMap = super.getParameterMap(); diff --git a/server/src/com/vaadin/server/VaadinPortletRequest.java b/server/src/com/vaadin/server/VaadinPortletRequest.java index 259b227b42..7243a610c9 100644 --- a/server/src/com/vaadin/server/VaadinPortletRequest.java +++ b/server/src/com/vaadin/server/VaadinPortletRequest.java @@ -16,15 +16,17 @@ package com.vaadin.server; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; -import java.util.Locale; -import java.util.Map; +import java.text.ParseException; +import java.util.Enumeration; import javax.portlet.ClientDataRequest; import javax.portlet.PortletRequest; import javax.portlet.PortletSession; import javax.portlet.ResourceRequest; +import javax.portlet.filter.PortletRequestWrapper; import com.vaadin.shared.ApplicationConstants; @@ -37,9 +39,9 @@ import com.vaadin.shared.ApplicationConstants; * @see VaadinRequest * @see VaadinPortletResponse */ -public class VaadinPortletRequest implements VaadinRequest { +public class VaadinPortletRequest extends PortletRequestWrapper implements + VaadinRequest { - private final PortletRequest request; private final VaadinPortletService vaadinService; /** @@ -52,19 +54,14 @@ public class VaadinPortletRequest implements VaadinRequest { */ public VaadinPortletRequest(PortletRequest request, VaadinPortletService vaadinService) { - this.request = request; + super(request); this.vaadinService = vaadinService; } - @Override - public Object getAttribute(String name) { - return request.getAttribute(name); - } - @Override public int getContentLength() { try { - return ((ClientDataRequest) request).getContentLength(); + return ((ClientDataRequest) getRequest()).getContentLength(); } catch (ClassCastException e) { throw new IllegalStateException( "Content lenght only available for ClientDataRequests"); @@ -74,7 +71,7 @@ public class VaadinPortletRequest implements VaadinRequest { @Override public InputStream getInputStream() throws IOException { try { - return ((ClientDataRequest) request).getPortletInputStream(); + return ((ClientDataRequest) getRequest()).getPortletInputStream(); } catch (ClassCastException e) { throw new IllegalStateException( "Input data only available for ClientDataRequests"); @@ -82,22 +79,18 @@ public class VaadinPortletRequest implements VaadinRequest { } @Override - public String getParameter(String name) { - return request.getParameter(name); - } - - @Override - public Map getParameterMap() { - return request.getParameterMap(); - } - - @Override - public void setAttribute(String name, Object o) { - request.setAttribute(name, o); + public BufferedReader getReader() throws IOException { + try { + return ((ClientDataRequest) getRequest()).getReader(); + } catch (ClassCastException e) { + throw new IllegalStateException( + "Reader only available for ClientDataRequests"); + } } @Override public String getPathInfo() { + PortletRequest request = getRequest(); if (request instanceof ResourceRequest) { ResourceRequest resourceRequest = (ResourceRequest) request; String resourceID = resourceRequest.getResourceID(); @@ -119,8 +112,7 @@ public class VaadinPortletRequest implements VaadinRequest { @Override public WrappedSession getWrappedSession(boolean allowSessionCreation) { - PortletSession session = request - .getPortletSession(allowSessionCreation); + PortletSession session = getPortletSession(allowSessionCreation); if (session != null) { return new WrappedPortletSession(session); } else { @@ -134,13 +126,13 @@ public class VaadinPortletRequest implements VaadinRequest { * @return the unwrapped portlet request */ public PortletRequest getPortletRequest() { - return request; + return getRequest(); } @Override public String getContentType() { try { - return ((ResourceRequest) request).getContentType(); + return ((ResourceRequest) getRequest()).getContentType(); } catch (ClassCastException e) { throw new IllegalStateException( "Content type only available for ResourceRequests"); @@ -148,8 +140,23 @@ public class VaadinPortletRequest implements VaadinRequest { } @Override - public Locale getLocale() { - return request.getLocale(); + public String getCharacterEncoding() { + try { + return ((ClientDataRequest) getRequest()).getCharacterEncoding(); + } catch (ClassCastException e) { + throw new IllegalStateException( + "Character encoding only available for ClientDataRequest"); + } + } + + @Override + public String getMethod() { + try { + return ((ClientDataRequest) getRequest()).getMethod(); + } catch (ClassCastException e) { + throw new IllegalStateException( + "Method only available for ClientDataRequest"); + } } @Override @@ -158,8 +165,13 @@ public class VaadinPortletRequest implements VaadinRequest { } @Override - public boolean isSecure() { - return request.isSecure(); + public String getRemoteHost() { + return null; + } + + @Override + public int getRemotePort() { + return -1; } @Override @@ -176,7 +188,7 @@ public class VaadinPortletRequest implements VaadinRequest { * the property is not defined */ public String getPortalProperty(String name) { - return request.getPortalContext().getProperty(name); + return getRequest().getPortalContext().getProperty(name); } @Override @@ -185,7 +197,28 @@ public class VaadinPortletRequest implements VaadinRequest { } @Override - public String getContextPath() { - return request.getContextPath(); + public long getDateHeader(String name) { + String header = getHeader(name); + if (header == null) { + return -1; + } else { + try { + return VaadinPortletResponse.HTTP_DATE_FORMAT.parse(header) + .getTime(); + } catch (ParseException e) { + throw new IllegalArgumentException(e); + } + } + } + + @Override + public Enumeration getHeaderNames() { + return null; + } + + @Override + public Enumeration getHeaders(String name) { + return null; } + } diff --git a/server/src/com/vaadin/server/VaadinPortletResponse.java b/server/src/com/vaadin/server/VaadinPortletResponse.java index d9fe828bff..ab0fff486b 100644 --- a/server/src/com/vaadin/server/VaadinPortletResponse.java +++ b/server/src/com/vaadin/server/VaadinPortletResponse.java @@ -28,6 +28,7 @@ import java.util.TimeZone; import javax.portlet.MimeResponse; import javax.portlet.PortletResponse; import javax.portlet.ResourceResponse; +import javax.servlet.http.Cookie; /** * Wrapper for {@link PortletResponse} and its subclasses. @@ -39,7 +40,7 @@ import javax.portlet.ResourceResponse; * @see VaadinPortletRequest */ public class VaadinPortletResponse implements VaadinResponse { - private static final DateFormat HTTP_DATE_FORMAT = new SimpleDateFormat( + static final DateFormat HTTP_DATE_FORMAT = new SimpleDateFormat( "EEE, dd MMM yyyy HH:mm:ss zzz", Locale.ENGLISH); static { HTTP_DATE_FORMAT.setTimeZone(TimeZone.getTimeZone("GMT")); @@ -117,4 +118,9 @@ public class VaadinPortletResponse implements VaadinResponse { public VaadinPortletService getService() { return vaadinService; } + + @Override + public void addCookie(Cookie cookie) { + response.addProperty(cookie); + } } \ No newline at end of file diff --git a/server/src/com/vaadin/server/VaadinRequest.java b/server/src/com/vaadin/server/VaadinRequest.java index 9e454761f2..ebf405d4f3 100644 --- a/server/src/com/vaadin/server/VaadinRequest.java +++ b/server/src/com/vaadin/server/VaadinRequest.java @@ -16,14 +16,20 @@ package com.vaadin.server; +import java.io.BufferedReader; import java.io.IOException; import java.io.InputStream; import java.io.Serializable; +import java.io.UnsupportedEncodingException; +import java.security.Principal; +import java.util.Enumeration; import java.util.Locale; import java.util.Map; +import javax.portlet.ClientDataRequest; import javax.portlet.PortletRequest; import javax.servlet.ServletRequest; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletRequest; /** @@ -233,4 +239,250 @@ public interface VaadinRequest extends Serializable { */ public VaadinService getService(); + /** + * Returns an array containing all of the Cookie objects the + * client sent with this request. This method returns null if + * no cookies were sent. + * + * @return an array of all the Cookies included with this + * request, or null if the request has no cookies + * + * @see HttpServletRequest#getCookies() + * @see PortletRequest#getCookies() + */ + public Cookie[] getCookies(); + + /** + * Returns the name of the authentication scheme used for the connection + * between client and server, for example, BASIC_AUTH, + * CLIENT_CERT_AUTH, a custom one or null if there + * was no authentication. + * + * @return a string indicating the authentication scheme, or + * null if the request was not authenticated. + * + * @see HttpServletRequest#getAuthType() + * @see PortletRequest#getAuthType() + */ + public String getAuthType(); + + /** + * Returns the login of the user making this request, if the user has been + * authenticated, or null if the user has not been authenticated. Whether + * the user name is sent with each subsequent request depends on the browser + * and type of authentication. + * + * @return a String specifying the login of the user making this request, or + * null if the user login is not known. + * + * @see HttpServletRequest#getRemoteUser() + * @see PortletRequest#getRemoteUser() + */ + public String getRemoteUser(); + + /** + * Returns a java.security.Principal object containing the name + * of the current authenticated user. If the user has not been + * authenticated, the method returns null. + * + * @return a java.security.Principal containing the name of the + * user making this request; null if the user has not + * been authenticated + * + * @see HttpServletRequest#getUserPrincipal() + * @see PortletRequest#getUserPrincipal() + */ + public Principal getUserPrincipal(); + + /** + * Returns a boolean indicating whether the authenticated user is included + * in the specified logical "role". Roles and role membership can be defined + * using deployment descriptors. If the user has not been authenticated, the + * method returns false. + * + * @param role + * a String specifying the name of the role + * @return a boolean indicating whether the user making this request belongs + * to a given role; false if the user has not been + * authenticated + * + * @see HttpServletRequest#isUserInRole(String) + * @see PortletRequest#isUserInRole(String) + */ + public boolean isUserInRole(String role); + + /** + * Removes an attribute from this request. This method is not generally + * needed as attributes only persist as long as the request is being + * handled. + * + * @param name + * a String specifying the name of the attribute to remove + * + * @see ServletRequest#removeAttribute(String) + * @see PortletRequest#removeAttribute(String) + */ + public void removeAttribute(String name); + + /** + * Returns an Enumeration containing the names of the attributes available + * to this request. This method returns an empty Enumeration if the request + * has no attributes available to it. + * + * @return an Enumeration of strings containing the names of the request's + * attributes + * + * @see ServletRequest#getAttributeNames() + * @see PortletRequest#getAttributeNames() + */ + public Enumeration getAttributeNames(); + + /** + * Returns an Enumeration of Locale objects indicating, in decreasing order + * starting with the preferred locale, the locales that are acceptable to + * the client based on the Accept-Language header. If the client request + * doesn't provide an Accept-Language header, this method returns an + * Enumeration containing one Locale, the default locale for the server. + * + * @return an Enumeration of preferred Locale objects for the client + * + * @see HttpServletRequest#getLocales() + * @see PortletRequest#getLocales() + */ + public Enumeration getLocales(); + + /** + * Returns the fully qualified name of the client or the last proxy that + * sent the request. If the engine cannot or chooses not to resolve the + * hostname (to improve performance), this method returns the dotted-string + * form of the IP address. + * + * @return a String containing the fully qualified name of the client, or + * null if the information is not available. + * + * @see HttpServletRequest#getRemoteHost() + */ + public String getRemoteHost(); + + /** + * Returns the Internet Protocol (IP) source port of the client or last + * proxy that sent the request. + * + * @return an integer specifying the port number, or -1 if the information + * is not available. + * + * @see ServletRequest#getRemotePort() + */ + public int getRemotePort(); + + /** + * Returns the name of the character encoding used in the body of this + * request. This method returns null if the request does not + * specify a character encoding. + * + * @return a String containing the name of the character encoding, or null + * if the request does not specify a character encoding + * + * @see ServletRequest#getCharacterEncoding() + * @see ClientDataRequest#getCharacterEncoding() + */ + public String getCharacterEncoding(); + + /** + * Retrieves the body of the request as character data using a + * BufferedReader. The reader translates the character data + * according to the character encoding used on the body. Either this method + * or {@link #getInputStream()} may be called to read the body, not both. + * + * @return a BufferedReader containing the body of the request + * + * @throws UnsupportedEncodingException + * - if the character set encoding used is not supported and the + * text cannot be decoded + * @throws IllegalStateException + * - if {@link #getInputStream()} method has been called on this + * request + * @throws IOException + * if an input or output exception occurred + * + * @see ServletRequest#getReader() + * @see ClientDataRequest#getReader() + */ + public BufferedReader getReader() throws IOException; + + /** + * Returns the name of the HTTP method with which this request was made, for + * example, GET, POST, or PUT. + * + * @return a String specifying the name of the method with which this + * request was made + * + * @see HttpServletRequest#getMethod() + * @see ClientDataRequest#getMethod() + */ + public String getMethod(); + + /** + * Returns the value of the specified request header as a long value that + * represents a Date object. Use this method with headers that contain + * dates, such as If-Modified-Since. + *

+ * The date is returned as the number of milliseconds since January 1, 1970 + * GMT. The header name is case insensitive. + *

+ * If the request did not have a header of the specified name, this method + * returns -1. If the header can't be converted to a date, the method throws + * an IllegalArgumentException. + * + * @param name + * a String specifying the name of the header + * @return a long value representing the date specified in the header + * expressed as the number of milliseconds since January 1, 1970 + * GMT, or -1 if the named header was not included with the request + * @throws IllegalArgumentException + * If the header value can't be converted to a date + * @see HttpServletRequest#getDateHeader(String) + */ + public long getDateHeader(String name); + + /** + * Returns an enumeration of all the header names this request contains. If + * the request has no headers, this method returns an empty enumeration. + *

+ * Some implementations do not allow access headers using this method, in + * which case this method returns null + * + * @return an enumeration of all the header names sent with this request; if + * the request has no headers, an empty enumeration; if the + * implementation does not allow this method, null + * @see HttpServletRequest#getHeaderNames() + */ + public Enumeration getHeaderNames(); + + /** + * Returns all the values of the specified request header as an Enumeration + * of String objects. + *

+ * Some headers, such as Accept-Language can be sent by clients + * as several headers each with a different value rather than sending the + * header as a comma separated list. + *

+ * If the request did not include any headers of the specified name, this + * method returns an empty Enumeration. If the request does not support + * accessing headers, this method returns null. + *

+ * The header name is case insensitive. You can use this method with any + * request header. + * + * + * @param name + * a String specifying the header name + * @return an Enumeration containing the values of the requested header. If + * the request does not have any headers of that name return an + * empty enumeration. If the header information is not available, + * return null + * @see HttpServletRequest#getHeaders(String) + */ + public Enumeration getHeaders(String name); + } diff --git a/server/src/com/vaadin/server/VaadinResponse.java b/server/src/com/vaadin/server/VaadinResponse.java index d1b2ac1254..5b1df09ab8 100644 --- a/server/src/com/vaadin/server/VaadinResponse.java +++ b/server/src/com/vaadin/server/VaadinResponse.java @@ -25,6 +25,7 @@ import javax.portlet.MimeResponse; import javax.portlet.PortletResponse; import javax.portlet.ResourceResponse; import javax.servlet.ServletResponse; +import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; /** @@ -156,4 +157,16 @@ public interface VaadinResponse extends Serializable { * @see VaadinService */ public VaadinService getService(); + + /** + * Adds the specified cookie to the response. This method can be called + * multiple times to set more than one cookie. + * + * @param cookie + * the Cookie to return to the client + * + * @see HttpServletResponse#addCookie(Cookie) + * @see PortletResponse#addProperty(Cookie) + */ + public void addCookie(Cookie cookie); } diff --git a/server/src/com/vaadin/server/WrappedHttpSession.java b/server/src/com/vaadin/server/WrappedHttpSession.java index 65db010ba9..dbf3761987 100644 --- a/server/src/com/vaadin/server/WrappedHttpSession.java +++ b/server/src/com/vaadin/server/WrappedHttpSession.java @@ -93,4 +93,29 @@ public class WrappedHttpSession implements WrappedSession { return session.getId(); } + @Override + public long getCreationTime() { + return session.getCreationTime(); + } + + @Override + public long getLastAccessedTime() { + return session.getLastAccessedTime(); + } + + @Override + public boolean isNew() { + return session.isNew(); + } + + @Override + public void removeAttribute(String name) { + session.removeAttribute(name); + } + + @Override + public void setMaxInactiveInterval(int interval) { + session.setMaxInactiveInterval(interval); + } + } diff --git a/server/src/com/vaadin/server/WrappedPortletSession.java b/server/src/com/vaadin/server/WrappedPortletSession.java index f4a6003ed5..dd9a1558f8 100644 --- a/server/src/com/vaadin/server/WrappedPortletSession.java +++ b/server/src/com/vaadin/server/WrappedPortletSession.java @@ -79,4 +79,29 @@ public class WrappedPortletSession implements WrappedSession { public String getId() { return session.getId(); } + + @Override + public long getCreationTime() { + return session.getCreationTime(); + } + + @Override + public long getLastAccessedTime() { + return session.getLastAccessedTime(); + } + + @Override + public boolean isNew() { + return session.isNew(); + } + + @Override + public void removeAttribute(String name) { + session.removeAttribute(name); + } + + @Override + public void setMaxInactiveInterval(int interval) { + session.setMaxInactiveInterval(interval); + } } diff --git a/server/src/com/vaadin/server/WrappedSession.java b/server/src/com/vaadin/server/WrappedSession.java index cf0b1a2fbd..49c9d1643c 100644 --- a/server/src/com/vaadin/server/WrappedSession.java +++ b/server/src/com/vaadin/server/WrappedSession.java @@ -91,6 +91,85 @@ public interface WrappedSession { * Gets a string with a unique identifier for the session. * * @return a unique session id string + * + * @see HttpSession#getId() + * @see PortletSession#getId() */ public String getId(); + + /** + * Returns the time when this session was created, measured in milliseconds + * since midnight January 1, 1970 GMT. + * + * @return a long specifying when this session was created, expressed in + * milliseconds since 1/1/1970 GMT + * + * @throws IllegalStateException + * if this method is called on an invalidated session + * @see HttpSession#getCreationTime() + * @see PortletSession#getCreationTime() + */ + public long getCreationTime(); + + /** + * Returns the last time the client sent a request associated with this + * session, as the number of milliseconds since midnight January 1, 1970 + * GMT, and marked by the time the container received the request. + *

+ * Actions that your application takes, such as getting or setting a value + * associated with the session, do not affect the access time. + * + * @return a long representing the last time the client sent a request + * associated with this session, expressed in milliseconds since + * 1/1/1970 GMT + * + * @throws IllegalStateException + * if this method is called on an invalidated session + * + * @see HttpSession#getLastAccessedTime() + * @see PortletSession#getLastAccessedTime() + */ + public long getLastAccessedTime(); + + /** + * Returns true if the client does not yet know about the session or if the + * client chooses not to join the session. For example, if the server used + * only cookie-based sessions, and the client had disabled the use of + * cookies, then a session would be new on each request. + * + * @return true if the server has created a session, but the client has not + * yet joined + * @throws IllegalStateException + * if this method is called on an invalidated session + * @see HttpSession#isNew() + * @see PortletSession#isNew() + */ + public boolean isNew(); + + /** + * Removes the object bound with the specified name from this session. If + * the session does not have an object bound with the specified name, this + * method does nothing. + * + * @param name + * the name of the object to remove from this session + * @throws IllegalStateException + * if this method is called on an invalidated session + * @see HttpSession#removeAttribute(String) + * @see PortletSession#removeAttribute(String) + */ + public void removeAttribute(String name); + + /** + * Specifies the time, in seconds, between client requests before the + * servlet container will invalidate this session. A negative time indicates + * the session should never timeout. + * + * @param interval + * An integer specifying the number of seconds + * @see HttpSession#setMaxInactiveInterval(int) + * @see PortletSession#setMaxInactiveInterval(int) + */ + public void setMaxInactiveInterval(int interval); + } -- 2.39.5