Search in sources :

Example 11 with EUnexpected

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

the class RestApiTranslator method extractLong.

private TRequest.Builder extractLong(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 longValue = Long.parseLong(stringValue);
        var subField = subFieldMapper.apply(request);
        subField.setField(targetField, longValue);
        return request;
    } catch (NumberFormatException e) {
        // Invalid values should not make it past the router matcher
        throw new EUnexpected();
    }
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 12 with EUnexpected

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

the class RestApiTranslator method extractEnum.

private TRequest.Builder extractEnum(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);
    var enumType = targetField.getEnumType();
    var enumValue = enumType.findValueByName(stringValue.toUpperCase());
    // This should be checked by RestApiRouteMatcher before a request is given to the handler
    if (enumValue == null)
        throw new EUnexpected();
    var subField = subFieldMapper.apply(request);
    subField.setField(targetField, enumValue);
    return request;
}
Also used : EUnexpected(com.accenture.trac.common.exception.EUnexpected)

Example 13 with EUnexpected

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

the class RestApiTranslator method translateRequest.

// -----------------------------------------------------------------------------------------------------------------
// Runtime methods
// -----------------------------------------------------------------------------------------------------------------
@SuppressWarnings("unchecked")
public TRequest translateRequest(String url, Message body) {
    // This should be set up correctly when the API route is created
    if (!this.hasBody)
        throw new EUnexpected();
    var request = blankRequest.newBuilderForType();
    // If the body is a sub field, use the sub field mapper to add it to the request
    if (bodySubFieldMapper != null) {
        var bodySubField = bodySubFieldMapper.apply(request);
        bodySubField.setField(bodyFieldDescriptor, body);
    } else
        // Otherwise the body is the top level request, merge it before applying URL fields
        request.mergeFrom(body);
    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)

Example 14 with EUnexpected

use of com.accenture.trac.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(com.accenture.trac.common.exception.EInputValidation) InputStreamReader(java.io.InputStreamReader) InvalidProtocolBufferException(com.google.protobuf.InvalidProtocolBufferException) EUnexpected(com.accenture.trac.common.exception.EUnexpected) ByteBufInputStream(io.netty.buffer.ByteBufInputStream) IOException(java.io.IOException)

Example 15 with EUnexpected

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

the class GrpcProxyBuilder method initChannel.

@Override
protected void initChannel(Channel channel) {
    log.info("Init gRPC proxy channel");
    var pipeline = channel.pipeline();
    pipeline.addLast("OBJECT_LOGGER", new ObjectLogger());
    var initialSettings = new Http2Settings().maxFrameSize(16 * 1024);
    log.info(initialSettings.toString());
    var http2Codec = Http2FrameCodecBuilder.forClient().frameLogger(new Http2FrameLogger(LogLevel.INFO)).initialSettings(initialSettings).autoAckSettingsFrame(true).autoAckPingFrame(true).build();
    pipeline.addLast(HTTP2_FRAME_CODEC, http2Codec);
    pipeline.addLast(GRPC_PROXY_HANDLER, new GrpcProxy());
    pipeline.addLast(GRPC_WEB_PROXY_HANDLER, new GrpcWebProxy());
    if (sourceHttpVersion == 1) {
        pipeline.addLast(HTTP_1_TO_2_FRAMING, new Http1to2Framing(routeConfig));
        pipeline.addLast(HTTP_1_ROUTER_LINK, routerLink);
    } else if (sourceHttpVersion == 2) {
        throw new RuntimeException("HTTP/2 source connection for REST not implemented yet");
    } else
        throw new EUnexpected();
}
Also used : Http1to2Framing(com.accenture.trac.gateway.proxy.http.Http1to2Framing) 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