LabradorRetriever { <T> T call() throws IOException { if (shouldMock()) { return this.<T> createMockResultOfCall(); } final long start = System.currentTimeMillis(); int dataLength = -1; try { final URLConnection connection = openConnection(); connection.setRequestProperty("Accept-Language", I18N.getCurrentLocale().getLanguage()); connection.connect(); final CounterInputStream counterInputStream = new CounterInputStream( connection.getInputStream()); final T result; try { @SuppressWarnings("unchecked") final T tmp = (T) read(connection, counterInputStream); result = tmp; } finally { counterInputStream.close(); dataLength = counterInputStream.getDataLength(); } LOG.debug("read on " + url + " : " + result); if (result instanceof RuntimeException) { throw (RuntimeException) result; } else if (result instanceof Error) { throw (Error) result; } else if (result instanceof IOException) { throw (IOException) result; } else if (result instanceof Exception) { throw createIOException((Exception) result); } return result; } catch (final ClassNotFoundException e) { throw createIOException(e); } finally { LOG.info("http call done in " + (System.currentTimeMillis() - start) + " ms with " + dataLength / 1024 + " KB read for " + url); } } LabradorRetriever(URL url);  LabradorRetriever(URL url, Map<String, String> headers); void copyTo(HttpServletRequest httpRequest, HttpServletResponse httpResponse); void post(ByteArrayOutputStream payload);  }
@Test public void testCall() throws IOException { Utils.setProperty(Parameters.PARAMETER_SYSTEM_PREFIX + "mockLabradorRetriever", "false"); final File file = File.createTempFile("test", ".ser"); try { final ObjectOutputStream output = new ObjectOutputStream(new FileOutputStream(file)); try { output.writeObject(new Counter("http", null)); } finally { output.close(); } final URL url = file.toURI().toURL(); final LabradorRetriever labradorRetriever = new LabradorRetriever(url); labradorRetriever.call(); final Map<String, String> headers = Collections.emptyMap(); final LabradorRetriever labradorRetriever2 = new LabradorRetriever(url, headers); labradorRetriever2.call(); } finally { if (!file.delete()) { fail("file.delete"); } } }