Search in sources :

Example 6 with Status

use of com.sun.jersey.api.client.ClientResponse.Status in project coprhd-controller by CoprHD.

the class KHRequests method sendGetRequest.

/*
     * Send GET request to KittyHawk server, and handle redirect/cookies
     */
private ClientResponse sendGetRequest(WebResource resource) throws VNXeException {
    _logger.info("getting data: {} ", _url);
    if (_client.isUnity() == true) {
        setFields();
    }
    ClientResponse response = buildRequest(addQueryParameters(buildResource(resource)).getRequestBuilder()).get(ClientResponse.class);
    Status statusCode = response.getClientResponseStatus();
    _logger.info(response.getStatus() + ":" + response.toString());
    if (statusCode == ClientResponse.Status.OK) {
        String emcCsrfToken = response.getHeaders().getFirst(EMC_CSRF_HEADER);
        if (emcCsrfToken != null) {
            saveEmcCsrfToken(emcCsrfToken);
        }
        saveClientCookies();
        return response;
    } else if (response.getClientResponseStatus() == ClientResponse.Status.UNAUTHORIZED) {
        authenticate();
        response = buildRequest(addQueryParameters(buildResource(resource)).getRequestBuilder()).get(ClientResponse.class);
        ;
    }
    int redirectTimes = 1;
    while (response.getClientResponseStatus() == ClientResponse.Status.FOUND && redirectTimes < VNXeConstants.REDIRECT_MAX) {
        String code = response.getClientResponseStatus().toString();
        String data = response.getEntity(String.class);
        _logger.debug("Returned code: {}, returned data:", code, data);
        WebResource newResource = handelRedirect(response);
        if (newResource != null) {
            response = buildRequest(newResource.getRequestBuilder()).get(ClientResponse.class);
            redirectTimes++;
        } else {
            // could not find the redirect url, return
            _logger.error(String.format("The post request to: %s failed with: %s %s", _url, response.getClientResponseStatus().toString(), response.getEntity(String.class)));
            throw VNXeException.exceptions.unexpectedDataError("Got redirect status code, but could not get redirected URL");
        }
    }
    if (redirectTimes >= VNXeConstants.REDIRECT_MAX) {
        _logger.error("redirected too many times for the request {}", _url);
        throw VNXeException.exceptions.unexpectedDataError("Redirected too many times while sending the request for " + _url);
    }
    checkResponse(response, GET_REQUEST);
    List<NewCookie> cookies = response.getCookies();
    if (cookies != null && !cookies.isEmpty()) {
        _requestCookies.addAll(cookies);
    }
    saveClientCookies();
    String emcCsrfToken = response.getHeaders().getFirst(EMC_CSRF_HEADER);
    if (emcCsrfToken != null) {
        saveEmcCsrfToken(emcCsrfToken);
    }
    return response;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Status(com.sun.jersey.api.client.ClientResponse.Status) WebResource(com.sun.jersey.api.client.WebResource) NewCookie(javax.ws.rs.core.NewCookie)

Example 7 with Status

use of com.sun.jersey.api.client.ClientResponse.Status in project coprhd-controller by CoprHD.

the class KHRequests method postRequest.

/*
     * Send POST request to KittyHawk server, and handle redirect/cookies
     * 
     * @param resource webResource
     * 
     * @param ParamBase parameters for post
     * 
     * @throws VNXeException
     */
public ClientResponse postRequest(ParamBase param) throws VNXeException {
    _logger.debug("post data: " + _url);
    ObjectMapper mapper = new ObjectMapper();
    String parmString = null;
    if (param != null) {
        try {
            parmString = mapper.writeValueAsString(param);
            _logger.debug("Content of the post: {}", parmString);
        } catch (JsonGenerationException e) {
            _logger.error("Post request param is not valid. ", e);
            throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
        } catch (JsonMappingException e) {
            _logger.error("Post request param is not valid. ", e);
            throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
        } catch (IOException e) {
            _logger.error("Post request param is not valid. ", e);
            throw VNXeException.exceptions.vnxeCommandFailed("Post request param is not valid.", e);
        }
    }
    ClientResponse response = buildRequest(addQueryParameters(buildResource(_resource)).getRequestBuilder()).entity(parmString).post(ClientResponse.class);
    Status statusCode = response.getClientResponseStatus();
    if (statusCode == ClientResponse.Status.CREATED || statusCode == ClientResponse.Status.ACCEPTED || statusCode == ClientResponse.Status.OK || statusCode == ClientResponse.Status.NO_CONTENT) {
        return response;
    } else if (statusCode == ClientResponse.Status.UNAUTHORIZED) {
        authenticate();
        response = buildRequest(addQueryParameters(buildResource(_resource)).getRequestBuilder()).entity(parmString).post(ClientResponse.class);
        ;
        statusCode = response.getClientResponseStatus();
        if (statusCode == ClientResponse.Status.OK || statusCode == ClientResponse.Status.ACCEPTED || statusCode == ClientResponse.Status.NO_CONTENT || statusCode == ClientResponse.Status.CREATED) {
            return response;
        }
    }
    int redirectTimes = 1;
    // handle redirect
    while (response.getClientResponseStatus() == ClientResponse.Status.FOUND && redirectTimes < VNXeConstants.REDIRECT_MAX) {
        String code = response.getClientResponseStatus().toString();
        String data = response.getEntity(String.class);
        _logger.debug("Returned code: {}, returned data {}", code, data);
        WebResource newResource = handelRedirect(response);
        if (newResource != null) {
            response = buildRequest(newResource.getRequestBuilder()).entity(parmString).post(ClientResponse.class);
            redirectTimes++;
        } else {
            // could not find the redirect url, return
            _logger.error(String.format("The post request to: %s failed with: %s %s", _url, response.getClientResponseStatus().toString(), response.getEntity(String.class)));
            throw VNXeException.exceptions.unexpectedDataError("Got redirect status code, but could not get redirected URL");
        }
    }
    if (redirectTimes >= VNXeConstants.REDIRECT_MAX) {
        _logger.error("redirected too many times for the request {}", _url);
        throw VNXeException.exceptions.unexpectedDataError("Redirected too many times while sending the request for " + _url);
    }
    checkResponse(response, POST_REQUEST);
    return response;
}
Also used : ClientResponse(com.sun.jersey.api.client.ClientResponse) Status(com.sun.jersey.api.client.ClientResponse.Status) JsonMappingException(org.codehaus.jackson.map.JsonMappingException) WebResource(com.sun.jersey.api.client.WebResource) IOException(java.io.IOException) JsonGenerationException(org.codehaus.jackson.JsonGenerationException) ObjectMapper(org.codehaus.jackson.map.ObjectMapper)

Aggregations

Status (com.sun.jersey.api.client.ClientResponse.Status)7 ClientResponse (com.sun.jersey.api.client.ClientResponse)5 WebResource (com.sun.jersey.api.client.WebResource)3 UnsupportedEncodingException (java.io.UnsupportedEncodingException)2 ConnectException (java.net.ConnectException)2 JSONException (org.codehaus.jettison.json.JSONException)2 MultivaluedMapImpl (com.sun.jersey.core.util.MultivaluedMapImpl)1 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 NewCookie (javax.ws.rs.core.NewCookie)1 JsonGenerationException (org.codehaus.jackson.JsonGenerationException)1 JsonMappingException (org.codehaus.jackson.map.JsonMappingException)1 ObjectMapper (org.codehaus.jackson.map.ObjectMapper)1 JSONObject (org.codehaus.jettison.json.JSONObject)1 OrcidInvalidScopeException (org.orcid.core.exception.OrcidInvalidScopeException)1 OAuthError (org.orcid.core.oauth.OAuthError)1 ScopePathType (org.orcid.jaxb.model.message.ScopePathType)1 InsufficientAuthenticationException (org.springframework.security.authentication.InsufficientAuthenticationException)1 Authentication (org.springframework.security.core.Authentication)1