use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAPIGraphQLSchema.
/**
* Get GraphQL Schema of given API
*
* @param apiId apiId
* @param accept
* @param ifNoneMatch If--Match header value
* @param messageContext message context
* @return Response with GraphQL Schema
*/
@Override
public Response getAPIGraphQLSchema(String apiId, String accept, String ifNoneMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// this will fail if user does not have access to the API or the API does not exist
APIIdentifier apiIdentifier;
if (ApiMgtDAO.getInstance().checkAPIUUIDIsARevisionUUID(apiId) != null) {
apiIdentifier = APIMappingUtil.getAPIInfoFromUUID(apiId, organization).getId();
} else {
apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
}
String schemaContent = apiProvider.getGraphqlSchema(apiIdentifier);
GraphQLSchemaDTO dto = new GraphQLSchemaDTO();
dto.setSchemaDefinition(schemaContent);
dto.setName(apiIdentifier.getProviderName() + APIConstants.GRAPHQL_SCHEMA_PROVIDER_SEPERATOR + apiIdentifier.getApiName() + apiIdentifier.getVersion() + APIConstants.GRAPHQL_SCHEMA_FILE_EXTENSION);
return Response.ok().entity(dto).build();
} catch (APIManagementException e) {
// to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving schema of API: " + apiId, e, log);
} else {
String errorMessage = "Error while retrieving schema of API: " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method addAPIClientCertificate.
@Override
public Response addAPIClientCertificate(String apiId, InputStream certificateInputStream, Attachment certificateDetail, String alias, String tier, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
ContentDisposition contentDisposition = certificateDetail.getContentDisposition();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String fileName = contentDisposition.getParameter(RestApiConstants.CONTENT_DISPOSITION_FILENAME);
if (StringUtils.isEmpty(alias) || StringUtils.isEmpty(apiId)) {
RestApiUtil.handleBadRequest("The alias and/ or apiId should not be empty", log);
}
if (StringUtils.isBlank(fileName)) {
RestApiUtil.handleBadRequest("Certificate addition failed. Proper Certificate file should be provided", log);
}
// validate if api exists
validateAPIExistence(apiId);
API api = apiProvider.getAPIbyUUID(apiId, organization);
api.setOrganization(organization);
// validate API update operation permitted based on the LC state
validateAPIOperationsPerLC(api.getStatus());
String userName = RestApiCommonUtil.getLoggedInUsername();
String base64EncodedCert = CertificateRestApiUtils.generateEncodedCertificate(certificateInputStream);
int responseCode = apiProvider.addClientCertificate(userName, api.getId(), base64EncodedCert, alias, tier, organization);
if (log.isDebugEnabled()) {
log.debug(String.format("Add certificate operation response code : %d", responseCode));
}
if (ResponseCode.SUCCESS.getResponseCode() == responseCode) {
// Handle api product case.
if (API_PRODUCT_TYPE.equals(api.getType())) {
APIIdentifier apiIdentifier = api.getId();
APIProductIdentifier apiProductIdentifier = new APIProductIdentifier(apiIdentifier.getProviderName(), apiIdentifier.getApiName(), apiIdentifier.getVersion());
APIProduct apiProduct = apiProvider.getAPIProduct(apiProductIdentifier);
apiProduct.setOrganization(organization);
apiProvider.updateAPIProduct(apiProduct);
} else {
apiProvider.updateAPI(api);
}
ClientCertMetadataDTO certificateDTO = new ClientCertMetadataDTO();
certificateDTO.setAlias(alias);
certificateDTO.setApiId(apiId);
certificateDTO.setTier(tier);
URI createdCertUri = new URI(RestApiConstants.CLIENT_CERTS_BASE_PATH + "?alias=" + alias);
return Response.created(createdCertUri).entity(certificateDTO).build();
} else if (ResponseCode.INTERNAL_SERVER_ERROR.getResponseCode() == responseCode) {
RestApiUtil.handleInternalServerError("Internal server error while adding the client certificate to " + "API " + apiId, log);
} else if (ResponseCode.ALIAS_EXISTS_IN_TRUST_STORE.getResponseCode() == responseCode) {
RestApiUtil.handleResourceAlreadyExistsError("The alias '" + alias + "' already exists in the trust store.", log);
} else if (ResponseCode.CERTIFICATE_EXPIRED.getResponseCode() == responseCode) {
RestApiUtil.handleBadRequest("Error while adding the certificate to the API " + apiId + ". " + "Certificate Expired.", log);
}
} catch (APIManagementException e) {
RestApiUtil.handleInternalServerError("APIManagement exception while adding the certificate to the API " + apiId + " due to an internal " + "server error", e, log);
} catch (IOException e) {
RestApiUtil.handleInternalServerError("IOException while generating the encoded certificate for the API " + apiId, e, log);
} catch (URISyntaxException e) {
RestApiUtil.handleInternalServerError("Error while generating the resource location URI for alias '" + alias + "'", e, log);
} catch (FaultGatewaysException e) {
RestApiUtil.handleInternalServerError("Error while publishing the certificate change to gateways for the alias " + alias, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method publishAPIToExternalStores.
/**
* Publish API to given external stores.
*
* @param apiId API Id
* @param externalStoreIds External Store Ids
* @param ifMatch If-match header value
* @param messageContext CXF Message Context
* @return Response of published external store list
*/
@Override
public Response publishAPIToExternalStores(String apiId, String externalStoreIds, String ifMatch, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
API api = null;
List<String> externalStoreIdList = Arrays.asList(externalStoreIds.split("\\s*,\\s*"));
try {
APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId);
if (apiIdentifier == null) {
throw new APIMgtResourceNotFoundException("Couldn't retrieve existing API with API UUID: " + apiId, ExceptionCodes.from(ExceptionCodes.API_NOT_FOUND, apiId));
}
api = apiProvider.getAPIbyUUID(apiId, organization);
api.setOrganization(organization);
} catch (APIManagementException e) {
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while getting API: " + apiId;
log.error(errorMessage, e);
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
if (apiProvider.publishToExternalAPIStores(api, externalStoreIdList)) {
Set<APIStore> publishedStores = apiProvider.getPublishedExternalAPIStores(api.getUuid());
APIExternalStoreListDTO apiExternalStoreListDTO = ExternalStoreMappingUtil.fromAPIExternalStoreCollectionToDTO(publishedStores);
return Response.ok().entity(apiExternalStoreListDTO).build();
}
return Response.serverError().build();
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAPIByID.
private APIDTO getAPIByID(String apiId, APIProvider apiProvider, String organization) {
try {
API api = apiProvider.getAPIbyUUID(apiId, organization);
api.setOrganization(organization);
return APIMappingUtil.fromAPItoDTO(api, apiProvider);
} catch (APIManagementException e) {
// to expose the existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("User is not authorized to access the API", e, log);
} else {
String errorMessage = "Error while retrieving API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIProvider in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAPIThumbnail.
/**
* Retrieves the thumbnail image of an API specified by API identifier
*
* @param apiId API Id
* @param ifNoneMatch If-None-Match header value
* @param messageContext If-Modified-Since header value
* @return Thumbnail image of the API
*/
@Override
public Response getAPIThumbnail(String apiId, String ifNoneMatch, MessageContext messageContext) {
try {
APIProvider apiProvider = RestApiCommonUtil.getLoggedInUserProvider();
String organization = RestApiUtil.getValidatedOrganization(messageContext);
// this will fail if user does not have access to the API or the API does not exist
// APIIdentifier apiIdentifier = APIMappingUtil.getAPIIdentifierFromUUID(apiId, tenantDomain);
ResourceFile thumbnailResource = apiProvider.getIcon(apiId, organization);
if (thumbnailResource != null) {
return Response.ok(thumbnailResource.getContent(), MediaType.valueOf(thumbnailResource.getContentType())).build();
} else {
return Response.noContent().build();
}
} catch (APIManagementException e) {
// existence of the resource
if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (isAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure("Authorization failure while retrieving thumbnail of API : " + apiId, e, log);
} else {
String errorMessage = "Error while retrieving thumbnail of API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
Aggregations