@Test(expected = RemoteApiException.class) public void testPostMessageWithIOException() throws RemoteApiException, IOException { Map<String, String> headerParams = new HashMap<>(); headerParams.put("sessionToken", MOCK_SESSION); headerParams.put("keyManagerToken", MOCK_KM_SESSION); Message message = mockMessage(); JsonNode node = JsonUtils.readTree(getClass().getClassLoader().getResourceAsStream(FILENAME_ENTITY_JSON)); message.setData(node.toString()); String path = "/v4/stream/" + MOCK_STREAM_ID + "/message/create"; doReturn(MOCK_STREAM_ID).when(httpClient).escapeString(MOCK_STREAM_ID); doThrow(IOException.class).when(httpClient) .doPost(eq(path), eq(headerParams), anyMap(), any(MultiPart.class), eq(Message.class)); apiClient.postMessage(MOCK_SESSION, MOCK_KM_SESSION, MOCK_STREAM_ID, message); }
@Override public Message postMessage(String sessionToken, String kmToken, String streamId, Message message) throws RemoteApiException { validateParams(sessionToken, kmToken, streamId, message); String path = "/v4/stream/" + apiClient.escapeString(streamId) + "/message/create"; Map<String, String> headerParams = new HashMap<>(); headerParams.put(SESSION_TOKEN_HEADER_PARAM, sessionToken); headerParams.put(KM_TOKEN_HEADER_PARAM, kmToken); try (FormDataMultiPart multiPart = new FormDataMultiPart()) { multiPart.field(MESSAGE_BODY, message.getMessage(), TEXT_PLAIN_TYPE); String entityJSON = message.getData(); if (StringUtils.isNotEmpty(entityJSON)) { multiPart.field(DATA_BODY, entityJSON, TEXT_PLAIN_TYPE); } return apiClient.doPost(path, headerParams, Collections.<String, String>emptyMap(), multiPart, Message.class); } catch (IOException e) { String errorMessage = logMessage.getMessage(POST_FAILURE, streamId, e.getMessage()); String solution = logMessage.getMessage(POST_FAILURE_SOLUTION); throw new RemoteApiException(500, errorMessage, e, solution); } }
V4MessageApiClient extends BaseMessageApiClient { @Override public Message postMessage(String sessionToken, String kmToken, String streamId, Message message) throws RemoteApiException { validateParams(sessionToken, kmToken, streamId, message); String path = "/v4/stream/" + apiClient.escapeString(streamId) + "/message/create"; Map<String, String> headerParams = new HashMap<>(); headerParams.put(SESSION_TOKEN_HEADER_PARAM, sessionToken); headerParams.put(KM_TOKEN_HEADER_PARAM, kmToken); try (FormDataMultiPart multiPart = new FormDataMultiPart()) { multiPart.field(MESSAGE_BODY, message.getMessage(), TEXT_PLAIN_TYPE); String entityJSON = message.getData(); if (StringUtils.isNotEmpty(entityJSON)) { multiPart.field(DATA_BODY, entityJSON, TEXT_PLAIN_TYPE); } return apiClient.doPost(path, headerParams, Collections.<String, String>emptyMap(), multiPart, Message.class); } catch (IOException e) { String errorMessage = logMessage.getMessage(POST_FAILURE, streamId, e.getMessage()); String solution = logMessage.getMessage(POST_FAILURE_SOLUTION); throw new RemoteApiException(500, errorMessage, e, solution); } } }
V4MessageApiClient extends BaseMessageApiClient { @Override public Message postMessage(String sessionToken, String kmToken, String streamId, Message message) throws RemoteApiException { validateParams(sessionToken, kmToken, streamId, message); String path = "/v4/stream/" + apiClient.escapeString(streamId) + "/message/create"; Map<String, String> headerParams = new HashMap<>(); headerParams.put(SESSION_TOKEN_HEADER_PARAM, sessionToken); headerParams.put(KM_TOKEN_HEADER_PARAM, kmToken); try (FormDataMultiPart multiPart = new FormDataMultiPart()) { multiPart.field(MESSAGE_BODY, message.getMessage(), TEXT_PLAIN_TYPE); String entityJSON = message.getData(); if (StringUtils.isNotEmpty(entityJSON)) { multiPart.field(DATA_BODY, entityJSON, TEXT_PLAIN_TYPE); } return apiClient.doPost(path, headerParams, Collections.<String, String>emptyMap(), multiPart, Message.class); } catch (IOException e) { String errorMessage = logMessage.getMessage(POST_FAILURE, streamId, e.getMessage()); String solution = logMessage.getMessage(POST_FAILURE_SOLUTION); throw new RemoteApiException(500, errorMessage, e, solution); } } V4MessageApiClient(HttpApiClient apiClient, LogMessageSource logMessage); }
V4MessageApiClient extends BaseMessageApiClient { @Override public Message postMessage(String sessionToken, String kmToken, String streamId, Message message) throws RemoteApiException { validateParams(sessionToken, kmToken, streamId, message); String path = "/v4/stream/" + apiClient.escapeString(streamId) + "/message/create"; Map<String, String> headerParams = new HashMap<>(); headerParams.put(SESSION_TOKEN_HEADER_PARAM, sessionToken); headerParams.put(KM_TOKEN_HEADER_PARAM, kmToken); try (FormDataMultiPart multiPart = new FormDataMultiPart()) { multiPart.field(MESSAGE_BODY, message.getMessage(), TEXT_PLAIN_TYPE); String entityJSON = message.getData(); if (StringUtils.isNotEmpty(entityJSON)) { multiPart.field(DATA_BODY, entityJSON, TEXT_PLAIN_TYPE); } return apiClient.doPost(path, headerParams, Collections.<String, String>emptyMap(), multiPart, Message.class); } catch (IOException e) { String errorMessage = logMessage.getMessage(POST_FAILURE, streamId, e.getMessage()); String solution = logMessage.getMessage(POST_FAILURE_SOLUTION); throw new RemoteApiException(500, errorMessage, e, solution); } } V4MessageApiClient(HttpApiClient apiClient, LogMessageSource logMessage); @Override Message postMessage(String sessionToken, String kmToken, String streamId, Message message); }
V4MessageApiClient extends BaseMessageApiClient { @Override public Message postMessage(String sessionToken, String kmToken, String streamId, Message message) throws RemoteApiException { validateParams(sessionToken, kmToken, streamId, message); String path = "/v4/stream/" + apiClient.escapeString(streamId) + "/message/create"; Map<String, String> headerParams = new HashMap<>(); headerParams.put(SESSION_TOKEN_HEADER_PARAM, sessionToken); headerParams.put(KM_TOKEN_HEADER_PARAM, kmToken); try (FormDataMultiPart multiPart = new FormDataMultiPart()) { multiPart.field(MESSAGE_BODY, message.getMessage(), TEXT_PLAIN_TYPE); String entityJSON = message.getData(); if (StringUtils.isNotEmpty(entityJSON)) { multiPart.field(DATA_BODY, entityJSON, TEXT_PLAIN_TYPE); } return apiClient.doPost(path, headerParams, Collections.<String, String>emptyMap(), multiPart, Message.class); } catch (IOException e) { String errorMessage = logMessage.getMessage(POST_FAILURE, streamId, e.getMessage()); String solution = logMessage.getMessage(POST_FAILURE_SOLUTION); throw new RemoteApiException(500, errorMessage, e, solution); } } V4MessageApiClient(HttpApiClient apiClient, LogMessageSource logMessage); @Override Message postMessage(String sessionToken, String kmToken, String streamId, Message message);  }