use of io.joynr.messaging.http.operation.HttpPost 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) {
}
}
}
}
Aggregations