Search in sources :

Example 36 with Resource

use of org.wso2.carbon.registry.api.Resource in project carbon-apimgt by wso2.

the class ImportApiServiceImpl method importApisPut.

/**
 * Imports an updates a set of existing APIs which have been exported as a zip file
 *
 * @param fileInputStream content stream of the zip file which contains exported API(s)
 * @param fileDetail      meta information of the zip file
 * @param provider        provider of the API (if it needs to be updated)
 * @param request         ms4j request object
 * @return List of APIs that were imported
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response importApisPut(InputStream fileInputStream, FileInfo fileDetail, String provider, Request request) throws NotFoundException {
    APIPublisher publisher = null;
    try {
        publisher = RestAPIPublisherUtil.getApiPublisher(RestApiUtil.getLoggedInUsername(request));
        FileBasedApiImportExportManager importManager = new FileBasedApiImportExportManager(publisher, System.getProperty("java.io.tmpdir") + File.separator + "imported-api-archives-" + UUID.randomUUID().toString());
        APIListDTO apiList = importManager.importAPIs(fileInputStream, provider);
        return Response.status(Response.Status.OK).entity(apiList).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while importing the APIs";
        log.error(errorMessage, e);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO) FileBasedApiImportExportManager(org.wso2.carbon.apimgt.rest.api.publisher.utils.FileBasedApiImportExportManager)

Example 37 with Resource

use of org.wso2.carbon.registry.api.Resource in project carbon-apimgt by wso2.

the class ImportApiServiceImpl method importApisPost.

/**
 * Imports a set of new APIs which have been exported as a zip file
 *
 * @param fileInputStream content stream of the zip file which contains exported API(s)
 * @param fileDetail      meta information of the zip file
 * @param provider        provider of the API (if it needs to be updated)
 * @param request         ms4j request object
 * @return List of APIs that were imported
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response importApisPost(InputStream fileInputStream, FileInfo fileDetail, String provider, Request request) throws NotFoundException {
    APIPublisher publisher = null;
    try {
        publisher = RestAPIPublisherUtil.getApiPublisher(RestApiUtil.getLoggedInUsername(request));
        FileBasedApiImportExportManager importManager = new FileBasedApiImportExportManager(publisher, System.getProperty("java.io.tmpdir") + File.separator + "imported-api-archives-" + UUID.randomUUID().toString());
        APIListDTO apiList = importManager.importAndCreateAPIs(fileInputStream, provider);
        return Response.status(Response.Status.OK).entity(apiList).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while importing the APIs";
        log.error(errorMessage, e);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) APIListDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIListDTO) FileBasedApiImportExportManager(org.wso2.carbon.apimgt.rest.api.publisher.utils.FileBasedApiImportExportManager)

Example 38 with Resource

use of org.wso2.carbon.registry.api.Resource in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdSwaggerPut.

/**
 * 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 apisApiIdSwaggerPut(String apiId, String apiDefinition, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        String existingFingerprint = apisApiIdSwaggerGetFingerprint(apiId, null, null, request);
        if (!StringUtils.isEmpty(ifMatch) && !StringUtils.isEmpty(existingFingerprint) && !ifMatch.contains(existingFingerprint)) {
            return Response.status(Response.Status.PRECONDITION_FAILED).build();
        }
        KeyMgtConfigurations keyManagerConfiguration = APIMConfigurationService.getInstance().getApimConfigurations().getKeyManagerConfigs();
        Map<String, String> scopes = new APIDefinitionFromSwagger20().getScopesFromSecurityDefinition(apiDefinition);
        for (String scopeName : scopes.keySet()) {
            if (scopeName.contains(keyManagerConfiguration.getProductRestApiScopesKeyWord())) {
                String message = "scope name couldn't have the restricted keyword " + keyManagerConfiguration.getProductRestApiScopesKeyWord();
                ErrorDTO errorDTO = RestApiUtil.getErrorDTO(message, 900313L, message);
                return Response.status(Response.Status.PRECONDITION_FAILED).entity(errorDTO).build();
            }
        }
        apiPublisher.saveSwagger20Definition(apiId, apiDefinition);
        String apiSwagger = apiPublisher.getApiSwaggerDefinition(apiId);
        String newFingerprint = apisApiIdSwaggerGetFingerprint(apiId, null, null, request);
        return Response.ok().header(HttpHeaders.ETAG, "\"" + newFingerprint + "\"").entity(apiSwagger).build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while put swagger for 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 : KeyMgtConfigurations(org.wso2.carbon.apimgt.core.configuration.models.KeyMgtConfigurations) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) HashMap(java.util.HashMap) APIDefinitionFromSwagger20(org.wso2.carbon.apimgt.core.impl.APIDefinitionFromSwagger20) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher)

Example 39 with Resource

use of org.wso2.carbon.registry.api.Resource in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisChangeLifecyclePost.

/**
 * Change the lifecycle state of an API
 *
 * @param action             lifecycle action
 * @param apiId              UUID of API
 * @param lifecycleChecklist lifecycle check list items
 * @param ifMatch            If-Match header value
 * @param ifUnmodifiedSince  If-Unmodified-Since header value
 * @param request            msf4j request object
 * @return 200 OK if the operation is succesful
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisChangeLifecyclePost(String action, String apiId, String lifecycleChecklist, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    Map<String, Boolean> lifecycleChecklistMap = new HashMap<>();
    WorkflowResponseDTO response = null;
    try {
        if (lifecycleChecklist != null) {
            String[] checkList = lifecycleChecklist.split(",");
            for (String checkList1 : checkList) {
                StringTokenizer attributeTokens = new StringTokenizer(checkList1, ":");
                String attributeName = attributeTokens.nextToken();
                Boolean attributeValue = Boolean.valueOf(attributeTokens.nextToken());
                lifecycleChecklistMap.put(attributeName, attributeValue);
            }
        }
        if (action.trim().equals(APIMgtConstants.CHECK_LIST_ITEM_CHANGE_EVENT)) {
            RestAPIPublisherUtil.getApiPublisher(username).updateCheckListItem(apiId, action, lifecycleChecklistMap);
            WorkflowResponse workflowResponse = new GeneralWorkflowResponse();
            // since workflows are not defined for checklist items
            workflowResponse.setWorkflowStatus(WorkflowStatus.APPROVED);
            response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
            return Response.ok().entity(response).build();
        } else {
            WorkflowResponse workflowResponse = RestAPIPublisherUtil.getApiPublisher(username).updateAPIStatus(apiId, action, lifecycleChecklistMap);
            response = MappingUtil.toWorkflowResponseDTO(workflowResponse);
            // be in either pending or approved state) send back the workflow response
            if (WorkflowStatus.CREATED == workflowResponse.getWorkflowStatus()) {
                URI location = new URI(RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId);
                return Response.status(Response.Status.ACCEPTED).header(RestApiConstants.LOCATION_HEADER, location).entity(response).build();
            } else {
                return Response.ok().entity(response).build();
            }
        }
    } catch (APIManagementException e) {
        String errorMessage = "Error while updating lifecycle of API" + apiId + " to " + action;
        Map<String, String> paramList = new HashMap<>();
        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();
    } catch (URISyntaxException e) {
        String errorMessage = "Error while adding location header in response for api : " + apiId;
        Map<String, String> paramList = new HashMap<>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorHandler errorHandler = ExceptionCodes.LOCATION_HEADER_INCORRECT;
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorHandler, paramList);
        log.error(errorMessage, e);
        return Response.status(errorHandler.getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ErrorHandler(org.wso2.carbon.apimgt.core.exception.ErrorHandler) HashMap(java.util.HashMap) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) URISyntaxException(java.net.URISyntaxException) URI(java.net.URI) WorkflowResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.WorkflowResponseDTO) StringTokenizer(java.util.StringTokenizer) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Map(java.util.Map) HashMap(java.util.HashMap)

Example 40 with Resource

use of org.wso2.carbon.registry.api.Resource in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdLifecycleLifecyclePendingTaskDelete.

/**
 * Remove pending lifecycle state change workflow tasks.
 *
 * @param apiId   api id
 * @param request msf4j request object
 * @return Empty payload
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response apisApiIdLifecycleLifecyclePendingTaskDelete(String apiId, Request request) throws NotFoundException {
    try {
        String username = RestApiUtil.getLoggedInUsername(request);
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        apiPublisher.removePendingLifecycleWorkflowTaskForAPI(apiId);
        return Response.ok().build();
    } catch (APIManagementException e) {
        String errorMessage = "Error while removing pending task for API state change for api " + apiId;
        Map<String, String> paramList = new HashMap<>();
        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) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) APIPublisher(org.wso2.carbon.apimgt.core.api.APIPublisher) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)111 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)102 HashMap (java.util.HashMap)91 Test (org.testng.annotations.Test)64 HTTPCarbonMessage (org.wso2.transport.http.netty.message.HTTPCarbonMessage)59 HTTPTestRequest (org.ballerinalang.test.services.testutils.HTTPTestRequest)56 HttpMessageDataStreamer (org.wso2.transport.http.netty.message.HttpMessageDataStreamer)53 BJSON (org.ballerinalang.model.values.BJSON)46 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)38 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)29 BadRequestException (org.wso2.charon3.core.exceptions.BadRequestException)27 APIMgtAdminService (org.wso2.carbon.apimgt.core.api.APIMgtAdminService)24 CharonException (org.wso2.charon3.core.exceptions.CharonException)24 IOException (java.io.IOException)23 Map (java.util.Map)20 SCIMResponse (org.wso2.charon3.core.protocol.SCIMResponse)20 NotFoundException (org.wso2.charon3.core.exceptions.NotFoundException)17 SCIMResourceTypeSchema (org.wso2.charon3.core.schema.SCIMResourceTypeSchema)17 ArrayList (java.util.ArrayList)15 InternalErrorException (org.wso2.charon3.core.exceptions.InternalErrorException)15