Search in sources :

Example 1 with WebSocketRequestCallback

use of org.mockserver.closurecallback.websocketregistry.WebSocketRequestCallback in project mockserver by mock-server.

the class HttpForwardObjectCallbackActionHandler method handleViaWebSocket.

private void handleViaWebSocket(HttpActionHandler actionHandler, HttpObjectCallback httpObjectCallback, HttpRequest request, ResponseWriter responseWriter, boolean synchronous, Runnable expectationPostProcessor, String clientId) {
    final String webSocketCorrelationId = UUIDService.getUUID();
    webSocketClientRegistry.registerForwardCallbackHandler(webSocketCorrelationId, new WebSocketRequestCallback() {

        @Override
        public void handle(final HttpRequest callbackRequest) {
            if (MockServerLogger.isEnabled(TRACE)) {
                mockServerLogger.logEvent(new LogEntry().setLogLevel(TRACE).setHttpRequest(request).setMessageFormat("received request over websocket{}from client " + clientId + " for correlationId " + webSocketCorrelationId).setArguments(callbackRequest));
            }
            final HttpForwardActionResult responseFuture = sendRequest(callbackRequest.removeHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME), null, null);
            if (MockServerLogger.isEnabled(TRACE)) {
                mockServerLogger.logEvent(new LogEntry().setLogLevel(TRACE).setHttpRequest(request).setMessageFormat("received response for request{}from client " + clientId).setArguments(callbackRequest));
            }
            webSocketClientRegistry.unregisterForwardCallbackHandler(webSocketCorrelationId);
            if (expectationPostProcessor != null && isFalse(httpObjectCallback.getResponseCallback())) {
                expectationPostProcessor.run();
            }
            if (isTrue(httpObjectCallback.getResponseCallback())) {
                handleResponseViaWebSocket(callbackRequest, responseFuture, actionHandler, webSocketCorrelationId, clientId, expectationPostProcessor, responseWriter, httpObjectCallback, synchronous);
            } else {
                actionHandler.writeForwardActionResponse(responseFuture, responseWriter, callbackRequest, httpObjectCallback, synchronous);
            }
        }

        @Override
        public void handleError(HttpResponse httpResponse) {
            if (MockServerLogger.isEnabled(DEBUG)) {
                mockServerLogger.logEvent(new LogEntry().setLogLevel(DEBUG).setHttpRequest(request).setMessageFormat("error sending request over websocket for client " + clientId + " for correlationId " + webSocketCorrelationId));
            }
            webSocketClientRegistry.unregisterForwardCallbackHandler(webSocketCorrelationId);
            actionHandler.writeResponseActionResponse(httpResponse, responseWriter, request, httpObjectCallback, synchronous);
        }
    });
    if (!webSocketClientRegistry.sendClientMessage(clientId, request.clone().withHeader(WEB_SOCKET_CORRELATION_ID_HEADER_NAME, webSocketCorrelationId), null)) {
        if (MockServerLogger.isEnabled(WARN)) {
            mockServerLogger.logEvent(new LogEntry().setLogLevel(WARN).setHttpRequest(request).setMessageFormat("returning{}because client " + clientId + " has closed web socket connection").setArguments(notFoundResponse()));
        }
        actionHandler.writeForwardActionResponse(notFoundFuture(request), responseWriter, request, httpObjectCallback, synchronous);
    } else if (MockServerLogger.isEnabled(TRACE)) {
        mockServerLogger.logEvent(new LogEntry().setLogLevel(TRACE).setHttpRequest(request).setMessageFormat("sending request over websocket{}to client " + clientId + " for correlationId " + webSocketCorrelationId).setArguments(request));
    }
}
Also used : HttpRequest(org.mockserver.model.HttpRequest) WebSocketRequestCallback(org.mockserver.closurecallback.websocketregistry.WebSocketRequestCallback) HttpResponse(org.mockserver.model.HttpResponse) HttpRequestAndHttpResponse(org.mockserver.model.HttpRequestAndHttpResponse) LogEntry(org.mockserver.log.model.LogEntry)

Aggregations

WebSocketRequestCallback (org.mockserver.closurecallback.websocketregistry.WebSocketRequestCallback)1 LogEntry (org.mockserver.log.model.LogEntry)1 HttpRequest (org.mockserver.model.HttpRequest)1 HttpRequestAndHttpResponse (org.mockserver.model.HttpRequestAndHttpResponse)1 HttpResponse (org.mockserver.model.HttpResponse)1