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;
}
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;
}
Aggregations