use of com.quorum.tessera.transaction.publish.NodeOfflineException in project tessera by ConsenSys.
the class RestPayloadPublisher method publishPayload.
@Override
public void publishPayload(EncodedPayload payload, PublicKey recipientKey) {
final NodeInfo remoteNodeInfo = discovery.getRemoteNodeInfo(recipientKey);
final Set<String> supportedApiVersions = remoteNodeInfo.supportedApiVersions();
final EncodedPayloadCodec preferredCodec = EncodedPayloadCodec.getPreferredCodec(supportedApiVersions);
final PayloadEncoder payloadEncoder = PayloadEncoder.create(preferredCodec);
if (PrivacyMode.STANDARD_PRIVATE != payload.getPrivacyMode() && !supportedApiVersions.contains(EnhancedPrivacyVersion.API_VERSION_2)) {
throw new EnhancedPrivacyNotSupportedException("Transactions with enhanced privacy is not currently supported on recipient " + recipientKey.encodeToBase64());
}
if (PrivacyMode.MANDATORY_RECIPIENTS == payload.getPrivacyMode() && !supportedApiVersions.contains(MandatoryRecipientsVersion.API_VERSION_4)) {
throw new MandatoryRecipientsNotSupportedException("Transactions with mandatory recipients are not currently supported on recipient " + recipientKey.encodeToBase64());
}
final String targetUrl = remoteNodeInfo.getUrl();
LOGGER.info("Publishing message to {}", targetUrl);
final byte[] encoded = payloadEncoder.encode(payload);
try (Response response = client.target(targetUrl).path("/push").request().post(Entity.entity(encoded, MediaType.APPLICATION_OCTET_STREAM_TYPE))) {
if (Response.Status.OK.getStatusCode() != response.getStatus() && Response.Status.CREATED.getStatusCode() != response.getStatus()) {
throw new PublishPayloadException("Unable to push payload to recipient url " + targetUrl);
}
LOGGER.info("Published to {}", targetUrl);
} catch (ProcessingException ex) {
LOGGER.debug("", ex);
throw new NodeOfflineException(URI.create(targetUrl));
}
}
use of com.quorum.tessera.transaction.publish.NodeOfflineException in project tessera by ConsenSys.
the class RestPrivacyGroupPublisher method publishPrivacyGroup.
@Override
public void publishPrivacyGroup(byte[] data, PublicKey recipientKey) {
final NodeInfo remoteNodeInfo = discovery.getRemoteNodeInfo(recipientKey);
if (!remoteNodeInfo.supportedApiVersions().contains(PrivacyGroupVersion.API_VERSION_3)) {
throw new PrivacyGroupNotSupportedException("Transactions with privacy group is not currently supported on recipient " + recipientKey.encodeToBase64());
}
final String targetUrl = remoteNodeInfo.getUrl();
LOGGER.info("Publishing privacy group to {}", targetUrl);
try (Response response = restClient.target(targetUrl).path("/pushPrivacyGroup").request().post(Entity.entity(data, MediaType.APPLICATION_OCTET_STREAM_TYPE))) {
if (Response.Status.OK.getStatusCode() != response.getStatus()) {
throw new PrivacyGroupPublishException("Unable to push privacy group to recipient url " + targetUrl);
}
LOGGER.info("Published privacy group to {}", targetUrl);
} catch (ProcessingException ex) {
LOGGER.debug("", ex);
throw new NodeOfflineException(URI.create(targetUrl));
}
}
use of com.quorum.tessera.transaction.publish.NodeOfflineException in project tessera by ConsenSys.
the class RestPayloadPublisherTest method handleConnectionError.
@Test
public void handleConnectionError() {
final String targetUri = "http://jimmywhite.com";
final PublicKey recipientKey = mock(PublicKey.class);
Recipient recipient = mock(Recipient.class);
when(recipient.getKey()).thenReturn(recipientKey);
when(recipient.getUrl()).thenReturn(targetUri);
NodeInfo nodeInfo = mock(NodeInfo.class);
when(nodeInfo.getRecipients()).thenReturn(Set.of(recipient));
when(nodeInfo.getUrl()).thenReturn(targetUri);
when(discovery.getRemoteNodeInfo(recipientKey)).thenReturn(nodeInfo);
Client client = mock(Client.class);
when(client.target(targetUri)).thenThrow(ProcessingException.class);
final EncodedPayload payload = mock(EncodedPayload.class);
when(payload.getPrivacyMode()).thenReturn(PrivacyMode.STANDARD_PRIVATE);
when(payloadEncoder.encode(payload)).thenReturn("SomeData".getBytes());
RestPayloadPublisher restPayloadPublisher = new RestPayloadPublisher(client, discovery);
try {
restPayloadPublisher.publishPayload(payload, recipientKey);
failBecauseExceptionWasNotThrown(NodeOfflineException.class);
} catch (NodeOfflineException ex) {
assertThat(ex).hasMessageContaining(targetUri);
verify(client).target(targetUri);
verify(discovery).getRemoteNodeInfo(eq(recipientKey));
verify(payloadEncoder).encode(payload);
verify(discovery).getRemoteNodeInfo(eq(recipientKey));
payloadEncoderFactoryFunction.verify(() -> PayloadEncoder.create(any(EncodedPayloadCodec.class)));
}
}
use of com.quorum.tessera.transaction.publish.NodeOfflineException in project tessera by ConsenSys.
the class RestPrivacyGroupPublisherTest method testPublishSingleNodeOffline.
@Test
public void testPublishSingleNodeOffline() {
String targetUrl = "https://sometargeturl.com";
PublicKey recipient = PublicKey.from("PUBLIC_KEY".getBytes());
NodeInfo nodeInfo = mock(NodeInfo.class);
when(nodeInfo.supportedApiVersions()).thenReturn(Set.of(PrivacyGroupVersion.API_VERSION_3));
when(nodeInfo.getUrl()).thenReturn(targetUrl);
when(discovery.getRemoteNodeInfo(recipient)).thenReturn(nodeInfo);
Invocation.Builder invocationBuilder = mock(Invocation.Builder.class);
when(invocationBuilder.post(any(Entity.class))).thenThrow(ProcessingException.class);
WebTarget webTarget = mock(WebTarget.class);
when(client.target(targetUrl)).thenReturn(webTarget);
when(webTarget.path(anyString())).thenReturn(webTarget);
when(webTarget.request()).thenReturn(invocationBuilder);
final byte[] data = new byte[5];
try {
publisher.publishPrivacyGroup(data, PublicKey.from("PUBLIC_KEY".getBytes()));
failBecauseExceptionWasNotThrown(NodeOfflineException.class);
} catch (NodeOfflineException ex) {
verify(discovery).getRemoteNodeInfo(recipient);
verify(client).target(targetUrl);
}
}
use of com.quorum.tessera.transaction.publish.NodeOfflineException in project tessera by ConsenSys.
the class NodeOfflineExceptionMapperTest method toResponse.
@Test
public void toResponse() {
URI uri = URI.create("http://ouchthatsgottasmart.com");
final NodeOfflineException exception = new NodeOfflineException(uri);
final Response result = exceptionMapper.toResponse(exception);
verify(discovery).onDisconnect(uri);
assertThat(result.getStatus()).isEqualTo(Response.Status.GONE.getStatusCode());
assertThat(result.getStatusInfo().getReasonPhrase()).isEqualTo("Connection error while communicating with http://ouchthatsgottasmart.com");
}
Aggregations