Created
January 13, 2012 04:51
-
-
Save tistaharahap/1604734 to your computer and use it in GitHub Desktop.
OAUTHnesia for Blackberry
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.urbanesia.api; | |
import java.io.IOException; | |
import java.io.InputStream; | |
import java.io.OutputStream; | |
import javax.microedition.io.Connector; | |
import javax.microedition.io.HttpConnection; | |
import net.rim.device.api.crypto.MD5Digest; | |
import com.urbanesia.utils.Utils; | |
public class OAUTHnesia { | |
protected static final String BASE_URL = "http://api1.urbanesia.com/"; | |
public static final int OAUTH_SAFE_ENCODE = 1; | |
public static final int OAUTH_NO_SAFE_ENCODE = 0; | |
private static String CONSUMER_KEY; | |
private static String CONSUMER_SECRET; | |
private static String USER_KEY; | |
private static String USER_SECRET; | |
private static String API_URI; | |
private static String SAFE_ENCODE = "0"; | |
public OAUTHnesia(String cons_key, String cons_secret, int safe_encode) { | |
setConsumerKey(cons_key); | |
setConsumerSecret(cons_secret); | |
if(safe_encode == 1) | |
setSafeEncode("1"); | |
} | |
public OAUTHnesia(String cons_key, String cons_secret, String user_key, String user_secret, int safe_encode) { | |
setConsumerKey(cons_key); | |
setConsumerSecret(cons_secret); | |
setUserKey(user_key); | |
setUserSecret(user_secret); | |
if(safe_encode == 1) | |
setSafeEncode("1"); | |
} | |
public String oAuth(String oUri, String post, String get) { | |
setApiUri(oUri); | |
String oPost = "oauth_consumer_key=" + getConsumerKey() | |
+ "&oauth_nonce=" + getNonce() + "&oauth_signature_method=" | |
+ "HMAC-SHA1" + "&oauth_timestamp=" + getTime() | |
+ "&oauth_token=" + getUserKey() + "&oauth_version=" + "1.0"; | |
if (post.compareTo("") == 0) { | |
post = oPost; | |
} else { | |
post += "&" + oPost; | |
} | |
if (getSafeEncode().compareTo("1") == 0) { | |
post += "&safe_encode=" + Integer.toString(OAUTH_SAFE_ENCODE); | |
} | |
if (get.compareTo("") != 0) { | |
get = "&" + get; | |
} | |
String request = post + get; | |
String requestify = encodeForOAuth(request); | |
String base_sig = generateBaseSignature(requestify); | |
//Log.out(base_sig); | |
try { | |
String signature = Utils.hmacsha1(getConsumerSecret() + "&" | |
+ getUserSecret(), base_sig); | |
String oauth_sig = "?oauth_signature="; | |
oauth_sig += Utils.URLencode(signature); | |
String url = BASE_URL + getApiUri() + oauth_sig + get; | |
return sendRequest(url, post); | |
} catch (Exception e) { | |
return ""; | |
} | |
} | |
public String sendRequest(String url, String post) { | |
String urlString = url + ConnString.getConnectionString(); | |
//Log.out("Querying to Urbanesia.."); | |
try { | |
HttpConnection conn = (HttpConnection) Connector.open(urlString); | |
OutputStream os = null; | |
InputStream in = null; | |
try { | |
conn.setRequestMethod(HttpConnection.POST); | |
conn.setRequestProperty("Content-Type","application/x-www-form-urlencoded"); | |
conn.setRequestProperty("If-Modified-Since","29 Oct 1999 19:43:31 GMT"); | |
conn.setRequestProperty("User-Agent","Urbanesia Jajan Blackberry v1.0"); | |
conn.setRequestProperty("Content-Language", "en-US"); | |
os = conn.openOutputStream(); | |
StringBuffer buffer = new StringBuffer(); | |
buffer.append(post); | |
os.write(buffer.toString().getBytes()); | |
os.flush(); | |
in = conn.openInputStream(); | |
byte[] data = new byte[20]; | |
int len = 0; | |
int size = 0; | |
StringBuffer raw = new StringBuffer(); | |
while ( -1 != (len = in.read(data)) ) { | |
String received = new String(data, 0, len); | |
raw.append(received); | |
size += len; | |
} | |
in.close(); | |
String msg = raw.toString(); | |
return msg; | |
} catch(Exception e) { | |
//e.printStackTrace(); | |
return ""; | |
} finally { | |
if(conn != null) { | |
try { | |
conn.close(); | |
} catch(Exception e2) { | |
//e2.printStackTrace(); | |
} | |
} | |
} | |
} catch (IOException e) { | |
//e.printStackTrace(); | |
return ""; | |
} | |
} | |
private String generateBaseSignature(String s) { | |
return "POST&" + Utils.URLencode(BASE_URL + getApiUri()) + "&" | |
+ Utils.URLencode(s); | |
} | |
private String encodeForOAuth(String s) { | |
String[] par = Utils.split(s, "&"); | |
par = Utils.sort(par); | |
//Log.out(par[0]); | |
int max = par.length; | |
int j = 0; | |
String postify = ""; | |
for (int i = 0; i < max; i++) { | |
if (j == 1) | |
postify += "&"; | |
String[] temp = Utils.split(par[i], "="); | |
postify += Utils.URLencode(temp[0]) + "=" | |
+ Utils.URLencode(temp[1]); | |
j = 1; | |
} | |
return postify; | |
} | |
private String getNonce() { | |
return md5(Long.toString(System.currentTimeMillis())); | |
} | |
private String getTime() { | |
return Long.toString(System.currentTimeMillis()); | |
} | |
private String md5(String s) { | |
MD5Digest digest = new MD5Digest(); | |
digest.update(s.getBytes()); | |
return new String(s.getBytes()); | |
} | |
public void setApiUri(String s) { | |
OAUTHnesia.API_URI = s; | |
} | |
public String getApiUri() { | |
return OAUTHnesia.API_URI; | |
} | |
public void setSafeEncode(String s) { | |
OAUTHnesia.SAFE_ENCODE = s; | |
} | |
public String getSafeEncode() { | |
return OAUTHnesia.SAFE_ENCODE; | |
} | |
public void setConsumerKey(String s) { | |
OAUTHnesia.CONSUMER_KEY = s; | |
} | |
public String getConsumerKey() { | |
return OAUTHnesia.CONSUMER_KEY; | |
} | |
public void setConsumerSecret(String s) { | |
OAUTHnesia.CONSUMER_SECRET = s; | |
} | |
public String getConsumerSecret() { | |
return OAUTHnesia.CONSUMER_SECRET; | |
} | |
public void setUserKey(String s) { | |
OAUTHnesia.USER_KEY = s; | |
} | |
public String getUserKey() { | |
return OAUTHnesia.USER_KEY; | |
} | |
public void setUserSecret(String s) { | |
OAUTHnesia.USER_SECRET = s; | |
} | |
public String getUserSecret() { | |
return OAUTHnesia.USER_SECRET; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.urbanesia.utils; | |
import java.io.IOException; | |
import net.rim.device.api.crypto.CryptoTokenException; | |
import net.rim.device.api.crypto.CryptoUnsupportedOperationException; | |
import net.rim.device.api.crypto.HMAC; | |
import net.rim.device.api.crypto.HMACKey; | |
import net.rim.device.api.crypto.SHA1Digest; | |
import net.rim.device.api.io.Base64OutputStream; | |
import net.rim.device.api.system.GIFEncodedImage; | |
import net.rim.device.api.util.Comparator; | |
import net.rim.device.api.util.SimpleSortingVector; | |
public class Utils { | |
public static final String APP_NAME = "APPLICATION_NAME"; | |
public static final String SEARCH_SUBCAT = "DEFAULT_SEARCH_SUBCAT"; | |
public static final String DEFAULT_BIZ_PIC = "default_biz.png"; | |
public static final String ICON_SMALL = "ICON.png"; | |
public static final String MOBILE_WEB_URL = "http://m.urbanesia.com/"; | |
public static final String MOBILE_WEB_BIZ_PROFILE = MOBILE_WEB_URL + "profile/"; | |
public static final String MOBILE_WEB_UTM = "?from=APP_SHORTNAME"; | |
public static final String CONSUMER_KEY = "CONSUMER_KEY"; | |
public static final String CONSUMER_SECRET = "CONSUMER_SECRET"; | |
public static final String USER_KEY = "USER_KEY"; | |
public static final String USER_SECRET = "USER_SECRET"; | |
public static final String COORDS_DEFAULT_JKT = "-6.17535,106.827257"; | |
public static final String COORDS_DEFAULT_BALI = "-8.657054,115.217461"; | |
public static final String COORDS_DEFAULT_BANDUNG = "-6.921799,107.607077"; | |
public static AnimatedGIFField getLoadingGif() { | |
GIFEncodedImage f = (GIFEncodedImage) GIFEncodedImage.getEncodedImageResource("loading.gif"); | |
return new AnimatedGIFField(f); | |
} | |
public static String hmacsha1(String key, String message) throws CryptoTokenException, CryptoUnsupportedOperationException, IOException { | |
HMACKey k = new HMACKey(key.getBytes()); | |
HMAC hmac = new HMAC(k, new SHA1Digest()); | |
hmac.update(message.getBytes()); | |
byte[] mac = hmac.getMAC(); | |
return Base64OutputStream.encodeAsString(mac, 0, mac.length, false, false); | |
} | |
public static String URLencode(String s) { | |
if (s!=null) { | |
StringBuffer tmp = new StringBuffer(); | |
int i=0; | |
try { | |
while (true) { | |
char t = s.charAt(i); | |
int b = (int) t; | |
if ( | |
t == (char) '.' || | |
t == (char) '_' || | |
t == (char) '-' || | |
t == (char) '~' || | |
(b>=0x30 && b<=0x39) || | |
(b>=0x41 && b<=0x5A) || | |
(b>=0x61 && b<=0x7A) | |
) { | |
tmp.append((char)b); | |
} | |
else { | |
tmp.append("%"); | |
if (b <= 0xf) tmp.append("0"); | |
tmp.append(Integer.toHexString(b).toUpperCase()); | |
} | |
i++; | |
} | |
} | |
catch (Exception e) {} | |
return tmp.toString(); | |
} | |
return null; | |
} | |
private static class SortComparator implements Comparator { | |
public int compare(Object key1, Object key2) { | |
return ((((String) key1).compareTo((String) key2))); | |
} | |
} | |
public static String[] sort(String[] e) { | |
SimpleSortingVector v = new SimpleSortingVector(); | |
for(int i=0, max=e.length; i<max; i++) { | |
v.addElement(e[i]); | |
//Log.out("Unsorted Params " + String.valueOf(i) + ": " + e[i]); | |
} | |
v.setSortComparator(new SortComparator()); | |
v.reSort(); | |
int max = v.size(); | |
String[] ret = new String[max]; | |
for(int i=0; i<max; i++) { | |
ret[i] = (String) v.elementAt(i); | |
//Log.out("Sorted Params " + String.valueOf(i) + ": " + ret[i]); | |
} | |
return ret; | |
} | |
public static String[] split(String strString, String strDelimiter) { | |
String[] strArray; | |
int iOccurrences = 0; | |
int iIndexOfInnerString = 0; | |
int iIndexOfDelimiter = 0; | |
int iCounter = 0; | |
if (strString == null) { | |
throw new IllegalArgumentException("Input string cannot be null."); | |
} | |
if (strDelimiter.length() <= 0 || strDelimiter == null) { | |
throw new IllegalArgumentException("Delimeter cannot be null or empty."); | |
} | |
if (strString.startsWith(strDelimiter)) { | |
strString = strString.substring(strDelimiter.length()); | |
} | |
if (!strString.endsWith(strDelimiter)) { | |
strString += strDelimiter; | |
} | |
while((iIndexOfDelimiter = strString.indexOf(strDelimiter, | |
iIndexOfInnerString)) != -1) { | |
iOccurrences += 1; | |
iIndexOfInnerString = iIndexOfDelimiter + | |
strDelimiter.length(); | |
} | |
strArray = new String[iOccurrences]; | |
iIndexOfInnerString = 0; | |
iIndexOfDelimiter = 0; | |
while((iIndexOfDelimiter = strString.indexOf(strDelimiter, | |
iIndexOfInnerString)) != -1) { | |
strArray[iCounter] = strString.substring(iIndexOfInnerString,iIndexOfDelimiter); | |
iIndexOfInnerString = iIndexOfDelimiter + | |
strDelimiter.length(); | |
iCounter += 1; | |
} | |
return strArray; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment