Search in sources :

Example 1 with EUnexpected

use of org.finos.tracdap.common.exception.EUnexpected in project tracdap by finos.

the class RestApiTranslator method translateRequestBody.

public Message translateRequestBody(ByteBuf bodyBuffer) {
    if (!hasBody)
        throw new EUnexpected();
    var bodyType = (blankBody != null) ? blankBody : blankRequest;
    try (var jsonStream = new ByteBufInputStream(bodyBuffer);
        var jsonReader = new InputStreamReader(jsonStream)) {
        var bodyBuilder = bodyType.newBuilderForType();
        var jsonParser = JsonFormat.parser();
        jsonParser.merge(jsonReader, bodyBuilder);
        return bodyBuilder.build();
    } catch (InvalidProtocolBufferException e) {
        // Validation failures will go back to users (API users, i.e. application developers)
        // Strip out GSON class name from the error message for readability
        var detailMessage = e.getLocalizedMessage();
        var classNamePrefix = MalformedJsonException.class.getName() + ": ";
        if (detailMessage.startsWith(classNamePrefix))
            detailMessage = detailMessage.substring(classNamePrefix.length());
        var message = String.format("Invalid JSON input for type [%s]: %s", bodyType.getDescriptorForType().getName(), detailMessage);
        log.warn(message);
        throw new EInputValidation(message, e);
    } catch (IOException e) {
        // Shouldn't happen, reader source is a buffer already held in memory
        log.error("Unexpected IO error reading from internal buffer", e);
        throw new EUnexpected();
    }
}
Also used : EInputValidation(org.finos.tracdap.common.exception.EInputValidation) InputStreamReader(java.io.InputStreamReader) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) EUnexpected(org.finos.tracdap.common.exception.EUnexpected) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 2 with EUnexpected

use of org.finos.tracdap.common.exception.EUnexpected in project tracdap by finos.

the class RestApiTranslator method extractInt.

private TRequest.Builder extractInt(Function<URI, String> rawValueExtractor, Function<TRequest.Builder, Message.Builder> subFieldMapper, Descriptors.FieldDescriptor targetField, URI uri, TRequest.Builder request) {
    var rawValue = rawValueExtractor.apply(uri);
    var stringValue = URLDecoder.decode(rawValue, StandardCharsets.US_ASCII);
    try {
        var intValue = Integer.parseInt(stringValue);
        var subField = subFieldMapper.apply(request);
        subField.setField(targetField, intValue);
        return request;
    } catch (NumberFormatException e) {
        // Invalid values should not make it past the router matcher
        throw new EUnexpected();
    }
}
Also used : EUnexpected(org.finos.tracdap.common.exception.EUnexpected)

Example 3 with EUnexpected

use of org.finos.tracdap.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(org.finos.tracdap.common.exception.EUnexpected)

Example 4 with EUnexpected

use of org.finos.tracdap.common.exception.EUnexpected in project tracdap by finos.

the class Http1Router method processRequestContent.

private void processRequestContent(ChannelHandlerContext ctx, HttpContent msg) {
    try {
        var request = requests.getOrDefault(currentInboundRequest, null);
        if (request == null)
            throw new EUnexpected();
        if (REQUEST_STATUS_FINISHED.contains(request.status))
            return;
        if (!REQUEST_STATUS_CAN_RECEIVE.contains(request.status))
            throw new EUnexpected();
        var target = targets.getOrDefault(request.routeIndex, null);
        if (target == null)
            throw new EUnexpected();
        msg.retain();
        if (target.channel.isActive())
            target.channel.write(msg);
        else
            target.outboundQueue.add(msg);
    } finally {
        ReferenceCountUtil.release(msg);
    }
}
Also used : EUnexpected(org.finos.tracdap.common.exception.EUnexpected)

Example 5 with EUnexpected

use of org.finos.tracdap.common.exception.EUnexpected in project tracdap by finos.

the class Http1to2Framing method channelRead.

@Override
public void channelRead(@Nonnull ChannelHandlerContext ctx, @Nonnull Object msg) throws Exception {
    if (!(msg instanceof Http2Frame))
        throw new EUnexpected();
    var frame = (Http2Frame) msg;
    var httpObjs = translateResponseFrame(frame);
    for (var httpObj : httpObjs) ctx.fireChannelRead(httpObj);
}
Also used : EUnexpected(org.finos.tracdap.common.exception.EUnexpected)

Aggregations

EUnexpected (org.finos.tracdap.common.exception.EUnexpected)57 IOException (java.io.IOException)11 InvocationTargetException (java.lang.reflect.InvocationTargetException)6 ArrayList (java.util.ArrayList)5 EDataCorruption (org.finos.tracdap.common.exception.EDataCorruption)4 Message (com.google.protobuf.Message)3 HashMap (java.util.HashMap)3 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 Method (java.lang.reflect.Method)2 Modifier (java.lang.reflect.Modifier)2 BigDecimal (java.math.BigDecimal)2 LocalDate (java.time.LocalDate)2 LocalDateTime (java.time.LocalDateTime)2 List (java.util.List)2 Map (java.util.Map)2 ETrac (org.finos.tracdap.common.exception.ETrac)2