use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.
the class MessageStreamsTest method testStreamCollector.
@Test
public void testStreamCollector() throws IOException {
Serializer serializer = mock(Serializer.class);
OutputStream out = mock(OutputStream.class);
AtomicInteger i = new AtomicInteger();
Collector<CompactFields, AtomicInteger, Integer> collector = MessageCollectors.toStream(out, serializer);
doReturn(false).when(serializer).binaryProtocol();
doThrow(new IOException("write")).when(out).write(MessageStreams.READABLE_ENTRY_SEP);
i.set(5);
AtomicInteger j = new AtomicInteger(6);
assertThat(collector.combiner().apply(i, j).get(), is(11));
try {
collector.accumulator().accept(i, list.get(0));
fail("no exception");
} catch (UncheckedIOException e) {
assertThat(e.getMessage(), is("write"));
}
}
use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.
the class ProvidenceServlet method doPost.
@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
long startTime = System.nanoTime();
AtomicReference<PServiceCall> callRef = new AtomicReference<>();
AtomicReference<PServiceCall> responseRef = new AtomicReference<>();
PProcessor processor = new WrappedProcessor(processorProvider.processorForRequest(req), (c, r) -> {
callRef.set(c);
responseRef.set(r.handleCall(c));
return responseRef.get();
});
try {
Serializer requestSerializer = serializerProvider.getDefault();
if (req.getContentType() != null) {
try {
MediaType mediaType = MediaType.parse(req.getContentType());
requestSerializer = serializerProvider.getSerializer(mediaType.withoutParameters().toString());
} catch (IllegalArgumentException e) {
resp.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unknown content-type: " + req.getContentType());
LOGGER.warn("Unknown content type in request", e);
return;
}
} else {
LOGGER.debug("Request is missing content type.");
}
Serializer responseSerializer = requestSerializer;
String acceptHeader = req.getHeader("Accept");
if (acceptHeader != null) {
String[] entries = acceptHeader.split("[,]");
for (String entry : entries) {
entry = entry.trim();
if (entry.isEmpty()) {
continue;
}
if ("*/*".equals(entry)) {
// Then responding same as request is good.
break;
}
try {
MediaType mediaType = MediaType.parse(entry);
responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters().toString());
break;
} catch (IllegalArgumentException ignore) {
// Ignore. Bad header input is pretty common.
}
}
}
MessageReader reader = new IOMessageReader(req.getInputStream(), requestSerializer);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
MessageWriter writer = new IOMessageWriter(baos, responseSerializer);
getHandler(processor).process(reader, writer);
resp.setStatus(HttpServletResponse.SC_OK);
if (baos.size() > 0) {
resp.setContentType(responseSerializer.mediaType());
resp.setContentLength(baos.size());
resp.getOutputStream().write(baos.toByteArray());
resp.getOutputStream().flush();
}
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onComplete(duration, callRef.get(), responseRef.get());
} catch (Throwable th) {
LOGGER.error("Exception in service instrumentation", th);
}
} catch (EOFException e) {
// output stream closed before write is complete.
// So we cannot even try to respond.
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onTransportException(e, duration, callRef.get(), responseRef.get());
} catch (Throwable th) {
LOGGER.error("Exception in service instrumentation", th);
}
} catch (Exception e) {
try {
resp.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR, "Internal server error: " + e.getMessage());
} catch (IOException ioEx) {
e.addSuppressed(ioEx);
}
long endTime = System.nanoTime();
double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS;
try {
instrumentation.onTransportException(e, duration, callRef.get(), responseRef.get());
} catch (Throwable th) {
LOGGER.error("Exception in service instrumentation", th);
}
}
}
use of net.morimekta.providence.serializer.Serializer in project providence by morimekta.
the class RPCOptions method getHandler.
public PServiceCallHandler getHandler() {
Serializer serializer = getSerializer(format);
URI uri = URI.create(endpoint);
if (uri.getScheme() == null || uri.getScheme().length() == 0) {
throw new ArgumentException("No protocol on URI: " + endpoint);
}
if (uri.getScheme().startsWith("thrift")) {
if (// Must have host and port.
(uri.getPort() < 1) || (uri.getHost() == null || uri.getHost().length() == 0) || // No path, query or fragment.
(uri.getFragment() != null && uri.getFragment().length() > 0) || (uri.getQuery() != null && uri.getQuery().length() > 0) || (uri.getPath() != null && uri.getPath().length() > 0)) {
throw new ArgumentException("Illegal thrift URI: " + endpoint);
}
InetSocketAddress address = new InetSocketAddress(uri.getHost(), uri.getPort());
switch(uri.getScheme()) {
case "thrift":
return new SocketClientHandler(serializer, address, connect_timeout, read_timeout);
case "thrift+nonblocking":
return new NonblockingSocketClientHandler(serializer, address, connect_timeout, read_timeout);
default:
throw new ArgumentException("Unknown thrift protocol " + uri.getScheme());
}
}
GenericUrl url = new GenericUrl(endpoint);
Map<String, String> hdrs = new HashMap<>();
for (String hdr : headers) {
String[] parts = hdr.split("[:]", 2);
if (parts.length != 2) {
throw new ArgumentException("Invalid headers param: " + hdr);
}
hdrs.put(parts[0].trim(), parts[1].trim());
}
HttpTransport transport = new NetHttpTransport();
HttpRequestFactory factory = transport.createRequestFactory(new SetHeadersInitializer(hdrs, connect_timeout, read_timeout));
SerializerProvider serializerProvider = new ThriftSerializerProvider(serializer.mediaType());
return new HttpClientHandler(() -> url, factory, serializerProvider);
}
Aggregations