Search in sources :

Example 91 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdImplementationPut.

@Override
public Response compositeApisApiIdImplementationPut(String apiId, InputStream apiImplementationInputStream, FileInfo apiImplementationDetail, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = compositeApisApiIdImplementationGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && ifMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        apiStore.updateCompositeApiImplementation(apiId, apiImplementationInputStream);
        String uriString = RestApiConstants.RESOURCE_PATH_IMPLEMENTATION.replace(RestApiConstants.APIID_PARAM, apiId);
        FileInfoDTO infoDTO = new FileInfoDTO();
        infoDTO.setRelativePath(uriString);
        infoDTO.setMediaType(MediaType.APPLICATION_OCTET_STREAM);
        String newFingerprint = compositeApisApiIdImplementationGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(infoDTO).build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) FileInfoDTO(org.wso2.carbon.apimgt.rest.api.store.dto.FileInfoDTO)

Example 92 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdDedicatedGatewayGet.

/**
 * Retrieve Dedicated Gateway of an API
 *
 * @param apiId             UUID of API
 * @param ifNoneMatch       ifNoneMatch header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request           msf4j request object
 * @return 200 OK if the operation was successful
 * @throws NotFoundException when the particular resource does not exist
 */
@Override
public Response compositeApisApiIdDedicatedGatewayGet(String apiId, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (!apiStore.isCompositeAPIExist(apiId)) {
            String errorMessage = "API not found : " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, ifNoneMatch, ifModifiedSince, request);
        if (!StringUtils.isEmpty(ifNoneMatch) && !StringUtils.isEmpty(existingFingerprint) && ifNoneMatch.contains(existingFingerprint)) {
            return Response.notModified().build();
        }
        DedicatedGateway dedicatedGateway = apiStore.getDedicatedGateway(apiId);
        if (dedicatedGateway != null) {
            DedicatedGatewayDTO dedicatedGatewayDTO = DedicatedGatewayMappingUtil.toDedicatedGatewayDTO(dedicatedGateway);
            return Response.ok().header(HttpHeaders.ETAG, "\"" + existingFingerprint + "\"").entity(dedicatedGatewayDTO).build();
        } else {
            String msg = "Dedicated Gateway not found for " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(msg, ExceptionCodes.DEDICATED_GATEWAY_DETAILS_NOT_FOUND);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(msg, e);
            return Response.status(Response.Status.NOT_FOUND).entity(errorDTO).build();
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while retrieving dedicated gateway of the API : " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) DedicatedGatewayDTO(org.wso2.carbon.apimgt.rest.api.store.dto.DedicatedGatewayDTO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 93 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisPost.

@Override
public Response compositeApisPost(CompositeAPIDTO body, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        CompositeAPI.Builder apiBuilder = CompositeAPIMappingUtil.toAPI(body);
        APIStore apiStore = RestApiUtil.getConsumer(username);
        Application app = apiStore.getApplicationByUuid(apiBuilder.getApplicationId());
        // One application can only have one Composite API in default implementation
        if (apiStore.getAPISubscriptionsByApplication(app, ApiType.COMPOSITE).size() > 0) {
            String errorMessage = "A Composite API already exists for application : " + app.getId();
            APIMgtResourceAlreadyExistsException e = new APIMgtResourceAlreadyExistsException(errorMessage, ExceptionCodes.COMPOSITE_API_ALREADY_EXISTS);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.APPLICATION_ID, app.getId());
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        apiStore.addCompositeApi(apiBuilder);
        CompositeAPI returnAPI = apiStore.getCompositeAPIbyId(apiBuilder.build().getId());
        return Response.status(Response.Status.CREATED).entity(CompositeAPIMappingUtil.toCompositeAPIDTO(returnAPI)).build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, body.getName());
        paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, body.getVersion());
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) APIMgtResourceAlreadyExistsException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceAlreadyExistsException) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 94 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdDedicatedGatewayPut.

/**
 * Add or update Dedicated Gateway of an API
 *
 * @param apiId             UUID of API
 * @param body              DedicatedGatewayDTO
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return 200 OK if the operation was successful
 * @throws NotFoundException when the particular resource does not exist
 */
@Override
public Response compositeApisApiIdDedicatedGatewayPut(String apiId, DedicatedGatewayDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        if (!apiStore.isCompositeAPIExist(apiId)) {
            String errorMessage = "API not found : " + apiId;
            APIMgtResourceNotFoundException e = new APIMgtResourceNotFoundException(errorMessage, ExceptionCodes.API_NOT_FOUND);
            HashMap<String, String> paramList = new HashMap<String, String>();
            paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
            ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
            log.error(errorMessage, e);
            return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
        }
        String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        DedicatedGateway dedicatedGateway = DedicatedGatewayMappingUtil.fromDTOtoDedicatedGateway(body, apiId, username);
        apiStore.updateDedicatedGateway(dedicatedGateway);
        DedicatedGateway updatedDedicatedGateway = apiStore.getDedicatedGateway(apiId);
        String newFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(DedicatedGatewayMappingUtil.toDedicatedGatewayDTO(updatedDedicatedGateway)).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating dedicated gateway of the composite API : " + apiId;
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIMgtResourceNotFoundException(org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) DedicatedGateway(org.wso2.carbon.apimgt.core.models.DedicatedGateway)

Example 95 with ErrorDTO

use of org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdPut.

/**
 * Updates an API by UUID
 *
 * @param apiId             UUID of API
 * @param body              Updated API details
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Updated API
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response compositeApisApiIdPut(String apiId, CompositeAPIDTO body, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        CompositeAPI.Builder api = CompositeAPIMappingUtil.toAPI(body).id(apiId);
        apiStore.updateCompositeApi(api);
        String newFingerprint = compositeApisApiIdGetFingerprint(apiId, null, null, request);
        CompositeAPIDTO apidto = CompositeAPIMappingUtil.toCompositeAPIDTO(apiStore.getCompositeAPIbyId(apiId));
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(apidto).build();
    } catch (APIManagementException e) {
        HashMap<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) CompositeAPIDTO(org.wso2.carbon.apimgt.rest.api.store.dto.CompositeAPIDTO) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)155 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)154 HashMap (java.util.HashMap)106 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)48 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)45 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)40 Map (java.util.Map)25 APIMgtResourceNotFoundException (org.wso2.carbon.apimgt.core.exception.APIMgtResourceNotFoundException)15 ErrorHandler (org.wso2.carbon.apimgt.core.exception.ErrorHandler)15 IOException (java.io.IOException)11 Application (org.wso2.carbon.apimgt.core.models.Application)11 URI (java.net.URI)10 URISyntaxException (java.net.URISyntaxException)10 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)9 Subscription (org.wso2.carbon.apimgt.core.models.Subscription)7 ArrayList (java.util.ArrayList)6 API (org.wso2.carbon.apimgt.core.models.API)6 Endpoint (org.wso2.carbon.apimgt.core.models.Endpoint)6 Label (org.wso2.carbon.apimgt.core.models.Label)6 ErrorDTO (org.wso2.carbon.apimgt.rest.api.authenticator.dto.ErrorDTO)6