Example usage for javax.servlet.http HttpServletResponse SC_MOVED_TEMPORARILY

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

Introduction

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

Prototype

int SC_MOVED_TEMPORARILY

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

Click Source Link

Document

Status code (302) indicating that the resource has temporarily moved to another location, but that future references should still use the original URI to access the resource.

Usage

From source file:RedirectNewLocation.java

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

    PrintWriter out = response.getWriter();

    response.setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
    response.setHeader("Location", "http://www.java2s.com");

    response.setContentType("text/html");
    return;/* w w  w.  j av  a  2 s .  c  o m*/
}

From source file:org.craftercms.security.authentication.impl.LogoutSuccessHandlerImplTest.java

@Test
public void testRedirectToTargetUrl() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);

    handler.handle(context, mock(Authentication.class));

    assertEquals(TARGET_URl, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());//from w  ww . j  a va 2  s  . co m
}

From source file:org.craftercms.security.authentication.impl.LoginFailureHandlerImplTest.java

@Test
public void testRedirectToTargetUrl() throws Exception {
    handler.setTargetUrl(TARGET_URL);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);

    handler.handle(context, new AuthenticationException());

    assertEquals(TARGET_URL, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());/*from www.  j  a  v  a 2s  .co m*/
}

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;/*  w ww  . ja 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.jl.crm.web.OAuthTest.java

@Test
public void formLoginForContentAll() throws Exception {
    request.addHeader("Accept", MediaType.ALL_VALUE);

    springSecurityFilterChain.doFilter(request, response, chain);

    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
}

From source file:org.craftercms.security.authentication.impl.AuthenticationRequiredHandlerImplTest.java

@Test
public void testRedirectToLoginFormUrl() throws Exception {
    handler.setLoginFormUrl(LOGIN_FORM_URL);

    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);

    handler.handle(context, new AuthenticationRequiredException(""));

    verify(requestCache).saveRequest(request, response);

    assertEquals(LOGIN_FORM_URL, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());/* w  w  w  . j a v  a2 s .  c  om*/
}

From source file:org.geoserver.ows.AbstractURLPublisher.java

protected ModelAndView handleRequestInternal(HttpServletRequest request, HttpServletResponse response)
        throws Exception {
    URL url = getUrl(request);//  w  w  w . j  av  a  2  s .c om

    // if not found return a 404
    if (url == null) {
        response.sendError(HttpServletResponse.SC_NOT_FOUND);
        return null;
    }

    File file = DataUtilities.urlToFile(url);
    if (file != null && file.exists() && file.isDirectory()) {
        String uri = request.getRequestURI().toString();
        uri += uri.endsWith("/") ? "index.html" : "/index.html";

        response.addHeader("Location", uri);
        response.sendError(HttpServletResponse.SC_MOVED_TEMPORARILY);

        return null;
    }

    // set the mime if known by the servlet container, set nothing otherwise
    // (Tomcat behaves like this when it does not recognize the file format)
    String mime = getServletContext().getMimeType(new File(url.getFile()).getName());
    if (mime != null) {
        response.setContentType(mime);
    }

    // set the content length and content type
    URLConnection connection = null;
    InputStream input = null;
    try {
        connection = url.openConnection();
        long length = connection.getContentLength();
        if (length > 0 && length <= Integer.MAX_VALUE) {
            response.setContentLength((int) length);
        }

        long lastModified = connection.getLastModified();
        if (lastModified > 0) {
            SimpleDateFormat format = new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss", Locale.ENGLISH);
            format.setTimeZone(TimeZone.getTimeZone("GMT"));
            String formatted = format.format(new Date(lastModified)) + " GMT";
            response.setHeader("Last-Modified", formatted);
        }

        // Guessing the charset (and closing the stream)
        EncodingInfo encInfo = null;
        OutputStream output = null;
        final byte[] b4 = new byte[4];
        int count = 0;
        // open the output
        input = connection.getInputStream();

        // Read the first four bytes, and determine charset encoding
        count = input.read(b4);
        encInfo = XmlCharsetDetector.getEncodingName(b4, count);
        response.setCharacterEncoding(encInfo.getEncoding() != null ? encInfo.getEncoding() : "UTF-8");

        // send out the first four bytes read
        output = response.getOutputStream();
        output.write(b4, 0, count);

        // copy the content to the output
        byte[] buffer = new byte[8192];
        int n = -1;
        while ((n = input.read(buffer)) != -1) {
            output.write(buffer, 0, n);
        }
    } finally {
        if (input != null)
            input.close();
    }

    return null;
}

From source file:org.craftercms.security.authentication.impl.LoginSuccessHandleImplTest.java

@Test
public void testRedirectToSavedRequest() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);
    SavedRequest savedRequest = mock(SavedRequest.class);

    when(savedRequest.getRedirectUrl()).thenReturn(SAVED_REQUEST_URL);
    when(requestCache.getRequest(request, response)).thenReturn(savedRequest);

    handler.handle(context, mock(Authentication.class));

    assertEquals(SAVED_REQUEST_URL, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());/*from   w w  w.  java 2 s .c o m*/
}

From source file:org.craftercms.security.authentication.impl.LoginSuccessHandleImplTest.java

@Test
public void testRedirectToDefaultTargetUrl() throws Exception {
    MockHttpServletRequest request = new MockHttpServletRequest();
    MockHttpServletResponse response = new MockHttpServletResponse();
    RequestContext context = new RequestContext(request, response);

    handler.handle(context, mock(Authentication.class));

    assertEquals(DEFAULT_TARGET_URL, response.getRedirectedUrl());
    assertEquals(HttpServletResponse.SC_MOVED_TEMPORARILY, response.getStatus());
    assertTrue(response.isCommitted());/*  w w  w .jav  a2s  .  c o m*/
}

From source file:net.sf.ehcache.constructs.web.GenericResponseWrapper.java

/**
 * Send the redirect. If the response is not ok, most of the logic is bypassed and the error is sent raw.
 * Also, the content is not cached./*from   w ww.ja  v a2s  .c o  m*/
 *
 * @param string the URL to redirect to
 * @throws IOException
 */
public void sendRedirect(String string) throws IOException {
    statusCode = HttpServletResponse.SC_MOVED_TEMPORARILY;
    super.sendRedirect(string);
}