use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.
the class PluginManager method createService.
@Override
public <T> T createService(Class<T> serviceClass, String protocol, Properties properties) {
var pluginKey = new PluginKey(serviceClass, protocol);
if (!PluginServiceInfo.SERVICE_NAMES.containsKey(serviceClass.getName()))
throw new EUnexpected();
if (!plugins.containsKey(pluginKey)) {
var rawTypeName = PluginServiceInfo.SERVICE_NAMES.get(serviceClass.getName());
var message = String.format("Plugin not available for %s protocol: [%s]", prettyTypeName(rawTypeName, false), protocol);
log.error(message);
throw new EPluginNotAvailable(message);
}
var plugin = plugins.get(pluginKey);
return plugin.createService(serviceClass, protocol, properties);
}
use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.
the class Http1Proxy method proxyRequest.
// No-op proxy translation to get things working
private HttpRequest proxyRequest(HttpRequest sourceRequest) {
var sourceUri = URI.create(sourceRequest.uri());
var sourcePath = sourceUri.getPath();
// Match should already be checked before a request is sent to this handler
if (!sourcePath.startsWith(routeConfig.getMatch().getPath()))
throw new EUnexpected();
// var targetPath = sourceMatch.replaceFirst(routeConfig.getTarget().getPath());
var targetPath = sourcePath.replaceFirst(routeConfig.getMatch().getPath(), routeConfig.getTarget().getPath());
var targetHeaders = new DefaultHttpHeaders();
targetHeaders.add(sourceRequest.headers());
targetHeaders.remove(HttpHeaderNames.HOST);
targetHeaders.add(HttpHeaderNames.HOST, routeConfig.getTarget().getHost());
if (sourceRequest instanceof FullHttpRequest) {
var fullRequest = (FullHttpRequest) sourceRequest;
return new DefaultFullHttpRequest(sourceRequest.protocolVersion(), sourceRequest.method(), targetPath, fullRequest.content(), targetHeaders, fullRequest.trailingHeaders());
} else {
return new DefaultHttpRequest(sourceRequest.protocolVersion(), sourceRequest.method(), targetPath, targetHeaders);
}
}
use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.
the class Http1to2Framing method translateRequestFrames.
private List<Http2Frame> translateRequestFrames(Object http1) {
var seqId = inboundSeqId;
var stream = streams.get(seqId);
if (stream == null)
throw new EUnexpected();
var frames = new ArrayList<Http2Frame>();
if (http1 instanceof HttpRequest) {
var h1Request = (HttpRequest) http1;
var h1Headers = h1Request.headers();
var h2Headers = new DefaultHttp2Headers().method(h1Request.method().name()).scheme(routeConfig.getTarget().getScheme()).path(h1Request.uri());
if (h1Headers.contains(HttpHeaderNames.HOST))
h2Headers.authority(h1Headers.get(HttpHeaderNames.HOST));
else
h2Headers.authority(routeConfig.getTarget().getHost());
// Copy across all other HTTP/1 headers that we are not explicitly changing or removing
var filterHeaders = List.of(HttpHeaderNames.HOST.toString(), HttpHeaderNames.CONNECTION.toString(), HttpHeaderNames.CONTENT_LENGTH.toString());
for (var header : h1Headers) if (!filterHeaders.contains(header.getKey().toLowerCase()))
h2Headers.add(header.getKey().toLowerCase(), header.getValue());
var frame = new DefaultHttp2HeadersFrame(h2Headers, false).stream(stream);
frames.add(frame);
}
if (http1 instanceof HttpContent) {
var h1Content = (HttpContent) http1;
var contentBuf = h1Content.content();
var MAX_DATA_SIZE = 16 * 1024;
contentBuf.retain();
log.info("Size of content: {}", contentBuf.readableBytes());
while (contentBuf.readableBytes() > MAX_DATA_SIZE) {
var slice = contentBuf.readSlice(MAX_DATA_SIZE);
var frame = new DefaultHttp2DataFrame(slice).stream(stream);
frames.add(frame);
}
var endStreamFlag = (http1 instanceof LastHttpContent);
var slice = contentBuf.readSlice(contentBuf.readableBytes());
log.info("Size of slice: {}", slice.readableBytes());
log.info("end of stream: {}", endStreamFlag);
var padding = 256 - (slice.readableBytes() % 256) % 256;
var frame = new DefaultHttp2DataFrame(slice, endStreamFlag, padding).stream(stream);
frames.add(frame);
}
if (frames.isEmpty())
throw new EUnexpected();
return frames;
}
use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.
the class LocalJobCache method createJob.
@Override
public void createJob(String jobKey, JobState jobState, Ticket ticket) {
var operationTime = Instant.now();
var cacheEntry = cache.computeIfPresent(jobKey, (key, priorEntry) -> {
if (priorEntry.ticket != ticket)
return priorEntry;
// Same job created twice, this is an unexpected logic error
if (priorEntry.jobState != null)
throw new EUnexpected();
var newEntry = priorEntry.clone();
newEntry.jobState = jobState;
newEntry.lastActivity = operationTime;
newEntry.revision = priorEntry.revision + 1;
return newEntry;
});
if (cacheEntry == null || cacheEntry.ticket != ticket)
// TODO: Error
throw new ECacheTicket("");
}
use of com.accenture.trac.common.exception.EUnexpected in project tracdap by finos.
the class RestApiProxyBuilder method initChannel.
@Override
protected void initChannel(Channel channel) throws Exception {
log.info("Init REST proxy channel");
var pipeline = channel.pipeline();
// HTTP/2 Codec, required for channels using the HTTP frame objects
var initialSettings = new Http2Settings().maxFrameSize(16 * 1024);
var http2Codec = Http2FrameCodecBuilder.forClient().frameLogger(new Http2FrameLogger(LogLevel.INFO)).initialSettings(initialSettings).autoAckSettingsFrame(true).autoAckPingFrame(true).build();
pipeline.addLast(http2Codec);
// REST proxy
// TODO: Build this after reading service config and pass it in
var restApiConfig = routeConfig.getRestMethods();
var grpcHost = routeConfig.getConfig().getTarget().getHost();
var grpcPort = (short) routeConfig.getConfig().getTarget().getPort();
var restApiProxy = new RestApiProxy(grpcHost, grpcPort, restApiConfig, executor);
pipeline.addLast(restApiProxy);
if (sourceHttpVersion == 1) {
pipeline.addLast(new Http1to2Framing(routeConfig.getConfig()));
pipeline.addLast(routerLink);
} else if (sourceHttpVersion == 2) {
throw new RuntimeException("HTTP/2 source connection for REST not implemented yet");
} else
throw new EUnexpected();
}
Aggregations