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;
}
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()));
}
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;
}
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;
}
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;
}
Aggregations