com.googlecode.flickrjandroid.util.UrlUtilities Maven / Gradle / Ivy
Show all versions of flickrj-android Show documentation
/*
* Copyright (c) 2005 Aetrion LLC.
*/
package com.googlecode.flickrjandroid.util;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLEncoder;
import java.util.Iterator;
import java.util.List;
import com.googlecode.flickrjandroid.Parameter;
/** @author Anthony Eden */
public class UrlUtilities {
public static final String UTF8 = "UTF-8";
/**
* Build a request URL.
* @param host The host
* @param port The port
* @param path The path
* @param parameters The parameters
* @return The URL
* @throws MalformedURLException
*/
public static URL buildUrl(
String host,
int port,
String path,
List parameters
) throws MalformedURLException {
// see: AuthUtilities.getSignature()
// AuthUtilities.addAuthToken(parameters);
StringBuffer buffer = new StringBuffer();
buffer.append("https://");
buffer.append(host);
if (port > 0 & port != 80) {
buffer.append(":");
buffer.append(port);
}
if (path == null) {
path = "/";
}
buffer.append(path);
Iterator iter = parameters.iterator();
if (iter.hasNext()) {
buffer.append("?");
}
while (iter.hasNext()) {
Parameter p = (Parameter) iter.next();
buffer.append(p.getName());
buffer.append("=");
Object value = p.getValue();
if (value != null) {
String string = UrlUtilities.encode(value.toString());
buffer.append(string);
}
if (iter.hasNext()) buffer.append("&");
}
/* RequestContext requestContext = RequestContext.getRequestContext();
Auth auth = requestContext.getAuth();
if (auth != null && !ignoreMethod(getMethod(parameters))) {
buffer.append("&api_sig=");
buffer.append(AuthUtilities.getSignature(sharedSecret, parameters));
} */
return new URL(buffer.toString());
}
public static URL buildPostUrl(String host, int port, String path) throws MalformedURLException {
StringBuffer buffer = new StringBuffer();
buffer.append("https://");
buffer.append(host);
if (port > 0) {
buffer.append(":");
buffer.append(port);
}
if (path == null) {
path = "/";
}
buffer.append(path);
return new URL(buffer.toString());
}
private static String getMethod(List parameters) {
Iterator iter = parameters.iterator();
while (iter.hasNext()) {
Parameter parameter = (Parameter) iter.next();
if ("method".equals(parameter.getName())) {
return String.valueOf(parameter.getValue());
}
}
return null;
}
private static boolean ignoreMethod(String method) {
if (method != null) {
if ("flickr.auth.checkToken".equals(method)) {
return true;
}
}
return false;
}
/**
* Construct the BuddyIconUrl.
* If none available, return the
* default,
* or an URL assembled from farm, iconserver and nsid.
*
* @see Flickr Documentation
* @param iconFarm
* @param iconServer
* @param id
* @return The BuddyIconUrl
*/
public static String createBuddyIconUrl(
int iconFarm,
int iconServer,
String id
) {
/**
* The default-URL, if the iconServer equals 0.
*/
String iconUrl = "https://www.flickr.com/images/buddyicon.jpg";
if (iconServer > 0) {
iconUrl = "https://farm"
+ iconFarm + ".static.flickr.com/"
+ iconServer + "/buddyicons/"
+ id + ".jpg";
}
return iconUrl;
}
/**
* @param value string to be encoded
* @return encoded string
* @see OAuth / TestCases
* @see Space encoding - OAuth | Google Groups
* @see RFC 3986 - Uniform Resource Identifier (URI): Generic Syntax - 2.1. Percent-Encoding
*/
public static String encode(String value) {
String encoded = null;
try {
encoded = URLEncoder.encode(value, "UTF-8");
} catch (UnsupportedEncodingException ignore) {
}
StringBuffer buf = new StringBuffer(encoded.length());
char focus;
for (int i = 0; i < encoded.length(); i++) {
focus = encoded.charAt(i);
if (focus == '*') {
buf.append("%2A");
} else if (focus == '+') {
buf.append("%20");
} else if (focus == '%' && (i + 1) < encoded.length()
&& encoded.charAt(i + 1) == '7' && encoded.charAt(i + 2) == 'E') {
buf.append('~');
i += 2;
} else {
buf.append(focus);
}
}
return buf.toString();
}
}