@Test public void testResolve_httpUrl() throws Exception { final int portNumber = 11200; final String content = "something"; final CountDownLatch latch = new CountDownLatch( 1 ); Runnable serverRunnable = new Runnable() { @Override public void run() { try { ServerSocket serverSocket = new ServerSocket( portNumber ); latch.countDown(); Socket clientSocket = serverSocket.accept(); clientSocket.getOutputStream().write( "HTTP/1.0 200 OK\r\n\r\n".getBytes( "UTF-8" )); clientSocket.getOutputStream().write( content.getBytes( "UTF-8" )); clientSocket.getOutputStream().flush(); clientSocket.close(); serverSocket.close(); } catch( IOException e ) { e.printStackTrace(); } } }; Thread t = new Thread( serverRunnable ); t.start(); latch.await(); DefaultUrlResolver resolver = new DefaultUrlResolver(); ResolvedFile resolvedFile = null; for( int i=0; i<5; i++ ) { Thread.sleep( 200 ); try { resolvedFile = resolver.resolve( "http: break; } catch( Exception e ) { } } t.join(); Assert.assertNotNull( resolvedFile ); Assert.assertTrue( resolvedFile.getFile().exists()); Assert.assertEquals( content, Utils.readFileContent( resolvedFile.getFile())); Assert.assertFalse( resolvedFile.existedBefore()); }
@Override public ResolvedFile resolve( String url ) throws IOException { ResolvedFile resolvedFile; try { if( url.toLowerCase().startsWith( "file:/" )) { File f = new File( UriUtils.urlToUri( url )); resolvedFile = new ResolvedFile( f, true ); } else { URI uri = UriUtils.urlToUri( url ); InputStream in = uri.toURL().openStream(); File f = File.createTempFile( "rbcf_download_", null ); try { Utils.copyStream( in, f ); } finally { Utils.closeQuietly( in ); } resolvedFile = new ResolvedFile( f, false ); } } catch( Exception e ) { throw new IOException( e ); } return resolvedFile; }
DefaultUrlResolver implements IUrlResolver { @Override public ResolvedFile resolve( String url ) throws IOException { ResolvedFile resolvedFile; try { if( url.toLowerCase().startsWith( "file:/" )) { File f = new File( UriUtils.urlToUri( url )); resolvedFile = new ResolvedFile( f, true ); } else { URI uri = UriUtils.urlToUri( url ); InputStream in = uri.toURL().openStream(); File f = File.createTempFile( "rbcf_download_", null ); try { Utils.copyStream( in, f ); } finally { Utils.closeQuietly( in ); } resolvedFile = new ResolvedFile( f, false ); } } catch( Exception e ) { throw new IOException( e ); } return resolvedFile; } }
DefaultUrlResolver implements IUrlResolver { @Override public ResolvedFile resolve( String url ) throws IOException { ResolvedFile resolvedFile; try { if( url.toLowerCase().startsWith( "file:/" )) { File f = new File( UriUtils.urlToUri( url )); resolvedFile = new ResolvedFile( f, true ); } else { URI uri = UriUtils.urlToUri( url ); InputStream in = uri.toURL().openStream(); File f = File.createTempFile( "rbcf_download_", null ); try { Utils.copyStream( in, f ); } finally { Utils.closeQuietly( in ); } resolvedFile = new ResolvedFile( f, false ); } } catch( Exception e ) { throw new IOException( e ); } return resolvedFile; }  }
DefaultUrlResolver implements IUrlResolver { @Override public ResolvedFile resolve( String url ) throws IOException { ResolvedFile resolvedFile; try { if( url.toLowerCase().startsWith( "file:/" )) { File f = new File( UriUtils.urlToUri( url )); resolvedFile = new ResolvedFile( f, true ); } else { URI uri = UriUtils.urlToUri( url ); InputStream in = uri.toURL().openStream(); File f = File.createTempFile( "rbcf_download_", null ); try { Utils.copyStream( in, f ); } finally { Utils.closeQuietly( in ); } resolvedFile = new ResolvedFile( f, false ); } } catch( Exception e ) { throw new IOException( e ); } return resolvedFile; }  @Override ResolvedFile resolve( String url ); }
DefaultUrlResolver implements IUrlResolver { @Override public ResolvedFile resolve( String url ) throws IOException { ResolvedFile resolvedFile; try { if( url.toLowerCase().startsWith( "file:/" )) { File f = new File( UriUtils.urlToUri( url )); resolvedFile = new ResolvedFile( f, true ); } else { URI uri = UriUtils.urlToUri( url ); InputStream in = uri.toURL().openStream(); File f = File.createTempFile( "rbcf_download_", null ); try { Utils.copyStream( in, f ); } finally { Utils.closeQuietly( in ); } resolvedFile = new ResolvedFile( f, false ); } } catch( Exception e ) { throw new IOException( e ); } return resolvedFile; }  @Override ResolvedFile resolve( String url );  }