use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class InboundWebSocketProcessor method setMatchingResource.
/**
* Get matching resource for invoking handshake request.
*
* @param ctx Channel context
* @param req Handshake request
* @param inboundMessageContext InboundMessageContext
* @throws WebSocketApiException If an error occurs
* @throws ResourceNotFoundException If no matching API or resource found
*/
private void setMatchingResource(ChannelHandlerContext ctx, FullHttpRequest req, InboundMessageContext inboundMessageContext) throws WebSocketApiException, ResourceNotFoundException {
String matchingResource;
try {
MessageContext synCtx = getMessageContext(inboundMessageContext);
API api = InboundWebsocketProcessorUtil.getApi(synCtx, inboundMessageContext);
if (api == null) {
throw new ResourceNotFoundException("No matching API found to dispatch the request");
}
inboundMessageContext.setApi(api);
reConstructFullUriWithVersion(req, synCtx, inboundMessageContext);
inboundMessageContext.setApiContext(api.getContext());
Resource selectedResource = null;
Utils.setSubRequestPath(api, synCtx);
Set<Resource> acceptableResources = new LinkedHashSet<>(Arrays.asList(api.getResources()));
if (!acceptableResources.isEmpty()) {
for (RESTDispatcher dispatcher : ApiUtils.getDispatchers()) {
Resource resource = dispatcher.findResource(synCtx, acceptableResources);
if (resource != null) {
selectedResource = resource;
if (APIUtil.isAnalyticsEnabled()) {
WebSocketUtils.setApiPropertyToChannel(ctx, APIMgtGatewayConstants.SYNAPSE_ENDPOINT_ADDRESS, WebSocketUtils.getEndpointUrl(resource, synCtx));
}
break;
}
}
}
setApiPropertiesToChannel(ctx, inboundMessageContext);
if (selectedResource == null) {
throw new ResourceNotFoundException("No matching resource found to dispatch the request");
}
if (APIConstants.GRAPHQL_API.equals(inboundMessageContext.getElectedAPI().getApiType())) {
inboundMessageContext.setGraphQLSchemaDTO(DataHolder.getInstance().getGraphQLSchemaDTOForAPI(inboundMessageContext.getElectedAPI().getUuid()));
}
matchingResource = selectedResource.getDispatcherHelper().getString();
if (log.isDebugEnabled()) {
log.info("Selected resource for API dispatch : " + matchingResource);
}
} catch (AxisFault | URISyntaxException e) {
throw new WebSocketApiException("Error while getting matching resource for Websocket API");
}
inboundMessageContext.setMatchingResource(matchingResource);
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext 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);
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext 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;
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class WebsocketHandlerTestCase method testWSWriteSuccessResponse.
/*
* This method tests write() when msg is a WebSocketFrame for WebSocket API.
* */
@Test
public void testWSWriteSuccessResponse() throws Exception {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
inboundMessageContext.setElectedAPI(websocketAPI);
InboundMessageContextDataHolder.getInstance().addInboundMessageContextForConnection(channelIdString, inboundMessageContext);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
PowerMockito.when(InboundWebsocketProcessorUtil.doThrottle(Mockito.anyInt(), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject())).thenReturn(responseDTO);
websocketHandler.write(channelHandlerContext, msg, channelPromise);
Assert.assertTrue((InboundMessageContextDataHolder.getInstance().getInboundMessageContextMap().containsKey(// No error has occurred context exists in data-holder map.
channelIdString)));
}
use of org.wso2.carbon.apimgt.gateway.inbound.InboundMessageContext in project carbon-apimgt by wso2.
the class WebsocketHandlerTestCase method testWSWriteErrorResponse.
/*
* This method tests write() when msg is a WebSocketFrame for WebSocket API and returns error responses.
* */
@Test
public void testWSWriteErrorResponse() throws Exception {
InboundMessageContext inboundMessageContext = new InboundMessageContext();
inboundMessageContext.setElectedAPI(websocketAPI);
InboundMessageContextDataHolder.getInstance().addInboundMessageContextForConnection(channelIdString, inboundMessageContext);
InboundProcessorResponseDTO responseDTO = new InboundProcessorResponseDTO();
responseDTO.setError(true);
responseDTO.setCloseConnection(true);
PowerMockito.when(InboundWebsocketProcessorUtil.doThrottle(Mockito.anyInt(), Mockito.anyObject(), Mockito.anyObject(), Mockito.anyObject())).thenReturn(responseDTO);
websocketHandler.write(channelHandlerContext, msg, channelPromise);
Assert.assertFalse(InboundMessageContextDataHolder.getInstance().getInboundMessageContextMap().containsKey(// Closing connection error has occurred
channelIdString));
// Websocket frame error has occurred
InboundMessageContextDataHolder.getInstance().addInboundMessageContextForConnection(channelIdString, inboundMessageContext);
responseDTO.setError(true);
responseDTO.setCloseConnection(false);
websocketHandler.write(channelHandlerContext, msg, channelPromise);
Assert.assertTrue((InboundMessageContextDataHolder.getInstance().getInboundMessageContextMap().containsKey(channelIdString)));
}
Aggregations