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