Search in sources :

Example 46 with Topic

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

the class ApiMgtDAO method getAPITopics.

/**
 * Retrieves the Topic for a specified async API.
 *
 * @param apiId API UUID
 * @return Set of Topic objects
 * @throws APIManagementException if failed to retrieve topics of the web hook API
 */
public Set<Topic> getAPITopics(String apiId) throws APIManagementException {
    Connection conn = null;
    ResultSet resultSet = null;
    PreparedStatement ps = null;
    String getTopicsQuery = SQLConstants.GET_ALL_TOPICS_BY_API_ID;
    Set<Topic> topicSet = new HashSet();
    try {
        conn = APIMgtDBUtil.getConnection();
        ps = conn.prepareStatement(getTopicsQuery);
        ps.setString(1, apiId);
        resultSet = ps.executeQuery();
        while (resultSet.next()) {
            Topic topic = new Topic();
            topic.setName(resultSet.getString("URL_PATTERN"));
            topic.setApiId(resultSet.getString("API_ID"));
            topic.setType(resultSet.getString("HTTP_METHOD"));
            topicSet.add(topic);
        }
        return topicSet;
    } catch (SQLException e) {
        handleException("Failed to retrieve topics available in api " + apiId, e);
    } finally {
        APIMgtDBUtil.closeAllConnections(ps, conn, resultSet);
    }
    return null;
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) ResultSet(java.sql.ResultSet) PreparedStatement(java.sql.PreparedStatement) Topic(org.wso2.carbon.apimgt.api.model.webhooks.Topic) LinkedHashSet(java.util.LinkedHashSet) HashSet(java.util.HashSet)

Example 47 with Topic

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

the class TemplateBuilderUtil method addWebSocketResourceEndpoints.

public static void addWebSocketResourceEndpoints(API api, APITemplateBuilder builder, GatewayAPIDTO gatewayAPIDTO) throws APITemplateException, XMLStreamException {
    Set<URITemplate> uriTemplates = api.getUriTemplates();
    Map<String, Map<String, String>> topicMappings = api.getWebSocketTopicMappingConfiguration().getMappings();
    List<GatewayContentDTO> endpointsToAdd = new ArrayList<>();
    for (URITemplate resource : uriTemplates) {
        String topic = resource.getUriTemplate();
        Map<String, String> endpoints = topicMappings.get(topic);
        // Production and Sandbox endpoints
        for (Map.Entry<String, String> endpointData : endpoints.entrySet()) {
            if (!"resourceKey".equals(endpointData.getKey())) {
                String endpointType = endpointData.getKey();
                String endpointUrl = endpointData.getValue();
                String endpointConfigContext = builder.getConfigStringForWebSocketEndpointTemplate(endpointType, getWebsocketResourceKey(topic), endpointUrl);
                GatewayContentDTO endpoint = new GatewayContentDTO();
                // For WS APIs, resource type is not applicable,
                // so we can just use the uriTemplate/uriMapping to identify the resource
                endpoint.setName(getEndpointName(endpointConfigContext));
                endpoint.setContent(endpointConfigContext);
                endpointsToAdd.add(endpoint);
            }
        }
        // once through resources is enough.
        if (APIConstants.GRAPHQL_API.equals(api.getType())) {
            break;
        }
    }
    gatewayAPIDTO.setEndpointEntriesToBeAdd(addGatewayContentsToList(endpointsToAdd, gatewayAPIDTO.getEndpointEntriesToBeAdd()));
}
Also used : URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) ArrayList(java.util.ArrayList) Map(java.util.Map) HashMap(java.util.HashMap) GatewayContentDTO(org.wso2.carbon.apimgt.api.gateway.GatewayContentDTO)

Example 48 with Topic

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

the class APIMappingUtil method getURITemplates.

/**
 * This method returns URI templates according to the given list of operations.
 *
 * @param operations List operations
 * @return URI Templates
 * @throws APIManagementException
 */
public static Set<URITemplate> getURITemplates(API model, List<APIOperationsDTO> operations) throws APIManagementException {
    boolean isHttpVerbDefined = false;
    Set<URITemplate> uriTemplates = new LinkedHashSet<>();
    if (operations == null || operations.isEmpty()) {
        operations = getDefaultOperationsList(model.getType());
    }
    for (APIOperationsDTO operation : operations) {
        URITemplate template = new URITemplate();
        String uriTempVal = operation.getTarget();
        String httpVerb = operation.getVerb();
        List<String> scopeList = operation.getScopes();
        if (scopeList != null) {
            for (String scopeKey : scopeList) {
                for (Scope definedScope : model.getScopes()) {
                    if (definedScope.getKey().equalsIgnoreCase(scopeKey)) {
                        template.setScopes(definedScope);
                        template.setScope(definedScope);
                        break;
                    }
                }
            }
        }
        // AWS Lambda: set arn to URI template
        String amznResourceName = operation.getAmznResourceName();
        if (amznResourceName != null) {
            template.setAmznResourceName(amznResourceName);
        }
        // Only continue for supported operations
        if (APIConstants.SUPPORTED_METHODS.contains(httpVerb.toLowerCase()) || (APIConstants.GRAPHQL_SUPPORTED_METHOD_LIST.contains(httpVerb.toUpperCase())) || (APIConstants.WEBSUB_SUPPORTED_METHOD_LIST.contains(httpVerb.toUpperCase())) || (APIConstants.SSE_SUPPORTED_METHOD_LIST.contains(httpVerb.toUpperCase())) || (APIConstants.WS_SUPPORTED_METHOD_LIST.contains(httpVerb.toUpperCase()))) {
            isHttpVerbDefined = true;
            String authType = operation.getAuthType();
            if (APIConstants.OASResourceAuthTypes.APPLICATION_OR_APPLICATION_USER.equals(authType)) {
                authType = APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN;
            } else if (APIConstants.OASResourceAuthTypes.APPLICATION_USER.equals(authType)) {
                authType = APIConstants.AUTH_APPLICATION_USER_LEVEL_TOKEN;
            } else if (APIConstants.OASResourceAuthTypes.NONE.equals(authType)) {
                authType = APIConstants.AUTH_NO_AUTHENTICATION;
            } else if (APIConstants.OASResourceAuthTypes.APPLICATION.equals(authType)) {
                authType = APIConstants.AUTH_APPLICATION_LEVEL_TOKEN;
            } else {
                authType = APIConstants.AUTH_APPLICATION_OR_USER_LEVEL_TOKEN;
            }
            template.setThrottlingTier(operation.getThrottlingPolicy());
            template.setThrottlingTiers(operation.getThrottlingPolicy());
            template.setUriTemplate(uriTempVal);
            template.setHTTPVerb(httpVerb.toUpperCase());
            template.setHttpVerbs(httpVerb.toUpperCase());
            template.setAuthType(authType);
            template.setAuthTypes(authType);
            if (operation.getOperationPolicies() != null) {
                template.setOperationPolicies(OperationPolicyMappingUtil.fromDTOToAPIOperationPoliciesList(operation.getOperationPolicies()));
            }
            uriTemplates.add(template);
        } else {
            if (APIConstants.GRAPHQL_API.equals(model.getType())) {
                handleException("The GRAPHQL operation Type '" + httpVerb + "' provided for operation '" + uriTempVal + "' is invalid");
            } else if (APIConstants.API_TYPE_WEBSUB.equals(model.getType())) {
                handleException("The WEBSUB operation Type '" + httpVerb + "' provided for operation '" + uriTempVal + "' is invalid");
            } else if (APIConstants.API_TYPE_SSE.equals(model.getType())) {
                handleException("The SSE operation Type '" + httpVerb + "' provided for operation '" + uriTempVal + "' is invalid");
            } else if (APIConstants.API_TYPE_WS.equals(model.getType())) {
                handleException("The WEBSOCKET operation Type '" + httpVerb + "' provided for operation '" + uriTempVal + "' is invalid");
            } else {
                handleException("The HTTP method '" + httpVerb + "' provided for resource '" + uriTempVal + "' is invalid");
            }
        }
        if (!isHttpVerbDefined) {
            if (APIConstants.GRAPHQL_API.equals(model.getType())) {
                handleException("Operation '" + uriTempVal + "' has global parameters without " + "Operation Type");
            } else if (APIConstants.API_TYPE_WEBSUB.equals(model.getType()) || APIConstants.API_TYPE_SSE.equals(model.getType())) {
                handleException("Topic '" + uriTempVal + "' has global parameters without " + "Operation Type");
            } else {
                handleException("Resource '" + uriTempVal + "' has global parameters without " + "HTTP methods");
            }
        }
    }
    return uriTemplates;
}
Also used : LinkedHashSet(java.util.LinkedHashSet) Scope(org.wso2.carbon.apimgt.api.model.Scope) URITemplate(org.wso2.carbon.apimgt.api.model.URITemplate) APIOperationsDTO(org.wso2.carbon.apimgt.rest.api.publisher.v1.dto.APIOperationsDTO)

Example 49 with Topic

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

the class AsyncAPIMappingUtil method fromTopicListToDTO.

/**
 * Converts Set of Topic objects to DTO.
 *
 * @param topics set of Topic objects
 * @return TopicListDTO containing TopicDTOs
 */
public static TopicListDTO fromTopicListToDTO(Set<Topic> topics) {
    TopicListDTO topicListDTO = new TopicListDTO();
    List<TopicDTO> topicDTOs = topicListDTO.getList();
    topicListDTO.setCount(topics.size());
    if (topicDTOs == null) {
        topicDTOs = new ArrayList<>();
        topicListDTO.setList(topicDTOs);
    }
    for (Topic topic : topics) {
        topicDTOs.add(fromTopicToDTO(topic));
    }
    return topicListDTO;
}
Also used : TopicListDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.TopicListDTO) TopicDTO(org.wso2.carbon.apimgt.rest.api.store.v1.dto.TopicDTO) Topic(org.wso2.carbon.apimgt.api.model.webhooks.Topic)

Example 50 with Topic

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

the class ApisApiServiceImpl method apisApiIdTopicsGet.

@Override
public Response apisApiIdTopicsGet(String apiId, String xWSO2Tenant, MessageContext messageContext) throws APIManagementException {
    if (org.apache.commons.lang.StringUtils.isNotEmpty(apiId)) {
        String username = RestApiCommonUtil.getLoggedInUsername();
        String organization = RestApiUtil.getValidatedOrganization(messageContext);
        Set<Topic> topics;
        try {
            APIConsumer apiConsumer = RestApiCommonUtil.getConsumer(username);
            ApiTypeWrapper apiTypeWrapper = apiConsumer.getAPIorAPIProductByUUID(apiId, organization);
            TopicListDTO topicListDTO;
            if (apiTypeWrapper.isAPIProduct()) {
                topics = apiConsumer.getTopics(apiTypeWrapper.getApiProduct().getUuid());
            } else {
                topics = apiConsumer.getTopics(apiTypeWrapper.getApi().getUuid());
            }
            topicListDTO = AsyncAPIMappingUtil.fromTopicListToDTO(topics);
            return Response.ok().entity(topicListDTO).build();
        } catch (APIManagementException e) {
            if (RestApiUtil.isDueToResourceNotFound(e) || RestApiUtil.isDueToAuthorizationFailure(e)) {
                RestApiUtil.handleResourceNotFoundError(RestApiConstants.RESOURCE_API, apiId, e, log);
            } else {
                RestApiUtil.handleInternalServerError("Failed to get topics of Async API " + apiId, e, log);
            }
        }
    } else {
        RestApiUtil.handleBadRequest("API Id is missing in request", 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) Topic(org.wso2.carbon.apimgt.api.model.webhooks.Topic)

Aggregations

Test (org.testng.annotations.Test)28 SiddhiAppRuntime (org.wso2.siddhi.core.SiddhiAppRuntime)25 SiddhiManager (org.wso2.siddhi.core.SiddhiManager)25 InMemoryBroker (org.wso2.siddhi.core.util.transport.InMemoryBroker)16 InputHandler (org.wso2.siddhi.core.stream.input.InputHandler)13 Event (org.wso2.siddhi.core.event.Event)7 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)6 HashMap (java.util.HashMap)5 Connection (java.sql.Connection)4 PreparedStatement (java.sql.PreparedStatement)4 SQLException (java.sql.SQLException)4 Timestamp (java.sql.Timestamp)4 ArrayList (java.util.ArrayList)4 Date (java.util.Date)4 LinkedHashSet (java.util.LinkedHashSet)4 JsonParser (com.google.gson.JsonParser)3 ResultSet (java.sql.ResultSet)3 HashSet (java.util.HashSet)3 Map (java.util.Map)3 APIConsumer (org.wso2.carbon.apimgt.api.APIConsumer)3