@Override public void hyperlinkUpdate(HyperlinkEvent e) { if (e.getEventType().equals(HyperlinkEvent.EventType.ACTIVATED)) try { Desktop.getDesktop().browse(new URI(e.getURL().toString())); } catch (IOException e1) { e1.printStackTrace(); } catch (URISyntaxException e1) { e1.printStackTrace(); } } });
private String validateAndNormalizeAvroSchemaUrl(String url, HdfsContext context) { try { new URL(url).openStream().close(); return url; } catch (MalformedURLException e) { // try locally if (new File(url).exists()) { // hive needs url to have a protocol return new File(url).toURI().toString(); } // try hdfs try { if (!hdfsEnvironment.getFileSystem(context, new Path(url)).exists(new Path(url))) { throw new PrestoException(INVALID_TABLE_PROPERTY, "Cannot locate Avro schema file: " + url); } return url; } catch (IOException ex) { throw new PrestoException(INVALID_TABLE_PROPERTY, "Avro schema file is not a valid file system URI: " + url, ex); } } catch (IOException e) { throw new PrestoException(INVALID_TABLE_PROPERTY, "Cannot open Avro schema file: " + url, e); } }
private InputStream streamOpener() throws IOException { final URL url = configurationUri.toURL(); final URLConnection connection = url.openConnection(); connection.setRequestProperty("Accept", "application/xml,text/xml,application/xhtml+xml"); return connection.getInputStream(); }
@Override public Schema load(String url) throws IOException { Schema.Parser parser = new Schema.Parser(); InputStream is = null; try { FileSystem fs = FileSystem.get(URI.create(url), conf); if (url.toLowerCase(Locale.ENGLISH).startsWith("hdfs:/")) { is = fs.open(new Path(url)); } else { is = new URL(url).openStream(); } return parser.parse(is); } finally { if (is != null) { is.close(); } } } });
private void setupEjbClientProps(String connectionPropertiesUrl) throws StartException { try { final File file = new File(connectionPropertiesUrl); final URL url; if (file.exists()) { url = file.toURI().toURL(); } else { url = new URL(connectionPropertiesUrl); } Properties properties = new Properties(); InputStream stream = null; try { stream = url.openStream(); properties.load(stream); } finally { if (stream != null) { try { stream.close(); } catch (IOException e) { //ignore } } } JBossEJBProperties ejbProps = JBossEJBProperties.fromProperties(connectionPropertiesUrl, properties); JBossEJBProperties.getContextManager().setGlobalDefault(ejbProps); } catch (Exception e) { throw new StartException(e); } }
try { new FileInputStream(file).close(); return file.toURI().toString(); }catch (IOException ignore) { return fileName; } else { try { url.openStream().close(); return url.toString(); } catch (IOException ignore) { return fileName;
/** * Returns the absolute uri of the Class-Path entry value as specified in <a * href="http://docs.oracle.com/javase/8/docs/technotes/guides/jar/jar.html#Main_Attributes">JAR * File Specification</a>. Even though the specification only talks about relative urls, * absolute urls are actually supported too (for example, in Maven surefire plugin). */ @VisibleForTesting static URL getClassPathEntry(File jarFile, String path) throws MalformedURLException { return new URL(jarFile.toURI().toURL(), path); } }
@Test public void testInputStreamThatFailsOnClose() throws Exception { final InputStream inputStream = PowerMockito.mock(InputStream.class); Mockito.doThrow(IOException.class).when(inputStream).close(); final int available = Mockito.doThrow(IOException.class).when(inputStream).available(); final URL url = PowerMockito.mock(URL.class); BDDMockito.given(url.openStream()).willReturn(inputStream); final URI uri = PowerMockito.mock(URI.class); BDDMockito.given(uri.toURL()).willReturn(url); try { ImportControlLoader.load(uri); //Using available to bypass 'ignored result' warning fail("exception expected " + available); } catch (CheckstyleException ex) { final Throwable[] suppressed = ex.getSuppressed(); assertEquals("Expected one suppressed exception", 1, suppressed.length); assertSame("Invalid exception class", IOException.class, suppressed[0].getClass()); } Mockito.verify(inputStream).close(); }
@Test public void getUriForSimplePath() throws URISyntaxException { URI uri = new URI("http://example.com/path"); mockRequest.setServerName(uri.getHost()); mockRequest.setServerPort(uri.getPort()); mockRequest.setRequestURI(uri.getPath()); mockRequest.setQueryString(uri.getQuery()); assertEquals(uri, request.getURI()); }
@Test // SPR-9317 public void fromUriEncodedQuery() throws URISyntaxException { URI uri = new URI("http://www.example.org/?param=aGVsbG9Xb3JsZA%3D%3D"); String fromUri = UriComponentsBuilder.fromUri(uri).build().getQueryParams().get("param").get(0); String fromUriString = UriComponentsBuilder.fromUriString(uri.toString()) .build().getQueryParams().get("param").get(0); assertEquals(fromUri, fromUriString); }
@Test public void testGetMetadataNotFound() throws Exception { try (PrestoS3FileSystem fs = new PrestoS3FileSystem()) { MockAmazonS3 s3 = new MockAmazonS3(); s3.setGetObjectMetadataHttpCode(SC_NOT_FOUND); fs.initialize(new URI("s3n://test-bucket/"), new Configuration()); fs.setS3Client(s3); assertEquals(fs.getS3ObjectMetadata(new Path("s3n://test-bucket/test")), null); } }
InputStream fis; if (resource instanceof URL) { // an URL resource fis = ((URL) resource).openStream(); properties.load(fis); } else if (resource instanceof String) { // a CLASSPATH resource URL url = getResource((String) resource); if (url != null) { URLConnection connection = url.openConnection(); if (connection instanceof JarURLConnection) { connection.setUseCaches(false); fis = connection.getInputStream(); properties.load(fis); fis = new FileInputStream(new File(((Path) resource).toUri().getPath())); properties.load(fis); } else if (resource instanceof InputStream) {
@Test public void testFromInstrumentedScheme() throws Exception { File tmpDir = Files.createTempDir(); tmpDir.deleteOnExit(); FileSystem fs = FileSystem.get(new URI(InstrumentedLocalFileSystem.SCHEME + ":///"), new Configuration()); Assert.assertTrue(fs instanceof InstrumentedLocalFileSystem); Assert.assertTrue(DecoratorUtils.resolveUnderlyingObject(fs) instanceof LocalFileSystem); Assert.assertEquals(fs.getFileStatus(new Path("/tmp")).getPath(), new Path("instrumented-file:///tmp")); Assert.assertEquals(fs.getUri().getScheme(), "instrumented-file"); Path basePath = new Path(tmpDir.getAbsolutePath()); Assert.assertTrue(fs.exists(basePath)); Path file = new Path(basePath, "file"); Assert.assertFalse(fs.exists(file)); fs.create(new Path(basePath, "file")); Assert.assertTrue(fs.exists(file)); Assert.assertEquals(fs.getFileStatus(file).getLen(), 0); Assert.assertEquals(fs.listStatus(basePath).length, 1); fs.delete(file, false); Assert.assertFalse(fs.exists(file)); }
@Override protected InputStream openObjectStream(URI object) throws IOException { return object.toURL().openConnection().getInputStream(); }
@Override public Mono<Void> handle(ServerHttpRequest request, ServerHttpResponse response) { URI uri = request.getURI(); assertEquals("http", uri.getScheme()); assertNotNull(uri.getHost()); assertNotEquals(-1, uri.getPort()); assertNotNull(request.getRemoteAddress()); assertEquals("/foo", uri.getPath()); assertEquals("param=bar", uri.getQuery()); return Mono.empty(); } }
@Test // SPR-13876 public void getUriWithEncoding() throws URISyntaxException { URI uri = new URI("https://example.com/%E4%B8%AD%E6%96%87" + "?redirect=https%3A%2F%2Fgithub.com%2Fspring-projects%2Fspring-framework"); mockRequest.setScheme(uri.getScheme()); mockRequest.setServerName(uri.getHost()); mockRequest.setServerPort(uri.getPort()); mockRequest.setRequestURI(uri.getRawPath()); mockRequest.setQueryString(uri.getRawQuery()); assertEquals(uri, request.getURI()); }
@Override protected InputStream openObjectStream(URI object, long start) throws IOException { if (supportContentRange) { final URLConnection connection = object.toURL().openConnection(); // Set header for range request. // Since we need to set only the start offset, the header is "bytes=<range-start>-". // See https://tools.ietf.org/html/rfc7233#section-2.1 connection.addRequestProperty(HttpHeaders.RANGE, StringUtils.format("bytes=%d-", start)); return connection.getInputStream(); } else { log.warn( "Since the input source doesn't support range requests, the object input stream is opened from the start and " + "then skipped. This may make the ingestion speed slower. Consider enabling prefetch if you see this message" + " a lot." ); final InputStream in = openObjectStream(object); in.skip(start); return in; } }