use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class TemplateBuilderUtil method createAPIGatewayDTOtoPublishAPI.
private static GatewayAPIDTO createAPIGatewayDTOtoPublishAPI(Environment environment, APIProduct apiProduct, APITemplateBuilder builder, String tenantDomain, Map<String, APIDTO> associatedAPIsMap, List<ClientCertificateDTO> clientCertificatesDTOList) throws APITemplateException, XMLStreamException, APIManagementException {
APIProductIdentifier id = apiProduct.getId();
GatewayAPIDTO productAPIDto = new GatewayAPIDTO();
productAPIDto.setProvider(id.getProviderName());
productAPIDto.setApiId(apiProduct.getUuid());
productAPIDto.setName(id.getName());
productAPIDto.setVersion(id.getVersion());
productAPIDto.setTenantDomain(tenantDomain);
productAPIDto.setKeyManagers(Collections.singletonList(APIConstants.KeyManager.API_LEVEL_ALL_KEY_MANAGERS));
String definition = apiProduct.getDefinition();
productAPIDto.setLocalEntriesToBeRemove(GatewayUtils.addStringToList(apiProduct.getUuid(), productAPIDto.getLocalEntriesToBeRemove()));
GatewayContentDTO productLocalEntry = new GatewayContentDTO();
productLocalEntry.setName(apiProduct.getUuid());
productLocalEntry.setContent("<localEntry key=\"" + apiProduct.getUuid() + "\">" + definition.replaceAll("&(?!amp;)", "&").replaceAll("<", "<").replaceAll(">", ">") + "</localEntry>");
productAPIDto.setLocalEntriesToBeAdd(addGatewayContentToList(productLocalEntry, productAPIDto.getLocalEntriesToBeAdd()));
setClientCertificatesToBeAdded(tenantDomain, productAPIDto, clientCertificatesDTOList);
for (Map.Entry<String, APIDTO> apidtoEntry : associatedAPIsMap.entrySet()) {
String apiExtractedPath = apidtoEntry.getKey();
APIDTO apidto = apidtoEntry.getValue();
API api = APIMappingUtil.fromDTOtoAPI(apidto, apidto.getProvider());
api.setUuid(apidto.getId());
GatewayUtils.setCustomSequencesToBeRemoved(apiProduct.getId(), api.getUuid(), productAPIDto);
APITemplateBuilder apiTemplateBuilder = new APITemplateBuilderImpl(api, apiProduct);
addEndpoints(api, apiTemplateBuilder, productAPIDto);
setCustomSequencesToBeAdded(apiProduct, api, productAPIDto, apiExtractedPath, apidto);
setAPIFaultSequencesToBeAdded(api, productAPIDto, apiExtractedPath, apidto);
String prefix = id.getName() + "--v" + id.getVersion();
setSecureVaultPropertyToBeAdded(prefix, api, productAPIDto);
}
productAPIDto.setApiDefinition(builder.getConfigStringForTemplate(environment));
return productAPIDto;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method deleteAPIProductDocument.
@Override
public Response deleteAPIProductDocument(String apiProductId, String documentId, String ifMatch, MessageContext messageContext) {
Documentation documentation;
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);
documentation = apiProvider.getDocumentation(apiProductId, documentId, organization);
if (documentation == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PRODUCT_DOCUMENTATION, documentId, log);
}
apiProvider.removeDocumentation(apiProductId, documentId, organization);
return Response.ok().build();
} catch (APIManagementException e) {
// Auth failure occurs when cross tenant accessing API Products. 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 deleting : " + documentId + " of API Product " + apiProductId, e, log);
} else {
String errorMessage = "Error while retrieving 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 ApiProductsApiServiceImpl method getAPIProductDocumentContent.
@Override
public Response getAPIProductDocumentContent(String apiProductId, String documentId, String accept, String ifNoneMatch, MessageContext messageContext) {
Documentation documentation;
try {
String username = RestApiCommonUtil.getLoggedInUsername();
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);
// documentation = apiProvider.getProductDocumentation(documentId, tenantDomain);
DocumentationContent docContent = apiProvider.getDocumentationContent(apiProductId, documentId, organization);
if (docContent == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PRODUCT_DOCUMENTATION, documentId, log);
return null;
}
// gets the content depending on the type of the document
if (docContent.getSourceType().equals(DocumentationContent.ContentSourceType.FILE)) {
String contentType = docContent.getResourceFile().getContentType();
contentType = contentType == null ? RestApiConstants.APPLICATION_OCTET_STREAM : contentType;
String name = docContent.getResourceFile().getName();
return Response.ok(docContent.getResourceFile().getContent()).header(RestApiConstants.HEADER_CONTENT_TYPE, contentType).header(RestApiConstants.HEADER_CONTENT_DISPOSITION, "attachment; filename=\"" + name + "\"").build();
} else if (docContent.getSourceType().equals(DocumentationContent.ContentSourceType.INLINE) || docContent.getSourceType().equals(DocumentationContent.ContentSourceType.MARKDOWN)) {
String content = docContent.getTextContent();
return Response.ok(content).header(RestApiConstants.HEADER_CONTENT_TYPE, DOCUMENTATION_INLINE_CONTENT_TYPE).build();
} else if (docContent.getSourceType().equals(DocumentationContent.ContentSourceType.URL)) {
String sourceUrl = docContent.getTextContent();
return Response.seeOther(new URI(sourceUrl)).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_PRODUCT_DOCUMENTATION, apiProductId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving document : " + documentId + " of API Product " + apiProductId, e, log);
} else {
String errorMessage = "Error while retrieving document " + documentId + " of the API Product" + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving source URI location of " + documentId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.model.APIProductIdentifier in project carbon-apimgt by wso2.
the class ApiProductsApiServiceImpl method getLifecycleState.
private LifecycleStateDTO getLifecycleState(String apiProductId, String organization) {
try {
APIProductIdentifier productIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, organization);
return PublisherCommonUtils.getLifecycleStateInformation(productIdentifier, organization);
} catch (APIManagementException e) {
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 the lifecycle " + "state information of API Product with id : " + apiProductId, e, log);
} else {
String errorMessage = "Error while retrieving the lifecycle state information of the API Product with" + " " + "id : " + 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 ApiProductsApiServiceImpl method updateAPIProductDocument.
@Override
public Response updateAPIProductDocument(String apiProductId, String documentId, DocumentDTO body, String ifMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String sourceUrl = body.getSourceUrl();
Documentation oldDocument = apiProvider.getDocumentation(apiProductId, documentId, organization);
// validation checks for existence of the document
if (oldDocument == null) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_PRODUCT_DOCUMENTATION, documentId, log);
return null;
}
if (body.getType() == null) {
throw new BadRequestException();
}
if (body.getType() == DocumentDTO.TypeEnum.OTHER && org.apache.commons.lang3.StringUtils.isBlank(body.getOtherTypeName())) {
// check otherTypeName for not null if doc type is OTHER
RestApiUtil.handleBadRequest("otherTypeName cannot be empty if type is OTHER.", log);
return null;
}
if (body.getSourceType() == DocumentDTO.SourceTypeEnum.URL && (org.apache.commons.lang3.StringUtils.isBlank(sourceUrl) || !RestApiCommonUtil.isURL(sourceUrl))) {
RestApiUtil.handleBadRequest("Invalid document sourceUrl Format", log);
return null;
}
// overriding some properties
body.setName(oldDocument.getName());
Documentation newDocumentation = DocumentationMappingUtil.fromDTOtoDocumentation(body);
// this will fail if user does not have access to the API or the API does not exist
APIProductIdentifier apiIdentifier = APIMappingUtil.getAPIProductIdentifierFromUUID(apiProductId, organization);
newDocumentation.setFilePath(oldDocument.getFilePath());
newDocumentation.setId(oldDocument.getId());
apiProvider.updateDocumentation(apiProductId, newDocumentation, organization);
// retrieve the updated documentation
newDocumentation = apiProvider.getDocumentation(apiProductId, documentId, organization);
return Response.ok().entity(DocumentationMappingUtil.fromDocumentationToDTO(newDocumentation)).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 updating document : " + documentId + " of API Product " + apiProductId, e, log);
} else {
String errorMessage = "Error while updating the document " + documentId + " for API Product : " + apiProductId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
Aggregations