Search in sources :

Example 66 with APIConsumer

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 67 with APIConsumer

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer) URISyntaxException(java.net.URISyntaxException) Map(java.util.Map) HashMap(java.util.HashMap) URI(java.net.URI)

Example 68 with APIConsumer

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 69 with APIConsumer

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;
}
Also used : Comment(org.wso2.carbon.apimgt.api.model.Comment) JSONObject(org.json.simple.JSONObject) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) ApiTypeWrapper(org.wso2.carbon.apimgt.api.model.ApiTypeWrapper) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Example 70 with APIConsumer

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;
}
Also used : APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) Environment(org.wso2.carbon.apimgt.api.model.Environment) API(org.wso2.carbon.apimgt.api.model.API) APIConsumer(org.wso2.carbon.apimgt.api.APIConsumer)

Aggregations

APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)91 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)79 Application (org.wso2.carbon.apimgt.api.model.Application)50 Test (org.junit.Test)46 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)46 HashMap (java.util.HashMap)32 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)29 ArrayList (java.util.ArrayList)28 API (org.wso2.carbon.apimgt.api.model.API)28 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)28 JSONObject (org.json.simple.JSONObject)23 ExportedApplication (org.wso2.carbon.apimgt.rest.api.store.v1.models.ExportedApplication)23 Subscriber (org.wso2.carbon.apimgt.api.model.Subscriber)20 Map (java.util.Map)19 Matchers.anyString (org.mockito.Matchers.anyString)19 ApiTypeWrapper (org.wso2.carbon.apimgt.api.model.ApiTypeWrapper)18 Tier (org.wso2.carbon.apimgt.api.model.Tier)18 DevPortalAPI (org.wso2.carbon.apimgt.persistence.dto.DevPortalAPI)15 URI (java.net.URI)13 URISyntaxException (java.net.URISyntaxException)13