@Test public void testCanReadTheContentsOfAUrl() throws Exception { final GetUrl get = new GetUrl(); final URL urlToGet = GetUrlTest.class.getResource("GetUrlTest_testCanReadTheContentsOfAUrl.txt"); assertNotNull(urlToGet); final Symbol result = get.execute(rhsFuncContext, Symbols.asList(syms, urlToGet.toExternalForm())); assertNotNull(result); final StringSymbol resultAsString = result.asString(); assertNotNull(resultAsString); final StringWriter expected = new StringWriter(); Resources.asCharSource(urlToGet, Charsets.UTF_8).copyTo(expected); assertEquals(expected.toString(), resultAsString.toString()); }
@Override public Symbol execute(final RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); final String urlString = arguments.get(0).toString(); final URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } final InputStream is; try { is = new BufferedInputStream(url.openStream()); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(is, out); return context.getSymbols().createString(out.toString("UTF-8")); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } } }
GetUrl extends AbstractRhsFunctionHandler { @Override public Symbol execute(final RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); final String urlString = arguments.get(0).toString(); final URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } final InputStream is; try { is = new BufferedInputStream(url.openStream()); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(is, out); return context.getSymbols().createString(out.toString("UTF-8")); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } } } }
GetUrl extends AbstractRhsFunctionHandler { @Override public Symbol execute(final RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); final String urlString = arguments.get(0).toString(); final URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } final InputStream is; try { is = new BufferedInputStream(url.openStream()); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(is, out); return context.getSymbols().createString(out.toString("UTF-8")); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } } } GetUrl(); }
GetUrl extends AbstractRhsFunctionHandler { @Override public Symbol execute(final RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); final String urlString = arguments.get(0).toString(); final URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } final InputStream is; try { is = new BufferedInputStream(url.openStream()); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(is, out); return context.getSymbols().createString(out.toString("UTF-8")); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } } } GetUrl(); @Override Symbol execute(final RhsFunctionContext context, List<Symbol> arguments); }
GetUrl extends AbstractRhsFunctionHandler { @Override public Symbol execute(final RhsFunctionContext context, List<Symbol> arguments) throws RhsFunctionException { RhsFunctions.checkArgumentCount(this, arguments); final String urlString = arguments.get(0).toString(); final URL url; try { url = new URL(urlString); } catch (MalformedURLException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } final InputStream is; try { is = new BufferedInputStream(url.openStream()); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); ByteStreams.copy(is, out); return context.getSymbols().createString(out.toString("UTF-8")); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } finally { try { is.close(); } catch (IOException e) { throw new RhsFunctionException("In '" + getName() + "' RHS function: " + e.getMessage()); } } } GetUrl(); @Override Symbol execute(final RhsFunctionContext context, List<Symbol> arguments);  }