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));
}
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;
}
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());
}
}*/
}
}
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());
}
}
}
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);
}
}
Aggregations