Search in sources :

Example 41 with InboundMessageContext

use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.

the class InboundWebsocketProcessorUtil method findMatchingVerb.

/**
 * Finds matching VerbInfoDTO for the subscription operation.
 *
 * @param operation             subscription operation name
 * @param inboundMessageContext InboundMessageContext
 * @return VerbInfoDTO
 */
public static VerbInfoDTO findMatchingVerb(String operation, InboundMessageContext inboundMessageContext) {
    String resourceCacheKey;
    VerbInfoDTO verbInfoDTO = null;
    if (inboundMessageContext.getResourcesMap() != null) {
        ResourceInfoDTO resourceInfoDTO = inboundMessageContext.getResourcesMap().get(operation);
        Set<VerbInfoDTO> verbDTOList = resourceInfoDTO.getHttpVerbs();
        for (VerbInfoDTO verb : verbDTOList) {
            if (verb.getHttpVerb().equals(GraphQLConstants.SubscriptionConstants.HTTP_METHOD_NAME)) {
                if (isResourcePathMatching(operation, resourceInfoDTO)) {
                    verbInfoDTO = verb;
                    resourceCacheKey = APIUtil.getResourceInfoDTOCacheKey(inboundMessageContext.getApiContext(), inboundMessageContext.getVersion(), operation, GraphQLConstants.SubscriptionConstants.HTTP_METHOD_NAME);
                    verb.setRequestKey(resourceCacheKey);
                    break;
                }
            }
        }
    }
    return verbInfoDTO;
}
Also used : VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) ResourceInfoDTO(org.wso2.carbon.apimgt.impl.dto.ResourceInfoDTO)

Example 42 with InboundMessageContext

use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.

the class HandshakeProcessor method processHandshake.

/**
 * This method process websocket handshake and perform authentication using the inbound message context.
 * For successful authentications, it sets the resource map of the invoking API to the context.
 *
 * @param inboundMessageContext InboundMessageContext
 * @return InboundProcessorResponseDTO with handshake processing response
 */
public InboundProcessorResponseDTO processHandshake(InboundMessageContext inboundMessageContext) {
    if (log.isDebugEnabled()) {
        log.debug("Processing handshake message for inbound websocket context: " + inboundMessageContext.getApiContext());
    }
    InboundProcessorResponseDTO inboundProcessorResponseDTO = new InboundProcessorResponseDTO();
    boolean isOAuthHeaderValid;
    try {
        isOAuthHeaderValid = InboundWebsocketProcessorUtil.isAuthenticated(inboundMessageContext);
    } catch (APIManagementException e) {
        log.error(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_GENERAL_MESSAGE, e);
        return InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_GENERAL_MESSAGE);
    } catch (APISecurityException e) {
        log.error(e);
        return InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, e.getMessage());
    }
    if (isOAuthHeaderValid) {
        if (log.isDebugEnabled()) {
            log.debug("Handshake authentication success for inbound websocket context: " + inboundMessageContext.getApiContext() + " Setting ResourceInfoDTOs of elected API " + "to inbound message context");
        }
        setResourcesMapToContext(inboundMessageContext);
    } else {
        log.error("Authentication failed for " + inboundMessageContext.getApiContext());
        return InboundWebsocketProcessorUtil.getHandshakeErrorDTO(WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_ERROR, WebSocketApiConstants.HandshakeErrorConstants.API_AUTH_INVALID_CREDENTIALS_MESSAGE);
    }
    return inboundProcessorResponseDTO;
}
Also used : APISecurityException(org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException) APIManagementException(org.wso2.carbon.apimgt.api.APIManagementException) InboundProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO)

Example 43 with InboundMessageContext

use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.

the class HandshakeProcessor method setResourcesMapToContext.

/**
 * Set the resource map with VerbInfoDTOs to the context using URL mappings from the InboundMessageContext.
 *
 * @param inboundMessageContext InboundMessageContext
 */
private void setResourcesMapToContext(InboundMessageContext inboundMessageContext) {
    List<URLMapping> urlMappings = inboundMessageContext.getElectedAPI().getResources();
    Map<String, ResourceInfoDTO> resourcesMap = inboundMessageContext.getResourcesMap();
    ResourceInfoDTO resourceInfoDTO;
    VerbInfoDTO verbInfoDTO;
    for (URLMapping urlMapping : urlMappings) {
        resourceInfoDTO = resourcesMap.get(urlMapping.getUrlPattern());
        if (resourceInfoDTO == null) {
            resourceInfoDTO = new ResourceInfoDTO();
            resourceInfoDTO.setUrlPattern(urlMapping.getUrlPattern());
            resourceInfoDTO.setHttpVerbs(new LinkedHashSet<>());
            resourcesMap.put(urlMapping.getUrlPattern(), resourceInfoDTO);
        }
        verbInfoDTO = new VerbInfoDTO();
        verbInfoDTO.setHttpVerb(urlMapping.getHttpMethod());
        verbInfoDTO.setAuthType(urlMapping.getAuthScheme());
        verbInfoDTO.setThrottling(urlMapping.getThrottlingPolicy());
        resourceInfoDTO.getHttpVerbs().add(verbInfoDTO);
    }
}
Also used : URLMapping(org.wso2.carbon.apimgt.api.model.subscription.URLMapping) VerbInfoDTO(org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO) ResourceInfoDTO(org.wso2.carbon.apimgt.impl.dto.ResourceInfoDTO)

Example 44 with InboundMessageContext

use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.

the class InboundWebSocketProcessor method getMessageContext.

/**
 * Get synapse message context from tenant domain.
 *
 * @param inboundMessageContext InboundMessageContext
 * @return MessageContext
 * @throws AxisFault          if an error occurs getting context
 * @throws URISyntaxException if an error occurs getting transport scheme
 */
private MessageContext getMessageContext(InboundMessageContext inboundMessageContext) throws AxisFault, URISyntaxException {
    String tenantDomain = inboundMessageContext.getTenantDomain();
    MessageContext synCtx = WebsocketUtil.getSynapseMessageContext(tenantDomain);
    org.apache.axis2.context.MessageContext msgCtx = ((Axis2MessageContext) synCtx).getAxis2MessageContext();
    msgCtx.setIncomingTransportName(new URI(inboundMessageContext.getFullRequestPath()).getScheme());
    msgCtx.setProperty(Constants.Configuration.TRANSPORT_IN_URL, inboundMessageContext.getFullRequestPath());
    // Sets axis2 message context in InboundMessageContext for later use
    inboundMessageContext.setAxis2MessageContext(msgCtx);
    return synCtx;
}
Also used : MessageContext(org.apache.synapse.MessageContext) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext) InboundMessageContext(org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext) URI(java.net.URI) Axis2MessageContext(org.apache.synapse.core.axis2.Axis2MessageContext)

Example 45 with InboundMessageContext

use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.

the class InboundWebSocketProcessor method handleRequest.

/**
 * This method process websocket request messages (publish messages) and
 * hand over the processing to relevant request intercepting processor for authentication, scope validation,
 * throttling etc.
 *
 * @param msg                   Websocket request message frame
 * @param inboundMessageContext InboundMessageContext
 * @return InboundProcessorResponseDTO with handshake processing response
 */
public InboundProcessorResponseDTO handleRequest(WebSocketFrame msg, InboundMessageContext inboundMessageContext) {
    RequestProcessor requestProcessor;
    String msgText = null;
    if (APIConstants.GRAPHQL_API.equals(inboundMessageContext.getElectedAPI().getApiType()) && msg instanceof TextWebSocketFrame) {
        requestProcessor = new GraphQLRequestProcessor();
        msgText = ((TextWebSocketFrame) msg).text();
    } else {
        requestProcessor = new RequestProcessor();
    }
    return requestProcessor.handleRequest(msg.content().capacity(), msgText, inboundMessageContext);
}
Also used : GraphQLRequestProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.request.GraphQLRequestProcessor) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) RequestProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.request.RequestProcessor) GraphQLRequestProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.request.GraphQLRequestProcessor)

Aggregations

InboundProcessorResponseDTO (org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO)32 InboundMessageContext (org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext)31 Test (org.junit.Test)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)15 GraphQLProcessorResponseDTO (org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO)12 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)8 GraphQLOperationDTO (org.wso2.carbon.apimgt.gateway.dto.GraphQLOperationDTO)7 GraphQLSchema (graphql.schema.GraphQLSchema)6 SchemaParser (graphql.schema.idl.SchemaParser)6 TypeDefinitionRegistry (graphql.schema.idl.TypeDefinitionRegistry)6 JSONObject (org.json.simple.JSONObject)6 JSONParser (org.json.simple.parser.JSONParser)6 GraphQLSchemaDTO (org.wso2.carbon.apimgt.api.gateway.GraphQLSchemaDTO)6 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)5 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)5 APISecurityException (org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException)5 WebSocketApiException (org.wso2.carbon.apimgt.gateway.handlers.streaming.websocket.WebSocketApiException)4 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)3 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)3