Author: lukaszlenart
Date: Thu May 23 14:12:24 2013
New Revision: 1485719
URL: http://svn.apache.org/r1485719
Log:
WW-4037 Adds functionality to create Cookies in action and apply them to
response
Added:
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProvider.java
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProviderInterceptor.java
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieProviderInterceptorTest.java
Modified:
struts/struts2/trunk/core/src/main/resources/struts-default.xml
Added:
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProvider.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProvider.java?rev=1485719&view=auto
==============================================================================
---
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProvider.java
(added)
+++
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProvider.java
Thu May 23 14:12:24 2013
@@ -0,0 +1,41 @@
+/*
+ * $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.interceptor;
+
+import javax.servlet.http.Cookie;
+import java.util.Set;
+
+/**
+ * Action can create cookies which will be stored in response
+ *
+ * @see CookieProviderInterceptor
+ */
+public interface CookieProvider {
+
+ /**
+ * Return cookies which should be send to client
+ *
+ * @return set of cookies or null
+ */
+ Set<Cookie> getCookies();
+
+}
\ No newline at end of file
Added:
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProviderInterceptor.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProviderInterceptor.java?rev=1485719&view=auto
==============================================================================
---
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProviderInterceptor.java
(added)
+++
struts/struts2/trunk/core/src/main/java/org/apache/struts2/interceptor/CookieProviderInterceptor.java
Thu May 23 14:12:24 2013
@@ -0,0 +1,115 @@
+/*
+ * $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.interceptor;
+
+import com.opensymphony.xwork2.ActionContext;
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.interceptor.AbstractInterceptor;
+import com.opensymphony.xwork2.interceptor.PreResultListener;
+import com.opensymphony.xwork2.util.logging.Logger;
+import com.opensymphony.xwork2.util.logging.LoggerFactory;
+import org.apache.struts2.StrutsStatics;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+import java.util.Set;
+
+/**
+ * <!-- START SNIPPET: description -->
+ * Allows actions to send cookies to client, action must implement {@link
CookieProvider}
+ * You must reference this interceptor in your default stack or in action's
stack, see example below.
+ *
+ * <!-- END SNIPPET: description -->
+ *
+ * <!-- START SNIPPET: parameters -->
+ *
+ * none
+ *
+ * <!-- END SNIPPET: parameters -->
+ *
+ * <!-- START SNIPPET: extending -->
+ *
+ * <ul>
+ * <li>addCookiesToResponse - this method applies cookie created by action
to response</li>
+ * </ul>
+ *
+ * <!-- END SNIPPET: extending -->
+ *
+ * <pre>
+ * <!-- START SNIPPET: example -->
+ *
+ * <action ... >
+ * <interceptor-ref name="defaultStack"/>
+ * <interceptor-ref name="cookieProvider"/>
+ * ...
+ * </action>
+ *
+ * <!-- END SNIPPET: example -->
+ * </pre>
+ */
+public class CookieProviderInterceptor extends AbstractInterceptor implements
PreResultListener {
+
+ private static final Logger LOG =
LoggerFactory.getLogger(CookieProviderInterceptor.class);
+
+ public String intercept(ActionInvocation invocation) throws Exception {
+ invocation.addPreResultListener(this);
+ return invocation.invoke();
+ }
+
+ /**
+ * Do what name suggests
+ *
+ * @param action {@link CookieProvider} action
+ * @param response current {@link HttpServletResponse}
+ */
+ protected void addCookiesToResponse(CookieProvider action,
HttpServletResponse response) {
+ Set<Cookie> cookies = action.getCookies();
+ if (cookies != null) {
+ for (Cookie cookie : cookies) {
+ if (LOG.isDebugEnabled()) {
+ LOG.debug("Sending cookie [#0] with value [#1] for domain
[#2]",
+ cookie.getName(), cookie.getValue(),
(cookie.getDomain() != null ? cookie.getDomain() : "no domain"));
+ }
+ response.addCookie(cookie);
+ }
+ }
+ }
+
+ public void beforeResult(ActionInvocation invocation, String resultCode) {
+ try {
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("beforeResult start");
+ }
+ ActionContext ac = invocation.getInvocationContext();
+ if (invocation.getAction() instanceof CookieProvider) {
+ HttpServletResponse response = (HttpServletResponse)
ac.get(StrutsStatics.HTTP_RESPONSE);
+ addCookiesToResponse((CookieProvider) invocation.getAction(),
response);
+ }
+ if (LOG.isTraceEnabled()) {
+ LOG.trace("beforeResult end");
+ }
+ } catch (Exception ex) {
+ LOG.error("Unable to setup cookies", ex);
+ }
+ }
+
+}
Modified: struts/struts2/trunk/core/src/main/resources/struts-default.xml
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/main/resources/struts-default.xml?rev=1485719&r1=1485718&r2=1485719&view=diff
==============================================================================
--- struts/struts2/trunk/core/src/main/resources/struts-default.xml (original)
+++ struts/struts2/trunk/core/src/main/resources/struts-default.xml Thu May 23
14:12:24 2013
@@ -145,6 +145,7 @@
<interceptor name="chain"
class="com.opensymphony.xwork2.interceptor.ChainingInterceptor"/>
<interceptor name="conversionError"
class="org.apache.struts2.interceptor.StrutsConversionErrorInterceptor"/>
<interceptor name="cookie"
class="org.apache.struts2.interceptor.CookieInterceptor"/>
+ <interceptor name="cookieProvider"
class="org.apache.struts2.interceptor.CookieProviderInterceptor"/>
<interceptor name="clearSession"
class="org.apache.struts2.interceptor.ClearSessionInterceptor" />
<interceptor name="createSession"
class="org.apache.struts2.interceptor.CreateSessionInterceptor" />
<interceptor name="debugging"
class="org.apache.struts2.interceptor.debugging.DebuggingInterceptor" />
Added:
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieProviderInterceptorTest.java
URL:
http://svn.apache.org/viewvc/struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieProviderInterceptorTest.java?rev=1485719&view=auto
==============================================================================
---
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieProviderInterceptorTest.java
(added)
+++
struts/struts2/trunk/core/src/test/java/org/apache/struts2/interceptor/CookieProviderInterceptorTest.java
Thu May 23 14:12:24 2013
@@ -0,0 +1,55 @@
+package org.apache.struts2.interceptor;
+
+import com.opensymphony.xwork2.ActionInvocation;
+import com.opensymphony.xwork2.XWorkTestCase;
+import org.easymock.EasyMock;
+
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServletResponse;
+import java.util.HashSet;
+import java.util.Set;
+
+public class CookieProviderInterceptorTest extends XWorkTestCase {
+
+ public void testPreResultListenerAddition() throws Exception {
+ // given
+ ActionInvocation invocation =
EasyMock.createNiceMock(ActionInvocation.class);
+ CookieProviderInterceptor interceptor = new
CookieProviderInterceptor();
+
+ invocation.addPreResultListener(interceptor);
+
+ EasyMock.replay(invocation);
+
+ // when
+ interceptor.intercept(invocation);
+
+ // then
+ EasyMock.verify(invocation);
+ }
+
+ public void testCookieCreation() throws Exception {
+ // given
+ CookieProviderInterceptor interceptor = new
CookieProviderInterceptor();
+
+ final Cookie cookie = new Cookie("foo", "bar");
+
+ CookieProvider action = new CookieProvider() {
+ public Set<Cookie> getCookies() {
+ Set<Cookie> cookies = new HashSet<Cookie>();
+ cookies.add(cookie);
+ return cookies;
+ }
+ };
+
+ HttpServletResponse response =
EasyMock.createNiceMock(HttpServletResponse.class);
+ response.addCookie(cookie);
+ EasyMock.replay(response);
+
+ // when
+ interceptor.addCookiesToResponse(action, response);
+
+ // then
+ EasyMock.verify(response);
+ }
+
+}