Search in sources :

Example 16 with Location

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

the class LocatableProperties method load.

@Override
public void load(InputStream in) throws IOException {
    try (PropertiesReader pr = new PropertiesReader(new InputStreamReader(in))) {
        while (pr.nextProperty()) {
            String name = pr.getPropertyName();
            String val = pr.getPropertyValue();
            int line = pr.getLineNumber();
            String desc = convertCommentsToString(pr.getCommentLines());
            Location loc = new LocationImpl(desc, location.getURI(), line, 0);
            setProperty(name, val, loc);
        }
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) PropertiesReader(com.opensymphony.xwork2.util.PropertiesReader)

Example 17 with Location

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

the class StrutsJavaConfigurationProvider method register.

@Override
public void register(ContainerBuilder builder, LocatableProperties props) throws ConfigurationException {
    Map<String, Object> loadedBeans = new HashMap<>();
    // bean
    List<BeanConfig> beanConfigs = javaConfig.beans();
    if (beanConfigs != null) {
        for (BeanConfig bc : beanConfigs) {
            if (bc != null) {
                registerBean(loadedBeans, builder, bc);
            }
        }
    }
    // constant
    List<ConstantConfig> constantConfigList = javaConfig.constants();
    if (constantConfigList != null) {
        for (ConstantConfig constantConf : constantConfigList) {
            if (constantConf != null) {
                Map<String, String> constantMap = constantConf.getAllAsStringsMap();
                for (Entry<String, String> entr : constantMap.entrySet()) {
                    if (entr.getKey() != null && entr.getValue() != null) {
                        registerConstant(props, entr.getKey(), entr.getValue());
                    }
                }
            }
        }
    }
    // bean-selection
    javaConfig.beanSelection().ifPresent(beanSelectionConfig -> {
        try {
            LOG.debug("Registering bean selection provider {} of type {}", beanSelectionConfig.getName(), beanSelectionConfig.getClazz().getName());
            BeanSelectionProvider provider = beanSelectionConfig.getClazz().newInstance();
            provider.register(builder, props);
        } catch (IllegalAccessException | InstantiationException e) {
            throw new ConfigurationException("Unable to load : name:" + beanSelectionConfig.getName() + " class:" + beanSelectionConfig.getClazz().getName());
        }
    });
    // unknown-handler-stack
    List<String> unknownHandlers = javaConfig.unknownHandlerStack();
    if (unknownHandlers != null) {
        List<UnknownHandlerConfig> unknownHandlerStack = new ArrayList<>();
        for (String unknownHandler : unknownHandlers) {
            Location location = LocationUtils.getLocation(unknownHandler);
            unknownHandlerStack.add(new UnknownHandlerConfig(unknownHandler, location));
        }
        if (!unknownHandlerStack.isEmpty()) {
            configuration.setUnknownHandlerStack(unknownHandlerStack);
        }
    }
}
Also used : BeanConfig(org.apache.struts2.config.entities.BeanConfig) BeanSelectionProvider(com.opensymphony.xwork2.config.BeanSelectionProvider) HashMap(java.util.HashMap) ArrayList(java.util.ArrayList) UnknownHandlerConfig(com.opensymphony.xwork2.config.entities.UnknownHandlerConfig) ConfigurationException(com.opensymphony.xwork2.config.ConfigurationException) ConstantConfig(org.apache.struts2.config.entities.ConstantConfig) Location(com.opensymphony.xwork2.util.location.Location)

Example 18 with Location

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

the class PortletVelocityResult method executeRenderResult.

/**
 * Creates a Velocity context from the action, loads a Velocity template and
 * executes the template. Output is written to the servlet output stream.
 *
 * @param finalLocation the location of the Velocity template
 * @param invocation an encapsulation of the action execution state.
 * @throws Exception if an error occurs when creating the Velocity context,
 *         loading or executing the template or writing output to the
 *         servlet response stream.
 */
public void executeRenderResult(String finalLocation, ActionInvocation invocation) throws Exception {
    ValueStack stack = ActionContext.getContext().getValueStack();
    HttpServletRequest request = ServletActionContext.getRequest();
    HttpServletResponse response = ServletActionContext.getResponse();
    ServletContext servletContext = ServletActionContext.getServletContext();
    Servlet servlet = JspSupportServlet.jspSupportServlet;
    velocityManager.init(servletContext);
    boolean usedJspFactory = false;
    PageContext pageContext = ActionContext.getContext().getPageContext();
    if (pageContext == null && servlet != null) {
        pageContext = jspFactory.getPageContext(servlet, request, response, null, true, 8192, true);
        ActionContext.getContext().withPageContext(pageContext);
        usedJspFactory = true;
    }
    try {
        String encoding = getEncoding(finalLocation);
        String contentType = getContentType(finalLocation);
        if (encoding != null) {
            contentType = contentType + ";charset=" + encoding;
        }
        response.setContentType(contentType);
        Template t = getTemplate(stack, velocityManager.getVelocityEngine(), invocation, finalLocation, encoding);
        Context context = createContext(velocityManager, stack, request, response, finalLocation);
        Writer writer = new OutputStreamWriter(response.getOutputStream(), encoding);
        t.merge(context, writer);
        // always flush the writer (we used to only flush it if this was a
        // jspWriter, but someone asked
        // to do it all the time (WW-829). Since Velocity support is being
        // deprecated, we'll oblige :)
        writer.flush();
    } catch (Exception e) {
        LOG.error("Unable to render Velocity Template, '" + finalLocation + "'", e);
        throw e;
    } finally {
        if (usedJspFactory) {
            jspFactory.releasePageContext(pageContext);
        }
    }
}
Also used : PageContext(javax.servlet.jsp.PageContext) PortletActionContext(org.apache.struts2.portlet.context.PortletActionContext) Context(org.apache.velocity.context.Context) ActionContext(com.opensymphony.xwork2.ActionContext) ServletActionContext(org.apache.struts2.ServletActionContext) ServletContext(javax.servlet.ServletContext) ValueStack(com.opensymphony.xwork2.util.ValueStack) HttpServletResponse(javax.servlet.http.HttpServletResponse) Template(org.apache.velocity.Template) HttpServletRequest(javax.servlet.http.HttpServletRequest) ServletContext(javax.servlet.ServletContext) JspSupportServlet(org.apache.struts2.views.JspSupportServlet) Servlet(javax.servlet.Servlet) PageContext(javax.servlet.jsp.PageContext) OutputStreamWriter(java.io.OutputStreamWriter) OutputStreamWriter(java.io.OutputStreamWriter) Writer(java.io.Writer)

Example 19 with Location

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

the class ServletActionRedirectResultTest method testBuildResultWithParameter.

public void testBuildResultWithParameter() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", ServletActionRedirectResult.class.getName()).addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("param1", "value 1").addParam("param2", "value 2").addParam("param3", "value 3").addParam("anchor", "fragment").build();
    ObjectFactory factory = container.getInstance(ObjectFactory.class);
    ServletActionRedirectResult result = (ServletActionRedirectResult) factory.buildResult(resultConfig, ActionContext.getContext().getContextMap());
    assertNotNull(result);
}
Also used : ResultConfig(com.opensymphony.xwork2.config.entities.ResultConfig) ObjectFactory(com.opensymphony.xwork2.ObjectFactory)

Example 20 with Location

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

the class ServletActionRedirectResultTest method testExpressionParameterInResultWithConditionParseOn.

public void testExpressionParameterInResultWithConditionParseOn() throws Exception {
    ResultConfig resultConfig = new ResultConfig.Builder("", "").addParam("actionName", "someActionName").addParam("namespace", "someNamespace").addParam("encode", "true").addParam("parse", "true").addParam("location", "someLocation").addParam("prependServletContext", "true").addParam("method", "someMethod").addParam("statusCode", "333").addParam("param1", "${#value1}").addParam("param2", "${#value2}").addParam("param3", "${#value3}").addParam("anchor", "${#fragment}").build();
    ActionContext context = ActionContext.getContext();
    ValueStack stack = context.getValueStack();
    context.getContextMap().put("value1", "value 1");
    context.getContextMap().put("value2", "value 2");
    context.getContextMap().put("value3", "value 3");
    context.getContextMap().put("namespaceName", "${1-1}");
    context.getContextMap().put("actionName", "${1-1}");
    context.getContextMap().put("methodName", "${1-1}");
    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();
    ServletActionRedirectResult result = new ServletActionRedirectResult();
    result.setNamespace("/myNamespace${#namespaceName}");
    result.setActionName("myAction${#actionName}");
    result.setMethod("myMethod${#methodName}");
    result.setParse(true);
    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).anyTimes();
    expect(mockInvocation.getResultCode()).andReturn("myResult").anyTimes();
    expect(mockActionProxy.getConfig()).andReturn(actionConfig).anyTimes();
    expect(mockInvocation.getInvocationContext()).andReturn(context).anyTimes();
    expect(mockInvocation.getStack()).andReturn(stack).anyTimes();
    control.replay();
    result.setActionMapper(container.getInstance(ActionMapper.class));
    result.execute(mockInvocation);
    assertEquals("/myNamespace${1-1}/myAction${1-1}!myMethod${1-1}.action?param1=value+1&param2=value+2&param3=value+3#fragment", res.getRedirectedUrl());
    req = new MockHttpServletRequest();
    res = new MockHttpServletResponse();
    context.put(ServletActionContext.HTTP_REQUEST, req);
    context.put(ServletActionContext.HTTP_RESPONSE, res);
    result.execute(mockInvocation);
    assertEquals("/myNamespace0/myAction0!myMethod0.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) ValueStack(com.opensymphony.xwork2.util.ValueStack) HashMap(java.util.HashMap) MockHttpServletRequest(org.springframework.mock.web.MockHttpServletRequest) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) ServletActionContext(org.apache.struts2.ServletActionContext) ActionContext(com.opensymphony.xwork2.ActionContext) IMocksControl(org.easymock.IMocksControl) ActionMapper(org.apache.struts2.dispatcher.mapper.ActionMapper) DefaultUrlHelper(org.apache.struts2.views.util.DefaultUrlHelper) MockHttpServletResponse(org.springframework.mock.web.MockHttpServletResponse)

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