Example usage for javax.servlet.http HttpServletResponse SC_MOVED_PERMANENTLY

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

Introduction

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

Prototype

int SC_MOVED_PERMANENTLY

To view the source code for javax.servlet.http HttpServletResponse SC_MOVED_PERMANENTLY.

Click Source Link

Document

Status code (301) indicating that the resource has permanently moved to a new location, and that future references should use a new URI with their requests.

Usage

From source file:core.NiprSyncController.java

@RequestMapping("/")
public void index(HttpServletRequest request, HttpServletResponse response) {

    response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
    response.setHeader("Location", "/login.html");
}

From source file:com.laxser.blitz.web.instruction.RedirectInstruction.java

@Override
public void doRender(Invocation inv) throws IOException {
    String location = resolvePlaceHolder(location(), inv);
    if (sc == null || sc == 302) {
        inv.getResponse().sendRedirect(location);
    } else {/*  www.jav a  2 s.  co  m*/
        Assert.isTrue(sc == HttpServletResponse.SC_MOVED_PERMANENTLY);
        inv.getResponse().setStatus(sc);
        inv.getResponse().setHeader("Location", location);
    }
}

From source file:com.sinosoft.one.mvc.web.instruction.RedirectInstruction.java

@Override
public void doRender(Invocation inv) throws IOException {
    String location = resolvePlaceHolder(location(), inv);
    if (sc == null || sc == 302) {
        String contextPath = inv.getRequest().getContextPath();
        if (!location.startsWith(contextPath)) {
            if (!location.startsWith("/")) {
                location = "/" + location;
            }/*from ww w .  j  a  va 2s .  c  o m*/
            location = contextPath + location;
        }
        inv.getResponse().sendRedirect(location);
    } else {
        Assert.isTrue(sc == HttpServletResponse.SC_MOVED_PERMANENTLY);
        inv.getResponse().setStatus(sc);
        inv.getResponse().setHeader("Location", location);
    }
}

From source file:com.laxser.blitz.web.instruction.RedirectInstruction.java

/**
 * 301/*from   w  ww  .j av  a 2 s . c om*/
 * 
 * @return
 */
public RedirectInstruction permanently() {
    this.sc = HttpServletResponse.SC_MOVED_PERMANENTLY;
    return this;
}

From source file:sf.net.experimaestro.server.ContentServlet.java

protected void doGet(HttpServletRequest request, HttpServletResponse response)
        throws ServletException, IOException {

    URL url = ContentServlet.class.getResource(format("/web%s", request.getRequestURI()));

    if (url != null) {
        FileSystemManager fsManager = VFS.getManager();
        FileObject file = fsManager.resolveFile(url.toExternalForm());
        if (file.getType() == FileType.FOLDER) {
            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", format("%sindex.html", request.getRequestURI()));
            return;
        }//from   ww  w .  ja  v a2 s  .c  om

        String filename = url.getFile();
        if (filename.endsWith(".html"))
            response.setContentType("text/html");
        else if (filename.endsWith(".png"))
            response.setContentType("image/png");
        else if (filename.endsWith(".css"))
            response.setContentType("text/css");
        response.setStatus(HttpServletResponse.SC_OK);

        final ServletOutputStream out = response.getOutputStream();
        InputStream in = url.openStream();
        byte[] buffer = new byte[8192];
        int read;
        while ((read = in.read(buffer)) > 0) {
            out.write(buffer, 0, read);
        }
        out.flush();
        in.close();
        return;
    }

    // Not found
    error404(request, response);

}

From source file:com.google.gwt.site.webapp.server.resources.ContentServlet.java

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    String fullPath = normalizePath(req.getRequestURI());

    Entity e = getResourceByKey(fullPath);

    if (e == null) {
        // temporary try to find the resource with .html appended
        // due to redirects from developers.google.com
        if (!fullPath.endsWith("/")) {
            fullPath = fullPath + ".html";
            e = getResourceByKey(fullPath);
            if (e != null) {
                // redirect so we use correct urls!
                resp.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
                resp.setHeader("Location", "/" + fullPath);
                return;
            }//w ww  .j a  v  a 2 s. c  o m
        }
        resp.sendError(404);
        return;
    }
    String html = ((Text) e.getProperty("html")).getValue();

    setContentTypeByFileEnding(resp, fullPath);

    if (isBinaryFile(fullPath)) {
        byte[] decodeBase64 = org.apache.commons.codec.binary.Base64.decodeBase64(html.getBytes("UTF-8"));
        resp.getOutputStream().write(decodeBase64);
    } else {
        resp.getWriter().write(html);
    }
}

From source file:io.wcm.samples.handler.controller.http.Redirect.java

@PostConstruct
protected void activate() throws IOException {
    // resolve link of redirect page
    String redirectUrl = linkHandler.get(resource).buildUrl();

    // in preview/publish mode redirect to target
    if ((wcmMode == WCMMode.DISABLED || wcmMode == WCMMode.PREVIEW) && StringUtils.isNotEmpty(redirectUrl)) {
        if (StringUtils.equals(redirectStatus, "301")) {
            response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
            response.setHeader("Location", redirectUrl);
        } else {//from  w w w.  j a v a2  s .  c o m
            response.sendRedirect(redirectUrl);
        }
        renderPage = false;
    } else if (wcmMode == WCMMode.DISABLED) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        renderPage = false;
    }
}

From source file:io.wcm.handler.link.ui.Redirect.java

@PostConstruct
private void activate() throws IOException {
    // resolve link of redirect page
    String redirectUrl = linkHandler.get(resource).buildUrl();

    // in publish mode redirect to target
    if (wcmMode == WCMMode.DISABLED) {
        renderPage = false;/*from w ww .j  a  v  a 2  s .co  m*/
        if (StringUtils.isNotEmpty(redirectUrl)) {
            if (StringUtils.equals(redirectStatus,
                    Integer.toString(HttpServletResponse.SC_MOVED_TEMPORARILY))) {
                response.sendRedirect(redirectUrl);
            } else {
                response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
                response.setHeader("Location", redirectUrl);
            }
        } else {
            response.sendError(HttpServletResponse.SC_NOT_FOUND);
        }
    }
}

From source file:com.meltmedia.cadmium.servlets.RedirectFilter.java

@Override
public void doFilter(ServletRequest req, ServletResponse resp, FilterChain chain)
        throws IOException, ServletException {
    Redirect redir = null;/*from ww  w .  ja v  a 2s  .c o m*/
    try {
        if (redirect != null && req instanceof HttpServletRequest && resp instanceof HttpServletResponse) {
            HttpServletRequest request = (HttpServletRequest) req;
            HttpServletResponse response = (HttpServletResponse) resp;
            String path = request.getRequestURI();
            String queryString = request.getQueryString();
            log.trace("Checking for existing redirect [{}?{}]", path, queryString);
            redir = redirect.requestMatches(path, queryString);
            if (redir != null) {
                String redirectTo = redir.getUrlSubstituted();
                if (StringUtils.isNotBlank(queryString) && !redirectTo.contains("?")) {

                    redirectTo += "?" + queryString;
                    log.debug("adding query string to redirect path: {}", redirectTo);
                }
                response.setHeader("Location", redirectTo);
                response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
                log.debug("Location response header: {}", redirectTo);
                return;
            }
        } else {
            log.trace("Redirect and/or req and resp are not http");
        }
        try {
            chain.doFilter(req, resp);
        } catch (IOException ioe) {
            log.trace("Failed down stream from redirect filter.", ioe);
            throw ioe;
        } catch (ServletException se) {
            log.trace("Failed down stream from redirect filter.", se);
            throw se;
        } catch (Throwable t) {
            StringWriter str = new StringWriter();
            t.printStackTrace(new PrintWriter(str));
            log.trace("Failed down stream from redirect filter: " + str.toString(), t);
            ServletException se = new ServletException(t);
            throw se;

        }
    } catch (Throwable t) {
        StringWriter str = new StringWriter();
        t.printStackTrace(new PrintWriter(str));
        log.debug("Failed in redirect filter: " + str.toString(), t);
        ServletException se = new ServletException(t);
        throw se;
    }
}

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

/**
 * Returns true if processing took place, even if it fails.
 *///from   w  w w. j a  v  a  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;
}