MG2>some confusion on where session is accessed

________________________________
From: Yasser Zamani <yasser.zam...@live.com> on behalf of Yasser Zamani 
<yasserzam...@apache.org>
Sent: Wednesday, April 18, 2018 2:57 AM
To: user@struts.apache.org
Subject: Re: [S2] ExecuteAndWait Interceptor // Only re-submit token parameters 
on refresh?



On 4/18/2018 1:21 AM, Martin Gainty wrote:
> MG>AFAIK a redirect terminates the old session and creates a new session

I think redirect to same domain:ip in same browser tab page should keep
session.

MG2>2 alternatives: build out HTTPGet with &params

 * Calls the {@link HttpServletResponse#sendRedirect(String) sendRedirect}
 * method to the location specified. The response is told to redirect the
 * browser to the specified location (a new request from the client). The
 * consequence of doing this means that the action (action instance, action
 * errors, field errors, etc) that was just executed is lost and no longer
 * available. This is because actions are built on a single-thread model. The
 * only way to pass data is through the session
MG2>with chain interceptor

 * or with web parameters

 * (url?name=value) which can be OGNL expressions.
MG2>essentially a HTTP Get with params............. here is example:
* <!-- START SNIPPET: example -->
 * &lt;!--
 *   The redirect URL generated will be:
 *   /foo.jsp#FRAGMENT
 * --&gt;
 * &lt;result name="success" type="redirect"&gt;
 *   &lt;param name="location"&gt;foo.jsp&lt;/param&gt;
 *   &lt;param name="parse"&gt;false&lt;/param&gt;
 *   &lt;param name="anchor"&gt;FRAGMENT&lt;/param&gt;
 * &lt;/result&gt;
 * <!-- END SNIPPET: example -->
MG2>

MG2>Here is code that *should access session params* to pass to new 
finalLocation (but session is not accessed?)
    /**
     * 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 = (HttpServletRequest) 
ctx.get(ServletActionContext.HTTP_REQUEST);
        HttpServletResponse response = (HttpServletResponse) 
ctx.get(ServletActionContext.HTTP_RESPONSE);

        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 
(prepend) the servlet context path to finalLocation
            if (prependServletContext && (request.getContextPath() != null) && 
(request.getContextPath().length() > 0)) {
                finalLocation = request.getContextPath() + finalLocation;
            }

MG2>
//where the session *should* be accessed
// public Map<String, ResultConfig> getResults()
//i see ResultConfig here but no reference to session?
//where in ResultConfig The Builder class I see
 /**
     * The builder for this object.  An instance of this object is the only way 
to construct a new instance.  The
     * purpose is to enforce the immutability of the object.
     * The methods are structured in a way to support chaining.
     * After setting any values you need, call the {@link #build()} method to 
create the object.
     */

MG2>

            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());
        }

        if (LOG.isDebugEnabled()) {
            LOG.debug("Redirecting to finalLocation " + finalLocation);
        }

        sendRedirect(response, finalLocation);
    }
MG2>

MG2>i found a testcase for redirect
public class ServletActionRedirectResultTest extends StrutsInternalTestCase {

    public void testIncludeParameterInResultWithConditionParseOn() 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("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");
        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<String, ResultConfig>();
        results.put("myResult", resultConfig);

        ActionConfig actionConfig = new ActionConfig.Builder("", "", "")
                .addResultConfigs(results).build();

        ServletActionRedirectResult result = new ServletActionRedirectResult();
        result.setActionName("myAction");
        result.setNamespace("/myNamespace");
        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);
        expect(mockInvocation.getResultCode()).andReturn("myResult");
        expect(mockActionProxy.getConfig()).andReturn(actionConfig);
        expect(mockInvocation.getInvocationContext()).andReturn(context);
        expect(mockInvocation.getStack()).andReturn(stack).anyTimes();

        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());
MG2>in this case a HTTPGet with appended params..but no session vars?

Regards.

MG2>am i reading the code incorrectly?
MG2>Regards

---------------------------------------------------------------------
To unsubscribe, e-mail: user-unsubscr...@struts.apache.org
For additional commands, e-mail: user-h...@struts.apache.org

Reply via email to