Search in sources :

Example 71 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class ServletUrlRenderer method renderUrl.

/**
 * {@inheritDoc}
 */
@Override
public void renderUrl(Writer writer, UrlProvider urlComponent) {
    String scheme = urlComponent.getHttpServletRequest().getScheme();
    if (urlComponent.getScheme() != null) {
        ValueStack vs = ActionContext.getContext().getValueStack();
        scheme = vs.findString(urlComponent.getScheme());
        if (scheme == null) {
            scheme = urlComponent.getScheme();
        }
    }
    String result;
    ActionInvocation ai = ActionContext.getContext().getActionInvocation();
    if (urlComponent.getValue() == null && urlComponent.getAction() != null) {
        result = urlComponent.determineActionURL(urlComponent.getAction(), urlComponent.getNamespace(), urlComponent.getMethod(), urlComponent.getHttpServletRequest(), urlComponent.getHttpServletResponse(), urlComponent.getParameters(), scheme, urlComponent.isIncludeContext(), urlComponent.isEncode(), urlComponent.isForceAddSchemeHostAndPort(), urlComponent.isEscapeAmp());
    } else if (urlComponent.getValue() == null && urlComponent.getAction() == null && ai != null) {
        // both are null, we will default to the current action
        final String action = ai.getProxy().getActionName();
        final String namespace = ai.getProxy().getNamespace();
        final String method = urlComponent.getMethod() != null || !ai.getProxy().isMethodSpecified() ? urlComponent.getMethod() : ai.getProxy().getMethod();
        result = urlComponent.determineActionURL(action, namespace, method, urlComponent.getHttpServletRequest(), urlComponent.getHttpServletResponse(), urlComponent.getParameters(), scheme, urlComponent.isIncludeContext(), urlComponent.isEncode(), urlComponent.isForceAddSchemeHostAndPort(), urlComponent.isEscapeAmp());
    } else {
        String _value = urlComponent.getValue();
        // prioritised before this [in start(Writer) method]
        if (_value != null && _value.indexOf('?') > 0) {
            _value = _value.substring(0, _value.indexOf('?'));
        }
        result = urlHelper.buildUrl(_value, urlComponent.getHttpServletRequest(), urlComponent.getHttpServletResponse(), urlComponent.getParameters(), scheme, urlComponent.isIncludeContext(), urlComponent.isEncode(), urlComponent.isForceAddSchemeHostAndPort(), urlComponent.isEscapeAmp());
    }
    String anchor = urlComponent.getAnchor();
    if (StringUtils.isNotEmpty(anchor)) {
        result += '#' + urlComponent.findString(anchor);
    }
    if (urlComponent.isPutInContext()) {
        String var = urlComponent.getVar();
        if (StringUtils.isNotEmpty(var)) {
            urlComponent.putInContext(result);
            // Note: Old comments stated that var was placed in the page scope, but interactive checks with EL on JSPs prove otherwise.
            // Add the var attribute to the request scope as well.
            urlComponent.getHttpServletRequest().setAttribute(var, result);
        } else {
            try {
                writer.write(result);
            } catch (IOException e) {
                throw new StrutsException("IOError: " + e.getMessage(), e);
            }
        }
    } else {
        try {
            writer.write(result);
        } catch (IOException e) {
            throw new StrutsException("IOError: " + e.getMessage(), e);
        }
    }
}
Also used : StrutsException(org.apache.struts2.StrutsException) ValueStack(com.opensymphony.xwork2.util.ValueStack) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) IOException(java.io.IOException)

Example 72 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class Bean method start.

public boolean start(Writer writer) {
    boolean result = super.start(writer);
    ValueStack stack = getStack();
    try {
        String beanName = findString(name, "name", "Bean name is required. Example: com.acme.FooBean or proper Spring bean ID");
        bean = objectFactory.buildBean(beanName, stack.getContext(), false);
    } catch (Exception e) {
        LOG.error("Could not instantiate bean", e);
        return false;
    }
    // push bean on stack
    stack.push(bean);
    // store for reference later
    putInContext(bean);
    return result;
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 73 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class Push method start.

public boolean start(Writer writer) {
    boolean result = super.start(writer);
    ValueStack stack = getStack();
    if (stack != null) {
        stack.push(findValue(value, "value", "You must specify a value to push on the stack. Example: person"));
        pushed = true;
    } else {
        // need to ensure push is assigned, otherwise we may have a leftover value
        pushed = false;
    }
    return result;
}
Also used : ValueStack(com.opensymphony.xwork2.util.ValueStack)

Example 74 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class PortletResultTest method testDoExecute_event_locationIsJsp.

public void testDoExecute_event_locationIsJsp() {
    Mock mockRequest = mock(ActionRequest.class);
    Mock mockResponse = mock(ActionResponse.class);
    Constraint[] params = new Constraint[] { eq(ACTION_PARAM), eq("renderDirect") };
    mockResponse.expects(once()).method("setRenderParameter").with(params);
    params = new Constraint[] { eq(MODE_PARAM), eq(PortletMode.VIEW.toString()) };
    mockResponse.expects(once()).method("setRenderParameter").with(params);
    params = new Constraint[] { eq(PortletConstants.RENDER_DIRECT_NAMESPACE), eq("/test") };
    mockResponse.expects(once()).method("setRenderParameter").with(params);
    mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
    ActionContext ctx = ActionContext.getContext();
    Map<String, Object> session = new HashMap<>();
    ctx.setSession(session);
    ctx.put(REQUEST, mockRequest.proxy());
    ctx.put(RESPONSE, mockResponse.proxy());
    ctx.put(PHASE, PortletPhase.ACTION_PHASE);
    PortletResult result = new PortletResult();
    try {
        result.doExecute("/WEB-INF/pages/testJsp.jsp", (ActionInvocation) mockInvocation.proxy());
    } catch (Exception e) {
        e.printStackTrace();
        fail("Error occurred!");
    }
    assertEquals("/WEB-INF/pages/testJsp.jsp", session.get(RENDER_DIRECT_LOCATION));
}
Also used : Constraint(org.jmock.core.Constraint) HashMap(java.util.HashMap) ActionContext(com.opensymphony.xwork2.ActionContext) Mock(org.jmock.Mock)

Example 75 with Result

use of com.opensymphony.xwork2.Result in project struts by apache.

the class PortletResultTest method testDoExecute_render.

public void testDoExecute_render() {
    Mock mockRequest = mock(RenderRequest.class);
    Mock mockResponse = mock(RenderResponse.class);
    Mock mockRd = mock(PortletRequestDispatcher.class);
    RenderRequest req = (RenderRequest) mockRequest.proxy();
    RenderResponse res = (RenderResponse) mockResponse.proxy();
    PortletRequestDispatcher rd = (PortletRequestDispatcher) mockRd.proxy();
    PortletContext ctx = (PortletContext) mockCtx.proxy();
    ActionInvocation inv = (ActionInvocation) mockInvocation.proxy();
    Constraint[] params = new Constraint[] { same(req), same(res) };
    mockRd.expects(once()).method("include").with(params);
    mockCtx.expects(once()).method("getRequestDispatcher").with(eq("/WEB-INF/pages/testPage.jsp")).will(returnValue(rd));
    mockCtx.expects(atLeastOnce()).method("getMajorVersion").will(returnValue(1));
    mockResponse.expects(once()).method("setContentType").with(eq("text/html"));
    mockRequest.stubs().method("getPortletMode").will(returnValue(PortletMode.VIEW));
    ActionContext ctxMap = ActionContext.getContext();
    ctxMap.put(RESPONSE, res);
    ctxMap.put(REQUEST, req);
    ctxMap.put(SERVLET_CONTEXT, ctx);
    ctxMap.put(PHASE, PortletPhase.RENDER_PHASE);
    PortletResult result = new PortletResult();
    try {
        result.doExecute("/WEB-INF/pages/testPage.jsp", inv);
    } catch (Exception e) {
        e.printStackTrace();
        fail("Error occurred!");
    }
}
Also used : PortletRequestDispatcher(javax.portlet.PortletRequestDispatcher) Constraint(org.jmock.core.Constraint) ActionInvocation(com.opensymphony.xwork2.ActionInvocation) RenderRequest(javax.portlet.RenderRequest) PortletContext(javax.portlet.PortletContext) RenderResponse(javax.portlet.RenderResponse) ActionContext(com.opensymphony.xwork2.ActionContext) Mock(org.jmock.Mock)

Aggregations

ActionSupport (com.opensymphony.xwork2.ActionSupport)63 Test (org.junit.Test)52 ActionProxy (com.opensymphony.xwork2.ActionProxy)51 List (java.util.List)49 ValueStack (com.opensymphony.xwork2.util.ValueStack)38 Result (edu.stanford.CVC4.Result)35 ResultConfig (com.opensymphony.xwork2.config.entities.ResultConfig)34 ActionContext (com.opensymphony.xwork2.ActionContext)33 Expr (edu.stanford.CVC4.Expr)32 SExpr (edu.stanford.CVC4.SExpr)32 CVC4.vectorExpr (edu.stanford.CVC4.vectorExpr)31 ArrayList (java.util.ArrayList)31 ActionInvocation (com.opensymphony.xwork2.ActionInvocation)30 HashMap (java.util.HashMap)29 Rational (edu.stanford.CVC4.Rational)25 ActionConfig (com.opensymphony.xwork2.config.entities.ActionConfig)22 Map (java.util.Map)21 ServletActionContext (org.apache.struts2.ServletActionContext)21 Result (com.opensymphony.xwork2.Result)18 PackageConfig (com.opensymphony.xwork2.config.entities.PackageConfig)18