Example usage for javax.servlet.http HttpServletResponse encodeRedirectUrl

List of usage examples for javax.servlet.http HttpServletResponse encodeRedirectUrl

Introduction

In this page you can find the example usage for javax.servlet.http HttpServletResponse encodeRedirectUrl.

Prototype

@Deprecated
public String encodeRedirectUrl(String url);

Source Link

Usage

From source file:org.muse.mneme.tool.AssessmentAccessView.java

/**
 * {@inheritDoc}/*from  w w w  .  ja  v  a 2 s  .  c o m*/
 */
public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // we need 3 parameters: sort, aid, access id
    if (params.length < 5) {
        throw new IllegalArgumentException();
    }
    String sort = params[2];
    String assessmentId = params[3];
    String accessId = params[4];

    // get the assessment
    Assessment assessment = assessmentService.getAssessment(assessmentId);
    if (assessment == null) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
        return;
    }

    // security check
    if (!assessmentService.allowEditAssessment(assessment)) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    // if access id is actually a user id, find the access that is for this user (only),
    AssessmentAccess access = null;
    if (accessId.startsWith("USER:")) {
        String[] parts = StringUtil.splitFirst(accessId, ":");
        access = assessment.getSpecialAccess().assureUserAccess(parts[1]);
    } else {
        access = assessment.getSpecialAccess().getAccess(accessId);
    }
    if (access == null) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
        return;
    }

    // setup the model
    context.put("access", access);

    // read the form
    String destination = uiService.decode(req, context);

    // save
    try {
        this.assessmentService.saveAssessment(assessment);
    } catch (AssessmentPermissionException e) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    } catch (AssessmentPolicyException e) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.policy)));
        return;
    }

    // redirect to the next destination
    res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
}

From source file:org.muse.mneme.tool.PoolEditView.java

/**
 * {@inheritDoc}/*  w w  w.  ja  v a 2s  . c o m*/
 */
public void get(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // pools sort, pool id, optional sort, optional paging
    if ((params.length != 4) && (params.length != 5) && (params.length != 6)) {
        throw new IllegalArgumentException();
    }

    if (!this.poolService.allowManagePools(toolManager.getCurrentPlacement().getContext())) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    // pools view sort
    String poolsSortCode = null;
    poolsSortCode = params[2];
    context.put("poolsSortCode", poolsSortCode);

    // pool
    String pid = params[3];
    Pool pool = this.poolService.getPool(pid);
    if (pool == null) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
        return;
    }
    context.put("pool", pool);

    // sort
    String sortCode = "0A";
    if (params.length > 4)
        sortCode = params[4];
    if ((sortCode == null) || (sortCode.length() != 2)) {
        throw new IllegalArgumentException();
    }
    context.put("sort_column", sortCode.charAt(0));
    context.put("sort_direction", sortCode.charAt(1));
    QuestionService.FindQuestionsSort sort = null;
    // 0 is description
    if ((sortCode.charAt(0) == '0') && (sortCode.charAt(1) == 'A'))
        sort = QuestionService.FindQuestionsSort.description_a;
    else if ((sortCode.charAt(0) == '0') && (sortCode.charAt(1) == 'D'))
        sort = QuestionService.FindQuestionsSort.description_d;
    // 1 is type
    else if ((sortCode.charAt(0) == '1') && (sortCode.charAt(1) == 'A'))
        sort = QuestionService.FindQuestionsSort.type_a;
    else if ((sortCode.charAt(0) == '1') && (sortCode.charAt(1) == 'D'))
        sort = QuestionService.FindQuestionsSort.type_d;
    else {
        throw new IllegalArgumentException();
    }

    // paging
    String pagingParameter = "1-" + Integer.toString(this.pageSizes.get(0));
    if (params.length > 5)
        pagingParameter = params[5];
    Integer maxQuestions = this.questionService.countQuestions(pool, null, null, null, null);
    Paging paging = uiService.newPaging();
    paging.setMaxItems(maxQuestions);
    paging.setCurrentAndSize(pagingParameter);
    context.put("paging", paging);

    // get questions
    List<Question> questions = questionService.findQuestions(pool, sort, null, null,
            paging.getSize() == 0 ? null : paging.getCurrent(), paging.getSize() == 0 ? null : paging.getSize(),
            null, null);
    context.put("questions", questions);

    // pages sizes
    if (this.pageSizes.size() > 1) {
        context.put("pageSizes", this.pageSizes);
    }

    uiService.render(ui, context);
}

From source file:org.muse.mneme.tool.PoolEditView.java

/**
 * {@inheritDoc}/*  w ww. jav a  2 s .c  o  m*/
 */
public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // pools sort, pool id, optional sort, optional paging
    if ((params.length != 4) && (params.length != 5) && (params.length != 6)) {
        throw new IllegalArgumentException();
    }

    if (!this.poolService.allowManagePools(toolManager.getCurrentPlacement().getContext())) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    // for the selected questions to delete
    Values values = this.uiService.newValues();
    context.put("questionids", values);

    // read form
    String destination = this.uiService.decode(req, context);

    if (destination.equals("DELETE")) {
        for (String id : values.getValues()) {
            Question question = this.questionService.getQuestion(id);
            if (question != null) {
                try {
                    this.questionService.removeQuestion(question);
                } catch (AssessmentPermissionException e) {
                    // redirect to error
                    res.sendRedirect(
                            res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
                    return;
                }
            }
        }

        // stay here
        destination = context.getDestination();
    }

    else if (destination.trim().startsWith("DUPLICATE:")) {
        String[] parts = StringUtil.split(destination, ":");
        if (parts.length != 2) {
            throw new IllegalArgumentException();
        }
        String qid = parts[1];
        try {
            Question question = this.questionService.getQuestion(qid);
            if (question != null) {
                // copy within the same pool
                this.questionService.copyQuestion(question, null);
            }

            // stay here
            destination = context.getDestination();
        } catch (AssessmentPermissionException e) {
            // redirect to error
            res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
            return;
        }
    }

    else if ((destination.trim().startsWith("/question_copy"))
            || (destination.trim().startsWith("/question_move"))) {
        // add the selected ids to the destination
        StringBuilder buf = new StringBuilder();
        buf.append(destination);
        buf.append("/");
        for (String id : values.getValues()) {
            buf.append(id);
            buf.append("+");
        }
        buf.setLength(buf.length() - 1);

        destination = buf.toString();
    }

    res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
}

From source file:org.codehaus.groovy.grails.web.metaclass.RedirectDynamicMethod.java

private Object redirectResponse(String serverBaseURL, String actualUri, HttpServletRequest request,
        HttpServletResponse response, boolean permanent) {
    if (LOG.isDebugEnabled()) {
        LOG.debug("Dynamic method [redirect] forwarding request to [" + actualUri + "]");
    }//from w  ww .ja  v  a 2  s.co  m

    if (LOG.isDebugEnabled()) {
        LOG.debug("Executing redirect with response [" + response + "]");
    }
    String processedActualUri = processedUrl(actualUri, request);
    String absoluteURL = processedActualUri.contains("://") ? processedActualUri
            : serverBaseURL + processedActualUri;
    String redirectUrl = useJessionId ? response.encodeRedirectURL(absoluteURL) : absoluteURL;
    int status = permanent ? HttpServletResponse.SC_MOVED_PERMANENTLY
            : HttpServletResponse.SC_MOVED_TEMPORARILY;

    response.setStatus(status);
    response.setHeader(HttpHeaders.LOCATION, redirectUrl);

    if (redirectListeners != null) {
        for (RedirectEventListener redirectEventListener : redirectListeners) {
            redirectEventListener.responseRedirected(redirectUrl);
        }
    }

    request.setAttribute(GRAILS_REDIRECT_ISSUED, processedActualUri);
    return null;
}

From source file:org.etudes.mneme.tool.InstructionsView.java

/**
 * {@inheritDoc}//from ww w.ja v a2  s  .c o  m
 */
public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // sid and return
    if (params.length < 4) {
        throw new IllegalArgumentException();
    }

    String submissionId = params[2];

    String returnDestination = "/" + StringUtil.unsplit(params, 3, params.length - 3, "/");

    // read form
    String destination = this.uiService.decode(req, context);

    // if other than the /submitted destination, just go there
    if ((!destination.startsWith("/submitted")) && (!destination.equals("AUTO"))) {
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
        return;
    }

    // for AUTO, the final return is just /list - not the destination encoded in the params
    if (destination.equals("AUTO")) {
        returnDestination = "/list";
    }

    // this post is from the timer, and completes the submission
    // TODO: this returnDestination may not be quite right -ggolden
    TocView.submissionCompletePost(req, res, context, submissionId, this.uiService, this.submissionService,
            returnDestination);
}

From source file:org.muse.mneme.tool.AssessmentsView.java

/**
 * {@inheritDoc}/*from   w w w.j  ava 2 s . c o  m*/
 */
public void get(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // sort (optional)
    if ((params.length != 2) && (params.length != 3)) {
        throw new IllegalArgumentException();
    }

    // security
    if (!this.assessmentService.allowManageAssessments(toolManager.getCurrentPlacement().getContext())) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    // default is due date, ascending
    String sortCode = (params.length > 2) ? params[2] : "0A";
    if (sortCode.length() != 2) {
        throw new IllegalArgumentException();
    }

    // due (0), open (1), title (2), publish (3), view/type (4)
    AssessmentService.AssessmentsSort sort = null;
    if (sortCode.charAt(0) == '0') {
        if (sortCode.charAt(1) == 'A') {
            sort = AssessmentService.AssessmentsSort.ddate_a;
        } else {
            sort = AssessmentService.AssessmentsSort.ddate_d;
        }
    } else if (sortCode.charAt(0) == '1') {
        if (sortCode.charAt(1) == 'A') {
            sort = AssessmentService.AssessmentsSort.odate_a;
        } else {
            sort = AssessmentService.AssessmentsSort.odate_d;
        }
    } else if (sortCode.charAt(0) == '2') {
        if (sortCode.charAt(1) == 'A') {
            sort = AssessmentService.AssessmentsSort.title_a;
        } else {
            sort = AssessmentService.AssessmentsSort.title_d;
        }

    } else if (sortCode.charAt(0) == '3') {
        if (sortCode.charAt(1) == 'A') {
            sort = AssessmentService.AssessmentsSort.published_a;
        } else {
            sort = AssessmentService.AssessmentsSort.published_d;
        }
    } else if (sortCode.charAt(0) == '4') {
        if (sortCode.charAt(1) == 'A') {
            sort = AssessmentService.AssessmentsSort.type_a;
        } else {
            sort = AssessmentService.AssessmentsSort.type_d;
        }
    } else {
        throw new IllegalArgumentException();
    }
    context.put("sort_column", sortCode.charAt(0));
    context.put("sort_direction", sortCode.charAt(1));

    // collect the assessments in this context
    List<Assessment> assessments = this.assessmentService
            .getContextAssessments(this.toolManager.getCurrentPlacement().getContext(), sort, Boolean.FALSE);
    context.put("assessments", assessments);

    // disable the tool navigation to this view
    context.put("disableAssessments", Boolean.TRUE);

    // pre-read question counts per pool
    this.questionService.preCountContextQuestions(toolManager.getCurrentPlacement().getContext(), Boolean.TRUE);

    // render
    uiService.render(ui, context);
}

From source file:org.etudes.mneme.tool.AssessmentAccessView.java

/**
 * {@inheritDoc}//from  w  w w. j a  v  a 2 s .  c om
 */
public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // we need 3 parameters: sort, aid, access id
    if (params.length < 5) {
        throw new IllegalArgumentException();
    }
    String sort = params[2];
    String assessmentId = params[3];
    String accessId = params[4];

    // get the assessment
    Assessment assessment = assessmentService.getAssessment(assessmentId);
    if (assessment == null) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
        return;
    }

    // security check
    if (!assessmentService.allowEditAssessment(assessment)) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    // if access id is actually a user id, find the access that is for this user (only),
    AssessmentAccess access = null;
    if (accessId.startsWith("USER:")) {
        String[] parts = StringUtil.splitFirst(accessId, ":");
        access = assessment.getSpecialAccess().assureUserAccess(parts[1]);
    } else {
        access = assessment.getSpecialAccess().getAccess(accessId);
    }
    if (access == null) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
        return;
    }

    // setup the model
    context.put("access", access);

    // read the form
    String destination = uiService.decode(req, context);

    if ("DELETE".equals(destination)) {
        if (params.length > 5) {
            destination = "/" + StringUtil.unsplit(params, 5, params.length - 5, "/");
        } else {
            destination = "/assessment_special/" + sort + "/" + assessment.getId();
        }

        // delete
        assessment.getSpecialAccess().removeAccess(access);
    }

    // save
    try {
        this.assessmentService.saveAssessment(assessment);
    } catch (AssessmentPermissionException e) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    } catch (AssessmentPolicyException e) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.policy)));
        return;
    }

    // redirect to the next destination
    res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
}

From source file:org.etudes.mneme.tool.ReviewView.java

/**
 * {@inheritDoc}//ww w.  ja  v a2  s.  c o  m
 */
public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // read form
    String destination = this.uiService.decode(req, context);
    if (params.length < 3) {
        throw new IllegalArgumentException();
    }

    String submissionId = params[2];
    // collect the submission
    Submission submission = submissionService.getSubmission(submissionId);
    if (submission == null) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
        return;
    }

    // go there
    res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
}

From source file:org.muse.mneme.tool.AssessmentEditView.java

/**
 * {@inheritDoc}//w  w w.  j a  v  a2  s . c o  m
 */
public void post(HttpServletRequest req, HttpServletResponse res, Context context, String[] params)
        throws IOException {
    // sort, aid
    if (params.length != 4) {
        throw new IllegalArgumentException();
    }
    String sort = params[2];
    String assessmentId = params[3];

    Assessment assessment = assessmentService.getAssessment(assessmentId);
    if (assessment == null) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
        return;
    }

    // security check
    if (!assessmentService.allowEditAssessment(assessment)) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    }

    // setup the model: the selected assessment
    context.put("assessment", assessment);

    // value holders for the selection checkboxes
    Values values = this.uiService.newValues();
    context.put("ids", values);

    // for the upload of attachments
    Upload upload = new Upload(this.toolManager.getCurrentPlacement().getContext(), AttachmentService.DOCS_AREA,
            false, this.attachmentService);
    context.put("upload", upload);

    // read the form
    String destination = uiService.decode(req, context);

    // save the attachments upload
    if (upload.getUpload() != null) {
        assessment.getPresentation().addAttachment(upload.getUpload());
    }

    // handle an attachments remove
    if (destination.startsWith("REMOVE:")) {
        String[] parts = StringUtil.split(destination, ":");
        if (parts.length != 2) {
            throw new IllegalArgumentException();
        }
        String refString = parts[1];
        Reference ref = this.entityManager.newReference(refString);

        // remove from the assessment
        assessment.getPresentation().removeAttachment(ref);

        // remove the attachment
        // TODO: really?
        this.attachmentService.removeAttachment(ref);

        // stay here
        destination = context.getDestination();
    }

    try {
        if (destination.equals("DRAW")) {
            DrawPart dPart = assessment.getParts().addDrawPart();
            this.assessmentService.saveAssessment(assessment);

            // create url for draw
            destination = "/part_edit/" + sort + "/" + assessment.getId() + "/" + dPart.getId();
        }

        else if (destination.equals("MANUAL")) {
            ManualPart mPart = assessment.getParts().addManualPart();
            this.assessmentService.saveAssessment(assessment);

            // create url for manual
            destination = "/part_edit/" + sort + "/" + assessment.getId() + "/" + mPart.getId();
        }

        else if (destination.equals("DELETE")) {
            for (String id : values.getValues()) {
                Part part = assessment.getParts().getPart(id);
                if (part == null) {
                    // redirect to error
                    res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.invalid)));
                    return;
                }

                // remove part
                assessment.getParts().removePart(part);
            }
            this.assessmentService.saveAssessment(assessment);

            destination = context.getDestination();
        } else {
            this.assessmentService.saveAssessment(assessment);
        }
    } catch (AssessmentPermissionException e) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.unauthorized)));
        return;
    } catch (AssessmentPolicyException e) {
        // redirect to error
        res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, "/error/" + Errors.policy)));
        return;
    }

    // redirect to the next destination
    res.sendRedirect(res.encodeRedirectURL(Web.returnUrl(req, destination)));
}

From source file:org.mycore.frontend.editor.MCREditorServlet.java

private void sendToURL(HttpServletRequest req, HttpServletResponse res) throws IOException {
    StringBuilder url = new StringBuilder(req.getParameter("_target-url"));
    url.append('?').append(req.getQueryString());
    res.sendRedirect(res.encodeRedirectURL(url.toString()));
}