Search in sources :

Example 36 with InboundProcessorResponseDTO

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

the class InboundWebsocketProcessorUtil method doThrottleForGraphQL.

/**
 * Checks if the request is throttled for GraphQL subscriptions.
 *
 * @param msgSize               Websocket msg size
 * @param verbInfoDTO           VerbInfoDTO for invoking operation.
 * @param inboundMessageContext InboundMessageContext
 * @param operationId           Operation ID
 * @return InboundProcessorResponseDTO
 */
public static InboundProcessorResponseDTO doThrottleForGraphQL(int msgSize, VerbInfoDTO verbInfoDTO, InboundMessageContext inboundMessageContext, String operationId) {
    GraphQLProcessorResponseDTO responseDTO = new GraphQLProcessorResponseDTO();
    responseDTO.setId(operationId);
    return InboundWebsocketProcessorUtil.doThrottle(msgSize, verbInfoDTO, inboundMessageContext, responseDTO);
}
Also used : GraphQLProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO)

Example 37 with InboundProcessorResponseDTO

use of org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO 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 38 with InboundProcessorResponseDTO

use of org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO 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)

Example 39 with InboundProcessorResponseDTO

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

the class InboundWebSocketProcessor method handleResponse.

/**
 * This method process websocket response messages (subscribe messages) and
 * hand over the processing to relevant response 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 handleResponse(WebSocketFrame msg, InboundMessageContext inboundMessageContext) throws Exception {
    ResponseProcessor responseProcessor;
    String msgText = null;
    if (APIConstants.GRAPHQL_API.equals(inboundMessageContext.getElectedAPI().getApiType()) && msg instanceof TextWebSocketFrame) {
        responseProcessor = new GraphQLResponseProcessor();
        msgText = ((TextWebSocketFrame) msg).text();
    } else {
        responseProcessor = new ResponseProcessor();
    }
    return responseProcessor.handleResponse(msg.content().capacity(), msgText, inboundMessageContext);
}
Also used : GraphQLResponseProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.response.GraphQLResponseProcessor) TextWebSocketFrame(io.netty.handler.codec.http.websocketx.TextWebSocketFrame) GraphQLResponseProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.response.GraphQLResponseProcessor) ResponseProcessor(org.wso2.carbon.apimgt.gateway.inbound.websocket.response.ResponseProcessor)

Example 40 with InboundProcessorResponseDTO

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

the class GraphQLRequestProcessor method validateQueryPayload.

/**
 * Validates GraphQL query payload using QueryValidator and graphql schema of the invoking API.
 *
 * @param inboundMessageContext InboundMessageContext
 * @param document              Graphql payload
 * @param operationId           Graphql message id
 * @return InboundProcessorResponseDTO
 */
private InboundProcessorResponseDTO validateQueryPayload(InboundMessageContext inboundMessageContext, Document document, String operationId) {
    GraphQLProcessorResponseDTO responseDTO = new GraphQLProcessorResponseDTO();
    responseDTO.setId(operationId);
    QueryValidator queryValidator = new QueryValidator(new Validator());
    // payload validation
    String validationErrorMessage = queryValidator.validatePayload(inboundMessageContext.getGraphQLSchemaDTO().getGraphQLSchema(), document);
    if (validationErrorMessage != null) {
        String error = WebSocketApiConstants.FrameErrorConstants.GRAPHQL_INVALID_QUERY_MESSAGE + " : " + validationErrorMessage;
        log.error(error);
        responseDTO.setError(true);
        responseDTO.setErrorCode(WebSocketApiConstants.FrameErrorConstants.GRAPHQL_INVALID_QUERY);
        responseDTO.setErrorMessage(error);
        return responseDTO;
    }
    return responseDTO;
}
Also used : GraphQLProcessorResponseDTO(org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO) QueryValidator(org.wso2.carbon.apimgt.common.gateway.graphql.QueryValidator) QueryValidator(org.wso2.carbon.apimgt.common.gateway.graphql.QueryValidator) Validator(graphql.validation.Validator)

Aggregations

InboundProcessorResponseDTO (org.wso2.carbon.apimgt.gateway.inbound.websocket.InboundProcessorResponseDTO)35 InboundMessageContext (org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext)28 Test (org.junit.Test)26 PrepareForTest (org.powermock.core.classloader.annotations.PrepareForTest)26 VerbInfoDTO (org.wso2.carbon.apimgt.impl.dto.VerbInfoDTO)13 GraphQLProcessorResponseDTO (org.wso2.carbon.apimgt.gateway.inbound.websocket.GraphQLProcessorResponseDTO)12 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 APIKeyValidationInfoDTO (org.wso2.carbon.apimgt.impl.dto.APIKeyValidationInfoDTO)6 TextWebSocketFrame (io.netty.handler.codec.http.websocketx.TextWebSocketFrame)5 APISecurityException (org.wso2.carbon.apimgt.gateway.handlers.security.APISecurityException)4 CloseWebSocketFrame (io.netty.handler.codec.http.websocketx.CloseWebSocketFrame)3 WebSocketFrame (io.netty.handler.codec.http.websocketx.WebSocketFrame)3 JSONObject (org.json.JSONObject)3 APIManagementException (org.wso2.carbon.apimgt.api.APIManagementException)3