use of org.wso2.carbon.apimgt.core.api.WSDLProcessor 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();
}
}
use of org.wso2.carbon.apimgt.core.api.WSDLProcessor in project carbon-apimgt by wso2.
the class APIPublisherImpl method extractAndValidateWSDLArchive.
@Override
public WSDLArchiveInfo extractAndValidateWSDLArchive(InputStream inputStream) throws APIMgtDAOException, APIMgtWSDLException {
String path = System.getProperty(APIMgtConstants.JAVA_IO_TMPDIR) + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVES_FOLDERNAME + File.separator + UUID.randomUUID().toString();
String archivePath = path + File.separator + APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME;
String extractedLocation = APIFileUtils.extractUploadedArchive(inputStream, APIMgtConstants.WSDLConstants.EXTRACTED_WSDL_ARCHIVE_FOLDERNAME, archivePath, path);
if (log.isDebugEnabled()) {
log.debug("Successfully extracted WSDL archive. Location: " + extractedLocation);
}
WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessorForPath(extractedLocation);
if (!processor.canProcess()) {
throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
WSDLArchiveInfo archiveInfo = new WSDLArchiveInfo(path, APIMgtConstants.WSDLConstants.WSDL_ARCHIVE_FILENAME);
archiveInfo.setWsdlInfo(processor.getWsdlInfo());
return archiveInfo;
}
use of org.wso2.carbon.apimgt.core.api.WSDLProcessor in project carbon-apimgt by wso2.
the class APIPublisherImpl method addAPIFromWSDLFile.
@Override
public String addAPIFromWSDLFile(API.APIBuilder apiBuilder, InputStream inputStream, boolean isHttpBinding) throws APIManagementException {
byte[] wsdlContent;
try {
wsdlContent = IOUtils.toByteArray(inputStream);
} catch (IOException e) {
throw new APIMgtWSDLException("Error while converting input stream to byte array", e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
}
WSDLProcessor processor = WSDLProcessFactory.getInstance().getWSDLProcessor(wsdlContent);
apiBuilder.uriTemplates(APIMWSDLUtils.getUriTemplatesForWSDLOperations(processor.getWsdlInfo().getHttpBindingOperations(), isHttpBinding));
if (!processor.canProcess()) {
throw new APIMgtWSDLException("Unable to process WSDL by the processor " + processor.getClass().getName(), ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
String uuid = addAPI(apiBuilder);
if (log.isDebugEnabled()) {
log.debug("Successfully added the API. uuid: " + uuid);
}
getApiDAO().addOrUpdateWSDL(uuid, wsdlContent, getUsername());
if (log.isDebugEnabled()) {
log.debug("Successfully added the WSDL file to database. API uuid: " + uuid);
}
if (APIMgtConstants.WSDLConstants.WSDL_VERSION_20.equals(processor.getWsdlInfo().getVersion())) {
log.info("Extraction of HTTP Binding operations is not supported for WSDL 2.0.");
}
return uuid;
}
use of org.wso2.carbon.apimgt.core.api.WSDLProcessor in project carbon-apimgt by wso2.
the class WSDLProcessFactory method getWSDLProcessor.
/**
* Returns the appropriate WSDL 1.1 or 2.0 processor based on the content {@code wsdlContent}.
*
* @param wsdlContent Content of the WSDL
* @return WSDL 1.1 or 2.0 processor for the provided content
* @throws APIMgtWSDLException If an error occurs while determining the processor
*/
public WSDLProcessor getWSDLProcessor(byte[] wsdlContent) throws APIMgtWSDLException {
for (String clazz : getWSDLProcessorClasses()) {
WSDLProcessor processor;
try {
processor = (WSDLProcessor) Class.forName(clazz).newInstance();
boolean canProcess = processor.init(wsdlContent);
if (canProcess) {
return processor;
}
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new APIMgtWSDLException("Error while instantiating " + clazz, e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
}
}
// no processors found if this line reaches
throw new APIMgtWSDLException("No WSDL processor found to process WSDL content", ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
use of org.wso2.carbon.apimgt.core.api.WSDLProcessor in project carbon-apimgt by wso2.
the class WSDLProcessFactory method getWSDLProcessorForPath.
/**
* Returns the appropriate WSDL 1.1 or 2.0 processor based on the file path {@code wsdlPath}.
*
* @param wsdlPath File path containing WSDL files and dependant files
* @return WSDL 1.1 or 2.0 processor for the provided content
* @throws APIMgtWSDLException If an error occurs while determining the processor
*/
public WSDLProcessor getWSDLProcessorForPath(String wsdlPath) throws APIMgtWSDLException {
for (String clazz : getWSDLProcessorClasses()) {
WSDLProcessor processor;
try {
processor = (WSDLProcessor) Class.forName(clazz).newInstance();
boolean canProcess = processor.initPath(wsdlPath);
if (canProcess) {
return processor;
}
} catch (InstantiationException | IllegalAccessException | ClassNotFoundException e) {
throw new APIMgtWSDLException("Error while instantiating " + clazz, e, ExceptionCodes.INTERNAL_WSDL_EXCEPTION);
}
}
// no processors found if this line reaches
throw new APIMgtWSDLException("No WSDL processor found to process WSDL content", ExceptionCodes.CANNOT_PROCESS_WSDL_CONTENT);
}
Aggregations