Search in sources :

Example 36 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CookieInterceptorTest method testInterceptAll1.

public void testInterceptAll1() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(new Cookie("cookie1", "cookie1value"), new Cookie("cookie2", "cookie2value"), new Cookie("cookie3", "cookie3value"));
    ServletActionContext.setRequest(request);
    MockActionWithCookieAware action = new MockActionWithCookieAware();
    ActionContext.getContext().getValueStack().push(action);
    ActionInvocation invocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(invocation.getAction()).andReturn(action);
    expect(invocation.invoke()).andReturn(Action.SUCCESS);
    replay(invocation);
    CookieInterceptor interceptor = new CookieInterceptor();
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("*");
    interceptor.setCookiesValue("*");
    interceptor.intercept(invocation);
    assertFalse(action.getCookiesMap().isEmpty());
    assertEquals(action.getCookiesMap().size(), 3);
    assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
    assertEquals(action.getCookiesMap().get("cookie2"), "cookie2value");
    assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
    assertEquals(action.getCookie1(), "cookie1value");
    assertEquals(action.getCookie2(), "cookie2value");
    assertEquals(action.getCookie3(), "cookie3value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), "cookie2value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
    verify(invocation);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

Example 37 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CookieInterceptorTest method testInterceptSelectedCookiesNameOnly2.

public void testInterceptSelectedCookiesNameOnly2() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(new Cookie("cookie1", "cookie1value"), new Cookie("cookie2", "cookie2value"), new Cookie("cookie3", "cookie3value"));
    ServletActionContext.setRequest(request);
    MockActionWithCookieAware action = new MockActionWithCookieAware();
    ActionContext.getContext().getValueStack().push(action);
    ActionInvocation invocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(invocation.getAction()).andReturn(action);
    expect(invocation.invoke()).andReturn(Action.SUCCESS);
    replay(invocation);
    CookieInterceptor interceptor = new CookieInterceptor();
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("cookie1, cookie3");
    interceptor.setCookiesValue("*");
    interceptor.intercept(invocation);
    assertFalse(action.getCookiesMap().isEmpty());
    assertEquals(action.getCookiesMap().size(), 2);
    assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
    assertEquals(action.getCookiesMap().get("cookie2"), null);
    assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
    assertEquals(action.getCookie1(), "cookie1value");
    assertEquals(action.getCookie2(), null);
    assertEquals(action.getCookie3(), "cookie3value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
    verify(invocation);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

Example 38 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CookieInterceptorTest method testInterceptSelectedCookiesNameOnly3.

public void testInterceptSelectedCookiesNameOnly3() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    request.setCookies(new Cookie("cookie1", "cookie1value"), new Cookie("cookie2", "cookie2value"), new Cookie("cookie3", "cookie3value"));
    ServletActionContext.setRequest(request);
    MockActionWithCookieAware action = new MockActionWithCookieAware();
    ActionContext.getContext().getValueStack().push(action);
    ActionInvocation invocation = (ActionInvocation) createMock(ActionInvocation.class);
    expect(invocation.getAction()).andReturn(action);
    expect(invocation.invoke()).andReturn(Action.SUCCESS);
    replay(invocation);
    CookieInterceptor interceptor = new CookieInterceptor();
    interceptor.setExcludedPatternsChecker(new DefaultExcludedPatternsChecker());
    interceptor.setAcceptedPatternsChecker(new DefaultAcceptedPatternsChecker());
    interceptor.setCookiesName("cookie1, cookie3");
    interceptor.setCookiesValue("");
    interceptor.intercept(invocation);
    assertFalse(action.getCookiesMap().isEmpty());
    assertEquals(action.getCookiesMap().size(), 2);
    assertEquals(action.getCookiesMap().get("cookie1"), "cookie1value");
    assertEquals(action.getCookiesMap().get("cookie2"), null);
    assertEquals(action.getCookiesMap().get("cookie3"), "cookie3value");
    assertEquals(action.getCookie1(), "cookie1value");
    assertEquals(action.getCookie2(), null);
    assertEquals(action.getCookie3(), "cookie3value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie1"), "cookie1value");
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie2"), null);
    assertEquals(ActionContext.getContext().getValueStack().findValue("cookie3"), "cookie3value");
    verify(invocation);
}
Also used : Cookie(javax.servlet.http.Cookie) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) DefaultAcceptedPatternsChecker(com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker) DefaultExcludedPatternsChecker(com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)

Example 39 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class CookieProviderInterceptorTest method testPreResultListenerAddition.

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);
}
Also used : ActionInvocation(com.opensymphony.xwork2.ActionInvocation)

Example 40 with Interceptor

use of com.opensymphony.xwork2.interceptor.Interceptor in project struts by apache.

the class ScopeInterceptor method beforeResult.

/* (non-Javadoc)
     * @see com.opensymphony.xwork2.interceptor.PreResultListener#beforeResult(com.opensymphony.xwork2.ActionInvocation, java.lang.String)
     */
public void beforeResult(ActionInvocation invocation, String resultCode) {
    String key = getKey(invocation);
    Map<String, Object> application = ActionContext.getContext().getApplication();
    final ValueStack stack = ActionContext.getContext().getValueStack();
    if (this.application != null)
        for (String string : this.application) {
            Object value = stack.findValue(string);
            LOG.debug("Application scoped variable saved {} = {}", string, String.valueOf(value));
            // if( value != null)
            application.put(key + string, nullConvert(value));
        }
    boolean ends = "end".equals(type);
    Map<String, Object> session = ActionContext.getContext().getSession();
    if (session != null) {
        if (this.session != null) {
            for (String string : this.session) {
                if (ends) {
                    session.remove(key + string);
                } else {
                    Object value = stack.findValue(string);
                    LOG.debug("Session scoped variable saved {} = {}", string, String.valueOf(value));
                    // Null value should be scoped too
                    // if( value != null)
                    session.put(key + string, nullConvert(value));
                }
            }
        }
        unlock(session);
    } else {
        LOG.debug("No HttpSession created... Cannot save session scoped variables.");
    }
    LOG.debug("scope interceptor after (before result)");
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Aggregations

ActionInvocation (com.opensymphony.xwork2.ActionInvocation)32 HashMap (java.util.HashMap)28 ActionContext (com.opensymphony.xwork2.ActionContext)20 InterceptorMapping (com.opensymphony.xwork2.config.entities.InterceptorMapping)19 MockActionInvocation (com.opensymphony.xwork2.mock.MockActionInvocation)19 ServletActionContext (org.apache.struts2.ServletActionContext)14 InterceptorStackConfig (com.opensymphony.xwork2.config.entities.InterceptorStackConfig)13 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)12 LinkedHashMap (java.util.LinkedHashMap)12 Map (java.util.Map)11 MockHttpServletRequest (org.springframework.mock.web.MockHttpServletRequest)11 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)10 InterceptorConfig (com.opensymphony.xwork2.config.entities.InterceptorConfig)10 DefaultAcceptedPatternsChecker (com.opensymphony.xwork2.security.DefaultAcceptedPatternsChecker)10 DefaultExcludedPatternsChecker (com.opensymphony.xwork2.security.DefaultExcludedPatternsChecker)10 Cookie (javax.servlet.http.Cookie)10 ArrayList (java.util.ArrayList)9 List (java.util.List)9 Mock (com.mockobjects.dynamic.Mock)8 ActionSupport (com.opensymphony.xwork2.ActionSupport)8