use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdGraphqlSchemaGet.
@Override
public Response apisApiIdGraphqlSchemaGet(String apiId, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
// keep this line to check the existence of the api
apiConsumer.getLightweightAPIByUUID(apiId, organization);
String graphQLSchema = apiConsumer.getGraphqlSchemaDefinition(apiId, organization);
return Response.ok().entity(graphQLSchema).build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while retrieving API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getCommentOfAPI.
@Override
public Response getCommentOfAPI(String commentId, String apiId, String xWSO2Tenant, String ifNoneMatch, Boolean includeCommenterInfo, Integer replyLimit, Integer replyOffset, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = apiConsumer.getComment(apiTypeWrapper, commentId, replyLimit, replyOffset);
if (comment != null) {
CommentDTO commentDTO;
if (includeCommenterInfo) {
Map<String, Map<String, String>> userClaimsMap = CommentMappingUtil.retrieveUserClaims(comment.getUser(), new HashMap<>());
commentDTO = CommentMappingUtil.fromCommentToDTOWithUserInfo(comment, userClaimsMap);
} else {
commentDTO = CommentMappingUtil.fromCommentToDTO(comment);
}
String uriString = RestApiConstants.RESOURCE_PATH_APIS + "/" + apiId + RestApiConstants.RESOURCE_PATH_COMMENTS + "/" + commentId;
URI uri = new URI(uriString);
return Response.ok(uri).entity(commentDTO).build();
} else {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while retrieving comment for API : " + apiId + "with comment ID " + commentId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
} catch (URISyntaxException e) {
String errorMessage = "Error while retrieving comment content location : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method getAPIByAPIId.
private APIDTO getAPIByAPIId(String apiId, String organization) {
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper api = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
String status = api.getStatus();
// Extracting clicked API name by the user, for the recommendation system
String userName = RestApiCommonUtil.getLoggedInUsername();
apiConsumer.publishClickedAPI(api, userName);
if (APIConstants.PUBLISHED.equals(status) || APIConstants.PROTOTYPED.equals(status) || APIConstants.DEPRECATED.equals(status)) {
return APIMappingUtil.fromAPItoDTO(api, organization);
} else {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, log);
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while retrieving API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method deleteComment.
@Override
public Response deleteComment(String commentId, String apiId, String ifMatch, MessageContext messageContext) throws APIManagementException {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
String username = RestApiCommonUtil.getLoggedInUsername();
try {
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
Comment comment = apiConsumer.getComment(apiTypeWrapper, commentId, 0, 0);
if (comment != null) {
String[] tokenScopes = (String[]) PhaseInterceptorChain.getCurrentMessage().getExchange().get(RestApiConstants.USER_REST_API_SCOPES);
if (Arrays.asList(tokenScopes).contains("apim:admin") || comment.getUser().equals(username)) {
if (apiConsumer.deleteComment(apiTypeWrapper, commentId)) {
JSONObject obj = new JSONObject();
obj.put("id", commentId);
obj.put("message", "The comment has been deleted");
return Response.ok(obj).type(MediaType.APPLICATION_JSON).build();
} else {
return Response.status(405, "Method Not Allowed").type(MediaType.APPLICATION_JSON).build();
}
} else {
return Response.status(403, "Forbidden").type(MediaType.APPLICATION_JSON).build();
}
} else {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_COMMENTS, String.valueOf(commentId), log);
}
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while deleting comment " + commentId + "for API " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
use of org.wso2.carbon.apimgt.api.APIConsumer in project carbon-apimgt by wso2.
the class ApisApiServiceImpl method apisApiIdSwaggerGet.
/**
* Retrieves the swagger document of an API
*
* @param apiId API identifier
* @param environmentName name of the gateway environment
* @param ifNoneMatch If-None-Match header value
* @param xWSO2Tenant requested tenant domain for cross tenant invocations
* @param messageContext CXF message context
* @return Swagger document of the API for the given cluster or gateway environment
*/
@Override
public Response apisApiIdSwaggerGet(String apiId, String environmentName, String ifNoneMatch, String xWSO2Tenant, MessageContext messageContext) {
try {
String organization = RestApiUtil.getValidatedOrganization(messageContext);
APIConsumer apiConsumer = RestApiCommonUtil.getLoggedInUserConsumer();
API api = apiConsumer.getLightweightAPIByUUID(apiId, organization);
if (api.getUuid() == null) {
api.setUuid(apiId);
}
if (api.getSwaggerDefinition() != null) {
api.setSwaggerDefinition(APIUtil.removeXMediationScriptsFromSwagger(api.getSwaggerDefinition()));
} else {
api.setSwaggerDefinition(apiConsumer.getOpenAPIDefinition(apiId, organization));
}
// gets the first available environment if environment is not provided
if (StringUtils.isEmpty(environmentName)) {
Map<String, Environment> existingEnvironments = APIUtil.getEnvironments(organization);
// then the old gateway environment name becomes invalid
for (String environmentNameOfApi : api.getEnvironments()) {
if (existingEnvironments.get(environmentNameOfApi) != null) {
environmentName = environmentNameOfApi;
break;
}
}
// if all environment of API are invalid or there are no environments (i.e. empty)
if (StringUtils.isEmpty(environmentName)) {
// This is to make sure the swagger doesn't have invalid endpoints
if (!existingEnvironments.keySet().isEmpty()) {
environmentName = existingEnvironments.keySet().iterator().next();
}
}
}
String apiSwagger = null;
if (StringUtils.isNotEmpty(environmentName)) {
try {
apiSwagger = apiConsumer.getOpenAPIDefinitionForEnvironment(api, environmentName);
} catch (APIManagementException e) {
// handle gateway not found exception otherwise pass it
if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError("Gateway environment '" + environmentName + "' not found", e, log);
return null;
}
throw e;
}
} else {
apiSwagger = api.getSwaggerDefinition();
}
return Response.ok().entity(apiSwagger).header("Content-Disposition", "attachment; filename=\"" + "swagger.json" + "\"").build();
} catch (APIManagementException e) {
if (RestApiUtil.isDueToAuthorizationFailure(e)) {
RestApiUtil.handleAuthorizationFailure(RestApiConstants.RESOURCE_API, apiId, e, log);
} else if (RestApiUtil.isDueToResourceNotFound(e)) {
RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
} else {
String errorMessage = "Error while retrieving swagger of API : " + apiId;
RestApiUtil.handleInternalServerError(errorMessage, e, log);
}
}
return null;
}
Aggregations