Java Code Examples for play.libs.ws.WSResponse#getStatus()

The following examples show how to use play.libs.ws.WSResponse#getStatus() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: HttpRequestHelper.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
public static void checkStatusCode(WSResponse response, WSRequest request)
        throws ResourceNotFoundException, ConflictingResourceException, ExternalDependencyException {
    if (response.getStatus() == 200) {
        return;
    }

    log.info(String.format("Config returns %s for request %s",
            response.getStatus(), request.getUrl().toString()));

    switch (response.getStatus()) {
        case 404:
            throw new ResourceNotFoundException(String.format("%s, request URL = %s,",
                    response.getBody(), request.getUrl()));
        case 409:
            throw new ConflictingResourceException(String.format("%s, request URL = %s,",
                    response.getBody(), request.getUrl()));

        default:
            throw new ExternalDependencyException(
                    String.format("WS-request failed, status code = %s, content = %s, request URL = %s",
                            response.getStatus(), response.getBody(), request.getUrl()));
    }
}
 
Example 2
Source File: StatusService.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
private StatusResultServiceModel PingService(String serviceName, String serviceURL) {
    StatusResultServiceModel result = new StatusResultServiceModel(false, serviceName + " check failed");

    try {
        WSResponse response = this.wsRequestBuilder
            .prepareRequest(serviceURL + "/status")
            .get()
            .toCompletableFuture()
            .get();

        if (response.getStatus() != HttpStatus.SC_OK) {
            result.setMessage("Status code: " + response.getStatus() + ", Response: " + response.getBody());
        } else {
            ObjectMapper mapper = new ObjectMapper();
            StatusServiceModel data = mapper.readValue(response.getBody(), StatusServiceModel.class);
            result.setStatusResultServiceModel(data.getStatus());
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }

    return result;
}
 
Example 3
Source File: StatusService.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
private StatusResultServiceModel PingService(String serviceName, String serviceURL) {
    StatusResultServiceModel result = new StatusResultServiceModel(false, serviceName + " check failed");

    try {
        WSResponse response = this.wsRequestBuilder
            .prepareRequest(serviceURL + "/status")
            .get()
            .toCompletableFuture()
            .get();

        if (response.getStatus() != HttpStatus.SC_OK) {
            result.setMessage("Status code: " + response.getStatus() + ", Response: " + response.getBody());
        } else {
            ObjectMapper mapper = new ObjectMapper();
            StatusServiceModel data = mapper.readValue(response.getBody(), StatusServiceModel.class);
            result.setStatusResultServiceModel(data.getStatus());
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }

    return result;
}
 
Example 4
Source File: StatusService.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
private StatusResultServiceModel PingService(String serviceName, String serviceURL) {
    StatusResultServiceModel result = new StatusResultServiceModel(false, serviceName + " check failed");

    try {
        WSResponse response = this.wsRequestBuilder
            .prepareRequest(serviceURL + "/status")
            .get()
            .toCompletableFuture()
            .get();

        if (response.getStatus() != HttpStatus.SC_OK) {
            result.setMessage("Status code: " + response.getStatus() + ", Response: " + response.getBody());
        } else {
            ObjectMapper mapper = new ObjectMapper();
            StatusServiceModel data = mapper.readValue(response.getBody(), StatusServiceModel.class);
            result.setStatusResultServiceModel(data.getStatus());
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }

    return result;
}
 
Example 5
Source File: StatusService.java    From remote-monitoring-services-java with MIT License 6 votes vote down vote up
private StatusResultServiceModel PingSimulationService(String serviceName, String serviceURL) {
    StatusResultServiceModel result = new StatusResultServiceModel(false, serviceName + " check failed");

    try {
        WSResponse response = this.wsRequestBuilder
            .prepareRequest(serviceURL + "/status")
            .get()
            .toCompletableFuture()
            .get();

        if (response.getStatus() != HttpStatus.SC_OK) {
            result.setMessage("Status code: " + response.getStatus() + ", Response: " + response.getBody());
        } else {
            result.setIsHealthy(response.asJson().findPath("").asText().startsWith("OK:"));
            result.setMessage(response.asJson().findPath("").asText());
        }
    } catch (Exception e) {
        log.error(e.getMessage());
    }

    return result;
}
 
Example 6
Source File: WsResponseHelper.java    From remote-monitoring-services-java with MIT License 5 votes vote down vote up
/***
 * Helper method that throws if the status code returned by a service is
 * an unauthorized exception (401 or 403).
 *
 * @param response
 * @throws CompletionException
 */
public static void checkUnauthorizedStatus(WSResponse response) throws CompletionException {
    if (response != null) {
        // If the error is 403 or 401, the user who did the deployment is not authorized
        // to assign the role for the application to have Contributor access.
        if (response.getStatus() == HttpStatus.SC_FORBIDDEN ||
                response.getStatus() == HttpStatus.SC_UNAUTHORIZED) {
            String message = String.format("The application is not authorized and has not been " +
                    "assigned Contributor permissions for the subscription. Go to the Azure portal and " +
                    "assign the application as a Contributor in order to retrieve the token.");
            log.error(message);
            throw new CompletionException(new NotAuthorizedException(message));
        }
    }
}
 
Example 7
Source File: WsResponseHelper.java    From remote-monitoring-services-java with MIT License 5 votes vote down vote up
/****
 * Helper method that checks if the response from an external service is a success.
 *
 * @param message message to log if non-success status code
 * @throws CompletionException
 */
public static void checkSuccessStatusCode(WSResponse response, String message) {
    if (response != null) {
        if (response.getStatus() != Http.Status.OK) {
            log.error(message);
            throw new CompletionException(new ExternalDependencyException(message));
        }
    }
}