/** * Validates and read the response. * * @param url the URL where the response was received from. * @param statusCode the HTTP status code. * @param response the raw response of fetching the given URL. * @return the response, as {@link String}. * * @throws RobotConnectionException if the response code is not HTTP OK (200). */ public static String validateAndReadResponse(String url, int statusCode, byte[] response) throws RobotConnectionException { if (statusCode != HttpServletResponse.SC_OK) { String msg = "Robot fetch http failure: " + url + ": " + statusCode; throw new RobotConnectionException(msg, statusCode); } // Read the response. return new String(response, Charsets.UTF_8); }
public void testValidateAndReadResponseOnNonOkResponse() throws Exception { String url = "http://foo.appspot.com/_wave/robot/jsonrpc"; int statusCode = 404; try { RobotConnectionUtil.validateAndReadResponse(url, statusCode, (byte[]) null); } catch (RobotConnectionException e) { assertEquals(statusCode, e.getStatusCode()); assertTrue(e.getMessage().contains(url)); } } }
public void testValidateAndReadResponseOnNonOkResponse() throws Exception { String url = "http://foo.appspot.com/_wave/robot/jsonrpc"; int statusCode = 404; try { RobotConnectionUtil.validateAndReadResponse(url, statusCode, (byte[]) null); } catch (RobotConnectionException e) { assertEquals(statusCode, e.getStatusCode()); assertTrue(e.getMessage().contains(url)); } } }
/** * Validates and read the response. * * @param url the URL where the response was received from. * @param statusCode the HTTP status code. * @param response the raw response of fetching the given URL. * @return the response, as {@link String}. * * @throws RobotConnectionException if the response code is not HTTP OK (200). */ public static String validateAndReadResponse(String url, int statusCode, InputStream response) throws RobotConnectionException { try { return validateAndReadResponse(url, statusCode, ByteStreams.toByteArray(response)); } catch (IOException e) { String msg = "Robot fetch http failure: " + url + "."; throw new RobotConnectionException(msg, statusCode); } } }
@Override public String postJson(String url, String body) throws RobotConnectionException { PostMethod method = new PostMethod(url); try { method.setRequestEntity(new StringRequestEntity(body, RobotConnection.JSON_CONTENT_TYPE, Charsets.UTF_8.name())); return fetch(url, method); } catch (IOException e) { String msg = "Robot fetch http failure: " + url + ": " + e; throw new RobotConnectionException(msg, e); } }
/** * Fetches the given URL, given a method ({@code GET} or {@code POST}). * * @param url the URL to be fetched. * @param method the method to fetch the URL, can be {@code GET} or * {@code POST}. * @return the content of the URL. * * @throws RobotConnectionException if there is a problem fetching the URL, * for example, if the response code is not HTTP OK (200). */ private String fetch(String url, HttpMethod method) throws RobotConnectionException { try { int statusCode = httpClient.executeMethod(method); return RobotConnectionUtil.validateAndReadResponse(url, statusCode, method.getResponseBodyAsStream()); } catch (IOException e) { String msg = "Robot fetch http failure: " + url + "."; throw new RobotConnectionException(msg, e); } finally { method.releaseConnection(); } } }
public void testConnectionFailsSafely() throws Exception { when(serializer.serialize(BUNDLE, PROTOCOL_VERSION)).thenReturn(SERIALIZED_BUNDLE); when(connection.postJson(TEST_RPC_ENDPOINT, SERIALIZED_BUNDLE)).thenThrow( new RobotConnectionException("Connection Failed")); List<OperationRequest> operations = connector.sendMessageBundle(BUNDLE, robot, PROTOCOL_VERSION); assertTrue("Expected no operations to be returned", operations.isEmpty()); }
public void testConnectionFailsSafely() throws Exception { when(serializer.serialize(BUNDLE, PROTOCOL_VERSION)).thenReturn(SERIALIZED_BUNDLE); when(connection.postJson(TEST_RPC_ENDPOINT, SERIALIZED_BUNDLE)).thenThrow( new RobotConnectionException("Connection Failed")); List<OperationRequest> operations = connector.sendMessageBundle(BUNDLE, robot, PROTOCOL_VERSION); assertTrue("Expected no operations to be returned", operations.isEmpty()); }