Example usage for org.springframework.web.client HttpStatusCodeException getStatusText

List of usage examples for org.springframework.web.client HttpStatusCodeException getStatusText

Introduction

In this page you can find the example usage for org.springframework.web.client HttpStatusCodeException getStatusText.

Prototype

public String getStatusText() 

Source Link

Document

Return the HTTP status text.

Usage

From source file:org.eclipse.cft.server.core.internal.CloudErrorUtil.java

/**
 * /*from   ww w.j a va 2 s  . c  om*/
 * @param error
 * @return Error message containing description of the error. If no
 * description is found, it will return the exception error or HTTP status
 * text message, if present. May return null if no message can be resolved.
 */
protected static String getHttpErrorMessage(HttpStatusCodeException error) {
    String message = null;
    if (error instanceof CloudFoundryException) {
        message = ((CloudFoundryException) error).getDescription();
    }

    if (message == null) {
        message = error.getMessage();
        if (message == null) {
            message = error.getStatusText();
            if (message == null) {
                message = error.getResponseBodyAsString();
            }
        }
    }
    return message;
}

From source file:com.weibo.handler.ErrorCodeHandler.java

public ErrorCode handle(HttpStatusCodeException error) {
    ObjectMapper objectMapper = new ObjectMapper();
    ErrorCode errorCode = new ErrorCode();
    errorCode.setRequest(StringUtils.EMPTY);
    errorCode.setErrorCode(error.getStatusCode().toString());
    errorCode.setError(error.getStatusText());
    try {/*from w ww. j  a v  a 2  s .co m*/
        errorCode = objectMapper.readValue(error.getResponseBodyAsByteArray(), ErrorCode.class);
    } catch (JsonParseException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (JsonMappingException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (IOException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    }
    return errorCode;
}

From source file:com.t163.handler.T163ErrorCodeHandler.java

public T163ErrorCode handle(HttpStatusCodeException error) {
    ObjectMapper objectMapper = new ObjectMapper();
    T163ErrorCode errorCode = new T163ErrorCode();
    errorCode.setRequest(StringUtils.EMPTY);
    errorCode.setErrorCode(error.getStatusCode().toString());
    errorCode.setError(error.getStatusText());
    try {//from www  . j  a v a 2  s . c  om
        errorCode = objectMapper.readValue(error.getResponseBodyAsByteArray(), T163ErrorCode.class);
    } catch (JsonParseException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (JsonMappingException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    } catch (IOException e) {
        log.error(ExceptionUtils.getFullStackTrace(e));
    }
    return errorCode;
}

From source file:com.netflix.genie.web.controllers.JobRestController.java

/**
 * Kill job based on given job ID.//from   w  ww.  j av  a 2 s. co m
 *
 * @param id            id for job to kill
 * @param forwardedFrom The host this request was forwarded from if present
 * @param request       the servlet request
 * @param response      the servlet response
 * @throws GenieException   For any error
 * @throws IOException      on redirect error
 * @throws ServletException when trying to handle the request
 */
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
@ResponseStatus(HttpStatus.ACCEPTED)
public void killJob(@PathVariable("id") final String id,
        @RequestHeader(name = JobConstants.GENIE_FORWARDED_FROM_HEADER, required = false) final String forwardedFrom,
        final HttpServletRequest request, final HttpServletResponse response)
        throws GenieException, IOException, ServletException {
    log.info("[killJob] Called for job id: {}. Forwarded from: {}", id, forwardedFrom);

    // If forwarded from is null this request hasn't been forwarded at all. Check we're on the right node
    if (this.jobsProperties.getForwarding().isEnabled() && forwardedFrom == null) {
        final String jobHostname = this.jobSearchService.getJobHost(id);
        if (!this.hostName.equals(jobHostname)) {
            log.info("Job {} is not on this node. Forwarding kill request to {}", id, jobHostname);
            final String forwardUrl = buildForwardURL(request, jobHostname);
            try {
                //Need to forward job
                restTemplate.execute(forwardUrl, HttpMethod.DELETE,
                        forwardRequest -> copyRequestHeaders(request, forwardRequest),
                        (final ClientHttpResponse forwardResponse) -> {
                            response.setStatus(HttpStatus.ACCEPTED.value());
                            copyResponseHeaders(response, forwardResponse);
                            return null;
                        });
            } catch (HttpStatusCodeException e) {
                log.error("Failed killing job on {}. Error: {}", forwardUrl, e.getMessage());
                response.sendError(e.getStatusCode().value(), e.getStatusText());
            } catch (Exception e) {
                log.error("Failed killing job on {}. Error: {}", forwardUrl, e.getMessage());
                response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
            }

            // No need to do anything on this node
            return;
        }
    }

    log.info("Job {} is on this node. Attempting to kill.", id);
    // Job is on this node so try to kill it
    this.jobCoordinatorService.killJob(id);
    response.setStatus(HttpStatus.ACCEPTED.value());
}

From source file:com.netflix.genie.web.controllers.JobRestController.java

/**
 * Get the job output directory./*from   w w w .j a  v  a2s  . c  om*/
 *
 * @param id            The id of the job to get output for
 * @param forwardedFrom The host this request was forwarded from if present
 * @param request       the servlet request
 * @param response      the servlet response
 * @throws IOException      on redirect error
 * @throws ServletException when trying to handle the request
 * @throws GenieException   on any Genie internal error
 */
@RequestMapping(value = { "/{id}/output", "/{id}/output/",
        "/{id}/output/**" }, method = RequestMethod.GET, produces = MediaType.ALL_VALUE)
public void getJobOutput(@PathVariable("id") final String id,
        @RequestHeader(name = JobConstants.GENIE_FORWARDED_FROM_HEADER, required = false) final String forwardedFrom,
        final HttpServletRequest request, final HttpServletResponse response)
        throws IOException, ServletException, GenieException {
    log.info("[getJobOutput] Called for job with id: {}", id);

    // if forwarded from isn't null it's already been forwarded to this node. Assume data is on this node.
    if (this.jobsProperties.getForwarding().isEnabled() && forwardedFrom == null) {
        // TODO: It's possible that could use the JobMonitorCoordinator to check this in memory
        //       However that could get into problems where the job finished or died
        //       and it would return false on check if the job with given id is running on that node
        final String jobHostname = this.jobSearchService.getJobHost(id);
        if (!this.hostName.equals(jobHostname)) {
            log.info("Job {} is not or was not run on this node. Forwarding to {}", id, jobHostname);
            final String forwardUrl = buildForwardURL(request, jobHostname);
            try {
                this.restTemplate.execute(forwardUrl, HttpMethod.GET,
                        forwardRequest -> copyRequestHeaders(request, forwardRequest),
                        new ResponseExtractor<Void>() {
                            @Override
                            public Void extractData(final ClientHttpResponse forwardResponse)
                                    throws IOException {
                                response.setStatus(HttpStatus.OK.value());
                                copyResponseHeaders(response, forwardResponse);
                                // Documentation I could find pointed to the HttpEntity reading the bytes off
                                // the stream so this should resolve memory problems if the file returned is large
                                ByteStreams.copy(forwardResponse.getBody(), response.getOutputStream());
                                return null;
                            }
                        });
            } catch (HttpStatusCodeException e) {
                log.error("Failed getting the remote job output from {}. Error: {}", forwardUrl,
                        e.getMessage());
                response.sendError(e.getStatusCode().value(), e.getStatusText());
            } catch (Exception e) {
                log.error("Failed getting the remote job output from {}. Error: {}", forwardUrl,
                        e.getMessage());
                response.sendError(HttpStatus.INTERNAL_SERVER_ERROR.value(), e.getMessage());
            }

            //No need to search on this node
            return;
        }
    }

    log.info("Job {} is running or was run on this node. Fetching requested resource...", id);
    final String path = ControllerUtils.getRemainingPath(request);
    if (StringUtils.isNotBlank(path)) {
        request.setAttribute(GenieResourceHttpRequestHandler.GENIE_JOB_IS_ROOT_DIRECTORY, false);
    } else {
        request.setAttribute(GenieResourceHttpRequestHandler.GENIE_JOB_IS_ROOT_DIRECTORY, true);
    }
    log.debug("PATH = {}", path);
    request.setAttribute(HandlerMapping.PATH_WITHIN_HANDLER_MAPPING_ATTRIBUTE, id + "/" + path);

    this.resourceHttpRequestHandler.handleRequest(request, response);
}

From source file:org.kaaproject.kaa.server.common.admin.TestMessage.java

public String getMessage(KaaRestTemplate kaaRestTemplate, TestHttpMethods httpMethods) {
    String result;/*from   w w  w  .  j  av  a  2  s . c  o m*/
    try {
        switch (httpMethods) {
        case GET: {
            String httpResult = kaaRestTemplate.getForObject("http://localhost:8080/kaaTest", String.class);
            result = "Message SUCCESS result: " + httpResult;
            break;
        }
        case PUT: {
            String putText = "putText";
            kaaRestTemplate.put("http://localhost:8080/kaaTest/put", putText);
            result = "Result SUCCESS" + putText;
            break;
        }
        case DELETE: {
            kaaRestTemplate.delete("http://localhost:8080/kaaTest/delete");
            result = "Result SUCCESS";
            break;
        }
        case POST: {
            String httpResult = kaaRestTemplate.postForObject("http://localhost:8080/kaaTest/post",
                    new String("postText"), String.class);
            result = "Message SUCCESS result: " + httpResult;
            break;
        }
        default: {
            result = "ERROR";
        }
        }

    } catch (HttpStatusCodeException e) {
        result = "Get FAILED with HttpStatusCode: " + e.getStatusCode() + "|" + e.getStatusText();
    } catch (RuntimeException e) {
        result = "Get FAILED\n" + ExceptionUtils.getFullStackTrace(e);
    }
    return result;
}