Search in sources :

Example 6 with WSDLArchiveInfo

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo in project carbon-apimgt by wso2.

the class APIStoreImplTestCase method testGetAPIWSDLArchive.

@Test(description = "Retrieve a WSDL archive of an API")
public void testGetAPIWSDLArchive() throws APIManagementException, IOException {
    final String labelName = "SampleLabel";
    Label label = SampleTestObjectCreator.createLabel(labelName, SampleTestObjectCreator.LABEL_TYPE_STORE).build();
    List<String> labels = new ArrayList<>();
    labels.add(label.getName());
    API api = SampleTestObjectCreator.createDefaultAPI().labels(labels).build();
    ApiDAO apiDAO = Mockito.mock(ApiDAO.class);
    LabelDAO labelDAO = Mockito.mock(LabelDAO.class);
    APIStore apiStore = getApiStoreImpl(apiDAO, labelDAO);
    Mockito.when(apiDAO.getAPI(api.getId())).thenReturn(api);
    Mockito.when(labelDAO.getLabelByName(labelName)).thenReturn(label);
    Mockito.when(apiDAO.getWSDLArchive(api.getId())).thenReturn(SampleTestObjectCreator.createDefaultWSDL11ArchiveInputStream());
    String expectedEndpoint = SampleTestObjectCreator.ACCESS_URL + labelName + (api.getContext().startsWith("/") ? api.getContext() : "/" + api.getContext());
    WSDLArchiveInfo archiveInfo = apiStore.getAPIWSDLArchive(api.getId(), label.getName());
    Assert.assertNotNull(archiveInfo);
    Assert.assertNotNull(archiveInfo.getWsdlInfo());
    Assert.assertNotNull(archiveInfo.getWsdlInfo().getEndpoints());
    Map<String, String> endpoints = archiveInfo.getWsdlInfo().getEndpoints();
    Assert.assertTrue(endpoints.containsValue(expectedEndpoint));
    Assert.assertFalse(endpoints.containsValue(SampleTestObjectCreator.ORIGINAL_ENDPOINT_STOCK_QUOTE));
    Assert.assertFalse(endpoints.containsValue(SampleTestObjectCreator.ORIGINAL_ENDPOINT_WEATHER));
}
Also used : Label(org.wso2.carbon.apimgt.core.models.Label) ArrayList(java.util.ArrayList) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) LabelDAO(org.wso2.carbon.apimgt.core.dao.LabelDAO) ApiDAO(org.wso2.carbon.apimgt.core.dao.ApiDAO) APIStore(org.wso2.carbon.apimgt.core.api.APIStore) Test(org.testng.annotations.Test) BeforeTest(org.testng.annotations.BeforeTest)

Example 7 with WSDLArchiveInfo

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo in project carbon-apimgt by wso2.

the class APIMWSDLReader method extractAndValidateWSDLArchive.

/**
 * Extract the WSDL archive zip and validates it
 *
 * @param inputStream zip input stream
 * @return Validation information
 * @throws APIManagementException Error occurred during validation
 */
public static WSDLValidationResponse extractAndValidateWSDLArchive(InputStream inputStream) throws APIManagementException {
    String path = System.getProperty(APIConstants.JAVA_IO_TMPDIR) + File.separator + APIConstants.WSDL_ARCHIVES_TEMP_FOLDER + File.separator + UUID.randomUUID().toString();
    String archivePath = path + File.separator + APIConstants.WSDL_ARCHIVE_ZIP_FILE;
    String extractedLocation = APIFileUtil.extractUploadedArchive(inputStream, APIConstants.API_WSDL_EXTRACTED_DIRECTORY, archivePath, path);
    if (log.isDebugEnabled()) {
        log.debug("Successfully extracted WSDL archive. Location: " + extractedLocation);
    }
    WSDLProcessor processor;
    try {
        processor = getWSDLProcessor(extractedLocation);
    } catch (APIManagementException e) {
        return handleExceptionDuringValidation(e);
    }
    WSDLValidationResponse wsdlValidationResponse = new WSDLValidationResponse();
    if (processor.hasError()) {
        wsdlValidationResponse.setValid(false);
        wsdlValidationResponse.setError(processor.getError());
    } else {
        wsdlValidationResponse.setValid(true);
        WSDLArchiveInfo wsdlArchiveInfo = new WSDLArchiveInfo(path, APIConstants.WSDL_ARCHIVE_ZIP_FILE, processor.getWsdlInfo());
        wsdlValidationResponse.setWsdlArchiveInfo(wsdlArchiveInfo);
        wsdlValidationResponse.setWsdlInfo(processor.getWsdlInfo());
        wsdlValidationResponse.setWsdlProcessor(processor);
    }
    return wsdlValidationResponse;
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.impl.wsdl.WSDLProcessor) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) WSDLValidationResponse(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLValidationResponse) WSDLArchiveInfo(org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo)

Example 8 with WSDLArchiveInfo

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo in project carbon-apimgt by wso2.

the class ApisApiServiceImpl method apisApiIdWsdlGet.

/**
 * Retrieves the WSDL of the particular API. If the WSDL is added as a single file/URL, the text content of the WSDL
 * will be retrived. If the WSDL is added as an archive, the binary content of the archive will be retrieved.
 *
 * @param apiId           UUID of API
 * @param labelName       Name of the label
 * @param ifNoneMatch     If-None-Match header value
 * @param ifModifiedSince If-Modified-Since header value
 * @param request         msf4j request
 * @return WSDL archive/file content
 * @throws NotFoundException
 */
@Override
public Response apisApiIdWsdlGet(String apiId, String labelName, String ifNoneMatch, String ifModifiedSince, Request request) throws NotFoundException {
    String username = RestApiUtil.getLoggedInUsername(request);
    WSDLArchiveInfo wsdlArchiveInfo = null;
    try {
        APIStore apiStore = RestApiUtil.getConsumer(username);
        String wsdlString;
        if (!apiStore.isWSDLExists(apiId)) {
            if (log.isDebugEnabled()) {
                log.debug("WSDL has no content for API: " + apiId);
            }
            return Response.noContent().build();
        }
        if (StringUtils.isBlank(labelName)) {
            if (log.isDebugEnabled()) {
                log.debug("Label not provided since retrieving WSDL archive for default label. API: " + apiId);
            }
            labelName = APIMgtConstants.LabelConstants.DEFAULT;
        }
        boolean isWSDLArchiveExists = apiStore.isWSDLArchiveExists(apiId);
        if (log.isDebugEnabled()) {
            log.debug("API has WSDL archive?: " + isWSDLArchiveExists);
        }
        if (isWSDLArchiveExists) {
            wsdlArchiveInfo = apiStore.getAPIWSDLArchive(apiId, labelName);
            if (log.isDebugEnabled()) {
                log.debug("Successfully retrieved WSDL archive for API: " + apiId);
            }
            // wsdlArchiveInfo will not be null all the time so no need null check
            File archive = new File(wsdlArchiveInfo.getAbsoluteFilePath());
            return Response.ok(archive).header(HttpHeaders.CONTENT_TYPE, MediaType.APPLICATION_OCTET_STREAM_TYPE).header(HttpHeaders.CONTENT_DISPOSITION, "attachment; filename=\"" + wsdlArchiveInfo.getFileName() + "\"").build();
        } else {
            wsdlString = apiStore.getAPIWSDL(apiId, labelName);
            if (log.isDebugEnabled()) {
                log.debug("Successfully retrieved WSDL for API: " + apiId);
            }
            return Response.ok(wsdlString).header(HttpHeaders.CONTENT_TYPE, MediaType.TEXT_PLAIN).build();
        }
    } catch (APIManagementException e) {
        Map<String, String> paramList = new HashMap<String, String>();
        paramList.put(APIMgtConstants.ExceptionsConstants.API_ID, apiId);
        ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
        log.error("Error while getting WSDL for API:" + apiId + " and label:" + labelName, e);
        return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
    } finally {
    // Commented below since MSFJ fails to reply when the files are already deleted. Need to fix this properly
    /*
            if (wsdlArchiveInfo != null) {
                try {
                    APIFileUtils.deleteDirectory(wsdlArchiveInfo.getLocation());
                } catch (APIMgtDAOException e) {
                    //This is not a blocker. Give a warning and continue
                    log.warn("Error occured while deleting processed WSDL artifacts folder : " + wsdlArchiveInfo
                            .getLocation());
                }
            }*/
    }
}
Also used : APIManagementException(org.wso2.carbon.apimgt.core.exception.APIManagementException) ErrorDTO(org.wso2.carbon.apimgt.rest.api.common.dto.ErrorDTO) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) File(java.io.File) HashMap(java.util.HashMap) Map(java.util.Map) APIStore(org.wso2.carbon.apimgt.core.api.APIStore)

Example 9 with WSDLArchiveInfo

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo in project carbon-apimgt by wso2.

the class APIPublisherImpl method updateAPIWSDLArchive.

@Override
public void updateAPIWSDLArchive(String apiId, InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
    WSDLArchiveInfo archiveInfo = null;
    InputStream fileInputStream = null;
    try {
        archiveInfo = extractAndValidateWSDLArchive(inputStream);
        if (log.isDebugEnabled()) {
            log.debug("Successfully extracted and validated WSDL file. Location: " + archiveInfo.getAbsoluteFilePath());
        }
        fileInputStream = new FileInputStream(archiveInfo.getAbsoluteFilePath());
        getApiDAO().addOrUpdateWSDLArchive(apiId, fileInputStream, getUsername());
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated the WSDL archive in DB. API uuid: " + apiId);
        }
    } catch (IOException e) {
        throw new APIMgtWSDLException("Unable to process WSDL archive at " + archiveInfo.getAbsoluteFilePath(), e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
    } finally {
        try {
            if (fileInputStream != null) {
                fileInputStream.close();
            }
            if (archiveInfo != null) {
                APIFileUtils.deleteDirectory(archiveInfo.getLocation());
            }
        } catch (APIMgtDAOException | IOException e) {
            // This is not a blocker. Give a warning and continue
            log.warn("Error occured while deleting processed WSDL artifacts folder : " + archiveInfo.getLocation());
        }
    }
}
Also used : APIMgtDAOException(org.wso2.carbon.apimgt.core.exception.APIMgtDAOException) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) FileInputStream(java.io.FileInputStream)

Example 10 with WSDLArchiveInfo

use of org.wso2.carbon.apimgt.impl.wsdl.model.WSDLArchiveInfo in project carbon-apimgt by wso2.

the class APIStoreImpl method getAPIWSDLArchive.

@Override
public WSDLArchiveInfo getAPIWSDLArchive(String apiId, String labelName) throws APIMgtDAOException, APIMgtWSDLException, APINotFoundException, LabelException {
    API api = getApiDAO().getAPI(apiId);
    if (api == null) {
        throw new APINotFoundException("API with id " + apiId + " not found.", ExceptionCodes.API_NOT_FOUND);
    }
    // api.getLabels() should not be null and the labels should contain labelName
    if ((api.getLabels() == null || !api.getLabels().contains(labelName))) {
        throw new LabelException("API with id " + apiId + " does not contain label " + labelName, ExceptionCodes.LABEL_NOT_FOUND_IN_API);
    }
    try (InputStream wsdlZipInputStream = getApiDAO().getWSDLArchive(apiId)) {
        String rootPath = System.getProperty(APIMgtConstants.JAVA_IO_TMPDIR) + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVES_FOLDERNAME + File.separator + UUID.randomUUID().toString();
        String archivePath = rootPath + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME;
        String extractedLocation = APIFileUtils.extractUploadedArchive(wsdlZipInputStream, APIMgtConstants.WSDLConstants.EXTRACTED_WSDL_ARCHIVE_FOLDERNAME, archivePath, rootPath);
        if (log.isDebugEnabled()) {
            log.debug("Successfully extracted WSDL archive in path: " + extractedLocation);
        }
        Label label = getLabelDAO().getLabelByName(labelName);
        WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessorForPath(extractedLocation);
        String wsdlPath = processor.getUpdatedWSDLPath(api, label);
        if (log.isDebugEnabled()) {
            log.debug("Successfully updated WSDLs in path [" + extractedLocation + "] with endpoints of label: " + labelName + " and context of API " + api.getContext());
        }
        String wsdlArchiveProcessedFileName = api.getProvider() + "-" + api.getName() + "-" + api.getVersion() + "-" + labelName + "-wsdl";
        APIFileUtils.archiveDirectory(wsdlPath, rootPath, wsdlArchiveProcessedFileName);
        if (log.isDebugEnabled()) {
            log.debug("Successfully archived WSDL files: " + wsdlPath);
        }
        WSDLArchiveInfo archiveInfo = new WSDLArchiveInfo(rootPath, wsdlArchiveProcessedFileName + ".zip");
        archiveInfo.setWsdlInfo(processor.getWsdlInfo());
        return archiveInfo;
    } catch (IOException e) {
        throw new APIMgtWSDLException(e);
    }
}
Also used : WSDLProcessor(org.wso2.carbon.apimgt.core.api.WSDLProcessor) APIMgtWSDLException(org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) Label(org.wso2.carbon.apimgt.core.models.Label) CompositeAPI(org.wso2.carbon.apimgt.core.models.CompositeAPI) API(org.wso2.carbon.apimgt.core.models.API) WSDLArchiveInfo(org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo) IOException(java.io.IOException) LabelException(org.wso2.carbon.apimgt.core.exception.LabelException) APINotFoundException(org.wso2.carbon.apimgt.core.exception.APINotFoundException)

Aggregations

WSDLArchiveInfo (org.wso2.carbon.apimgt.core.models.WSDLArchiveInfo)9 IOException (java.io.IOException)5 File (java.io.File)4 InputStream (java.io.InputStream)4 APIMgtWSDLException (org.wso2.carbon.apimgt.core.exception.APIMgtWSDLException)4 FileInputStream (java.io.FileInputStream)3 Response (javax.ws.rs.core.Response)3 APIStore (org.wso2.carbon.apimgt.core.api.APIStore)3 WSDLProcessor (org.wso2.carbon.apimgt.core.api.WSDLProcessor)3 Test (org.junit.Test)2 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)2 APIPublisher (org.wso2.carbon.apimgt.core.api.APIPublisher)2 WorkflowResponse (org.wso2.carbon.apimgt.core.api.WorkflowResponse)2 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)2 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)2 API (org.wso2.carbon.apimgt.core.models.API)2 CompositeAPI (org.wso2.carbon.apimgt.core.models.CompositeAPI)2 Label (org.wso2.carbon.apimgt.core.models.Label)2 WSDLInfo (org.wso2.carbon.apimgt.core.models.WSDLInfo)2 GeneralWorkflowResponse (org.wso2.carbon.apimgt.core.workflow.GeneralWorkflowResponse)2