Example usage for org.springframework.http HttpStatus valueOf

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

Introduction

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

Prototype

public static HttpStatus valueOf(int statusCode) 

Source Link

Document

Return the enum constant of this type with the specified numeric value.

Usage

From source file:org.dthume.spring.http.client.httpcomponents.HttpComponentsClientHttpResponse.java

/** {@inheritDoc} */
public HttpStatus getStatusCode() throws IOException {
    final int status = response.getStatusLine().getStatusCode();
    return HttpStatus.valueOf(status);
}

From source file:JettyEngine.java

@Override
public ResponseEntity<String> submit(JCurlRequestOptions requestOptions) throws Exception {

    final SslContextFactory sslContextFactory = new SslContextFactory();
    sslContextFactory.setSslContext(SSLContext.getDefault());
    HttpClient httpClient = new HttpClient(sslContextFactory);
    // Configure HttpClient here
    httpClient.start();//  w  w  w.  j av  a2 s.co  m

    ResponseEntity<String> responseEntity = null;

    for (int i = 0; i < requestOptions.getCount(); i++) {
        final Request request = httpClient.newRequest(requestOptions.getUrl());

        for (Map.Entry<String, String> e : requestOptions.getHeaderMap().entrySet()) {
            request.header(e.getKey(), e.getValue());
        }

        System.out.println("\nSending 'GET' request to URL : " + requestOptions.getUrl());
        final ContentResponse response = request.send();

        int responseCode = response.getStatus();
        System.out.println("Response Code : " + responseCode);

        String responseContent = IOUtils.toString(response.getContent(), "utf-8");

        //print result
        System.out.println(responseContent);

        responseEntity = new ResponseEntity<String>(responseContent, HttpStatus.valueOf(responseCode));
    }

    httpClient.stop();
    return responseEntity;
}

From source file:com.github.lynxdb.server.api.http.handlers.EpError.java

@RequestMapping(value = "", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity error(HttpServletRequest request, HttpServletResponse response) {

    HttpStatus status = HttpStatus.valueOf((int) request.getAttribute("javax.servlet.error.status_code"));

    ObjectMapper mapper = new ObjectMapper();
    ObjectNode error = mapper.createObjectNode();
    error.put("code", status.value());
    error.put("message", status.getReasonPhrase());
    error.put("details", getErrorAttributes(request, true).get("message").toString());
    if (getErrorAttributes(request, true).get("exception") != null) {
        error.put("trace", getErrorAttributes(request, true).get("exception").toString() + "\n"
                + getErrorAttributes(request, true).get("trace").toString());
    }/*  w  ww .  j  a v a2  s  .  com*/

    return ResponseEntity.status(status).body(error.toString());
}

From source file:com.nimble.http.client.GzipClientHttpResponse.java

public HttpStatus getStatusCode() throws IOException {
    return HttpStatus.valueOf(this.connection.getResponseCode());
}

From source file:org.cateproject.test.functional.mockmvc.MockWebResponseBuilder.java

private String statusMessage(int statusCode) {
    String errorMessage = response.getErrorMessage();
    if (errorMessage != null) {
        return errorMessage;
    }// w  ww. j  av  a  2  s.  com
    try {
        return HttpStatus.valueOf(statusCode).getReasonPhrase();
    } catch (IllegalArgumentException useDefault) {
    }
    ;
    return "N/A";
}

From source file:fr.esiea.esieaddress.controllers.exception.ExceptionHandlerCtrl.java

@ExceptionHandler(value = { NotConnectedException.class })
protected ResponseEntity<Object> handleException(NotConnectedException ex, WebRequest request) {
    // avoid a useless log each time that the user ping (open the web-app)
    Object model = ex.getModel();
    return new ResponseEntity<Object>(model, HttpStatus.valueOf(ex.getStatus()));
}

From source file:fr.esiea.windmeal.controller.exception.handler.ExceptionHandlerCtrl.java

@ExceptionHandler(value = { NotConnectedException.class })
protected ResponseEntity<Object> handleException(NotConnectedException ex, WebRequest request) {
    //Permit to avoid error in log every time that the client ping to know
    //if the user is connected and get back information about him
    Object model = ex.getModel();
    return new ResponseEntity<Object>(model, HttpStatus.valueOf(ex.getStatus()));
}

From source file:de.hska.ld.core.client.ClientRequest.java

public HttpStatus getResponseStatusCode() {
    return HttpStatus.valueOf(this.response.getStatusLine().getStatusCode());
}

From source file:io.github.howiefh.jeews.modules.oauth2.controller.AuthorizeController.java

@RequestMapping("/authentication")
public Object authorize(HttpServletRequest request) throws URISyntaxException, OAuthSystemException {
    try {/*from   ww w.  jav a  2  s. c om*/

        // OAuth ?
        OAuthAuthzRequest oauthRequest = new OAuthAuthzRequest(request);

        // id?
        if (!oAuthService.checkClientId(oauthRequest.getClientId())) {
            OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_BAD_REQUEST)
                    .setError(OAuthError.TokenResponse.INVALID_CLIENT)
                    .setErrorDescription(Constants.INVALID_CLIENT_DESCRIPTION).buildJSONMessage();
            return new ResponseEntity<String>(response.getBody(),
                    HttpStatus.valueOf(response.getResponseStatus()));
        }

        Subject subject = SecurityUtils.getSubject();
        // ?
        if (!subject.isAuthenticated()) {
            if (!login(subject, request)) {// ?
                // TODO
                HttpHeaders headers = new HttpHeaders();
                headers.setLocation(new URI(loginUrl));
                return new ResponseEntity<Object>(headers, HttpStatus.UNAUTHORIZED);
            }
        }

        String username = (String) subject.getPrincipal();
        // ???
        String authorizationCode = null;
        // responseType??CODE?TOKEN
        String responseType = oauthRequest.getParam(OAuth.OAUTH_RESPONSE_TYPE);
        OAuthIssuerImpl oauthIssuerImpl = new OAuthIssuerImpl(new MD5Generator());
        // OAuth?
        OAuthASResponse.OAuthAuthorizationResponseBuilder builder = OAuthASResponse
                .authorizationResponse(request, HttpServletResponse.SC_FOUND);
        if (responseType.equals(ResponseType.CODE.toString())) {
            authorizationCode = oauthIssuerImpl.authorizationCode();
            oAuthService.addAuthCode(authorizationCode, username);
            // ??
            builder.setCode(authorizationCode);
        } else if (responseType.equals(ResponseType.TOKEN.toString())) {
            final String accessToken = oauthIssuerImpl.accessToken();
            oAuthService.addAccessToken(accessToken, username);
            builder.setAccessToken(accessToken);
            builder.setParam("token_type", TokenType.BEARER.toString());
            builder.setExpiresIn(oAuthService.getExpireIn());
        }

        // ???
        String redirectURI = oauthRequest.getParam(OAuth.OAUTH_REDIRECT_URI);

        // ?
        final OAuthResponse response = builder.location(redirectURI).buildQueryMessage();

        // ?OAuthResponseResponseEntity?
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(new URI(response.getLocationUri()));
        return new ResponseEntity<Object>(headers, HttpStatus.valueOf(response.getResponseStatus()));
    } catch (OAuthProblemException e) {
        // ?
        String redirectUri = e.getRedirectUri();
        if (OAuthUtils.isEmpty(redirectUri)) {
            // redirectUri
            return new ResponseEntity<String>("OAuth callback url needs to be provided by client!!!",
                    HttpStatus.NOT_FOUND);
        }

        // ??error=
        final OAuthResponse response = OAuthASResponse.errorResponse(HttpServletResponse.SC_FOUND).error(e)
                .location(redirectUri).buildQueryMessage();
        HttpHeaders headers = new HttpHeaders();
        headers.setLocation(new URI(response.getLocationUri()));
        return new ResponseEntity<Object>(headers, HttpStatus.valueOf(response.getResponseStatus()));
    }
}

From source file:com.evolveum.midpoint.web.page.error.PageError.java

public PageError(Integer code, Exception ex) {
    this.code = code;

    if (ex == null) {
        // Log this on debug level, this is normal during application initialization
        LOGGER.debug("Creating error page for code {}, no exception", code);
    } else {//w w w  . ja v  a 2 s .c  o  m
        LOGGER.warn("Creating error page for code {}, exception {}: {}", ex.getClass().getName(),
                ex.getMessage(), ex);
    }

    Label codeLabel = new Label(ID_CODE, code);
    add(codeLabel);

    String errorLabel = "Unexpected error";
    if (code != null) {
        HttpStatus httpStatus = HttpStatus.valueOf(code);
        if (httpStatus != null) {
            errorLabel = httpStatus.getReasonPhrase();
        }
    }
    Label labelLabel = new Label(ID_LABEL, errorLabel);
    add(labelLabel);

    if (ex != null) {
        exClass = ex.getClass().getName();
        exMessage = ex.getMessage();
    }

    final IModel<String> message = new AbstractReadOnlyModel<String>() {

        @Override
        public String getObject() {
            if (exClass == null) {
                return null;
            }

            SimpleDateFormat df = new SimpleDateFormat();
            return df.format(new Date()) + "\t" + exClass + ": " + exMessage;
        }
    };

    Label label = new Label(ID_MESSAGE, message);
    label.add(new VisibleEnableBehaviour() {

        @Override
        public boolean isVisible() {
            return StringUtils.isNotEmpty(message.getObject());
        }
    });
    add(label);

    AjaxButton back = new AjaxButton(ID_BACK, createStringResource("PageError.button.back")) {

        @Override
        public void onClick(AjaxRequestTarget target) {
            if (WebMiscUtil.isAuthorized(AuthorizationConstants.AUTZ_UI_DASHBOARD_URL,
                    AuthorizationConstants.AUTZ_UI_HOME_ALL_URL)) {
                setResponsePage(PageDashboard.class);
            } else {
                setResponsePage(PageSelfDashboard.class);
            }
        }
    };
    add(back);
}