Example usage for org.springframework.http HttpStatus MOVED_TEMPORARILY

List of usage examples for org.springframework.http HttpStatus MOVED_TEMPORARILY

Introduction

In this page you can find the example usage for org.springframework.http HttpStatus MOVED_TEMPORARILY.

Prototype

HttpStatus MOVED_TEMPORARILY

To view the source code for org.springframework.http HttpStatus MOVED_TEMPORARILY.

Click Source Link

Document

302 Moved Temporarily .

Usage

From source file:com.hypersocket.auth.json.AuthenticatedController.java

@ExceptionHandler(RedirectException.class)
@ResponseStatus(value = HttpStatus.MOVED_TEMPORARILY)
public void redirectToLogin(HttpServletRequest request, HttpServletResponse response,
        RedirectException redirect) {//w  w  w  .  j  a  va2 s .  co m
    response.setHeader(LOCATION, redirect.getMessage());
}

From source file:eionet.webq.web.interceptor.CdrAuthorizationInterceptor.java

@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)
        throws Exception {
    String authorization = request.getHeader(AUTHORIZATION_HEADER);
    //        if (true) return PROCEED;
    if (StringUtils.isNotEmpty(authorization) || request.getParameter("auth") != null) {
        // if Basic auth is present in the request, then try to log in to CDR to test if it is valid token for given domain.
        // "auth" parameter is just meant for testing the CDR API in development environment - WebQ asks to authenticate.
        HttpHeaders headers = new HttpHeaders();
        headers.add(AUTHORIZATION_HEADER, authorization);
        //            return PROCEED;
        try {//from  w ww  .  ja  v  a  2s.  c  o m
            ResponseEntity<String> loginResponse = restOperations.postForEntity(
                    extractCdrUrl(request) + "/" + cdrLoginMethod, new HttpEntity<Object>(headers),
                    String.class);
            LOGGER.info("Response code received from CDR basic authorization request "
                    + loginResponse.getStatusCode());
            return PROCEED;
        } catch (HttpStatusCodeException e) {
            if (e.getStatusCode() != HttpStatus.UNAUTHORIZED) {
                LOGGER.warn("Authorization against CDR failed with unexpected HTTP status code", e);
            }
        }
    } else {
        // if Basic auth is not present, then test if user is already authorised in this domain
        // by using provided cookies to fetch CDR envelope properties page.
        Cookie[] cookies = request.getCookies();
        if (cookies != null) {
            HttpHeaders headers = new HttpHeaders();
            for (Cookie cookie : cookies) {
                // put ZopeId parameter to request header. It works only when the value is surrounded with quotes.
                headers.add("Cookie", cookiesConverter.convertCookieToString(cookie));
            }
            String urlToFetch = extractCdrEnvelopeUrl(request) + "/" + cdrEnvelopePropertiesMethod;
            //ResponseEntity<String> loginResponse = restOperations.exchange(urlToFetch, HttpMethod.GET,
            //        new HttpEntity<Object>(headers), String.class);

            HttpResponse responseFromCdr = fetchUrlWithoutRedirection(urlToFetch, headers);
            try {
                int statusCode = responseFromCdr.getStatusLine().getStatusCode();

                LOGGER.info("Response code received from CDR envelope request using cookies " + statusCode);
                if (statusCode == HttpStatus.OK.value()) {
                    request.setAttribute(PARSED_COOKIES_ATTRIBUTE,
                            cookiesConverter.convertCookiesToString(cookies));
                    return PROCEED;
                } else if ((statusCode == HttpStatus.MOVED_PERMANENTLY.value()
                        || statusCode == HttpStatus.MOVED_TEMPORARILY.value())
                        && responseFromCdr.getFirstHeader("Location") != null) {
                    // redirect to CDR login page
                    String redirectUrl = extractCdrUrl(request)
                            + responseFromCdr.getFirstHeader("Location").getValue();
                    LOGGER.info("Redirect to " + redirectUrl);
                    response.sendRedirect(redirectUrl);
                }
            } catch (HttpStatusCodeException e) {
                if (e.getStatusCode() != HttpStatus.UNAUTHORIZED) {
                    LOGGER.warn("Fetching CDR envelope page failed with unexpected HTTP status code", e);
                }
            }
        }
    }

    if (isFailureCountsEqualsToAllowedFailuresCount()) {
        request.setAttribute(AUTHORIZATION_FAILED_ATTRIBUTE, AUTHORIZATION_FAILED_ATTRIBUTE);
        session.removeAttribute(AUTHORIZATION_TRY_COUNT);
        return PROCEED;
    }

    increaseFailedAuthorizationsCount();
    response.addHeader("WWW-Authenticate", "Basic realm=\"Please login to use webforms.\"");
    response.sendError(HttpServletResponse.SC_UNAUTHORIZED);
    return STOP_REQUEST_PROPAGATION;
}