WW-4037 Adds functionality to create Cookies in action and apply them to response

git-svn-id: https://svn.apache.org/repos/asf/struts/struts2/trunk@1485719 13f79535-47bb-0310-9956-ffa450edef68
This commit is contained in:
Lukasz Lenart
2013-05-23 14:12:24 +00:00
parent b7907fa602
commit 9d6e6a6b0f
4 changed files with 212 additions and 0 deletions
@@ -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();
}
@@ -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 -->
*
* &lt;action ... &gt;
* &lt;interceptor-ref name="defaultStack"/&gt;
* &lt;interceptor-ref name="cookieProvider"/&gt;
* ...
* &lt;/action&gt;
*
* <!-- 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);
}
}
}
@@ -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" />
@@ -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);
}
}