mercredi 26 septembre 2018

How pass any type of class inside my Aspect, then cast the jointpoint object to it

I have a spring AOP interceptor annotation on my API level below:

  @ETISLog(etisModule = EtisModules.SYSAD, etisSubmodule = EtisSubmodules.MAINTENANCE,
      logEvent = LogEvent.CREATE)
  @RequestMapping(method = RequestMethod.POST)
  public ResponseEntity<MaintApplicationType> save(
      @RequestBody MaintApplicationType maintApplicationType) {
    LOGGER.info("API: SAVE {}", maintApplicationType);

    return new ResponseEntity<>(maintApplicationTypeService.save(maintApplicationType),
        HttpStatus.CREATED);
  }

Below is my around method inside my Aspect class: I cast the intercepted object from the jointpoint to a specific class type

public Object auditLogAround(ProceedingJoinPoint joinPoint, ETISLog etisLog)
      throws Throwable {
    LOGGER.info("INTERCEPTING REFERENCE SERVICE...");
    Object interceptedObject = new Object();

      if (LogEvent.CREATE.equals(etisLog.logEvent())) {

        interceptedObject = joinPoint.proceed();

      ResponseEntity<MaintApplicationType> httpResponseReference = (ResponseEntity<MaintApplicationType>) interceptedObject;


      MaintApplicationType referenceData = httpResponseReference.getBody();
        auditMainDetails.setUsername(referenceData.getCreatedBy());
 }
}

The code above works fine, but it has limitation, it only works for MaintApplicationType Entity. If i intercept another API level of another Entity i get ClassCastException here:

ResponseEntity<MaintApplicationType> httpResponseReference = (ResponseEntity<MaintApplicationType>) interceptedObject;

How can i make my auditLogAround method flexible enough to accept any class type to intercept.

I created an ENUM below but dont know how to implement this.

public enum MaintenanceTypeEnum {

  ACCREDITED_PRINTER, ALPHANUMERIC_TAX_CODE, APPLICATION_TYPE, ATTACHMENT_TYPE,
  AUTHORIZATION_TYPE, BARANGAY, BOOK_OF_ACCOUNT, CASE_EVENT, CASE_TYPE, CITY_MUNICIPALITY,
  COUNTRY, FORM_TYPE, IDENTIFIER_TYPE, INCENTIVE_CLASSIFICATION
}

Aucun commentaire:

Enregistrer un commentaire