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.codehaus.groovy.grails.plugins.springsecurity.RedirectUtils.java

/**
 * Send a redirect.//  www.j  a  v  a2s .c  o m
 * @param request  the request
 * @param response  the response
 * @param url the target url to redirect to
 * @throws IOException  if there's a problem
 */
public static void sendRedirect(final HttpServletRequest request, final HttpServletResponse response,
        final String url) throws IOException {

    String redirect = buildRedirectUrl(request, response, url);
    response.sendRedirect(response.encodeRedirectURL(redirect));
}

From source file:org.wallride.web.support.ControllerUtils.java

public static ResponseEntity<?> createRedirectResponseEntity(HttpServletRequest nativeRequest,
        HttpServletResponse nativeResponse, String path) {
    UrlPathHelper pathHelper = new UrlPathHelper();
    String url = pathHelper.getContextPath(nativeRequest) + path;
    String encodedUrl = nativeResponse.encodeRedirectURL(url);

    HttpHeaders headers = new HttpHeaders();
    headers.setLocation(URI.create(encodedUrl));

    ResponseEntity<?> response = new ResponseEntity<>(null, headers, HttpStatus.FOUND);
    return response;
}

From source file:com.util.ConvertJspToStringHtml.java

public static String generateHtml(String url, HttpServletRequest request, HttpServletResponse response) {
    String html = null;/* w  w w . j av a 2 s  .  c om*/
    try {
        BufferedHttpResponseWrapper wrapper = new BufferedHttpResponseWrapper(response);
        ServletContext context = request.getSession().getServletContext();
        String urlEncode = response.encodeRedirectURL(url);
        RequestDispatcher dispatcher = context.getRequestDispatcher(urlEncode);
        dispatcher.include(request, wrapper);
        //            html = ASCII2Unicode(StringEscapeUtils.unescapeHtml(wrapper.getOutput()));

        html = html.replaceAll("\\r\\n|\\r|\\n", "");
        html = html.replaceAll("&nbsp;", "");
    } catch (Exception e) {
        System.err.println(e.getMessage());
    }
    return html;
}

From source file:org.entando.entando.aps.system.services.controller.executor.AbstractWidgetExecutorService.java

protected static String extractJspOutput(RequestContext reqCtx, String jspPath)
        throws ServletException, IOException {
    HttpServletRequest request = reqCtx.getRequest();
    HttpServletResponse response = reqCtx.getResponse();
    BufferedHttpResponseWrapper wrapper = new BufferedHttpResponseWrapper(response);
    ServletContext context = request.getSession().getServletContext();
    String url = response.encodeRedirectURL(jspPath);
    RequestDispatcher dispatcher = context.getRequestDispatcher(url);
    dispatcher.include(request, wrapper);
    return wrapper.getOutput();
}

From source file:info.magnolia.cms.util.RequestDispatchUtil.java

/**
 * Returns true if processing took place, even if it fails.
 */// w  w  w .j  ava 2  s  .  c  o m
public static boolean dispatch(String targetUri, HttpServletRequest request, HttpServletResponse response) {

    if (targetUri == null) {
        return false;
    }

    if (targetUri.startsWith(REDIRECT_PREFIX)) {
        String redirectUrl = StringUtils.substringAfter(targetUri, REDIRECT_PREFIX);
        try {

            if (isInternal(redirectUrl)) {
                redirectUrl = request.getContextPath() + redirectUrl;
            }

            response.sendRedirect(response.encodeRedirectURL(redirectUrl));

        } catch (IOException e) {
            log.error("Failed to redirect to {}:{}", targetUri, e.getMessage());
        }
        return true;
    }

    if (targetUri.startsWith(PERMANENT_PREFIX)) {
        String permanentUrl = StringUtils.substringAfter(targetUri, PERMANENT_PREFIX);
        try {

            if (isInternal(permanentUrl)) {
                if (isUsingStandardPort(request)) {
                    permanentUrl = new URL(request.getScheme(), request.getServerName(),
                            request.getContextPath() + permanentUrl).toExternalForm();
                } else {
                    permanentUrl = new URL(request.getScheme(), request.getServerName(),
                            request.getServerPort(), request.getContextPath() + permanentUrl).toExternalForm();
                }
            }

            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", permanentUrl);

        } catch (MalformedURLException e) {
            log.error("Failed to create permanent url to redirect to {}:{}", targetUri, e.getMessage());
        }
        return true;
    }

    if (targetUri.startsWith(FORWARD_PREFIX)) {
        String forwardUrl = StringUtils.substringAfter(targetUri, FORWARD_PREFIX);
        try {
            request.getRequestDispatcher(forwardUrl).forward(request, response);
        } catch (Exception e) {
            log.error("Failed to forward to {} - {}:{}",
                    new Object[] { forwardUrl, ClassUtils.getShortClassName(e.getClass()), e.getMessage() });
        }
        return true;
    }

    return false;
}

From source file:fll.web.DoLogin.java

/**
 * Does the work of login. Exists as a separate method so that it can be
 * called from {@link fll.web.admin.CreateUser}
 *//*from  ww w. j a va2  s  . co  m*/
public static void doLogin(final HttpServletRequest request, final HttpServletResponse response,
        final ServletContext application, final HttpSession session) throws IOException, ServletException {
    final DataSource datasource = ApplicationAttributes.getDataSource(application);
    Connection connection = null;
    try {
        connection = datasource.getConnection();

        // check for authentication table
        if (Queries.isAuthenticationEmpty(connection)) {
            LOGGER.warn("No authentication information in the database");
            session.setAttribute(SessionAttributes.MESSAGE,
                    "<p class='error'>No authentication information in the database - see administrator</p>");
            response.sendRedirect(response.encodeRedirectURL("login.jsp"));
            return;
        }

        // compute hash
        final String user = request.getParameter("user");
        final String pass = request.getParameter("pass");
        if (null == user || user.isEmpty() || null == pass || pass.isEmpty()) {
            LOGGER.warn("Form fields missing");
            session.setAttribute(SessionAttributes.MESSAGE,
                    "<p class='error'>You must fill out all fields in the form.</p>");
            response.sendRedirect(response.encodeRedirectURL("login.jsp"));
            return;
        }
        final String hashedPass = DigestUtils.md5Hex(pass);

        // compare login information
        if (LOGGER.isTraceEnabled()) {
            LOGGER.trace("Checking user: " + user + " hashedPass: " + hashedPass);
        }
        final Map<String, String> authInfo = Queries.getAuthInfo(connection);
        for (final Map.Entry<String, String> entry : authInfo.entrySet()) {
            if (user.equals(entry.getKey()) && hashedPass.equals(entry.getValue())) {
                // clear out old login cookies first
                CookieUtils.clearLoginCookies(application, request, response);

                final String magicKey = String.valueOf(System.currentTimeMillis());
                Queries.addValidLogin(connection, user, magicKey);
                CookieUtils.setLoginCookie(response, magicKey);

                String redirect = SessionAttributes.getRedirectURL(session);
                if (null == redirect) {
                    redirect = "index.jsp";
                }
                response.sendRedirect(response.encodeRedirectURL(redirect));
                return;
            } else {
                if (LOGGER.isTraceEnabled()) {
                    LOGGER.trace("Didn't match user: " + entry.getKey() + " pass: " + entry.getValue());
                }
            }
        }

        LOGGER.warn("Incorrect login credentials user: " + user);
        session.setAttribute(SessionAttributes.MESSAGE,
                "<p class='error'>Incorrect login information provided</p>");
        response.sendRedirect(response.encodeRedirectURL("login.jsp"));
        return;
    } catch (final SQLException e) {
        throw new RuntimeException(e);
    } finally {
        SQLFunctions.close(connection);
    }
}

From source file:org.dspace.webmvc.utils.Authenticate.java

/**
 * Resume a previously interrupted request. This is invoked when a user has
 * been successfully authenticated. The request which led to authentication
 * will be resumed./*from   w  w  w  .ja  v a  2s. c  o  m*/
 * 
 * @param request
 *            <em>current</em> HTTP request
 * @param response
 *            HTTP response
 */
public static void resumeInterruptedRequest(HttpServletRequest request, HttpServletResponse response)
        throws IOException {
    HttpSession session = request.getSession();
    String originalURL = (String) session.getAttribute("interrupted.request.url");

    if (originalURL == null) {
        // If for some reason we don't have the original URL, redirect
        // to My DSpace
        originalURL = request.getContextPath() + "/mydspace";
    } else {
        // Set the flag in the session, so that when the redirect is
        // followed, we'll know to resume the interrupted request
        session.setAttribute("resuming.request", Boolean.TRUE);
    }

    // Send the redirect
    response.sendRedirect(response.encodeRedirectURL(originalURL));
}

From source file:edu.cornell.mannlib.vedit.forwarder.impl.UrlForwarder.java

public void doForward(HttpServletRequest request, HttpServletResponse response, EditProcessObject epo) {
    try {//from w  w  w. j av a2s . c  o m
        response.sendRedirect(response.encodeRedirectURL(theUrl));
    } catch (IOException ioe) {
        log.error("doForward() could not send redirect.");
    }
}

From source file:furkan.app.tictactoewebsocket.TicTacToeServlet.java

private void list(HttpServletRequest request, HttpServletResponse response) throws IOException {
    response.sendRedirect(response.encodeRedirectURL(request.getContextPath() + "/tictactoe"));
}

From source file:org.freeeed.search.web.interceptors.SessionInterceptor.java

private void redirectToLoginRequired(HttpServletResponse res) throws IOException {
    String url = res.encodeRedirectURL(WebConstants.LOGIN_REQUIRED_PAGE);
    res.sendRedirect(url);/*from  w w w . j  a va2 s.  c  o m*/
}