Search in sources :

Example 1 with ApiResponseEvent

use of gov.cms.ab2d.eventlogger.events.ApiResponseEvent in project ab2d by CMSgov.

the class ErrorHandler method handleInvalidContractErrors.

@ExceptionHandler({ InvalidContractException.class })
public ResponseEntity<Void> handleInvalidContractErrors(Exception ex, HttpServletRequest request) {
    HttpStatus status = getErrorResponse(ex.getClass());
    String description = API_INVALID_CONTRACT + " " + getRootCause(ex);
    eventLogger.log(new ErrorEvent(MDC.get(ORGANIZATION), null, ErrorEvent.ErrorType.UNAUTHORIZED_CONTRACT, description));
    ApiResponseEvent responseEvent = new ApiResponseEvent(MDC.get(ORGANIZATION), null, status, "API Error", description, (String) request.getAttribute(REQUEST_ID));
    eventLogger.logAndAlert(responseEvent, Ab2dEnvironment.PROD_LIST);
    return new ResponseEntity<>(null, null, status);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) ErrorEvent(gov.cms.ab2d.eventlogger.events.ErrorEvent) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) ExceptionHandler(org.springframework.web.bind.annotation.ExceptionHandler) ResponseEntityExceptionHandler(org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)

Example 2 with ApiResponseEvent

use of gov.cms.ab2d.eventlogger.events.ApiResponseEvent in project ab2d by CMSgov.

the class ErrorHandler method generateFHIRError.

private ResponseEntity<JsonNode> generateFHIRError(Exception e, HttpHeaders httpHeaders, HttpServletRequest request) throws IOException {
    String msg = getRootCause(e);
    HttpStatus httpStatus = getErrorResponse(e.getClass());
    FhirVersion version = FhirVersion.fromAB2DUrl(request.getRequestURI());
    IBaseResource operationOutcome = version.getErrorOutcome(msg);
    String encoded = version.outcomePrettyToJSON(operationOutcome);
    // Log so that Splunk can pick this up and alert
    log.warn("{} {}", ExceptionUtils.getRootCause(e).getClass(), msg);
    eventLogger.log(new ApiResponseEvent(MDC.get(ORGANIZATION), null, ErrorHandler.getErrorResponse(e.getClass()), "FHIR Error", msg, (String) request.getAttribute(REQUEST_ID)));
    return new ResponseEntity<>(new ObjectMapper().readTree(encoded), httpHeaders, httpStatus);
}
Also used : ResponseEntity(org.springframework.http.ResponseEntity) HttpStatus(org.springframework.http.HttpStatus) FhirVersion(gov.cms.ab2d.fhir.FhirVersion) IBaseResource(org.hl7.fhir.instance.model.api.IBaseResource) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 3 with ApiResponseEvent

use of gov.cms.ab2d.eventlogger.events.ApiResponseEvent in project ab2d by CMSgov.

the class ApiCommon method returnStatusForJobCreation.

public ResponseEntity<Void> returnStatusForJobCreation(String jobGuid, String apiPrefix, String requestId, HttpServletRequest request) {
    String statusURL = getUrl(apiPrefix + FHIR_PREFIX + "/Job/" + jobGuid + "/$status", request);
    HttpHeaders responseHeaders = new HttpHeaders();
    responseHeaders.add(CONTENT_LOCATION, statusURL);
    eventLogger.log(new ApiResponseEvent(MDC.get(ORGANIZATION), jobGuid, HttpStatus.ACCEPTED, "Job Created", "Job " + jobGuid + " was created", requestId));
    return new ResponseEntity<>(null, responseHeaders, HttpStatus.ACCEPTED);
}
Also used : HttpHeaders(org.springframework.http.HttpHeaders) ResponseEntity(org.springframework.http.ResponseEntity) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent)

Example 4 with ApiResponseEvent

use of gov.cms.ab2d.eventlogger.events.ApiResponseEvent in project ab2d by CMSgov.

the class AuthenticationTests method testClientNoAuthorization.

@Test
public void testClientNoAuthorization() throws Exception {
    PdpClient pdpClient = pdpClientRepository.findByClientId(TEST_PDP_CLIENT);
    pdpClient.setRoles(Collections.emptySet());
    pdpClientRepository.save(pdpClient);
    this.mockMvc.perform(get(API_PREFIX_V1 + FHIR_PREFIX + "/Patient/$export").header("Authorization", "Bearer " + token).contentType(MediaType.APPLICATION_JSON)).andExpect(status().is(403));
    List<LoggableEvent> apiRequestEvents = loggerEventRepository.load(ApiRequestEvent.class);
    assertEquals(1, apiRequestEvents.size());
    ApiRequestEvent requestEvent = (ApiRequestEvent) apiRequestEvents.get(0);
    List<LoggableEvent> apiResponseEvents = loggerEventRepository.load(ApiResponseEvent.class);
    assertEquals(1, apiResponseEvents.size());
    ApiResponseEvent responseEvent = (ApiResponseEvent) apiResponseEvents.get(0);
    assertEquals(HttpStatus.FORBIDDEN.value(), responseEvent.getResponseCode());
    assertEquals(requestEvent.getRequestId(), responseEvent.getRequestId());
}
Also used : LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) PdpClient(gov.cms.ab2d.common.model.PdpClient) ApiRequestEvent(gov.cms.ab2d.eventlogger.events.ApiRequestEvent) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Example 5 with ApiResponseEvent

use of gov.cms.ab2d.eventlogger.events.ApiResponseEvent in project ab2d by CMSgov.

the class AuthenticationTests method testClientDoesNotExist.

@Test
public void testClientDoesNotExist() throws Exception {
    PdpClient pdpClient = pdpClientRepository.findByClientId(TEST_PDP_CLIENT);
    pdpClientRepository.delete(pdpClient);
    this.mockMvc.perform(get(API_PREFIX_V1 + FHIR_PREFIX + "/Patient/$export").header("Authorization", "Bearer " + token).contentType(MediaType.APPLICATION_JSON)).andExpect(status().is(403));
    List<LoggableEvent> apiRequestEvents = loggerEventRepository.load(ApiRequestEvent.class);
    assertEquals(1, apiRequestEvents.size());
    ApiRequestEvent requestEvent = (ApiRequestEvent) apiRequestEvents.get(0);
    List<LoggableEvent> apiResponseEvents = loggerEventRepository.load(ApiResponseEvent.class);
    assertEquals(1, apiResponseEvents.size());
    ApiResponseEvent responseEvent = (ApiResponseEvent) apiResponseEvents.get(0);
    assertEquals(HttpStatus.FORBIDDEN.value(), responseEvent.getResponseCode());
    assertEquals(requestEvent.getRequestId(), responseEvent.getRequestId());
}
Also used : LoggableEvent(gov.cms.ab2d.eventlogger.LoggableEvent) PdpClient(gov.cms.ab2d.common.model.PdpClient) ApiRequestEvent(gov.cms.ab2d.eventlogger.events.ApiRequestEvent) ApiResponseEvent(gov.cms.ab2d.eventlogger.events.ApiResponseEvent) SpringBootTest(org.springframework.boot.test.context.SpringBootTest)

Aggregations

ApiResponseEvent (gov.cms.ab2d.eventlogger.events.ApiResponseEvent)19 ResponseEntity (org.springframework.http.ResponseEntity)11 PdpClient (gov.cms.ab2d.common.model.PdpClient)5 LoggableEvent (gov.cms.ab2d.eventlogger.LoggableEvent)5 ApiRequestEvent (gov.cms.ab2d.eventlogger.events.ApiRequestEvent)4 SpringBootTest (org.springframework.boot.test.context.SpringBootTest)4 HttpStatus (org.springframework.http.HttpStatus)4 HttpHeaders (org.springframework.http.HttpHeaders)3 ExceptionHandler (org.springframework.web.bind.annotation.ExceptionHandler)3 ResponseEntityExceptionHandler (org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler)3 IParser (ca.uhn.fhir.parser.IParser)2 Operation (io.swagger.v3.oas.annotations.Operation)2 ApiResponses (io.swagger.v3.oas.annotations.responses.ApiResponses)2 GetMapping (org.springframework.web.bind.annotation.GetMapping)2 ResponseStatus (org.springframework.web.bind.annotation.ResponseStatus)2 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)1 JobCompletedResponse (gov.cms.ab2d.api.controller.JobCompletedResponse)1 JobProcessingException (gov.cms.ab2d.api.controller.JobProcessingException)1 TooManyRequestsException (gov.cms.ab2d.api.controller.TooManyRequestsException)1 JobPollResult (gov.cms.ab2d.common.dto.JobPollResult)1