Search in sources :

Example 36 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class Http1Router method processEndOfRequest.

private void processEndOfRequest(ChannelHandlerContext ctx, LastHttpContent msg) {
    var request = requests.getOrDefault(currentInboundRequest, null);
    if (request == null)
        throw new EUnexpected();
    switch(request.status) {
        case RECEIVING:
            request.status = RequestStatus.WAITING_FOR_RESPONSE;
            break;
        case RECEIVING_BIDI:
            request.status = RequestStatus.RESPONDING;
            break;
        case COMPLETED:
        case FAILED:
            requests.remove(currentInboundRequest);
            return;
        default:
            throw new EUnexpected();
    }
    var target = targets.getOrDefault(request.routeIndex, null);
    if (target == null)
        throw new EUnexpected();
    if (target.channel.isActive())
        target.channel.flush();
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 37 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class GrpcWebProxy method channelRead.

@Override
public void channelRead(ChannelHandlerContext ctx, Object msg) {
    // gRPC proxy layer expects all messages to be HTTP/2 frames
    if (!(msg instanceof Http2Frame))
        throw new EUnexpected();
    var frame = (Http2Frame) msg;
    if (frame instanceof Http2HeadersFrame) {
        var grpcFrame = (Http2HeadersFrame) frame;
        var grpcWebFrame = grpcFrame.headers().contains(":status") ? translateResponseHeaders(grpcFrame) : translateResponseTrailers(grpcFrame, ctx.alloc());
        ctx.fireChannelRead(grpcWebFrame);
    } else if (frame instanceof Http2DataFrame) {
        var grpcFrame = (Http2DataFrame) frame;
        var grpcWebFrame = isWebTextProtocol ? encodeWebTextFrame(grpcFrame) : grpcFrame.retain();
        ctx.fireChannelRead(grpcWebFrame);
        // The original frame is no longer needed and can be released
        grpcFrame.release();
    } else {
        // gRPC proxy layer does not interact with control frames (settings/ping/goaway/etc)
        // Most likely there shouldn't be any of these at this layer anyway
        log.warn("Unexpected HTTP/2 response frame ({}) in gRPC-web proxy layer", frame.name());
    // ctx.fireChannelRead(frame);
    }
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 38 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class GrpcWebProxy method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    // gRPC proxy layer expects all messages to be HTTP/2 frames
    if (!(msg instanceof Http2Frame))
        throw new EUnexpected();
    var frame = (Http2Frame) msg;
    if (frame instanceof Http2HeadersFrame) {
        var grpcWebFrame = (Http2HeadersFrame) frame;
        var grpcFrame = translateRequestHeaders(grpcWebFrame);
        log.info("Translating gRPC request for stream {}", grpcWebFrame.stream().id());
        ctx.write(grpcFrame, promise);
    } else if (frame instanceof Http2DataFrame) {
        var grpcWebFrame = (Http2DataFrame) frame;
        var grpcFrame = isWebTextProtocol ? decodeWebTextFrame(grpcWebFrame) : grpcWebFrame.retain();
        ctx.write(grpcFrame, promise);
        // The original frame is no longer needed and can be released
        grpcWebFrame.release();
    } else {
        // gRPC proxy layer does not interact with control frames (settings/ping/goaway/etc)
        // Most likely there shouldn't be any of these at this layer anyway
        log.warn("Unexpected HTTP/2 request frame ({}) in gRPC-web proxy layer", frame.name());
        ctx.write(msg, promise);
    }
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 39 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class RestApiProxy method write.

@Override
public void write(ChannelHandlerContext ctx, Object msg, ChannelPromise promise) {
    try {
        // REST proxy layer expects all messages to be HTTP/2 frames
        if (!(msg instanceof Http2Frame))
            throw new EUnexpected();
        var frame = (Http2Frame) msg;
        if (frame instanceof Http2HeadersFrame) {
            var headersFrame = (Http2HeadersFrame) frame;
            var stream = headersFrame.stream();
            if (!callStateMap.containsKey(stream)) {
                var method = lookupMethod(headersFrame);
                if (method == null) {
                    log.warn("PROXY REST CALL: {} {} ! NOT MAPPED", headersFrame.headers().method(), headersFrame.headers().path());
                    var httpCode = HttpResponseStatus.NOT_FOUND;
                    var httpContent = "REST API NOT MAPPED";
                    sendErrorResponse(stream, ctx, httpCode, httpContent);
                    return;
                } else {
                    log.info("PROXY REST CALL: {} {} -> {}", headersFrame.headers().method(), headersFrame.headers().path(), method.grpcMethod.getFullMethodName());
                    var callState = new RestApiCallState();
                    callState.method = method;
                    callState.requestHeaders = new DefaultHttp2Headers();
                    callState.requestContent = ctx.alloc().compositeBuffer();
                    callState.stream = stream;
                    callStateMap.put(stream, callState);
                }
            }
            var callState = callStateMap.get(stream);
            callState.requestHeaders.add(headersFrame.headers());
            if (headersFrame.isEndStream())
                dispatchUnaryRequest(callState.method, callState, ctx);
        } else if (frame instanceof Http2DataFrame) {
            var dataFrame = (Http2DataFrame) frame;
            var stream = dataFrame.stream();
            var callState = callStateMap.get(stream);
            // TODO: Check call state and stream are good
            if (dataFrame.content() != null && dataFrame.content().readableBytes() > 0)
                callState.requestContent.addComponent(true, dataFrame.content());
            if (dataFrame.isEndStream())
                dispatchUnaryRequest(callState.method, callState, ctx);
        } else {
            log.warn("Unexpected frame type {} will be dropped", frame.name());
        }
    } finally {
        // RestApiProxy uses the HTTP/2 codec and runs on an embedded channel
        // We are intercepting messages and then making calls to gRPC using the gRPC client
        // However, in order for the HTTP/2 codec to work, messages need to propagate down to the codec handler
        // So, instead of releasing the message, pass it on to the next handler
        // And the let the HTTP/2 codec at the end of the pipeline release it
        ctx.write(msg);
    }
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 40 with EUnexpected

use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.

the class RestApiTranslator method translateRequest.

@SuppressWarnings("unchecked")
public TRequest translateRequest(String url) {
    // This should be set up correctly when the API route is created
    if (this.hasBody)
        throw new EUnexpected();
    var request = blankRequest.newBuilderForType();
    var requestUrl = URI.create(url);
    for (var extractor : fieldExtractors) request = extractor.apply(requestUrl, request);
    return (TRequest) request.build();
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Aggregations

EUnexpected (com.accenture.trac.common.exception.EUnexpected)44 IOException (java.io.IOException)8 EDataCorruption (com.accenture.trac.common.exception.EDataCorruption)4 ArrayList (java.util.ArrayList)3 ECacheTicket (com.accenture.trac.common.exception.ECacheTicket)2 EInputValidation (com.accenture.trac.common.exception.EInputValidation)2 ByteSeekableChannel (com.accenture.trac.common.util.ByteSeekableChannel)2 Http1to2Framing (com.accenture.trac.gateway.proxy.http.Http1to2Framing)2 JacksonException (com.fasterxml.jackson.core.JacksonException)2 JsonToken (com.fasterxml.jackson.core.JsonToken)2 ByteString (com.google.protobuf.ByteString)2 InvalidProtocolBufferException (com.google.protobuf.InvalidProtocolBufferException)2 ByteBufInputStream (io.netty.buffer.ByteBufInputStream)2 MetadataBatchRequest (com.accenture.trac.api.MetadataBatchRequest)1 MetadataBatchResponse (com.accenture.trac.api.MetadataBatchResponse)1 MetadataWriteRequest (com.accenture.trac.api.MetadataWriteRequest)1 TrustedMetadataApiGrpc (com.accenture.trac.api.TrustedMetadataApiGrpc)1 EMetadataNotFound (com.accenture.trac.common.exception.EMetadataNotFound)1 EPluginNotAvailable (com.accenture.trac.common.exception.EPluginNotAvailable)1 EStartup (com.accenture.trac.common.exception.EStartup)1