1010import java .net .URLEncoder ;
1111import java .util .Map ;
1212
13- /** @author Anthony Eden */
13+ /**
14+ * @author Anthony Eden
15+ * @author Mike Chaberski
16+ */
1417public class UrlUtilities {
1518
1619 public static final String UTF8 = "UTF-8" ;
@@ -28,14 +31,33 @@ public class UrlUtilities {
2831 * The parameters
2932 * @return The URL
3033 * @throws MalformedURLException
34+ * @deprecated use {@link #buildSecureUrl(java.lang.String, int, java.lang.String, java.util.Map) }
3135 */
36+ @ Deprecated
3237 public static URL buildUrl (String host , int port , String path , Map <String , String > parameters ) throws MalformedURLException {
33- // see: AuthUtilities.getSignature()
34- // AuthUtilities.addAuthToken(parameters);
35-
36- StringBuffer buffer = new StringBuffer ();
37- if (!host .startsWith ("http://" )) {
38- buffer .append ("http://" );
38+ return buildUrl ("http" , port , path , parameters );
39+ }
40+
41+ /**
42+ * Build a request URL using a given scheme.
43+ *
44+ * @param scheme the scheme, either {@code http} or {@code https}
45+ * @param host
46+ * The host
47+ * @param port
48+ * The port
49+ * @param path
50+ * The path
51+ * @param parameters
52+ * The parameters
53+ * @return The URL
54+ * @throws MalformedURLException
55+ */
56+ public static URL buildUrl (String scheme , String host , int port , String path , Map <String , String > parameters ) throws MalformedURLException {
57+ checkSchemeAndPort (scheme , port );
58+ StringBuilder buffer = new StringBuilder ();
59+ if (!host .startsWith (scheme + "://" )) {
60+ buffer .append (scheme ).append ("://" );
3961 }
4062 buffer .append (host );
4163 if (port > 0 ) {
@@ -77,9 +99,64 @@ public static URL buildUrl(String host, int port, String path, Map<String, Strin
7799 return new URL (buffer .toString ());
78100 }
79101
102+ /**
103+ * Build a request URL.
104+ *
105+ * @param host
106+ * The host
107+ * @param port
108+ * The port
109+ * @param path
110+ * The path
111+ * @param parameters
112+ * The parameters
113+ * @return The URL
114+ * @throws MalformedURLException
115+ */
116+ public static URL buildSecureUrl (String host , int port , String path , Map <String , String > parameters ) throws MalformedURLException {
117+ return buildUrl ("https" , host , port , path , parameters );
118+ }
119+
120+ public static URL buildSecurePostUrl (String host , int port , String path ) throws MalformedURLException {
121+ return buildPostUrl ("https" , host , port , path );
122+ }
123+
124+ private static void checkScheme (String scheme ) {
125+ if (scheme == null || !("http" .equals (scheme ) || "https" .equals (scheme ))) {
126+ throw new IllegalArgumentException ("scheme must be http or https" );
127+ }
128+ }
129+
130+ private static void checkSchemeAndPort (String scheme , int port ) {
131+ checkScheme (scheme );
132+ /*
133+ * Be liberal about accepting non-default ports, but strict
134+ * about mismatching scheme and default port.
135+ */
136+ if ("http" .equals (scheme ) && port == 443 ) {
137+ throw new IllegalArgumentException ("port 443 is invalid with http scheme" );
138+ }
139+ if ("https" .equals (scheme ) && port == 80 ) {
140+ throw new IllegalArgumentException ("port 80 is invalid with https scheme" );
141+ }
142+ }
143+
144+ /**
145+ * Build a POST URL with {@code http} scheme.
146+ * @param host the host
147+ * @param port the port
148+ * @param path the path
149+ * @return
150+ * @throws MalformedURLException
151+ */
80152 public static URL buildPostUrl (String host , int port , String path ) throws MalformedURLException {
81- StringBuffer buffer = new StringBuffer ();
82- buffer .append ("http://" );
153+ return buildPostUrl ("http" , host , port , path );
154+ }
155+
156+ public static URL buildPostUrl (String scheme , String host , int port , String path ) throws MalformedURLException {
157+ checkSchemeAndPort (scheme , port );
158+ StringBuilder buffer = new StringBuilder ();
159+ buffer .append (scheme ).append ("://" );
83160 buffer .append (host );
84161 if (port > 0 ) {
85162 buffer .append (':' );
@@ -93,7 +170,7 @@ public static URL buildPostUrl(String host, int port, String path) throws Malfor
93170 }
94171
95172 /**
96- * Construct the BuddyIconUrl.
173+ * Construct the BuddyIconUrl with {@code http} scheme .
97174 * <p>
98175 * If none available, return the <a href="http://www.flickr.com/images/buddyicon.jpg">default</a>, or an URL assembled from farm, iconserver and nsid.
99176 *
@@ -102,14 +179,36 @@ public static URL buildPostUrl(String host, int port, String path) throws Malfor
102179 * @param iconServer
103180 * @param id
104181 * @return The BuddyIconUrl
182+ * @deprecated use {@link #createSecureBuddyIconUrl(int, int, java.lang.String) }
105183 */
184+ @ Deprecated
106185 public static String createBuddyIconUrl (int iconFarm , int iconServer , String id ) {
186+ return createBuddyIconUrl ("http" , iconFarm , iconServer , id );
187+ }
188+
189+ /**
190+ * Construct the BuddyIconUrl with {@code https} scheme.
191+ * <p>
192+ * If none available, return the <a href="https://www.flickr.com/images/buddyicon.jpg">default</a>, or an URL assembled from farm, iconserver and nsid.
193+ *
194+ * @see <a href="http://flickr.com/services/api/misc.buddyicons.html">Flickr Documentation</a>
195+ * @param iconFarm
196+ * @param iconServer
197+ * @param id
198+ * @return The BuddyIconUrl
199+ */
200+ public static String createSecureBuddyIconUrl (int iconFarm , int iconServer , String id ) {
201+ return createBuddyIconUrl ("https" , iconFarm , iconServer , id );
202+ }
203+
204+ public static String createBuddyIconUrl (String scheme , int iconFarm , int iconServer , String id ) {
205+ checkScheme (scheme );
107206 /**
108207 * The default-URL, if the iconServer equals 0.
109208 */
110- String iconUrl = "http ://www.flickr.com/images/buddyicon.jpg" ;
209+ String iconUrl = scheme + " ://www.flickr.com/images/buddyicon.jpg" ;
111210 if (iconServer > 0 ) {
112- iconUrl = "http ://farm" + iconFarm + ".static.flickr.com/" + iconServer + "/buddyicons/" + id + ".jpg" ;
211+ iconUrl = scheme + " ://farm" + iconFarm + ".static.flickr.com/" + iconServer + "/buddyicons/" + id + ".jpg" ;
113212 }
114213 return iconUrl ;
115214 }
0 commit comments