Search in sources :

Example 21 with Interceptor

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

the class ServletUrlRenderer method renderFormUrl.

/**
 * {@inheritDoc}
 */
@Override
public void renderFormUrl(Form formComponent) {
    String namespace = formComponent.determineNamespace(formComponent.namespace, formComponent.getStack(), formComponent.request);
    String action;
    ValueStack vs = ActionContext.getContext().getValueStack();
    String scheme = vs.findString("scheme");
    if (formComponent.action != null) {
        action = formComponent.findString(formComponent.action);
    } else {
        // no action supplied? ok, then default to the current request
        // (action or general URL)
        ActionInvocation ai = formComponent.getStack().getActionContext().getActionInvocation();
        if (ai != null) {
            action = ai.getProxy().getActionName();
            namespace = ai.getProxy().getNamespace();
        } else {
            // hmm, ok, we need to just assume the current URL cut down
            String uri = formComponent.request.getRequestURI();
            action = uri.substring(uri.lastIndexOf('/'));
        }
    }
    Map actionParams = null;
    if (action != null && action.indexOf('?') > 0) {
        String queryString = action.substring(action.indexOf('?') + 1);
        actionParams = urlHelper.parseQueryString(queryString, false);
        action = action.substring(0, action.indexOf('?'));
    }
    ActionMapping nameMapping = actionMapper.getMappingFromActionName(action);
    String actionName = nameMapping.getName();
    String actionMethod = nameMapping.getMethod();
    final ActionConfig actionConfig = formComponent.configuration.getRuntimeConfiguration().getActionConfig(namespace, actionName);
    if (actionConfig != null) {
        ActionMapping mapping = new ActionMapping(actionName, namespace, actionMethod, formComponent.parameters);
        String result = urlHelper.buildUrl(formComponent.actionMapper.getUriFromActionMapping(mapping), formComponent.request, formComponent.response, actionParams, scheme, formComponent.includeContext, true, false, false);
        formComponent.addParameter("action", result);
        // let's try to get the actual action class and name
        // this can be used for getting the list of validators
        formComponent.addParameter("actionName", actionName);
        try {
            Class clazz = formComponent.objectFactory.getClassInstance(actionConfig.getClassName());
            formComponent.addParameter("actionClass", clazz);
        } catch (ClassNotFoundException e) {
        // this is OK, we'll just move on
        }
        formComponent.addParameter("namespace", namespace);
        // if the name isn't specified, use the action name
        if (formComponent.name == null) {
            formComponent.addParameter("name", actionName);
        }
        // if the id isn't specified, use the action name
        if (formComponent.getId() == null && actionName != null) {
            String escapedId = formComponent.escape(actionName);
            formComponent.addParameter("id", escapedId);
            formComponent.addParameter("escapedId", escapedId);
        }
    } else if (action != null) {
        // was not found in the configuration.
        if (namespace != null && LOG.isWarnEnabled()) {
            LOG.warn("No configuration found for the specified action: '{}' in namespace: '{}'. Form action defaulting to 'action' attribute's literal value.", actionName, namespace);
        }
        String result = urlHelper.buildUrl(action, formComponent.request, formComponent.response, actionParams, scheme, formComponent.includeContext, true);
        formComponent.addParameter("action", result);
        // namespace: cut out anything between the start and the last /
        int slash = result.lastIndexOf('/');
        if (slash != -1) {
            formComponent.addParameter("namespace", result.substring(0, slash));
        } else {
            formComponent.addParameter("namespace", "");
        }
        // name/id: cut out anything between / and . should be the id and
        // name
        String id = formComponent.getId();
        if (id == null) {
            slash = result.lastIndexOf('/');
            int dot = result.indexOf('.', slash);
            if (dot != -1) {
                id = result.substring(slash + 1, dot);
            } else {
                id = result.substring(slash + 1);
            }
            String escapedId = formComponent.escape(id);
            formComponent.addParameter("id", escapedId);
            formComponent.addParameter("escapedId", escapedId);
        }
    }
    // WW-1284
    // evaluate if client-side js is to be enabled. (if validation
    // interceptor does allow validation eg. method is not filtered out)
    formComponent.evaluateClientSideJsEnablement(actionName, namespace, actionMethod);
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 22 with Interceptor

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

the class MessageStoreInterceptorTest method testRequestOperationMode1.

public void testRequestOperationMode1() {
    Map<String, Object> paramMap = new LinkedHashMap<>();
    paramMap.put("operationMode", new String[] { MessageStoreInterceptor.RETRIEVE_MODE });
    ActionContext actionContext = ActionContext.of(new HashMap<>()).bind();
    actionContext.setParameters(HttpParameters.create(paramMap).build());
    ActionInvocation mockActionInvocation = EasyMock.createControl().createMock(ActionInvocation.class);
    mockActionInvocation.getInvocationContext();
    EasyMock.expectLastCall().andReturn(actionContext);
    EasyMock.expectLastCall().anyTimes();
    EasyMock.replay(mockActionInvocation);
    MessageStoreInterceptor interceptor = new MessageStoreInterceptor();
    String operationMode = interceptor.getRequestOperationMode(mockActionInvocation);
    assertEquals(operationMode, MessageStoreInterceptor.RETRIEVE_MODE);
    EasyMock.verify(mockActionInvocation);
}
Also used : HashMap(java.util.HashMap) LinkedHashMap(java.util.LinkedHashMap) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap)

Example 23 with Interceptor

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

the class MultiselectInterceptorTest method setUp.

protected void setUp() throws Exception {
    super.setUp();
    param = new HashMap<>();
    interceptor = new MultiselectInterceptor();
    ai = new MockActionInvocation();
    ai.setInvocationContext(ActionContext.getContext());
    ActionContext.getContext().setParameters(HttpParameters.create(param).build());
}
Also used : MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation)

Example 24 with Interceptor

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

the class AnnotationValidationInterceptorTest method setUp.

public void setUp() throws Exception {
    super.setUp();
    test = new TestAction();
    interceptor = new AnnotationValidationInterceptor();
    container.inject(interceptor);
    config = new ActionConfig.Builder("", "foo", "").build();
    mockActionInvocation = new Mock(ActionInvocation.class);
    mockActionProxy = new Mock(ActionProxy.class);
    mockActionInvocation.matchAndReturn("getProxy", (ActionProxy) mockActionProxy.proxy());
    mockActionInvocation.matchAndReturn("getAction", test);
    mockActionInvocation.expect("invoke");
    ActionContext.getContext().withActionInvocation((ActionInvocation) mockActionInvocation.proxy());
}
Also used : ActionProxy(com.opensymphony.xwork2.ActionProxy) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) Mock(com.mockobjects.dynamic.Mock)

Example 25 with Interceptor

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

the class CspInterceptorTest method setUp.

@Override
protected void setUp() throws Exception {
    super.setUp();
    container.inject(interceptor);
    ServletActionContext.setRequest(request);
    ServletActionContext.setResponse(response);
    ActionContext context = ServletActionContext.getActionContext().bind();
    context.withSession(session);
    mai.setInvocationContext(context);
}
Also used : ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext)

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