Java tutorial
/** * theMovieDb-V3-API * Copyright (C) 2012 Amith GC * * This program is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with this program. If not, see <http://www.gnu.org/licenses/>. * * @author Amith GC */ package com.agc.tmdb.Util; import java.io.BufferedReader; import java.io.IOException; import java.io.InputStreamReader; import java.io.StringWriter; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.net.URLConnection; import java.nio.charset.Charset; import java.nio.charset.UnsupportedCharsetException; import java.util.HashMap; import java.util.Map; import java.util.regex.Matcher; import java.util.regex.Pattern; import org.apache.commons.codec.binary.Base64; import com.agc.tmdb.Exception.TMDbException; import com.agc.tmdb.Exception.TMDbExceptionTypes; public class URLFetcher { private static String proxyHost = null; private static String proxyPort = null; private static String proxyUsername = null; private static String proxyPassword = null; private static String proxyEncodedPassword = null; private static Map<String, String> browserProperties = new HashMap<String, String>(); /** * As this is a Utility class, we will be hiding the Constructor. */ protected URLFetcher() { throw new UnsupportedOperationException(); } /** * Can be used to Fetch the URL, where URL is a String. * @param url as String * @return * @throws TMDbException */ public static String fetch(String url) throws TMDbException { try { return fetch(new URL(url)); } catch (MalformedURLException exception) { throw new TMDbException(TMDbExceptionTypes.EXCEPTION_INVALID_URL, null, exception); } } /** * Used to set the Browser Properties. * @param URLConnection Object. */ private static void setBrowserProperties(URLConnection connectionObject) { if (browserProperties.isEmpty()) { browserProperties.put("User-Agent", "Mozilla/5.25 Netscape/5.0 (Windows; I; Win95)"); } for (Map.Entry<String, String> browserProperty : browserProperties.entrySet()) { connectionObject.setRequestProperty(browserProperty.getKey(), browserProperty.getValue()); } } /** * This is used to open a connection, Proxy details will be considered if provided. * @param url * @return * @throws TMDbException */ public static URLConnection openConnection(URL url) throws TMDbException { try { if (proxyHost != null) { System.getProperties().put("proxySet", "true"); System.getProperties().put("proxyHost", proxyHost); System.getProperties().put("proxyPort", proxyPort); } URLConnection cnx = url.openConnection(); if (proxyUsername != null) { cnx.setRequestProperty("Proxy-Authorization", proxyEncodedPassword); } return cnx; } catch (IOException ex) { throw new TMDbException(TMDbExceptionTypes.EXCEPTION_INVALID_URL, null, ex); } } /** * Can be used to Fetch the URL, where URL is Java.net.URL. * @param url * @return * @throws TMDbException */ public static String fetch(URL url) throws TMDbException { System.out.println("URL: " + url); StringWriter content = null; try { content = new StringWriter(); BufferedReader in = null; URLConnection connectionObject = null; try { connectionObject = openConnection(url); setBrowserProperties(connectionObject); in = new BufferedReader( new InputStreamReader(connectionObject.getInputStream(), getCharset(connectionObject))); String line; while ((line = in.readLine()) != null) { content.write(line); } } finally { if (in != null) { in.close(); } if (connectionObject instanceof HttpURLConnection) { ((HttpURLConnection) connectionObject).disconnect(); } } return content.toString(); } catch (IOException ex) { throw new TMDbException(TMDbExceptionTypes.EXCEPTION_CONNECTION_ERROR, null, ex); } finally { if (content != null) { try { content.close(); } catch (IOException ex) { throw new TMDbException(TMDbExceptionTypes.EXCEPTION_CONNECTION_ERROR, "Failed to Close the connection.", ex); } } } } /** * Used to get the Character Set. * @param URLConnection * @return */ private static Charset getCharset(URLConnection connectionObject) { Charset charset = null; String contentType = connectionObject.getContentType(); if (contentType != null) { Matcher m = Pattern.compile("harset *=[ '\"]*([^ ;'\"]+)[ ;'\"]*").matcher(contentType); if (m.find()) { String encoding = m.group(1); try { charset = Charset.forName(encoding); } catch (UnsupportedCharsetException e) { } } } if (charset == null) { charset = Charset.defaultCharset(); } return charset; } public static String getProxyHost() { return proxyHost; } public static void setProxyHost(String myProxyHost) { URLFetcher.proxyHost = myProxyHost; } public static String getProxyPort() { return proxyPort; } public static void setProxyPort(String myProxyPort) { URLFetcher.proxyPort = myProxyPort; } public static String getProxyUsername() { return proxyUsername; } public static void setProxyUsername(String myProxyUsername) { URLFetcher.proxyUsername = myProxyUsername; } public static String getProxyPassword() { return proxyPassword; } public static void setProxyPassword(String myProxyPassword) { URLFetcher.proxyPassword = myProxyPassword; if (proxyUsername != null) { proxyEncodedPassword = proxyUsername + ":" + proxyPassword; proxyEncodedPassword = "Basic " + new String(Base64.encodeBase64((proxyUsername + ":" + proxyPassword).getBytes())); } } }