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();
}
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);
}
}
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);
}
}
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);
}
}
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();
}
Aggregations