use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisImportDefinitionPost.
/**
* Import an API from a Swagger or WSDL
*
* @param type definition type. If not specified, default will be SWAGGER
* @param fileInputStream file content stream, can be either archive or a single text file
* @param fileDetail file details
* @param url URL of the definition
* @param additionalProperties Additional attributes specified as a stringified JSON with API's schema
* @param ifMatch If-Match header value
* @param ifUnmodifiedSince If-Unmodified-Since header value
* @param implementationType WSDL based API implementation type (SOAP or HTTP_BINDING)
* @param request msf4j request object
* @return Imported API
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisImportDefinitionPost(String type, InputStream fileInputStream, FileInfo fileDetail, String url, String additionalProperties, String implementationType, String ifMatch, String ifUnmodifiedSince, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
if (StringUtils.isBlank(type)) {
type = APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString();
}
Response response = buildResponseIfParamsInvalid(type, fileInputStream, url);
if (response != null) {
return response;
}
API.APIBuilder apiBuilder = null;
APIDTO additionalPropertiesAPI = null;
if (!StringUtils.isBlank(additionalProperties)) {
if (log.isDebugEnabled()) {
log.debug("Deseriallizing additionalProperties: " + additionalProperties);
}
ObjectMapper mapper = new ObjectMapper();
additionalPropertiesAPI = mapper.readValue(additionalProperties, APIDTO.class);
apiBuilder = MappingUtil.toAPI(additionalPropertiesAPI);
if (log.isDebugEnabled()) {
log.debug("Successfully deseriallized additionalProperties: " + additionalProperties);
}
}
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
String uuid = "";
if (APIDefinitionValidationResponseDTO.DefinitionTypeEnum.SWAGGER.toString().equals(type)) {
if (log.isDebugEnabled()) {
log.debug("Adding an API by importing a swagger.");
}
if (fileInputStream != null) {
uuid = apiPublisher.addApiFromDefinition(fileInputStream);
} else {
URL swaggerUrl = new URL(url);
HttpURLConnection urlConn = (HttpURLConnection) swaggerUrl.openConnection();
uuid = apiPublisher.addApiFromDefinition(urlConn);
}
} else {
if (log.isDebugEnabled()) {
log.debug("Adding an API by importing a WSDL.");
}
// context, version when creating an API from WSDL
if (additionalPropertiesAPI != null) {
final String soap = RestApiConstants.IMPORT_DEFINITION_WSDL_IMPL_TYPE_SOAP;
final String httpBinding = RestApiConstants.IMPORT_DEFINITION_WSDL_IMPL_TYPE_HTTP;
if (implementationType != null && !soap.equals(implementationType) && !httpBinding.equals(implementationType)) {
String msg = "Invalid implementation type. Should be one of '" + soap + "' or '" + httpBinding + "'";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
boolean isHttpBinding = httpBinding.equals(implementationType);
if (fileInputStream != null) {
if (fileDetail.getFileName() == null) {
String msg = "File name cannot be null.";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
if (fileDetail.getFileName().endsWith(".zip")) {
uuid = apiPublisher.addAPIFromWSDLArchive(apiBuilder, fileInputStream, isHttpBinding);
if (log.isDebugEnabled()) {
log.debug("Successfully added API with WSDL archive " + fileDetail.getFileName());
}
} else if (fileDetail.getFileName().endsWith(".wsdl")) {
uuid = apiPublisher.addAPIFromWSDLFile(apiBuilder, fileInputStream, isHttpBinding);
if (log.isDebugEnabled()) {
log.debug("Successfully added API with 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();
}
} else {
uuid = apiPublisher.addAPIFromWSDLURL(apiBuilder, url, isHttpBinding);
if (log.isDebugEnabled()) {
log.debug("Successfully added API with WSDL URL " + url);
}
}
} else {
String msg = "'additionalProperties' should be specified when creating an API from WSDL";
log.error(msg);
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(msg, 900700L, msg);
return Response.status(Response.Status.BAD_REQUEST).entity(errorDTO).build();
}
}
API returnAPI = apiPublisher.getAPIbyUUID(uuid);
return Response.status(Response.Status.CREATED).entity(MappingUtil.toAPIDto(returnAPI)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding new API";
HashMap<String, String> paramList = new HashMap<String, String>();
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (IOException e) {
String errorMessage = "Error while adding new API";
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.models.API.APIBuilder in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisPost.
/**
* Creates a new API
*
* @param body DTO model including the API details
* @param request msf4j request object
* @return Newly created API
* @throws NotFoundException When the particular resource does not exist in the system
*/
@Override
public Response apisPost(APIDTO body, Request request) throws NotFoundException {
String username = RestApiUtil.getLoggedInUsername(request);
try {
API.APIBuilder apiBuilder = MappingUtil.toAPI(body);
APIPublisher apiPublisher = RestAPIPublisherUtil.getApiPublisher(username);
apiPublisher.addAPI(apiBuilder);
API returnAPI = apiPublisher.getAPIbyUUID(apiBuilder.getId());
return Response.status(Response.Status.CREATED).entity(MappingUtil.toAPIDto(returnAPI)).build();
} catch (APIManagementException e) {
String errorMessage = "Error while adding new API : " + body.getProvider() + "-" + body.getName() + "-" + body.getVersion();
HashMap<String, String> paramList = new HashMap<String, String>();
paramList.put(APIMgtConstants.ExceptionsConstants.API_NAME, body.getName());
paramList.put(APIMgtConstants.ExceptionsConstants.API_VERSION, body.getVersion());
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(e.getErrorHandler(), paramList);
log.error(errorMessage, e);
return Response.status(e.getErrorHandler().getHttpStatusCode()).entity(errorDTO).build();
} catch (JsonProcessingException e) {
String errorMessage = "Error while adding new API";
ErrorDTO errorDTO = RestApiUtil.getErrorDTO(errorMessage, 900313L, errorMessage);
log.error(errorMessage, e);
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(errorDTO).build();
} catch (IOException e) {
String errorMessage = "Error while adding new API";
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.models.API.APIBuilder in project carbon-apimgt by wso2.
the class TestUtil method createApi.
protected static API.APIBuilder createApi(String provider, String apiId, String name, String version, String description, Map<String, Endpoint> endpointTypeToIdMap) throws APIManagementException {
Set<String> transport = new HashSet<>();
transport.add("http");
Set<Policy> policies = new HashSet<>();
policies.add(new SubscriptionPolicy("Silver"));
policies.add(new SubscriptionPolicy("Bronze"));
Set<String> tags = new HashSet<>();
tags.add("food");
tags.add("beverage");
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner("John Doe");
businessInformation.setBusinessOwnerEmail("john.doe@annonymous.com");
businessInformation.setTechnicalOwner("Jane Doe");
businessInformation.setBusinessOwnerEmail("jane.doe@annonymous.com");
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList("GET", "POST", "DELETE"));
corsConfiguration.setAllowHeaders(Arrays.asList("Authorization", "X-Custom"));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Collections.singletonList("*"));
API.APIBuilder apiBuilder = new API.APIBuilder(provider, name, version).id(apiId).context(UUID.randomUUID().toString()).description(description).lifeCycleStatus("CREATED").apiDefinition("swagger definition").wsdlUri("http://www.webservicex.net/globalweather.asmx?op=GetWeather?wsdl").isResponseCachingEnabled(true).cacheTimeout(120).isDefaultVersion(true).apiPolicy(new APIPolicy("Gold")).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.RESTRICTED).visibleRoles(new HashSet<>(Arrays.asList("customer", "manager", "employee"))).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy("Adam Doe").lastUpdatedTime(LocalDateTime.now()).endpoint(endpointTypeToIdMap);
apiBuilder.uriTemplates(Collections.emptyMap());
return apiBuilder;
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APIMappingUtilTestCase method createApi.
private static API.APIBuilder createApi(String provider, String apiId, String name, String version, String description, Map<String, Endpoint> endpointTypeToIdMap) throws APIManagementException {
Set<String> transport = new HashSet<>();
transport.add("http");
Set<Policy> policies = new HashSet<>();
policies.add(new SubscriptionPolicy("Silver"));
policies.add(new SubscriptionPolicy("Bronze"));
Set<String> tags = new HashSet<>();
tags.add("food");
tags.add("beverage");
BusinessInformation businessInformation = new BusinessInformation();
businessInformation.setBusinessOwner("John Doe");
businessInformation.setBusinessOwnerEmail("john.doe@annonymous.com");
businessInformation.setTechnicalOwner("Jane Doe");
businessInformation.setBusinessOwnerEmail("jane.doe@annonymous.com");
CorsConfiguration corsConfiguration = new CorsConfiguration();
corsConfiguration.setEnabled(true);
corsConfiguration.setAllowMethods(Arrays.asList("GET", "POST", "DELETE"));
corsConfiguration.setAllowHeaders(Arrays.asList("Authorization", "X-Custom"));
corsConfiguration.setAllowCredentials(true);
corsConfiguration.setAllowOrigins(Collections.singletonList("*"));
API.APIBuilder apiBuilder = new API.APIBuilder(provider, name, version).id(apiId).context(UUID.randomUUID().toString()).description(description).lifeCycleStatus("CREATED").apiDefinition("").wsdlUri("http://www.webservicex.net/globalweather.asmx?op=GetWeather?wsdl").isResponseCachingEnabled(true).cacheTimeout(120).isDefaultVersion(true).apiPolicy(new APIPolicy("Gold")).transport(transport).tags(tags).policies(policies).visibility(API.Visibility.RESTRICTED).visibleRoles(new HashSet<>(Arrays.asList("customer", "manager", "employee"))).businessInformation(businessInformation).corsConfiguration(corsConfiguration).createdTime(LocalDateTime.now()).createdBy("Adam Doe").lastUpdatedTime(LocalDateTime.now()).endpoint(endpointTypeToIdMap);
apiBuilder.uriTemplates(Collections.emptyMap());
return apiBuilder;
}
use of org.wso2.carbon.apimgt.core.models.API.APIBuilder in project carbon-apimgt by wso2.
the class APISubscriptionDAOImpl method createSubscriptionsWithApiInformationOnly.
private List<Subscription> createSubscriptionsWithApiInformationOnly(ResultSet rs) throws APIMgtDAOException {
List<Subscription> subscriptionList = new ArrayList<>();
try {
Subscription subscription;
while (rs.next()) {
String subscriptionId = rs.getString("SUBS_UUID");
String subscriptionTier = rs.getString("SUBS_POLICY");
API.APIBuilder apiBuilder = new API.APIBuilder(rs.getString("API_PROVIDER"), rs.getString("API_NAME"), rs.getString("API_VERSION"));
apiBuilder.id(rs.getString("API_ID"));
apiBuilder.context(rs.getString("API_CONTEXT"));
API api = apiBuilder.build();
subscription = new Subscription(subscriptionId, null, api, new SubscriptionPolicy(subscriptionTier));
subscription.setStatus(APIMgtConstants.SubscriptionStatus.valueOf(rs.getString("SUB_STATUS")));
subscriptionList.add(subscription);
}
} catch (SQLException e) {
throw new APIMgtDAOException(DAOUtil.DAO_ERROR_PREFIX + "creating subscriptions api information", e);
}
return subscriptionList;
}
Aggregations