use of io.joynr.exceptions.JoynrCommunicationException in project joynr by bmwcarit.
the class SerializationTest method serializeReplyWithJoynrCommunicationException.
@Test
public void serializeReplyWithJoynrCommunicationException() throws IOException {
JoynrCommunicationException error = new JoynrCommunicationException("detail message: JoynrCommunicationException");
Reply reply = new Reply(UUID.randomUUID().toString(), error);
String writeValueAsString = objectMapper.writeValueAsString(reply);
System.out.println(writeValueAsString);
Reply receivedReply = objectMapper.readValue(writeValueAsString, Reply.class);
Assert.assertEquals(reply, receivedReply);
}
use of io.joynr.exceptions.JoynrCommunicationException in project joynr by bmwcarit.
the class SerializationTest method serializeReplyWithJoynrCommunicationExceptionWithoutMessage.
@Test
public void serializeReplyWithJoynrCommunicationExceptionWithoutMessage() throws IOException {
JoynrCommunicationException error = new JoynrCommunicationException();
Reply reply = new Reply(UUID.randomUUID().toString(), error);
String writeValueAsString = objectMapper.writeValueAsString(reply);
System.out.println(writeValueAsString);
Reply receivedReply = objectMapper.readValue(writeValueAsString, Reply.class);
Assert.assertEquals(reply, receivedReply);
}
use of io.joynr.exceptions.JoynrCommunicationException in project joynr by bmwcarit.
the class DiscoveryEntryStoreInMemory method add.
/*
* (non-Javadoc)
* @see io.joynr.capabilities.CapabilitiesStore#add(io.joynr.
* capabilities .DiscoveryEntry)
*/
@Override
public synchronized void add(DiscoveryEntry discoveryEntry) {
if (discoveryEntry.getDomain() == null || discoveryEntry.getInterfaceName() == null || discoveryEntry.getParticipantId() == null) {
String message = "discoveryEntry being registered is not complete: " + discoveryEntry;
logger.error(message);
throw new JoynrCommunicationException(message);
}
synchronized (storeLock) {
String discoveryEntryId = domainInterfaceParticipantIdKey(discoveryEntry.getDomain(), discoveryEntry.getInterfaceName(), discoveryEntry.getParticipantId());
DiscoveryEntry entry = capabilityKeyToCapabilityMapping.get(discoveryEntryId);
// check if a DiscoveryEntry with the same Id already exists
if (entry != null) {
remove(discoveryEntry.getParticipantId());
}
// update participantId to capability mapping
capabilityKeyToCapabilityMapping.put(discoveryEntryId, discoveryEntry);
// update time mapping
registeredCapabilitiesTime.put(discoveryEntryId, System.currentTimeMillis());
// update interfaceDomain to capability mapping
String domainInterfaceId = domainInterfaceKey(discoveryEntry.getDomain(), discoveryEntry.getInterfaceName());
// if domainInterfaceId not in the mapping, map it to an empty map,
// otherwise use the mapping that is already there
List<String> newMapping = new ArrayList<String>();
List<String> mapping = interfaceAddressToCapabilityMapping.putIfAbsent(domainInterfaceId, newMapping);
if (mapping == null) {
mapping = newMapping;
}
mapping.add(discoveryEntryId);
// update participantId to capability mapping
String participantId = discoveryEntry.getParticipantId();
participantIdToCapabilityMapping.put(participantId, discoveryEntryId);
}
}
use of io.joynr.exceptions.JoynrCommunicationException in project joynr by bmwcarit.
the class HttpMessageSender method sendMessage.
public void sendMessage(ChannelAddress address, byte[] serializedMessage, SuccessAction successAction, FailureAction failureAction) {
// check if messageReceiver is ready to receive replies otherwise delay request by at least 100 ms
if (!messageReceiver.isReady()) {
long delay_ms = DELAY_RECEIVER_NOT_STARTED_MS;
failureAction.execute(new JoynrDelayMessageException(delay_ms, RECEIVER_NOT_STARTED_REASON));
}
String sendUrl = urlResolver.getSendUrl(address.getMessagingEndpointUrl());
logger.trace("SENDING: channelId: {} message: {}", sendUrl, serializedMessage);
HttpContext context = new BasicHttpContext();
// execute http command to send
CloseableHttpResponse response = null;
try {
HttpPost httpPost = httpRequestFactory.createHttpPost(URI.create(sendUrl));
httpPost.addHeader(new BasicHeader(httpConstants.getHEADER_CONTENT_TYPE(), httpConstants.getAPPLICATION_JSON() + ";charset=UTF-8"));
httpPost.setEntity(new ByteArrayEntity(serializedMessage));
// Clone the default config
Builder requestConfigBuilder = RequestConfig.copy(defaultRequestConfig);
requestConfigBuilder.setConnectionRequestTimeout(httpConstants.getSEND_MESSAGE_REQUEST_TIMEOUT());
httpPost.setConfig(requestConfigBuilder.build());
response = httpclient.execute(httpPost, context);
StatusLine statusLine = response.getStatusLine();
int statusCode = statusLine.getStatusCode();
String statusText = statusLine.getReasonPhrase();
switch(statusCode) {
case HttpURLConnection.HTTP_OK:
case HttpURLConnection.HTTP_CREATED:
logger.trace("SENT: channelId {} message: {}", sendUrl, serializedMessage);
successAction.execute();
break;
case HttpURLConnection.HTTP_BAD_REQUEST:
HttpEntity entity = response.getEntity();
if (entity == null) {
failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender. No further reason found in message body"));
return;
}
String body = EntityUtils.toString(entity, "UTF-8");
JoynrMessagingError error = objectMapper.readValue(body, JoynrMessagingError.class);
JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
logger.error(error.toString());
switch(joynrMessagingErrorCode) {
case JOYNRMESSAGINGERROR_CHANNELNOTFOUND:
failureAction.execute(new JoynrChannelMissingException("Channel does not exist. Status: " + statusCode + " error: " + error.getCode() + "reason:" + error.getReason()));
break;
default:
failureAction.execute(new JoynrCommunicationException("Error in HttpMessageSender: " + statusText + body + " error: " + error.getCode() + "reason:" + error.getReason()));
break;
}
break;
default:
failureAction.execute(new JoynrCommunicationException("Unknown Error in HttpMessageSender: " + statusText + " statusCode: " + statusCode));
break;
}
} catch (JoynrShutdownException e) {
failureAction.execute(new JoynrMessageNotSentException("Message not sent to: " + address, e));
} catch (Exception e) {
// An exception occured - this could still be a communication error (e.g Connection refused)
failureAction.execute(new JoynrCommunicationException(e.getClass().getName() + "Exception while communicating. error: " + e.getMessage()));
} finally {
if (response != null) {
try {
response.close();
} catch (IOException e) {
}
}
}
}
use of io.joynr.exceptions.JoynrCommunicationException in project joynr by bmwcarit.
the class LongPollChannel method longPoll.
private void longPoll() {
String responseBody = null;
if (shutdown) {
return;
}
final String asciiString = httpget.getURI().toASCIIString();
try {
responseBody = httpclient.execute(httpget, new ResponseHandler<String>() {
@Override
public String handleResponse(HttpResponse response) throws IOException {
HttpEntity entity = response.getEntity();
String body = entity == null ? null : EntityUtils.toString(entity, "UTF-8");
statusCode = response.getStatusLine().getStatusCode();
statusText = response.getStatusLine().getReasonPhrase();
logger.debug("Long poll returned: {} reason: url {}", statusCode, asciiString);
return body;
}
});
} catch (IllegalStateException e) {
logger.error("IllegalStateException in long poll: {} message: {}", asciiString, e.getMessage());
throw new JoynrShutdownException(e.getMessage(), e);
} catch (Exception e) {
logger.debug("Exception in long poll: " + asciiString, e);
delay();
return;
}
switch(statusCode) {
case HttpStatus.SC_OK:
notifyDispatcher(responseBody);
break;
case HttpStatus.SC_NOT_FOUND:
logger.error(responseBody);
delay();
throw new JoynrChannelMissingException("Not found");
case HttpStatus.SC_BAD_REQUEST:
if (responseBody != null) {
try {
JoynrMessagingError error = objectMapper.readValue(responseBody, JoynrMessagingError.class);
JoynrMessagingErrorCode joynrMessagingErrorCode = JoynrMessagingErrorCode.getJoynrMessagingErrorCode(error.getCode());
logger.error(error.toString());
switch(joynrMessagingErrorCode) {
case JOYNRMESSAGINGERROR_CHANNELNOTFOUND:
throw new JoynrChannelMissingException(error.getReason());
default:
throw new JoynrCommunicationException(error.getReason());
}
} catch (IOException e) {
throw new JoynrCommunicationException(statusText, e);
}
}
default:
delay();
break;
}
}
Aggregations