Search in sources :

Example 11 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo 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 12 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo 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 13 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisValidateDefinitionPost.

/**
 * Validates a provided API definition
 *
 * @param type            API definition type (SWAGGER or WSDL)
 * @param fileInputStream file content stream
 * @param fileDetail      file details
 * @param url             URL of the definition
 * @param request         msf4j request
 * @return API definition validation information
 * @throws NotFoundException
 */
@Override
public Response apisValidateDefinitionPost(String type, InputStream fileInputStream, FileInfo fileDetail, String url, Request request) throws NotFoundException {
    String errorMessage = "Error while validating the definition";
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
        if (StringUtils.isBlank(type)) {
            type = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
        }
        Response responseIfParamsInvalid = buildResponseIfParamsInvalid(type, fileInputStream, url);
        if (responseIfParamsInvalid != null) {
            return responseIfParamsInvalid;
        }
        if (APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString().equals(type)) {
            if (log.isDebugEnabled()) {
                log.debug("Validating a swagger file.");
            }
            // TODO implement swagger validation
            return Response.noContent().build();
        } else {
            // WSDL type
            WSDLProcessor processor = null;
            WSDLInfo info = null;
            if (!StringUtils.isBlank(url)) {
                processor = WSDLProcessFactory.getInstance().getWSDLProcessor(url);
                info = processor.getWsdlInfo();
                if (log.isDebugEnabled()) {
                    log.debug("Successfully validated WSDL URL " + url);
                }
            } else {
                if (fileDetail.getFileName().endsWith(".zip")) {
                    WSDLArchiveInfo archiveInfo = apiPublisher.extractAndValidateWSDLArchive(fileInputStream);
                    info = archiveInfo.getWsdlInfo();
                    if (log.isDebugEnabled()) {
                        log.debug("Successfully validated WSDL archive " + fileDetail.getFileName());
                    }
                } else if (fileDetail.getFileName().endsWith(".wsdl")) {
                    byte[] wsdlContent = IOUtils.toByteArray(fileInputStream);
                    processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
                    info = processor.getWsdlInfo();
                    if (log.isDebugEnabled()) {
                        log.debug("Successfully validated WSDL file " + fileDetail.getFileName());
                    }
                } else {
                    String msg = "Unsupported extension type of file: " + fileDetail.getFileName();
                    log.error(msg);
                    ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
                    return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
                }
            }
            if (info != null) {
                APIDefinitionValidationResponseDTO responseDTO = MappingUtil.toWSDLValidationResponseDTO(info);
                return Response.ok(responseDTO).build();
            }
            APIDefinitionValidationResponseDTO responseDTO = new APIDefinitionValidationResponseDTO();
            responseDTO.isValid(false);
            return Response.ok().entity(responseDTO).build();
        }
    } catch (APIManagementException e) {
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        log.error(errorMessage, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } catch (IOException e) {
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
        log.error(errorMessage, e);
        return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
    }
}
Also used : WorkflowResponse(org.wso2.carbon.apimgt.core.api.WorkflowResponse) GeneralWorkflowResponse(org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse) Response(javax.ws.rs.core.Response) WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) WSDLInfo(org.wso2.carbon.apimgt.core.models.WSDLInfo) 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) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) APIDefinitionValidationResponseDTO(org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDefinitionValidationResponseDTO)

Example 14 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class ImportApiServiceImpl method importApplicationsPut.

@Override
public Response importApplicationsPut(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
    APIStore consumer = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        consumer = RestApiUtil.getConsumer(RestApiUtil.getLoggedInUsername(request));
        FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
        Application applicationDetails = importExportManager.importApplication(fileInputStream);
        applicationDetails.setCreatedUser(username);
        applicationDetails.setUpdatedUser(username);
        Application updatedApplication = importExportManager.updateApplication(applicationDetails, username);
        return Response.status(Response.Status.OK).entity(updatedApplication).build();
    } catch (APIManagementException e) {
        String errorMsg = "Error while importing the Applications";
        log.error(errorMsg, 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) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 15 with FileInfo

use of org.wso2.msf4j.formparam.FileInfo in project carbon-apimgt by wso2.

the class ImportApiServiceImpl method importApplicationsPost.

/**
 * Import an Application which has been exported to a zip file
 *
 * @param fileInputStream content stream of the zip file which contains exported Application
 * @param fileDetail      meta information of the zip file
 * @param request         msf4j request object
 * @return Application that was imported
 * @throws NotFoundException When the particular resource does not exist in the system
 */
@Override
public Response importApplicationsPost(InputStream fileInputStream, FileInfo fileDetail, Request request) throws NotFoundException {
    APIStore consumer = null;
    String username = RestApiUtil.getLoggedInUsername(request);
    try {
        consumer = RestApiUtil.getConsumer(username);
        FileBasedApplicationImportExportManager importExportManager = new FileBasedApplicationImportExportManager(consumer, System.getProperty("java.io.tmpdir") + File.separator + "exported-app-archives-" + UUID.randomUUID().toString());
        Application applicationDetails = importExportManager.importApplication(fileInputStream);
        applicationDetails.setCreatedUser(username);
        applicationDetails.setUpdatedUser(username);
        ApplicationCreationResponse response = consumer.addApplication(applicationDetails);
        return Response.status(Response.Status.OK).entity(response).build();
    } catch (APIManagementException e) {
        String errorMsg = "Error while importing the Applications";
        log.error(errorMsg, e);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler());
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    }
}
Also used : ApplicationCreationResponse(org.wso2.carbon.apimgt.core.workflow.ApplicationCreationResponse) APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) FileBasedApplicationImportExportManager(org.wso2.carbon.apimgt.rest.api.store.utils.FileBasedApplicationImportExportManager) Application(org.wso2.carbon.apimgt.core.models.Application) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Aggregations

Response (javax.ws.rs.core.Response)24 Test (org.junit.Test)22 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)22 FileInfo (org.wso2.msf4j.formparam.FileInfo)22 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)21 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)21 File (java.io.File)19 FileInputStream (java.io.FileInputStream)19 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)17 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)10 API (org.wso2.carbon.apimgt.core.models.API)10 ErrorDTO (org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO)9 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)6 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)6 APIDTO (org.wso2.carbon.apimgt.rest.api.publisher.dto.APIDTO)6 InputStream (java.io.InputStream)4 HashMap (java.util.HashMap)4 DocumentInfo (org.wso2.carbon.apimgt.core.models.DocumentInfo)3 Request (org.wso2.msf4j.Request)3 IOException (java.io.IOException)2