@Test public void testSendRequest_errorMovedPermanently() { int httpStatusCode = 301; String tid = "test-tid-123"; String mockResponseContent = ""; Response mockResponse = new Response(httpStatusCode, mockResponseContent, tid); mockHttpRequestClient.setMockResponse(mockResponse); Request serviceRequest = new Request.RequestBuilder(MethodType.GET, "url") .context(new RequestContext()) .typeReference(new TypeReference<ECheck>() {}) .build(); try { serviceBase.sendRequest(serviceRequest); fail("Expected BaseException to be thrown"); } catch (BaseException baseEx) { Errors errors = baseEx.getErrors(); assertErrorsInException(errors, httpStatusCode, mockResponseContent); } }
public Response sendRequest(Request serviceRequest) throws BaseException { logger.debug("Enter ServiceBase::sendRequest"); if (serviceRequest.getMethod().equals(MethodType.POST)) { String payload = JsonUtil.serialize(serviceRequest.getRequestObject()); serviceRequest.setPostJson(payload); logger.debug("request payload" + payload); } HttpRequestClient client = new HttpRequestClient(serviceRequest.getContext().getProxyConfig()); Response serviceResponse = client.makeRequest(serviceRequest); if (serviceResponse == null) { logger.error("No response from Service: it is null"); throw new BaseException("Unexpected Error , service response object was null "); } int httpStatusCode = serviceResponse.getStatusCode(); logger.info("httpStatusCode::" + httpStatusCode); if (httpStatusCode >= HttpStatus.SC_OK && httpStatusCode <= 299) { logger.info("API call succeeded"); if (!serviceResponse.getContent().isEmpty() && serviceRequest.getTypeReference() != null) { Object object = JsonUtil.deserialize(serviceResponse.getContent(), serviceRequest.getTypeReference()); serviceResponse.setResponseObject(object); } return serviceResponse; } else { Errors errors = null; if (StringUtils.isNotBlank(serviceResponse.getContent())) { try { TypeReference<Errors> typeReference = new TypeReference<Errors>() { }; errors = (Errors) JsonUtil.deserialize(serviceResponse.getContent(), typeReference); serviceResponse.setErrors(errors); } catch (SerializationException se) { logger.debug("unable to deserialize to an error list", se); } } if (errors == null) { errors = new Errors(); List<Error> errorList = new ArrayList<Error>(); Error error = new Error(); error.setCode("HttpStatusCode-" + httpStatusCode); error.setDetail("ResponsePayload: " + serviceResponse.getContent()); errorList.add(error); errors.setErrorList(errorList); serviceResponse.setErrors(errors); } if (httpStatusCode == HttpStatus.SC_UNAUTHORIZED) { logger.error("Authorization Error httpStatusCode {}", httpStatusCode); AuthorizationException exception = new AuthorizationException(errors); throw exception; } else if (httpStatusCode >= HttpStatus.SC_BAD_REQUEST && httpStatusCode <= 499) { logger.error("Bad Request Exception {}", httpStatusCode); BadRequestException badRequestException = new BadRequestException(errors); logger.debug("Client Side Error httpStatusCode {}", httpStatusCode); throw badRequestException; } else if (httpStatusCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR && httpStatusCode <= 599) { logger.error("Service exception {}", httpStatusCode); ServiceException serviceException = new ServiceException(errors); throw serviceException; } else { logger.error("unexpected exception {}", httpStatusCode); BaseException baseException = new BaseException(errors); throw baseException; } } }
ServiceBase { public Response sendRequest(Request serviceRequest) throws BaseException { logger.debug("Enter ServiceBase::sendRequest"); if (serviceRequest.getMethod().equals(MethodType.POST)) { String payload = JsonUtil.serialize(serviceRequest.getRequestObject()); serviceRequest.setPostJson(payload); logger.debug("request payload" + payload); } HttpRequestClient client = new HttpRequestClient(serviceRequest.getContext().getProxyConfig()); Response serviceResponse = client.makeRequest(serviceRequest); if (serviceResponse == null) { logger.error("No response from Service: it is null"); throw new BaseException("Unexpected Error , service response object was null "); } int httpStatusCode = serviceResponse.getStatusCode(); logger.info("httpStatusCode::" + httpStatusCode); if (httpStatusCode >= HttpStatus.SC_OK && httpStatusCode <= 299) { logger.info("API call succeeded"); if (!serviceResponse.getContent().isEmpty() && serviceRequest.getTypeReference() != null) { Object object = JsonUtil.deserialize(serviceResponse.getContent(), serviceRequest.getTypeReference()); serviceResponse.setResponseObject(object); } return serviceResponse; } else { Errors errors = null; if (StringUtils.isNotBlank(serviceResponse.getContent())) { try { TypeReference<Errors> typeReference = new TypeReference<Errors>() { }; errors = (Errors) JsonUtil.deserialize(serviceResponse.getContent(), typeReference); serviceResponse.setErrors(errors); } catch (SerializationException se) { logger.debug("unable to deserialize to an error list", se); } } if (errors == null) { errors = new Errors(); List<Error> errorList = new ArrayList<Error>(); Error error = new Error(); error.setCode("HttpStatusCode-" + httpStatusCode); error.setDetail("ResponsePayload: " + serviceResponse.getContent()); errorList.add(error); errors.setErrorList(errorList); serviceResponse.setErrors(errors); } if (httpStatusCode == HttpStatus.SC_UNAUTHORIZED) { logger.error("Authorization Error httpStatusCode {}", httpStatusCode); AuthorizationException exception = new AuthorizationException(errors); throw exception; } else if (httpStatusCode >= HttpStatus.SC_BAD_REQUEST && httpStatusCode <= 499) { logger.error("Bad Request Exception {}", httpStatusCode); BadRequestException badRequestException = new BadRequestException(errors); logger.debug("Client Side Error httpStatusCode {}", httpStatusCode); throw badRequestException; } else if (httpStatusCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR && httpStatusCode <= 599) { logger.error("Service exception {}", httpStatusCode); ServiceException serviceException = new ServiceException(errors); throw serviceException; } else { logger.error("unexpected exception {}", httpStatusCode); BaseException baseException = new BaseException(errors); throw baseException; } } } }
ServiceBase { public Response sendRequest(Request serviceRequest) throws BaseException { logger.debug("Enter ServiceBase::sendRequest"); if (serviceRequest.getMethod().equals(MethodType.POST)) { String payload = JsonUtil.serialize(serviceRequest.getRequestObject()); serviceRequest.setPostJson(payload); logger.debug("request payload" + payload); } HttpRequestClient client = new HttpRequestClient(serviceRequest.getContext().getProxyConfig()); Response serviceResponse = client.makeRequest(serviceRequest); if (serviceResponse == null) { logger.error("No response from Service: it is null"); throw new BaseException("Unexpected Error , service response object was null "); } int httpStatusCode = serviceResponse.getStatusCode(); logger.info("httpStatusCode::" + httpStatusCode); if (httpStatusCode >= HttpStatus.SC_OK && httpStatusCode <= 299) { logger.info("API call succeeded"); if (!serviceResponse.getContent().isEmpty() && serviceRequest.getTypeReference() != null) { Object object = JsonUtil.deserialize(serviceResponse.getContent(), serviceRequest.getTypeReference()); serviceResponse.setResponseObject(object); } return serviceResponse; } else { Errors errors = null; if (StringUtils.isNotBlank(serviceResponse.getContent())) { try { TypeReference<Errors> typeReference = new TypeReference<Errors>() { }; errors = (Errors) JsonUtil.deserialize(serviceResponse.getContent(), typeReference); serviceResponse.setErrors(errors); } catch (SerializationException se) { logger.debug("unable to deserialize to an error list", se); } } if (errors == null) { errors = new Errors(); List<Error> errorList = new ArrayList<Error>(); Error error = new Error(); error.setCode("HttpStatusCode-" + httpStatusCode); error.setDetail("ResponsePayload: " + serviceResponse.getContent()); errorList.add(error); errors.setErrorList(errorList); serviceResponse.setErrors(errors); } if (httpStatusCode == HttpStatus.SC_UNAUTHORIZED) { logger.error("Authorization Error httpStatusCode {}", httpStatusCode); AuthorizationException exception = new AuthorizationException(errors); throw exception; } else if (httpStatusCode >= HttpStatus.SC_BAD_REQUEST && httpStatusCode <= 499) { logger.error("Bad Request Exception {}", httpStatusCode); BadRequestException badRequestException = new BadRequestException(errors); logger.debug("Client Side Error httpStatusCode {}", httpStatusCode); throw badRequestException; } else if (httpStatusCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR && httpStatusCode <= 599) { logger.error("Service exception {}", httpStatusCode); ServiceException serviceException = new ServiceException(errors); throw serviceException; } else { logger.error("unexpected exception {}", httpStatusCode); BaseException baseException = new BaseException(errors); throw baseException; } } }  }
ServiceBase { public Response sendRequest(Request serviceRequest) throws BaseException { logger.debug("Enter ServiceBase::sendRequest"); if (serviceRequest.getMethod().equals(MethodType.POST)) { String payload = JsonUtil.serialize(serviceRequest.getRequestObject()); serviceRequest.setPostJson(payload); logger.debug("request payload" + payload); } HttpRequestClient client = new HttpRequestClient(serviceRequest.getContext().getProxyConfig()); Response serviceResponse = client.makeRequest(serviceRequest); if (serviceResponse == null) { logger.error("No response from Service: it is null"); throw new BaseException("Unexpected Error , service response object was null "); } int httpStatusCode = serviceResponse.getStatusCode(); logger.info("httpStatusCode::" + httpStatusCode); if (httpStatusCode >= HttpStatus.SC_OK && httpStatusCode <= 299) { logger.info("API call succeeded"); if (!serviceResponse.getContent().isEmpty() && serviceRequest.getTypeReference() != null) { Object object = JsonUtil.deserialize(serviceResponse.getContent(), serviceRequest.getTypeReference()); serviceResponse.setResponseObject(object); } return serviceResponse; } else { Errors errors = null; if (StringUtils.isNotBlank(serviceResponse.getContent())) { try { TypeReference<Errors> typeReference = new TypeReference<Errors>() { }; errors = (Errors) JsonUtil.deserialize(serviceResponse.getContent(), typeReference); serviceResponse.setErrors(errors); } catch (SerializationException se) { logger.debug("unable to deserialize to an error list", se); } } if (errors == null) { errors = new Errors(); List<Error> errorList = new ArrayList<Error>(); Error error = new Error(); error.setCode("HttpStatusCode-" + httpStatusCode); error.setDetail("ResponsePayload: " + serviceResponse.getContent()); errorList.add(error); errors.setErrorList(errorList); serviceResponse.setErrors(errors); } if (httpStatusCode == HttpStatus.SC_UNAUTHORIZED) { logger.error("Authorization Error httpStatusCode {}", httpStatusCode); AuthorizationException exception = new AuthorizationException(errors); throw exception; } else if (httpStatusCode >= HttpStatus.SC_BAD_REQUEST && httpStatusCode <= 499) { logger.error("Bad Request Exception {}", httpStatusCode); BadRequestException badRequestException = new BadRequestException(errors); logger.debug("Client Side Error httpStatusCode {}", httpStatusCode); throw badRequestException; } else if (httpStatusCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR && httpStatusCode <= 599) { logger.error("Service exception {}", httpStatusCode); ServiceException serviceException = new ServiceException(errors); throw serviceException; } else { logger.error("unexpected exception {}", httpStatusCode); BaseException baseException = new BaseException(errors); throw baseException; } } }  Response sendRequest(Request serviceRequest); void prepareResponse(Request request, Response response, Entity entity); }
ServiceBase { public Response sendRequest(Request serviceRequest) throws BaseException { logger.debug("Enter ServiceBase::sendRequest"); if (serviceRequest.getMethod().equals(MethodType.POST)) { String payload = JsonUtil.serialize(serviceRequest.getRequestObject()); serviceRequest.setPostJson(payload); logger.debug("request payload" + payload); } HttpRequestClient client = new HttpRequestClient(serviceRequest.getContext().getProxyConfig()); Response serviceResponse = client.makeRequest(serviceRequest); if (serviceResponse == null) { logger.error("No response from Service: it is null"); throw new BaseException("Unexpected Error , service response object was null "); } int httpStatusCode = serviceResponse.getStatusCode(); logger.info("httpStatusCode::" + httpStatusCode); if (httpStatusCode >= HttpStatus.SC_OK && httpStatusCode <= 299) { logger.info("API call succeeded"); if (!serviceResponse.getContent().isEmpty() && serviceRequest.getTypeReference() != null) { Object object = JsonUtil.deserialize(serviceResponse.getContent(), serviceRequest.getTypeReference()); serviceResponse.setResponseObject(object); } return serviceResponse; } else { Errors errors = null; if (StringUtils.isNotBlank(serviceResponse.getContent())) { try { TypeReference<Errors> typeReference = new TypeReference<Errors>() { }; errors = (Errors) JsonUtil.deserialize(serviceResponse.getContent(), typeReference); serviceResponse.setErrors(errors); } catch (SerializationException se) { logger.debug("unable to deserialize to an error list", se); } } if (errors == null) { errors = new Errors(); List<Error> errorList = new ArrayList<Error>(); Error error = new Error(); error.setCode("HttpStatusCode-" + httpStatusCode); error.setDetail("ResponsePayload: " + serviceResponse.getContent()); errorList.add(error); errors.setErrorList(errorList); serviceResponse.setErrors(errors); } if (httpStatusCode == HttpStatus.SC_UNAUTHORIZED) { logger.error("Authorization Error httpStatusCode {}", httpStatusCode); AuthorizationException exception = new AuthorizationException(errors); throw exception; } else if (httpStatusCode >= HttpStatus.SC_BAD_REQUEST && httpStatusCode <= 499) { logger.error("Bad Request Exception {}", httpStatusCode); BadRequestException badRequestException = new BadRequestException(errors); logger.debug("Client Side Error httpStatusCode {}", httpStatusCode); throw badRequestException; } else if (httpStatusCode >= HttpStatus.SC_INTERNAL_SERVER_ERROR && httpStatusCode <= 599) { logger.error("Service exception {}", httpStatusCode); ServiceException serviceException = new ServiceException(errors); throw serviceException; } else { logger.error("unexpected exception {}", httpStatusCode); BaseException baseException = new BaseException(errors); throw baseException; } } }  Response sendRequest(Request serviceRequest); void prepareResponse(Request request, Response response, Entity entity);  }