use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method addAPIProductDocumentContent.
@Override
public Response addAPIProductDocumentContent(String apiProductId, String documentId, String ifMatch, InputStream fileInputStream, Attachment fileDetail, String inlineContent, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
APIProduct product = apiProvider.getAPIProductbyUUID(apiProductId, organization);
APIProductIdentifier productIdentifier = product.getId();
if (fileInputStream != null && inlineContent != null) {
RestApiUtil.handleBadRequest("Only one of 'file' and 'inlineContent' should be specified", log);
}
// retrieves the document and send 404 if not found
Documentation documentation = apiProvider.getDocumentation(apiProductId, documentId, organization);
if (documentation == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PRODUCT_DOCUMENTATION, documentId, log);
return null;
}
// add content depending on the availability of either input stream or inline content
if (fileInputStream != null) {
if (!documentation.getSourceType().equals(Documentation.DocumentSourceType.FILE)) {
RestApiUtil.handleBadRequest("Source type of product document " + documentId + " is not FILE", log);
}
RestApiPublisherUtils.attachFileToProductDocument(apiProductId, documentation, fileInputStream, fileDetail, organization);
} else if (inlineContent != null) {
if (!documentation.getSourceType().equals(Documentation.DocumentSourceType.INLINE) && !documentation.getSourceType().equals(Documentation.DocumentSourceType.MARKDOWN)) {
RestApiUtil.handleBadRequest("Source type of product document " + documentId + " is not INLINE " + "or MARKDOWN", log);
}
PublisherCommonUtils.addDocumentationContent(documentation, apiProvider, apiProductId, documentId, organization, inlineContent);
} else {
RestApiUtil.handleBadRequest("Either 'file' or 'inlineContent' should be specified", log);
}
// retrieving the updated doc and the URI
Documentation updatedDoc = apiProvider.getDocumentation(apiProductId, documentId, organization);
DocumentDTO documentDTO = DocumentationMappingUtil.fromDocumentationToDTO(updatedDoc);
String uriString = RestApiConstants.RESOURCE_PATH_PRODUCT_DOCUMENT_CONTENT.replace(RestApiConstants.APIPRODUCTID_PARAM, apiProductId).replace(RestApiConstants.DOCUMENTID_PARAM, documentId);
URI uri = new URI(uriString);
return Response.created(uri).entity(documentDTO).build();
} catch (APIManagementException e) {
// Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while adding content to the document: " + documentId + " of API Product " + apiProductId, e, log);
} else {
RestApiUtil.handleInternalServerError("Failed to add content to the document " + documentId, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving document content location : " + documentId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
} finally {
IOUtils.closeQuietly(fileInputStream);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method deleteAPIProduct.
@Override
public Response deleteAPIProduct(String apiProductId, String ifMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String username = RestApiCommonUtil.getLoggedInUsername();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProductIdentifier apiProductIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, organization);
if (log.isDebugEnabled()) {
log.debug("Delete API Product request: Id " + apiProductId + " by " + username);
}
APIProduct apiProduct = apiProvider.getAPIProductbyUUID(apiProductId, organization);
if (apiProduct == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, log);
}
boolean isAPIPublishedOrDeprecated = APIStatus.PUBLISHED.getStatus().equals(apiProduct.getState()) || APIStatus.DEPRECATED.getStatus().equals(apiProduct.getState());
List<SubscribedAPI> apiUsages = apiProvider.getAPIProductUsageByAPIProductId(apiProductIdentifier);
if (isAPIPublishedOrDeprecated && (apiUsages != null && apiUsages.size() > 0)) {
RestApiUtil.handleConflict("Cannot remove the API " + apiProductIdentifier + " as active subscriptions exist", log);
}
apiProduct.setOrganization(organization);
apiProvider.deleteAPIProduct(apiProduct);
return Response.ok().build();
} catch (APIManagementException e) {
String errorMessage = "Error while deleting API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ResourceConfigContextTest method getAPIProductResources.
private List<APIProductResource> getAPIProductResources(APIProductIdentifier apiProductIdentifier) {
APIProductResource apiProductResource = new APIProductResource();
apiProductResource.setApiIdentifier(new APIIdentifier("admin", "api1", "1.0.0", UUID.randomUUID().toString()));
URITemplate template = new URITemplate();
template.setUriTemplate("/test");
template.setHTTPVerb("GET");
template.setThrottlingTier("Unlimited");
template.setAuthType("Application");
template.setResourceURI("http://maps.googleapis.com/maps/api/geocode/json?address=Colombo");
template.setResourceSandboxURI("http://maps.googleapis.com/maps/api/geocode/json?address=Colombo");
apiProductResource.setProductIdentifier(apiProductIdentifier);
apiProductResource.setUriTemplate(template);
return Arrays.asList(new APIProductResource[] { apiProductResource });
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class APIConfigContext method setApiProductVelocityContext.
private void setApiProductVelocityContext(APIProduct apiProduct, VelocityContext context) {
APIProductIdentifier id = apiProduct.getId();
// set the api name version and context
context.put("apiName", id.getName());
context.put("apiVersion", "1.0.0");
// We set the context pattern now to support plugable version strategy
// context.put("apiContext", api.getContext());
context.put("apiContext", apiProduct.getContext());
// the api object will be passed on to the template so it properties can be used to
// customise how the synapse config is generated.
context.put("apiObj", apiProduct);
context.put("apiIsBlocked", Boolean.FALSE);
String apiSecurity = apiProduct.getApiSecurity();
if (apiSecurity == null || apiSecurity.contains(APIConstants.DEFAULT_API_SECURITY_OAUTH2)) {
context.put("apiIsOauthProtected", Boolean.TRUE);
} else {
context.put("apiIsOauthProtected", Boolean.FALSE);
}
if (apiProduct.isEnabledSchemaValidation()) {
context.put("enableSchemaValidation", Boolean.TRUE);
} else {
context.put("enableSchemaValidation", Boolean.FALSE);
}
if (apiProduct.isEnableStore()) {
context.put("enableStore", Boolean.TRUE);
} else {
context.put("enableStore", Boolean.FALSE);
}
// API test key
context.put("testKey", apiProduct.getTestKey());
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method getAPIProductDocuments.
@Override
public Response getAPIProductDocuments(String apiProductId, Integer limit, Integer offset, String accept, String ifNoneMatch, MessageContext messageContext) {
limit = limit != null ? limit : RestApiConstants.PAGINATION_LIMIT_DEFAULT;
offset = offset != null ? offset : RestApiConstants.PAGINATION_OFFSET_DEFAULT;
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// this will fail if user does not have access to the API Product or the API Product does not exist
APIProductIdentifier productIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, organization);
List<Documentation> allDocumentation = apiProvider.getAllDocumentation(apiProductId, organization);
DocumentListDTO documentListDTO = DocumentationMappingUtil.fromDocumentationListToDTO(allDocumentation, offset, limit);
DocumentationMappingUtil.setPaginationParams(documentListDTO, apiProductId, offset, limit, allDocumentation.size());
return Response.ok().entity(documentListDTO).build();
} catch (APIManagementException e) {
// Auth failure occurs when cross tenant accessing APIs. Sends 404, since we don't need to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API_PRODUCT, apiProductId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving documents of API Product : " + apiProductId, e, log);
} else {
String msg = "Error while retrieving documents of API Product " + apiProductId;
RestApiUtil.handleInternalServerError(msg, e, log);
}
}
return null;
}
Aggregations