Search in sources :

Example 11 with Serializer

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"));
    }
}
Also used : AtomicInteger(java.util.concurrent.atomic.AtomicInteger) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) ByteArrayOutputStream(java.io.ByteArrayOutputStream) OutputStream(java.io.OutputStream) UncheckedIOException(java.io.UncheckedIOException) IOException(java.io.IOException) UncheckedIOException(java.io.UncheckedIOException) CompactFields(net.morimekta.test.providence.core.CompactFields) BinarySerializer(net.morimekta.providence.serializer.BinarySerializer) Serializer(net.morimekta.providence.serializer.Serializer) JsonSerializer(net.morimekta.providence.serializer.JsonSerializer) PrettySerializer(net.morimekta.providence.serializer.PrettySerializer) Test(org.junit.Test)

Example 12 with Serializer

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);
        }
    }
}
Also used : PProcessor(net.morimekta.providence.PProcessor) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) AtomicReference(java.util.concurrent.atomic.AtomicReference) MessageReader(net.morimekta.providence.mio.MessageReader) IOMessageReader(net.morimekta.providence.mio.IOMessageReader) ByteArrayOutputStream(java.io.ByteArrayOutputStream) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) IOException(java.io.IOException) EOFException(java.io.EOFException) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) PServiceCall(net.morimekta.providence.PServiceCall) EOFException(java.io.EOFException) MediaType(com.google.common.net.MediaType) MessageWriter(net.morimekta.providence.mio.MessageWriter) IOMessageWriter(net.morimekta.providence.mio.IOMessageWriter) Serializer(net.morimekta.providence.serializer.Serializer)

Example 13 with Serializer

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);
}
Also used : SocketClientHandler(net.morimekta.providence.thrift.client.SocketClientHandler) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) NonblockingSocketClientHandler(net.morimekta.providence.thrift.client.NonblockingSocketClientHandler) HashMap(java.util.HashMap) HttpRequestFactory(com.google.api.client.http.HttpRequestFactory) InetSocketAddress(java.net.InetSocketAddress) GenericUrl(com.google.api.client.http.GenericUrl) URI(java.net.URI) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) HttpTransport(com.google.api.client.http.HttpTransport) SetHeadersInitializer(net.morimekta.providence.tools.rpc.utils.SetHeadersInitializer) NetHttpTransport(com.google.api.client.http.javanet.NetHttpTransport) ArgumentException(net.morimekta.console.args.ArgumentException) ThriftSerializerProvider(net.morimekta.providence.thrift.ThriftSerializerProvider) SerializerProvider(net.morimekta.providence.serializer.SerializerProvider) HttpClientHandler(net.morimekta.providence.client.HttpClientHandler) ThriftSerializerProvider(net.morimekta.providence.thrift.ThriftSerializerProvider) Serializer(net.morimekta.providence.serializer.Serializer)

Aggregations

Serializer (net.morimekta.providence.serializer.Serializer)13 IOException (java.io.IOException)7 BinarySerializer (net.morimekta.providence.serializer.BinarySerializer)6 ByteArrayOutputStream (java.io.ByteArrayOutputStream)5 Test (org.junit.Test)5 UncheckedIOException (java.io.UncheckedIOException)4 InetSocketAddress (java.net.InetSocketAddress)4 JsonSerializer (net.morimekta.providence.serializer.JsonSerializer)4 PrettySerializer (net.morimekta.providence.serializer.PrettySerializer)4 BufferedInputStream (java.io.BufferedInputStream)3 InputStream (java.io.InputStream)3 ConnectException (java.net.ConnectException)3 GenericUrl (com.google.api.client.http.GenericUrl)2 MediaType (com.google.common.net.MediaType)2 ByteArrayInputStream (java.io.ByteArrayInputStream)2 OutputStream (java.io.OutputStream)2 AtomicInteger (java.util.concurrent.atomic.AtomicInteger)2 AtomicReference (java.util.concurrent.atomic.AtomicReference)2 Nonnull (javax.annotation.Nonnull)2 ServiceCallInstrumentation (net.morimekta.providence.util.ServiceCallInstrumentation)2