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