List of usage examples for java.net URLConnection getURL
public URL getURL()
From source file:com.afreire.plugins.video.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();// w w w . ja va2 s. c o m InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); 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, ""); //Remove the path to the file from any location if (filepath.contains("www/file:")) { filepath = filepath.replace("www/file://", ""); } // 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); //It uses a fixed name to optimize memory space File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + VIDEO_FILE_NAME); //Always copy the file //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:com.github.reverseproxy.ReverseProxyJettyHandler.java
private URLConnection getUrlConnection(HttpServletRequest request, String method, String requestBody, String forwardUrl) throws MalformedURLException, IOException, ProtocolException { final URL url = new URL(forwardUrl); final Proxy proxy = new Proxy(Proxy.Type.HTTP, new InetSocketAddress(proxyHost, proxyPort)); URLConnection urlConnection = url.openConnection(proxy); Map<String, String> requestHeaders = getRequestHeaders(request); requestHeaders.put("Host", urlConnection.getURL().getHost()); ((HttpURLConnection) urlConnection).setRequestMethod(method); if (forwardUrl.startsWith("https")) { SSLSocketFactory sslSocketFactory = null; try {/*from ww w . j a v a 2s .c om*/ sslSocketFactory = getSSLSocketFactory(); } catch (KeyManagementException e) { throw new IOException("Exception caught while setting up SSL props : ", e); } catch (NoSuchAlgorithmException e) { throw new IOException("Exception caught while setting up SSL props : ", e); } ((HttpsURLConnection) urlConnection).setSSLSocketFactory(sslSocketFactory); } urlConnection.setDoInput(true); setHeaders(urlConnection, requestHeaders); setDoOutput(method, requestBody, urlConnection); return urlConnection; }
From source file:com.moust.cordova.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();/*ww w . j av a 2s .c om*/ InputStream is = con.getInputStream(); //new redirected url url = con.getURL().toString(); 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, ""); //Remove the path to the file from any location if (filepath.contains("www/file:")) { filepath = filepath.replace("www/file://", ""); } // 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); //It uses a fixed name to optimize memory space File fp = new File(this.cordova.getActivity().getFilesDir() + "/" + VIDEO_FILE_NAME); //Always copy the file //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:com.moviejukebox.tools.WebBrowser.java
private String createCookieHeader(URLConnection cnx) { String host = cnx.getURL().getHost(); StringBuilder cookiesHeader = new StringBuilder(); for (Map.Entry<String, Map<String, String>> domainCookies : cookies.entrySet()) { if (host.endsWith(domainCookies.getKey())) { for (Map.Entry<String, String> cookie : domainCookies.getValue().entrySet()) { cookiesHeader.append(cookie.getKey()); cookiesHeader.append("="); cookiesHeader.append(cookie.getValue()); cookiesHeader.append(";"); }/*from w w w . j a va2s. c o m*/ } } if (cookiesHeader.length() > 0) { // remove last ; char cookiesHeader.deleteCharAt(cookiesHeader.length() - 1); } return cookiesHeader.toString(); }
From source file:com.ramforth.webserver.http.modules.HttpUrlModule.java
private void addContentTypeHeaderForURL(IHttpResponse httpResponse, URLConnection urlConnection) { String mimeType = urlConnection.getContentType(); if (StringUtilities.isNullOrEmpty(mimeType) || "content/unknown".equals(mimeType)) { String extension = FilenameUtils.getExtension(urlConnection.getURL().getPath()); mimeType = "application/octet-stream"; if (!StringUtilities.isNullOrEmpty(extension)) { String mimeType2 = MimeTypeMap.getInstance().getMimeTypeForExtension(extension); if (mimeType2 != null) { mimeType = mimeType2;//from w w w. j av a 2 s . com } } } httpResponse.setContentType(mimeType); }
From source file:org.mycore.common.xml.MCRXMLResource.java
public URL getURL(String name, ClassLoader classLoader) throws IOException { URLConnection con = getResourceURLConnection(name, classLoader); if (con == null) { return null; }// ww w . ja v a2s.c o m try { return con.getURL(); } finally { closeURLConnection(con); } }
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();// w w w . j a va 2s . com InputStream instr = connection.getInputStream(); instr.close(); if (isCancelled) { return result; } result[i] = connection.getURL().toString(); Utils.log("got " + result[i]); } return result; }
From source file:org.callimachusproject.xml.InputSourceResolver.java
/** * returns null for 404 resources.//from ww w . j a va2s . co m */ private InputSource resolveURL(String systemId) throws IOException { URLConnection con = new URL(systemId).openConnection(); con.addRequestProperty("Accept", getAcceptHeader()); con.addRequestProperty("Accept-Encoding", "gzip"); try { String base = con.getURL().toExternalForm(); String type = con.getContentType(); String encoding = con.getHeaderField("Content-Encoding"); InputStream in = con.getInputStream(); if (encoding != null && encoding.contains("gzip")) { in = new GZIPInputStream(in); } return create(type, in, base); } catch (FileNotFoundException e) { return null; } catch (IOException e) { logger.warn("Could not resolve {}", systemId); throw e; } }
From source file:ee.ria.xroad.common.conf.globalconf.ConfigurationDownloader.java
byte[] downloadContent(ConfigurationLocation location, ConfigurationFile file) throws Exception { URLConnection connection = getDownloadURLConnection(getDownloadURL(location, file)); log.info("Downloading content from {}", connection.getURL()); try (InputStream in = connection.getInputStream()) { return IOUtils.toByteArray(in); }/*from w w w . ja v a2s . c om*/ }
From source file:com.moviejukebox.tools.WebBrowser.java
/** * Get URL - allow to know if there is some redirect * * @param urlString/*from www .ja v a 2 s . co m*/ * @return */ public String getUrl(final String urlString) { URL url; try { url = new URL(urlString); } catch (MalformedURLException ex) { LOG.warn("Unable to convert URL: {} - Error: {}", urlString, ex.getMessage()); return Movie.UNKNOWN; } ThreadExecutor.enterIO(url); try { URLConnection cnx = openProxiedConnection(url); sendHeader(cnx); readHeader(cnx); return cnx.getURL().toString(); } catch (IOException ex) { LOG.warn("Unable to retrieve URL: {} - Error: {}", urlString, ex.getMessage()); return Movie.UNKNOWN; } finally { ThreadExecutor.leaveIO(); } }