struts-dev mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From lukaszlenart <...@git.apache.org>
Subject [GitHub] struts pull request: New result 'JSONActionRedirectResult' in json...
Date Wed, 13 Jan 2016 15:55:42 GMT
Github user lukaszlenart commented on a diff in the pull request:

    https://github.com/apache/struts/pull/72#discussion_r49608114
  
    --- Diff: plugins/json/src/main/java/org/apache/struts2/json/JSONActionRedirectResult.java
---
    @@ -0,0 +1,70 @@
    +package org.apache.struts2.json;
    +
    +import java.io.IOException;
    +import java.io.PrintWriter;
    +
    +import javax.servlet.http.HttpServletRequest;
    +import javax.servlet.http.HttpServletResponse;
    +
    +import org.apache.struts2.ServletActionContext;
    +import org.apache.struts2.result.ServletActionRedirectResult;
    +
    +/**
    + * Specialized form of {@link ServletActionRedirectResult} which takes care of
    + * situation that browser has a JS/AJAX context, there are no validation errors
    + * and action is executed. In this case a http redirect is harmful as browsers
    + * don't pass them to JS handlers. So this result produces a JSON response
    + * containing redirect data.
    + *
    + * <p>To be used along with {@link JSONValidationInterceptor}.</p>
    + *
    + * <p>Response JSON looks like this:
    + * 
    + *     <pre>{"location": "$redirect url$"}</pre>
    + * </p>
    + *
    + */
    +public class JSONActionRedirectResult extends ServletActionRedirectResult {
    +
    +    private static final long serialVersionUID = 3107276294073879542L;
    +
    +    @Override
    +    protected void sendRedirect(HttpServletResponse response, String finalLocation) throws
IOException {
    +        if (sendJsonInsteadOfRedirect()) {
    +            printJson(response, finalLocation);
    +        } else {
    +            super.sendRedirect(response, finalLocation);
    +        }
    +    }
    +
    +    /**
    +     * If browser has called action in a JS/AJAX context we cannot send a
    +     * redirect as response.
    +     *
    +     * @return true if a JSON response shall be generated, false if a redirect
    +     *         shall be sent.
    +     */
    +    private boolean sendJsonInsteadOfRedirect() {
    +        HttpServletRequest request = ServletActionContext.getRequest();
    +        return isJsonEnabled(request) && !isValidateOnly(request);
    +    }
    +
    +    private void printJson(HttpServletResponse response, String finalLocation) throws
IOException {
    +        response.setStatus(HttpServletResponse.SC_OK);
    +        response.setContentType("application/json");
    +        response.setHeader("Location", finalLocation);
    +        PrintWriter writer = response.getWriter();
    +        writer.write("{\"location\": \"");
    +        writer.write(finalLocation);
    +        writer.write("\"}");
    +        writer.close();
    +    }
    +
    +    private boolean isJsonEnabled(HttpServletRequest request) {
    +        return "true".equals(request.getParameter(JSONValidationInterceptor.VALIDATE_JSON_PARAM));
    +    }
    +
    +    private boolean isValidateOnly(HttpServletRequest request) {
    --- End diff --
    
    Could you make this function `protected`? This allow further extensions by users.


---
If your project is set up for it, you can reply to this email and have your
reply appear on GitHub as well. If your project does not have this feature
enabled and wishes so, or if the feature is enabled but not working, please
contact infrastructure at infrastructure@apache.org or file a JIRA ticket
with INFRA.
---

---------------------------------------------------------------------
To unsubscribe, e-mail: dev-unsubscribe@struts.apache.org
For additional commands, e-mail: dev-help@struts.apache.org


Mime
View raw message