Search in sources :

Example 91 with Header

use of org.wso2.carbon.messaging.Header 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 Header

use of org.wso2.carbon.messaging.Header 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 Header

use of org.wso2.carbon.messaging.Header 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 94 with Header

use of org.wso2.carbon.messaging.Header 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)

Example 95 with Header

use of org.wso2.carbon.messaging.Header in project carbon-apimgt by wso2.

the class CompositeApisApiServiceImpl method compositeApisApiIdSwaggerPut.

/**
 * Updates the swagger defnition of an API
 *
 * @param apiId             UUID of API
 * @param apiDefinition     updated swagger defintion
 * @param ifMatch           If-Match header value
 * @param ifUnmodifiedSince If-Unmodified-Since header value
 * @param request           msf4j request object
 * @return Updated swagger definition
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response compositeApisApiIdSwaggerPut(String apiId, String apiDefinition, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String existingFingerprint = compositeApisApiIdSwaggerGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        apiStore.updateCompositeApiDefinition(apiId, apiDefinition);
        String apiSwagger = apiStore.getCompositeApiDefinition(apiId);
        String newFingerprint = compositeApisApiIdSwaggerGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(apiSwagger).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)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)103 HashMap (java.util.HashMap)95 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)95 Test (org.testng.annotations.Test)36 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)35 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)33 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)30 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)28 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)24 ArrayList (java.util.ArrayList)23 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)19 CharonException (org.wso2.charon3.core.exceptions.CharonException)19 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)19 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)18 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)18 Header (org.wso2.carbon.messaging.Header)17 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)16 IOException (java.io.IOException)15 JSONEncoder (org.wso2.charon3.core.encoder.JSONEncoder)15 Map (java.util.Map)14