Search in sources :

Example 31 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project api-core by ca-cwds.

the class SimpleResourceDelegate method handleException.

/**
 * <p>
 * Handle API request by delegating to service method,
 * {@link ApiSimpleResourceService#handle(Request)} and wrapping resulting API
 * {@link gov.ca.cwds.rest.api.Response} with a Java standard web service response.
 * </p>
 *
 * <p>
 * Web Service Response, not a CWDS API Response.
 * </p>
 *
 * <p>
 * HTTP Response Codes
 * </p>
 *
 * The default method implementation may return the following HTTP response codes:
 * <ul>
 * <li>{@link javax.ws.rs.core.Response.Status#OK}</li>
 * <li>{@link javax.ws.rs.core.Response.Status#NOT_FOUND}</li>
 * <li>{@link javax.ws.rs.core.Response.Status#CONFLICT}</li>
 * <li>{@link javax.ws.rs.core.Response.Status#INTERNAL_SERVER_ERROR}</li>
 * <li>{@link javax.ws.rs.core.Response.Status#EXPECTATION_FAILED}</li>
 * <li>{@link javax.ws.rs.core.Response.Status#NOT_IMPLEMENTED}</li>
 * <li>{@link javax.ws.rs.core.Response.Status#SERVICE_UNAVAILABLE}</li>
 * </ul>
 *
 * @param e {@link ServiceException} to inspect
 * @return web service {@link Response}
 * @see ApiSimpleResourceService#handle(Request)
 * @see ApiSimpleResourceService#find(Serializable)
 * @deprecated Moved to ServiceExceptionMapper
 */
@Deprecated
protected Response handleException(Exception e) throws ServiceException {
    Response ret;
    // Gold plating. Waiting for further requirements.
    if (e != null) {
        if (e.getCause() != null) {
            if (e.getCause() instanceof EntityNotFoundException) {
                throw new ServiceException("EntityNotFoundException", e.getCause());
            } else if (e.getCause() instanceof EntityExistsException) {
                throw new ServiceException("EntityExistsException", e.getCause());
            } else if (e.getCause() instanceof NullPointerException) {
                throw new ServiceException("NullPointerException", e.getCause());
            } else if (e.getCause() instanceof ClassNotFoundException) {
                LOGGER.error("Class not found! {}", e.getMessage(), e);
                throw new ServiceException("ClassNotFoundException", e.getCause());
            } else if (e.getCause() instanceof NotImplementedException) {
                LOGGER.error("Not implemented", e);
                throw new ServiceException("NotImplementedException", e.getCause());
            } else {
                LOGGER.error("Unable to handle request", e);
                throw new ServiceException("Unable to handle request", e.getCause());
            }
        } else if (e instanceof ServiceException) {
            final ServiceException svcEx = (ServiceException) e;
            LOGGER.error("ServiceException without attached cause: {}", svcEx.getMessage(), e);
            throw new ServiceException("ServiceException without attached cause", e);
        } else if (e instanceof IllegalArgumentException) {
            final IllegalArgumentException lae = (IllegalArgumentException) e;
            LOGGER.error("Argument cannot be null: {}", lae.getMessage(), e);
            throw new ServiceException("Argument cannot be null", e);
        } else if (e instanceof ConstraintViolationException) {
            final ConstraintViolationException cve = (ConstraintViolationException) e;
            LOGGER.error("ConstraintViolationException: {}", cve.getMessage(), e);
            throw new ServiceException("ConstraintViolationException: " + cve.getMessage(), e);
        } else {
            LOGGER.error("Unhandled error condition: {}", e.getMessage(), e);
            throw new ServiceException("Unhandled error condition", e);
        }
    } else {
        throw new ServiceException("No exception to handle");
    }
}
Also used : Response(javax.ws.rs.core.Response) ServiceException(gov.ca.cwds.rest.services.ServiceException) NotImplementedException(org.apache.commons.lang3.NotImplementedException) ConstraintViolationException(javax.validation.ConstraintViolationException) EntityNotFoundException(javax.persistence.EntityNotFoundException) EntityExistsException(javax.persistence.EntityExistsException)

Example 32 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project API by ca-cwds.

the class IndexQueryService method handleRequest.

@Override
protected IndexQueryResponse handleRequest(IndexQueryRequest req) {
    checkArgument(req != null, "query cannot be Null or empty");
    @SuppressWarnings("unchecked") String query = new JSONObject((Map<String, String>) req.getQuery()).toString();
    if (StringUtils.isBlank(query)) {
        LOGGER.error("query cannot be null.");
        throw new ServiceException("query cannot be null.");
    }
    return new IndexQueryResponse(callDao(req.getIndex(), query));
}
Also used : IndexQueryResponse(gov.ca.cwds.rest.api.domain.es.IndexQueryResponse) JSONObject(org.json.JSONObject) ServiceException(gov.ca.cwds.rest.services.ServiceException) Map(java.util.Map)

Example 33 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project API by ca-cwds.

the class ServiceUtils method extractKeyValuePairs.

@SuppressWarnings("javadoc")
public static Map<String, String> extractKeyValuePairs(Serializable nameValuePairsSerializable) {
    Map<String, String> keyValuePairs = new HashMap<>();
    if (!(nameValuePairsSerializable instanceof String)) {
        throw new ServiceException("Unable to read nameValuePairs as string");
    }
    try {
        String primaryKeyString = (String) nameValuePairsSerializable;
        for (String keyValueString : primaryKeyString.split(",")) {
            String[] keyValuePair = keyValueString.split("=");
            keyValuePairs.put(keyValuePair[0].trim(), keyValuePair[1].trim());
        }
        return keyValuePairs;
    } catch (Exception e) {
        throw new ServiceException("Problem parsing name value pairs", e);
    }
}
Also used : ServiceException(gov.ca.cwds.rest.services.ServiceException) HashMap(java.util.HashMap) ServiceException(gov.ca.cwds.rest.services.ServiceException)

Example 34 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project API by ca-cwds.

the class AddressService method update.

@Override
public Response update(Serializable primaryKey, Request request) {
    assert primaryKey instanceof String;
    assert request instanceof gov.ca.cwds.rest.api.domain.cms.Address;
    gov.ca.cwds.rest.api.domain.cms.Address address = (gov.ca.cwds.rest.api.domain.cms.Address) request;
    try {
        String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
        Address managed = new Address((String) primaryKey, address, lastUpdatedId);
        managed = addressDao.update(managed);
        return new gov.ca.cwds.rest.api.domain.cms.Address(managed, true);
    } catch (EntityNotFoundException e) {
        LOGGER.info("Address not found : {}", address);
        throw new ServiceException(e);
    }
}
Also used : Address(gov.ca.cwds.data.persistence.cms.Address) PostedAddress(gov.ca.cwds.rest.api.domain.cms.PostedAddress) ServiceException(gov.ca.cwds.rest.services.ServiceException) EntityNotFoundException(javax.persistence.EntityNotFoundException)

Example 35 with ServiceException

use of gov.ca.cwds.rest.services.ServiceException in project API by ca-cwds.

the class AllegationPerpetratorHistoryService method create.

/**
   * {@inheritDoc}
   * 
   * @see gov.ca.cwds.rest.services.CrudsService#create(gov.ca.cwds.rest.api.Request)
   */
@Override
public PostedAllegationPerpetratorHistory create(Request request) {
    assert request instanceof gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory;
    gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory allegationPerpetratorHistory = (gov.ca.cwds.rest.api.domain.cms.AllegationPerpetratorHistory) request;
    try {
        String lastUpdatedId = staffPersonIdRetriever.getStaffPersonId();
        AllegationPerpetratorHistory managed = new AllegationPerpetratorHistory(CmsKeyIdGenerator.cmsIdGenertor(lastUpdatedId), allegationPerpetratorHistory, lastUpdatedId);
        managed = allegationPerpetratorHistoryDao.create(managed);
        return new PostedAllegationPerpetratorHistory(managed);
    } catch (EntityExistsException e) {
        LOGGER.info("AllegationPerpetratorHistory already exists : {}", allegationPerpetratorHistory);
        throw new ServiceException(e);
    }
}
Also used : PostedAllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory) AllegationPerpetratorHistory(gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory) PostedAllegationPerpetratorHistory(gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory) EntityExistsException(javax.persistence.EntityExistsException) ServiceException(gov.ca.cwds.rest.services.ServiceException)

Aggregations

ServiceException (gov.ca.cwds.rest.services.ServiceException)59 Test (org.junit.Test)22 EntityNotFoundException (javax.persistence.EntityNotFoundException)16 EntityExistsException (javax.persistence.EntityExistsException)15 StaffPerson (gov.ca.cwds.data.persistence.cms.StaffPerson)7 PostedReferral (gov.ca.cwds.rest.api.domain.cms.PostedReferral)5 PostedReporter (gov.ca.cwds.rest.api.domain.cms.PostedReporter)5 PostedStaffPerson (gov.ca.cwds.rest.api.domain.cms.PostedStaffPerson)5 StaffPerson (gov.ca.cwds.rest.api.domain.cms.StaffPerson)5 PostedAllegation (gov.ca.cwds.rest.api.domain.cms.PostedAllegation)4 PostedAllegationPerpetratorHistory (gov.ca.cwds.rest.api.domain.cms.PostedAllegationPerpetratorHistory)4 PostedClient (gov.ca.cwds.rest.api.domain.cms.PostedClient)4 PostedLongText (gov.ca.cwds.rest.api.domain.cms.PostedLongText)4 Referral (gov.ca.cwds.rest.api.domain.cms.Referral)3 Reporter (gov.ca.cwds.rest.api.domain.cms.Reporter)3 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)2 Address (gov.ca.cwds.data.persistence.cms.Address)2 Allegation (gov.ca.cwds.data.persistence.cms.Allegation)2 AllegationPerpetratorHistory (gov.ca.cwds.data.persistence.cms.AllegationPerpetratorHistory)2 ChildClient (gov.ca.cwds.data.persistence.cms.ChildClient)2