@Test public void testBadRequest() throws IOException { HttpClientHandler handler = new HttpClientHandler(this::endpoint); try { handler.handleCall(new PServiceCall<>("foo", PServiceCallType.EXCEPTION, 1, new PApplicationException("", PApplicationExceptionType.INTERNAL_ERROR)), TestService.kDescriptor); fail("no exception"); } catch (PApplicationException e) { assertThat(e.getMessage(), is("Request with invalid call type: EXCEPTION")); assertThat(e.getId(), is(PApplicationExceptionType.INVALID_MESSAGE_TYPE)); } }
@Override public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException { if (call.getType() == PServiceCallType.EXCEPTION || call.getType() == PServiceCallType.REPLY) { throw new PApplicationException("Request with invalid call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } long startTime = System.nanoTime(); PServiceCall<Response, ResponseField> reply = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); requestSerializer.serialize(baos, call); ByteArrayContent content = new ByteArrayContent(requestSerializer.mediaType(), baos.toByteArray()); @Nonnull GenericUrl url = urlSupplier.get(); try { HttpRequest request = factory.buildPostRequest(url, content); request.getHeaders() .setAccept(requestSerializer.mediaType()); HttpResponse response = request.execute(); try { if (call.getType() == PServiceCallType.CALL) { Serializer responseSerializer = requestSerializer; if (response.getContentType() != null) { try { MediaType mediaType = MediaType.parse(response.getContentType()); responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters() .toString()); } catch (IllegalArgumentException e) { throw new PApplicationException("Unknown content-type in response: " + response.getContentType(), PApplicationExceptionType.INVALID_PROTOCOL).initCause(e); } } reply = responseSerializer.deserialize(response.getContent(), service); if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) { throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } if (reply.getSequence() != call.getSequence()) { throw new PApplicationException( "Reply sequence out of order: call = " + call.getSequence() + ", reply = " + reply.getSequence(), PApplicationExceptionType.BAD_SEQUENCE_ID); } } long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onComplete(duration, call, reply); } catch (Exception ignore) { } return reply; } finally { response.ignore(); } } catch (HttpHostConnectException e) { throw e; } catch (ConnectException e) { throw new HttpHostConnectException(e, new HttpHost(url.getHost(), url.getPort(), url.getScheme())); } } catch (IOException | RuntimeException e) { long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onTransportException(e, duration, call, reply); } catch (Throwable ie) { e.addSuppressed(ie); } throw e; } }
HttpClientHandler implements PServiceCallHandler { @Override public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException { if (call.getType() == PServiceCallType.EXCEPTION || call.getType() == PServiceCallType.REPLY) { throw new PApplicationException("Request with invalid call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } long startTime = System.nanoTime(); PServiceCall<Response, ResponseField> reply = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); requestSerializer.serialize(baos, call); ByteArrayContent content = new ByteArrayContent(requestSerializer.mediaType(), baos.toByteArray()); @Nonnull GenericUrl url = urlSupplier.get(); try { HttpRequest request = factory.buildPostRequest(url, content); request.getHeaders() .setAccept(requestSerializer.mediaType()); HttpResponse response = request.execute(); try { if (call.getType() == PServiceCallType.CALL) { Serializer responseSerializer = requestSerializer; if (response.getContentType() != null) { try { MediaType mediaType = MediaType.parse(response.getContentType()); responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters() .toString()); } catch (IllegalArgumentException e) { throw new PApplicationException("Unknown content-type in response: " + response.getContentType(), PApplicationExceptionType.INVALID_PROTOCOL).initCause(e); } } reply = responseSerializer.deserialize(response.getContent(), service); if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) { throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } if (reply.getSequence() != call.getSequence()) { throw new PApplicationException( "Reply sequence out of order: call = " + call.getSequence() + ", reply = " + reply.getSequence(), PApplicationExceptionType.BAD_SEQUENCE_ID); } } long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onComplete(duration, call, reply); } catch (Exception ignore) { } return reply; } finally { response.ignore(); } } catch (HttpHostConnectException e) { throw e; } catch (ConnectException e) { throw new HttpHostConnectException(e, new HttpHost(url.getHost(), url.getPort(), url.getScheme())); } } catch (IOException | RuntimeException e) { long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onTransportException(e, duration, call, reply); } catch (Throwable ie) { e.addSuppressed(ie); } throw e; } } }
HttpClientHandler implements PServiceCallHandler { @Override public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException { if (call.getType() == PServiceCallType.EXCEPTION || call.getType() == PServiceCallType.REPLY) { throw new PApplicationException("Request with invalid call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } long startTime = System.nanoTime(); PServiceCall<Response, ResponseField> reply = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); requestSerializer.serialize(baos, call); ByteArrayContent content = new ByteArrayContent(requestSerializer.mediaType(), baos.toByteArray()); @Nonnull GenericUrl url = urlSupplier.get(); try { HttpRequest request = factory.buildPostRequest(url, content); request.getHeaders() .setAccept(requestSerializer.mediaType()); HttpResponse response = request.execute(); try { if (call.getType() == PServiceCallType.CALL) { Serializer responseSerializer = requestSerializer; if (response.getContentType() != null) { try { MediaType mediaType = MediaType.parse(response.getContentType()); responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters() .toString()); } catch (IllegalArgumentException e) { throw new PApplicationException("Unknown content-type in response: " + response.getContentType(), PApplicationExceptionType.INVALID_PROTOCOL).initCause(e); } } reply = responseSerializer.deserialize(response.getContent(), service); if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) { throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } if (reply.getSequence() != call.getSequence()) { throw new PApplicationException( "Reply sequence out of order: call = " + call.getSequence() + ", reply = " + reply.getSequence(), PApplicationExceptionType.BAD_SEQUENCE_ID); } } long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onComplete(duration, call, reply); } catch (Exception ignore) { } return reply; } finally { response.ignore(); } } catch (HttpHostConnectException e) { throw e; } catch (ConnectException e) { throw new HttpHostConnectException(e, new HttpHost(url.getHost(), url.getPort(), url.getScheme())); } } catch (IOException | RuntimeException e) { long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onTransportException(e, duration, call, reply); } catch (Throwable ie) { e.addSuppressed(ie); } throw e; } } HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory,
                             @Nonnull SerializerProvider serializerProvider);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory,
                             @Nonnull SerializerProvider serializerProvider,
                             @Nonnull ServiceCallInstrumentation instrumentation); }
HttpClientHandler implements PServiceCallHandler { @Override public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException { if (call.getType() == PServiceCallType.EXCEPTION || call.getType() == PServiceCallType.REPLY) { throw new PApplicationException("Request with invalid call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } long startTime = System.nanoTime(); PServiceCall<Response, ResponseField> reply = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); requestSerializer.serialize(baos, call); ByteArrayContent content = new ByteArrayContent(requestSerializer.mediaType(), baos.toByteArray()); @Nonnull GenericUrl url = urlSupplier.get(); try { HttpRequest request = factory.buildPostRequest(url, content); request.getHeaders() .setAccept(requestSerializer.mediaType()); HttpResponse response = request.execute(); try { if (call.getType() == PServiceCallType.CALL) { Serializer responseSerializer = requestSerializer; if (response.getContentType() != null) { try { MediaType mediaType = MediaType.parse(response.getContentType()); responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters() .toString()); } catch (IllegalArgumentException e) { throw new PApplicationException("Unknown content-type in response: " + response.getContentType(), PApplicationExceptionType.INVALID_PROTOCOL).initCause(e); } } reply = responseSerializer.deserialize(response.getContent(), service); if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) { throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } if (reply.getSequence() != call.getSequence()) { throw new PApplicationException( "Reply sequence out of order: call = " + call.getSequence() + ", reply = " + reply.getSequence(), PApplicationExceptionType.BAD_SEQUENCE_ID); } } long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onComplete(duration, call, reply); } catch (Exception ignore) { } return reply; } finally { response.ignore(); } } catch (HttpHostConnectException e) { throw e; } catch (ConnectException e) { throw new HttpHostConnectException(e, new HttpHost(url.getHost(), url.getPort(), url.getScheme())); } } catch (IOException | RuntimeException e) { long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onTransportException(e, duration, call, reply); } catch (Throwable ie) { e.addSuppressed(ie); } throw e; } } HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory,
                             @Nonnull SerializerProvider serializerProvider);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory,
                             @Nonnull SerializerProvider serializerProvider,
                             @Nonnull ServiceCallInstrumentation instrumentation); @Override PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call,
                                                     PService service); }
HttpClientHandler implements PServiceCallHandler { @Override public <Request extends PMessage<Request, RequestField>, Response extends PMessage<Response, ResponseField>, RequestField extends PField, ResponseField extends PField> PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call, PService service) throws IOException { if (call.getType() == PServiceCallType.EXCEPTION || call.getType() == PServiceCallType.REPLY) { throw new PApplicationException("Request with invalid call type: " + call.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } long startTime = System.nanoTime(); PServiceCall<Response, ResponseField> reply = null; try { ByteArrayOutputStream baos = new ByteArrayOutputStream(); requestSerializer.serialize(baos, call); ByteArrayContent content = new ByteArrayContent(requestSerializer.mediaType(), baos.toByteArray()); @Nonnull GenericUrl url = urlSupplier.get(); try { HttpRequest request = factory.buildPostRequest(url, content); request.getHeaders() .setAccept(requestSerializer.mediaType()); HttpResponse response = request.execute(); try { if (call.getType() == PServiceCallType.CALL) { Serializer responseSerializer = requestSerializer; if (response.getContentType() != null) { try { MediaType mediaType = MediaType.parse(response.getContentType()); responseSerializer = serializerProvider.getSerializer(mediaType.withoutParameters() .toString()); } catch (IllegalArgumentException e) { throw new PApplicationException("Unknown content-type in response: " + response.getContentType(), PApplicationExceptionType.INVALID_PROTOCOL).initCause(e); } } reply = responseSerializer.deserialize(response.getContent(), service); if (reply.getType() == PServiceCallType.CALL || reply.getType() == PServiceCallType.ONEWAY) { throw new PApplicationException("Reply with invalid call type: " + reply.getType(), PApplicationExceptionType.INVALID_MESSAGE_TYPE); } if (reply.getSequence() != call.getSequence()) { throw new PApplicationException( "Reply sequence out of order: call = " + call.getSequence() + ", reply = " + reply.getSequence(), PApplicationExceptionType.BAD_SEQUENCE_ID); } } long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onComplete(duration, call, reply); } catch (Exception ignore) { } return reply; } finally { response.ignore(); } } catch (HttpHostConnectException e) { throw e; } catch (ConnectException e) { throw new HttpHostConnectException(e, new HttpHost(url.getHost(), url.getPort(), url.getScheme())); } } catch (IOException | RuntimeException e) { long endTime = System.nanoTime(); double duration = ((double) (endTime - startTime)) / NS_IN_MILLIS; try { instrumentation.onTransportException(e, duration, call, reply); } catch (Throwable ie) { e.addSuppressed(ie); } throw e; } } HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory,
                             @Nonnull SerializerProvider serializerProvider);  HttpClientHandler(@Nonnull Supplier<GenericUrl> urlSupplier,
                             @Nonnull HttpRequestFactory factory,
                             @Nonnull SerializerProvider serializerProvider,
                             @Nonnull ServiceCallInstrumentation instrumentation); @Override PServiceCall<Response, ResponseField> handleCall(PServiceCall<Request, RequestField> call,
                                                     PService service);  }