Search in sources :

Example 56 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class ServletRedirectResultTest method testIncludeParameterInResult.

public void testIncludeParameterInResult() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").build();
    ActionContext context = ActionContext.getContext();
    MockHttpServletRequest req = new MockHttpServletRequest();
    MockHttpServletResponse res = new MockHttpServletResponse();
    context.put(ServletActionContext.HTTP_REQUEST, req);
    context.put(ServletActionContext.HTTP_RESPONSE, res);
    Map<String, ResultConfig> results = new HashMap<>();
    results.put("myResult", resultConfig);
    ActionConfig actionConfig = new ActionConfig.Builder("", "", "").addResultConfigs(results).build();
    ServletRedirectResult result = new ServletRedirectResult();
    result.setLocation("/myNamespace/myAction.action");
    result.setParse(false);
    result.setEncode(false);
    result.setPrependServletContext(false);
    result.setAnchor("fragment");
    result.setUrlHelper(new DefaultUrlHelper());
    IMocksControl control = createControl();
    ActionProxy mockActionProxy = control.createMock(ActionProxy.class);
    ActionInvocation mockInvocation = control.createMock(ActionInvocation.class);
    expect(mockInvocation.getProxy()).andReturn(mockActionProxy);
    expect(mockInvocation.getResultCode()).andReturn("myResult");
    expect(mockActionProxy.getConfig()).andReturn(actionConfig);
    expect(mockInvocation.getInvocationContext()).andReturn(context);
    control.replay();
    result.setActionMapper(container.getInstance(ActionMapper.class));
    result.execute(mockInvocation);
    assertEquals("/myNamespace/myAction.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    control.verify();
}
Also used : ActionConfig(com.opensymphony.xwork2.config.entities.ActionConfig) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ActionProxy(com.opensymphony.xwork2.ActionProxy) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) MockActionInvocation(com.opensymphony.xwork2.mock.MockActionInvocation) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

Example 57 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class StrutsResultSupportTest method testNoParseAndEncode.

public void testNoParseAndEncode() throws Exception {
    ValueStack stack = ActionContext.getContext().getValueStack();
    stack.push(new ActionSupport() {

        public String getMyLocation() {
            return "myLocation.jsp";
        }
    });
    ActionInvocation mockActionInvocation = EasyMock.createNiceMock(ActionInvocation.class);
    EasyMock.replay(mockActionInvocation);
    InternalStrutsResultSupport result = new InternalStrutsResultSupport();
    result.setParse(false);
    // don't really need this, as encode is only valid when parse is true.
    result.setEncode(false);
    result.setLocation("/pages/myJsp.jsp?location=${myLocation}");
    result.execute(mockActionInvocation);
    assertNotNull(result.getInternalLocation());
    assertEquals("/pages/myJsp.jsp?location=${myLocation}", result.getInternalLocation());
    EasyMock.verify(mockActionInvocation);
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ActionSupport(com.opensymphony.xwork2.ActionSupport)

Example 58 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class PostbackResult method makePostbackUri.

protected String makePostbackUri(ActionInvocation invocation) {
    ActionContext ctx = invocation.getInvocationContext();
    HttpServletRequest request = ctx.getServletRequest();
    String postbackUri;
    if (actionName != null) {
        actionName = conditionalParse(actionName, invocation);
        parseLocation = false;
        if (namespace == null) {
            namespace = invocation.getProxy().getNamespace();
        } else {
            namespace = conditionalParse(namespace, invocation);
        }
        if (method == null) {
            method = "";
        } else {
            method = conditionalParse(method, invocation);
        }
        postbackUri = request.getContextPath() + actionMapper.getUriFromActionMapping(new ActionMapping(actionName, namespace, method, null));
    } else {
        String location = getLocation();
        // Do not prepend if the URL is a FQN
        if (!location.matches("^([a-zA-Z]+:)?//.*")) {
            // If the URL is relative to the servlet context, prepend the servlet context path
            if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) {
                location = request.getContextPath() + location;
            }
        }
        postbackUri = location;
    }
    return postbackUri;
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ActionContext(com.opensymphony.xwork2.ActionContext)

Example 59 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class ServletRedirectResult method doExecute.

/**
 * Redirects to the location specified by calling
 * {@link HttpServletResponse#sendRedirect(String)}.
 *
 * @param finalLocation the location to redirect to.
 * @param invocation    an encapsulation of the action execution state.
 * @throws Exception if an error occurs when redirecting.
 */
protected void doExecute(String finalLocation, ActionInvocation invocation) throws Exception {
    ActionContext ctx = invocation.getInvocationContext();
    HttpServletRequest request = ctx.getServletRequest();
    HttpServletResponse response = ctx.getServletResponse();
    if (isPathUrl(finalLocation)) {
        if (!finalLocation.startsWith("/")) {
            ActionMapping mapping = actionMapper.getMapping(request, Dispatcher.getInstance().getConfigurationManager());
            String namespace = null;
            if (mapping != null) {
                namespace = mapping.getNamespace();
            }
            if ((namespace != null) && (namespace.length() > 0) && (!"/".equals(namespace))) {
                finalLocation = namespace + "/" + finalLocation;
            } else {
                finalLocation = "/" + finalLocation;
            }
        }
        // if the URL's are relative to the servlet context, append the servlet context path
        if (prependServletContext && (request.getContextPath() != null) && (request.getContextPath().length() > 0)) {
            finalLocation = request.getContextPath() + finalLocation;
        }
    }
    ResultConfig resultConfig = invocation.getProxy().getConfig().getResults().get(invocation.getResultCode());
    if (resultConfig != null) {
        Map<String, String> resultConfigParams = resultConfig.getParams();
        List<String> prohibitedResultParams = getProhibitedResultParams();
        for (Map.Entry<String, String> e : resultConfigParams.entrySet()) {
            if (!prohibitedResultParams.contains(e.getKey())) {
                Collection<String> values = conditionalParseCollection(e.getValue(), invocation, suppressEmptyParameters);
                if (!suppressEmptyParameters || !values.isEmpty()) {
                    requestParameters.put(e.getKey(), values);
                }
            }
        }
    }
    StringBuilder tmpLocation = new StringBuilder(finalLocation);
    urlHelper.buildParametersString(requestParameters, tmpLocation, "&");
    // add the anchor
    if (anchor != null) {
        tmpLocation.append('#').append(anchor);
    }
    finalLocation = response.encodeRedirectURL(tmpLocation.toString());
    LOG.debug("Redirecting to finalLocation: {}", finalLocation);
    sendRedirect(response, finalLocation);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) ActionMapping(org.apache.struts2.dispatcher.mapper.ActionMapping) ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) HttpServletResponse(javax.servlet.http.HttpServletResponse) ActionContext(com.opensymphony.xwork2.ActionContext) LinkedHashMap(java.util.LinkedHashMap) Map(java.util.Map)

Example 60 with Location

use of com.opensymphony.xwork2.util.location.Location in project struts by apache.

the class StrutsJavaConfigurationProvider method registerBean.

private void registerBean(Map<String, Object> loadedBeans, ContainerBuilder containerBuilder, BeanConfig beanConf) {
    try {
        if (beanConf.isOnlyStatic()) {
            // Force loading of class to detect no class def found
            // exceptions
            beanConf.getClazz().getDeclaredClasses();
            containerBuilder.injectStatics(beanConf.getClazz());
        } else {
            if (containerBuilder.contains(beanConf.getType(), beanConf.getName())) {
                Location loc = LocationUtils.getLocation(loadedBeans.get(beanConf.getType().getName() + beanConf.getName()));
                if (throwExceptionOnDuplicateBeans) {
                    throw new ConfigurationException("Bean type " + beanConf.getType() + " with the name " + beanConf.getName() + " has already been loaded by " + loc, javaConfig);
                }
            }
            // Force loading of class to detect no class def found
            // exceptions
            beanConf.getClazz().getDeclaredConstructors();
            LOG.debug("Loaded type: {} name: {} clazz: {}", beanConf.getType(), beanConf.getName(), beanConf.getClazz());
            containerBuilder.factory(beanConf.getType(), beanConf.getName(), new LocatableFactory(beanConf.getName(), beanConf.getType(), beanConf.getClazz(), beanConf.getScope(), javaConfig), beanConf.getScope());
        }
        loadedBeans.put(beanConf.getType().getName() + beanConf.getName(), javaConfig);
    } catch (Throwable ex) {
        if (!beanConf.isOptional()) {
            throw new ConfigurationException("Unable to load bean: type:" + beanConf.getType() + " class:" + beanConf.getClazz(), ex);
        } else {
            LOG.debug("Unable to load optional class: {}", beanConf.getClazz());
        }
    }
}
Also used : ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) LocatableFactory(com.opensymphony.xwork2.config.impl.LocatableFactory) Location(com.opensymphony.xwork2.util.location.Location)

Aggregations

ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)32 ServletContext (javax.servlet.ServletContext)22 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)21 HashSet (java.util.HashSet)15 ConfigurationException (com.opensymphony.xwork2.config.ConfigurationException)11 ActionContext (com.opensymphony.xwork2.ActionContext)10 ValueStack (com.opensymphony.xwork2.util.ValueStack)10 HashMap (java.util.HashMap)9 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)8 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)8 ServletActionContext (org.apache.struts2.ServletActionContext)8 ResultTypeConfig (com.opensymphony.xwork2.config.entities.ResultTypeConfig)7 Location (com.opensymphony.xwork2.util.location.Location)7 ArrayList (java.util.ArrayList)7 NoAnnotationAction (org.apache.struts2.convention.actions.NoAnnotationAction)7 ClassLevelResultPathAction (org.apache.struts2.convention.actions.resultpath.ClassLevelResultPathAction)6 Action (org.apache.struts2.convention.annotation.Action)6 NodeList (org.w3c.dom.NodeList)6 ActionProxy (com.opensymphony.xwork2.ActionProxy)5 HttpServletRequest (javax.servlet.http.HttpServletRequest)5