Search in sources :

Example 51 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method setApiResourceBuilderProperties.

/**
 * Extract properties in Operation entry and assign them to api resource builder properties.
 *
 * @param operationEntry     Map entry to be extracted properties
 * @param uriTemplateBuilder Uri template builder to assign related properties
 * @param resourcePath       resource path
 * @return APIResource.Builder object
 */
private APIResource.Builder setApiResourceBuilderProperties(Map.Entry<HttpMethod, Operation> operationEntry, UriTemplate.UriTemplateBuilder uriTemplateBuilder, String resourcePath) {
    Operation operation = operationEntry.getValue();
    APIResource.Builder apiResourceBuilder = new APIResource.Builder();
    List<String> producesList = operation.getProduces();
    if (producesList != null) {
        String produceSeparatedString = "\"";
        produceSeparatedString += String.join("\",\"", producesList) + "\"";
        apiResourceBuilder.produces(produceSeparatedString);
    }
    List<String> consumesList = operation.getConsumes();
    if (consumesList != null) {
        String consumesSeparatedString = "\"";
        consumesSeparatedString += String.join("\",\"", consumesList) + "\"";
        apiResourceBuilder.consumes(consumesSeparatedString);
    }
    if (operation.getOperationId() != null) {
        uriTemplateBuilder.templateId(operation.getOperationId());
    } else {
        uriTemplateBuilder.templateId(APIUtils.generateOperationIdFromPath(resourcePath, operationEntry.getKey().name()));
    }
    uriTemplateBuilder.httpVerb(operationEntry.getKey().name());
    apiResourceBuilder.uriTemplate(uriTemplateBuilder.build());
    return apiResourceBuilder;
}
Also used : APIResource(org.wso2.carbon.apimgt.core.models.APIResource) Operation(io.swagger.models.Operation)

Example 52 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIDefinitionFromSwagger20 method generateSwaggerFromResources.

@Override
public String generateSwaggerFromResources(CompositeAPI.Builder api) {
    Swagger swagger = new Swagger();
    Info info = new Info();
    info.setTitle(api.getName());
    info.setDescription(api.getDescription());
    info.setVersion(api.getVersion());
    swagger.setInfo(info);
    Map<String, Path> stringPathMap = new HashMap();
    for (UriTemplate uriTemplate : api.getUriTemplates().values()) {
        String uriTemplateString = uriTemplate.getUriTemplate();
        List<Parameter> parameterList = getParameters(uriTemplateString);
        if (!HttpMethod.GET.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.DELETE.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.OPTIONS.toString().equalsIgnoreCase(uriTemplate.getHttpVerb()) && !HttpMethod.HEAD.toString().equalsIgnoreCase(uriTemplate.getHttpVerb())) {
            parameterList.add(getDefaultBodyParameter());
        }
        Operation operation = new Operation();
        operation.setParameters(parameterList);
        operation.setOperationId(uriTemplate.getTemplateId());
        operation.addResponse("200", getDefaultResponse());
        if (stringPathMap.containsKey(uriTemplateString)) {
            Path path = stringPathMap.get(uriTemplateString);
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
        } else {
            Path path = new Path();
            path.set(uriTemplate.getHttpVerb().toLowerCase(), operation);
            stringPathMap.put(uriTemplateString, path);
        }
    }
    swagger.setPaths(stringPathMap);
    swagger.setPaths(stringPathMap);
    return Json.pretty(swagger);
}
Also used : Path(io.swagger.models.Path) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) HashMap(java.util.HashMap) Swagger(io.swagger.models.Swagger) FormParameter(io.swagger.models.parameters.FormParameter) PathParameter(io.swagger.models.parameters.PathParameter) Parameter(io.swagger.models.parameters.Parameter) QueryParameter(io.swagger.models.parameters.QueryParameter) BodyParameter(io.swagger.models.parameters.BodyParameter) Operation(io.swagger.models.Operation) Info(io.swagger.models.Info) ServiceMethodInfo(org.wso2.msf4j.ServiceMethodInfo) UriTemplate(org.wso2.carbon.apimgt.core.models.UriTemplate)

Example 53 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method addCompositeAPI.

/**
 * @see APIGateway#addCompositeAPI(CompositeAPI api)
 */
@Override
public void addCompositeAPI(CompositeAPI api) throws GatewayException {
    // build the message to send
    APIEvent gatewayDTO = new APIEvent(APIMgtConstants.GatewayEventTypes.API_CREATE);
    gatewayDTO.setLabels(api.getLabels());
    APISummary apiSummary = new APISummary();
    apiSummary.setName(api.getName());
    apiSummary.setVersion(api.getVersion());
    apiSummary.setContext(api.getContext());
    apiSummary.setThreatProtectionPolicies(api.getThreatProtectionPolicies());
    gatewayDTO.setApiSummary(apiSummary);
    publishToPublisherTopic(gatewayDTO);
}
Also used : APISummary(org.wso2.carbon.apimgt.core.models.APISummary) APIEvent(org.wso2.carbon.apimgt.core.models.events.APIEvent)

Example 54 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method addAPI.

@Override
public void addAPI(API api) throws GatewayException {
    // build the message to send
    APIEvent apiCreateEvent = new APIEvent(APIMgtConstants.GatewayEventTypes.API_CREATE);
    apiCreateEvent.setLabels(api.getLabels());
    apiCreateEvent.setApiSummary(toAPISummary(api));
    publishToPublisherTopic(apiCreateEvent);
    if (log.isDebugEnabled()) {
        log.debug("API : " + api.getName() + " created event has been successfully published to broker");
    }
}
Also used : APIEvent(org.wso2.carbon.apimgt.core.models.events.APIEvent)

Example 55 with API

use of org.wso2.carbon.apimgt.api.model.API in project carbon-apimgt by wso2.

the class APIGatewayPublisherImpl method toAPISummary.

/**
 * Convert API definition into APISummary
 *
 * @param api API definition
 * @return The summary of the API
 */
private APISummary toAPISummary(API api) {
    APISummary apiSummary = new APISummary();
    apiSummary.setId(api.getId());
    apiSummary.setName(api.getName());
    apiSummary.setVersion(api.getVersion());
    apiSummary.setContext(api.getContext());
    apiSummary.setLifeCycleStatus(api.getLifeCycleStatus());
    apiSummary.setLifeCycleStatus(api.getLifeCycleStatus());
    apiSummary.setCreatedTime(api.getCreatedTime());
    apiSummary.setLastUpdatedTime(api.getLastUpdatedTime());
    apiSummary.setSecurityScheme(api.getSecurityScheme());
    apiSummary.setThreatProtectionPolicies(api.getThreatProtectionPolicies());
    return apiSummary;
}
Also used : APISummary(org.wso2.carbon.apimgt.core.models.APISummary)

Aggregations

APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)582 ArrayList (java.util.ArrayList)374 API (org.wso2.carbon.apimgt.core.models.API)359 Test (org.testng.annotations.Test)350 HashMap (java.util.HashMap)318 Test (org.junit.Test)316 API (org.wso2.carbon.apimgt.api.model.API)307 APIIdentifier (org.wso2.carbon.apimgt.api.model.APIIdentifier)255 ApiDAO (org.wso2.carbon.apimgt.core.dao.ApiDAO)253 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)234 SQLException (java.sql.SQLException)190 SubscribedAPI (org.wso2.carbon.apimgt.api.model.SubscribedAPI)186 IOException (java.io.IOException)181 APIManagementException (org.wso2.carbon.apimgt.core.exception.APIManagementException)179 PreparedStatement (java.sql.PreparedStatement)169 Connection (java.sql.Connection)158 APIMgtDAOException (org.wso2.carbon.apimgt.core.exception.APIMgtDAOException)154 RegistryException (org.wso2.carbon.registry.core.exceptions.RegistryException)149 JSONObject (org.json.simple.JSONObject)142 Resource (org.wso2.carbon.registry.core.Resource)139