List of usage examples for java.net URLConnection connect
public abstract void connect() throws IOException;
From source file:jp.classmethod.aws.InstanceMetadataFactoryBean.java
private Reader createUserDataReader() throws IOException, MalformedURLException { URLConnection conn = new URL(USER_DATA_URL).openConnection(); conn.setReadTimeout(TIMEOUT); conn.setConnectTimeout(TIMEOUT); conn.connect(); return new BufferedReader(new InputStreamReader(conn.getInputStream())); }
From source file:com.dawsonloudon.videoplayer.VideoPlayer.java
private void playVideo(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString();// www.j ava2 s .c o m is.close(); } // Create URI Uri uri = Uri.parse(url); Intent intent = null; // Check to see if someone is trying to play a YouTube page. if (url.contains(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=com.google.android.youtube")); } } else if (url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); // Display video player intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); } else { // Display video player intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); } this.cordova.getActivity().startActivity(intent); }
From source file:org.seasar.uruma.eclipath.ProjectRefresher.java
protected void sendRequest(URL url) { URLConnection conn; InputStream is = null;//from w ww . j a v a 2 s .c o m try { conn = url.openConnection(); conn.setReadTimeout(TIMEOUT_TIME); conn.connect(); is = conn.getInputStream(); } catch (IOException ex) { throw new PluginRuntimeException("Failed to send request. : " + url.toExternalForm(), ex); } finally { IOUtils.closeQuietly(is); } return; }
From source file:com.pentaho.ctools.cdf.DialComponent.java
/** * ############################### Test Case 3 ############################### * * Test Case Name:/* ww w. j a va2s . c o m*/ * Dial Component * Description: * We pretend validate the generated graphic (in a image) and if url for * the image is valid. * Steps: * 1. Check if a graphic was generated * 2. Check the http request for the generated image */ @Test public void tc3_GenerateGraphic_GraphicGeneratedAndHttp200() { this.log.info("tc3_GenerateGraphic_GraphicGeneratedAndHttp200"); // ## Step 1 WebElement dialElement = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.cssSelector("img")); assertNotNull(dialElement); String attrSrc = this.elemHelper.GetAttribute(driver, By.cssSelector("img"), "src"); String attrWidth = this.elemHelper.GetAttribute(driver, By.cssSelector("img"), "width"); String attrHeight = this.elemHelper.GetAttribute(driver, By.cssSelector("img"), "height"); assertTrue(attrSrc.startsWith(baseUrl + "getImage?image=tmp_chart_admin-")); assertEquals(attrWidth, "400"); assertEquals(attrHeight, "200"); // ## Step 2 try { URL url = new URL(attrSrc); URLConnection connection = url.openConnection(); connection.connect(); assertEquals(HttpStatus.SC_OK, ((HttpURLConnection) connection).getResponseCode()); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:net.xisberto.phonetodesktop.network.URLOptions.java
protected String[] unshorten(String... params) throws IOException { String[] result = params.clone(); for (int i = 0; i < params.length; i++) { Utils.log("unshorten " + params[i]); URLConnection connection = new URL(params[i]).openConnection(); connection.connect(); InputStream instr = connection.getInputStream(); instr.close();// w w w.ja v a 2s. c o m if (isCancelled) { return result; } result[i] = connection.getURL().toString(); Utils.log("got " + result[i]); } return result; }
From source file:com.phonegap.plugins.videoplayer.VideoPlayer.java
private void playVideo(String url) throws IOException { if (url.contains("bit.ly/") || url.contains("goo.gl/") || url.contains("tinyurl.com/") || url.contains("youtu.be/")) { //support for google / bitly / tinyurl / youtube shortens URLConnection con = new URL(url).openConnection(); con.connect(); InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString();//w ww.j a va 2s.c o m is.close(); } // Create URI Uri uri = Uri.parse(url); Intent intent = null; // Check to see if someone is trying to play a YouTube page. if (url.contains(YOU_TUBE)) { // If we don't do it this way you don't have the option for youtube uri = Uri.parse("vnd.youtube:" + uri.getQueryParameter("v")); if (isYouTubeInstalled()) { intent = new Intent(Intent.ACTION_VIEW, uri); } else { intent = new Intent(Intent.ACTION_VIEW); intent.setData(Uri.parse("market://details?id=com.google.android.youtube")); } } else if (url.contains(ASSETS)) { // get file path in assets folder String filepath = url.replace(ASSETS, ""); // get actual filename from path as command to write to internal storage doesn't like folders String filename = filepath.substring(filepath.lastIndexOf("/") + 1, filepath.length()); // Don't copy the file if it already exists File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + filename); if (!fp.exists()) { this.copy(filepath, filename); } // change uri to be to the new file in internal storage uri = Uri.parse("file://" + this.cordova.getActivity().getFilesDir() + "/" + filename); // Display video player intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); } else { // Display video player intent = new Intent(Intent.ACTION_VIEW); intent.setDataAndType(uri, "video/*"); } this.cordova.getActivity().startActivity(intent); }
From source file:org.danann.cernunnos.io.CopyFileTask.java
public void perform(TaskRequest req, TaskResponse res) { URL loc = resource.evaluate(req, res); String dir = to_dir != null ? (String) to_dir.evaluate(req, res) : null; String destination = (String) to_file.evaluate(req, res); InputStream is = null;// ww w . j a v a 2s . co m OutputStream os = null; try { URLConnection conn = loc.openConnection(); conn.connect(); is = conn.getInputStream(); if (log.isTraceEnabled()) { StringBuilder msg = new StringBuilder(); msg.append("\n\turl=").append(loc.toString()).append("\n\tcontent-length=") .append(conn.getContentLength()); log.trace(msg); } File f = new File(dir, destination); if (f.getParentFile() != null) { // Make sure the necessary directories are in place... f.getParentFile().mkdirs(); } os = new FileOutputStream(f); int bytesRead = 0; byte[] buf = new byte[4096]; for (int len = is.read(buf); len > 0 || bytesRead < conn.getContentLength(); len = is.read(buf)) { os.write(buf, 0, len); bytesRead = bytesRead + len; } } catch (Throwable t) { String msg = "Unable to copy the specified file [" + loc.toExternalForm() + "] to the specified location [" + destination + "]."; throw new RuntimeException(msg, t); } finally { if (is != null) { try { is.close(); } catch (IOException ioe) { throw new RuntimeException(ioe); } } if (os != null) { try { os.close(); } catch (IOException ioe) { throw new RuntimeException(ioe); } } } }
From source file:org.xwiki.classloader.internal.protocol.attachmentjar.AttachmentURLStreamHandlerTest.java
@Test public void testAttachmentJarURL() throws Exception { URL url = new URL(null, "attachmentjar://Space.Page@filename", (URLStreamHandler) this.handler); final AttachmentReference attachmentReference = new AttachmentReference("filename", new DocumentReference("wiki", "space", "page")); getMockery().checking(new Expectations() { {//from ww w.ja v a2 s. c o m oneOf(AttachmentURLStreamHandlerTest.this.arf).resolve("Space.Page@filename"); will(returnValue(attachmentReference)); oneOf(AttachmentURLStreamHandlerTest.this.dab).getAttachmentContent(attachmentReference); will(returnValue(new ByteArrayInputStream("content".getBytes()))); } }); URLConnection connection = url.openConnection(); InputStream input = null; try { connection.connect(); input = connection.getInputStream(); Assert.assertEquals("content", IOUtils.toString(input)); } finally { if (input != null) { input.close(); } } }
From source file:com.pentaho.ctools.cdf.require.DialComponent.java
/** * ############################### Test Case 3 ############################### * * Test Case Name:/*from w ww.j av a2 s .c o m*/ * Dial Component * Description: * We pretend validate the generated graphic (in a image) and if url for * the image is valid. * Steps: * 1. Check if a graphic was generated * 2. Check the http request for the generated image */ @Test public void tc3_GenerateGraphic_GraphicGeneratedAndHttp200() { this.log.info("tc3_GenerateGraphic_GraphicGeneratedAndHttp200"); /* * ## Step 1 */ WebElement dialElement = this.elemHelper.WaitForElementPresenceAndVisible(driver, By.cssSelector("img")); assertNotNull(dialElement); String attrSrc = this.elemHelper.GetAttribute(driver, By.cssSelector("img"), "src"); String attrWidth = this.elemHelper.GetAttribute(driver, By.cssSelector("img"), "width"); String attrHeight = this.elemHelper.GetAttribute(driver, By.cssSelector("img"), "height"); assertTrue(attrSrc.startsWith(baseUrl + "getImage?image=tmp_chart_admin-")); assertEquals(attrWidth, "400"); assertEquals(attrHeight, "200"); // ## Step 2 try { URL url = new URL(attrSrc); URLConnection connection = url.openConnection(); connection.connect(); assertEquals(HttpStatus.SC_OK, ((HttpURLConnection) connection).getResponseCode()); } catch (Exception ex) { ex.printStackTrace(); } }
From source file:org.apache.htrace.util.TestHTracedProcess.java
private String doGet(final URL url) throws IOException { URLConnection connection = url.openConnection(); connection.setConnectTimeout(TIMEOUT); connection.setReadTimeout(TIMEOUT); connection.connect(); StringBuffer sb = new StringBuffer(); BufferedReader reader = new BufferedReader(new InputStreamReader(connection.getInputStream())); try {//from ww w . j a v a 2 s .co m String line = null; while ((line = reader.readLine()) != null) { System.out.println(line); sb.append(line); } } finally { reader.close(); } return sb.toString(); }