Skip to content

Commit

Permalink
1. Removed unwanted classes
Browse files Browse the repository at this point in the history
2. Renamed whereever necessary
3. Added 'shorten' link
  • Loading branch information
nischal committed Apr 26, 2012
1 parent 6f6761b commit e895c41
Show file tree
Hide file tree
Showing 152 changed files with 285 additions and 15,154 deletions.
File renamed without changes.
105 changes: 105 additions & 0 deletions src/com/bit4j/bitly/Bitly.java
@@ -0,0 +1,105 @@
package com.bit4j.bitly;

import java.io.Serializable;
import java.lang.reflect.Type;
import java.util.logging.Logger;

import org.apache.commons.httpclient.NameValuePair;

import com.bit4j.bitly.entity.bitly.ShortURL;
import com.bit4j.bitly.enums.HttpClientType;
import com.bit4j.bitly.exception.BitlyException;
import com.bit4j.bitly.http.APICallerFactory;
import com.bit4j.bitly.http.APICallerInterface;
import com.bit4j.bitly.util.Constants;
import com.bit4j.bitly.util.JSONToObjectTransformer;

/**
* This is the main Bitly class. All the action is here!
*
* @author Nischal Shetty - nischal@codigami.com
*/
public class Bitly implements Serializable {

private static final long serialVersionUID = -7949886324407828634L;

Logger logger = Logger.getLogger(Bitly.class.getName());

private OAuthAccessToken authAccessToken;

private APICallerInterface caller = null;

/**
* If only the access token is passed, then the Apache Http Client library is used for making http
* requests
*
* @param authAccessToken
*/
public Bitly(OAuthAccessToken authAccessToken) {
// apache http client is the default client type
this(authAccessToken, HttpClientType.APACHE_HTTP_CLIENT);
}

public Bitly(OAuthAccessToken authAccessToken, HttpClientType clientType) {
this.authAccessToken = authAccessToken;
caller = APICallerFactory.getAPICallerInstance(clientType);
}

public OAuthAccessToken getAuthAccessToken() {
return authAccessToken;
}

private NameValuePair getNameValuePairAccessToken() {
return new NameValuePair(Constants.PARAM_ACCESS_TOKEN, this.authAccessToken.getAccessToken());
}

public ShortURL shorten(String longUrl) throws BitlyException {
//bitly expects all long urls to have / before the param start i.e. before ?
//2 cases:
//case 1: http://grabinbox.com/?param=test
//case 2: http://grabinbox.com?param=test
//By aplying #replace twice, we make sure that both the cases are satisfied
longUrl = longUrl.replaceFirst("/\\?", "\\?").replaceFirst("\\?", "/?");

NameValuePair[] nameValuePairs = new NameValuePair[]{getNameValuePairAccessToken(), new NameValuePair(Constants.PARAM_LONG_URL,longUrl)};
return pullData(Constants.BITLY_URL+Constants.BITLY_LINKS_SHORTEN_URL, ShortURL.class, nameValuePairs);
}


/**
* Raw API method to pull any data in json form and transform it into the right object <br>
* An HTTP GET method is used here
*
* @param <E>
* @param url
* @param e The class into which the json object returned by the url fetch needs to be cast
* @param nameValuePairs Pass parameters that need to accompany the call
* @return
* @throws BitlyException
*/
public <E> E pullData(String url, Class<E> e, NameValuePair[] nameValuePairs) throws BitlyException {
// APICaller would retrieve the json string object from facebook by making a https call
// Once the json string object is obtaind, it is passed to obj transformer and the right object
// is retrieved
return JSONToObjectTransformer.getObject(caller.getData(url, nameValuePairs), e);
}

/**
* This method is useful when your json contains maps (key value pairs). Send in parameterized maps.<br>
* Example: Type type = new TypeToken<Map<String, User>>(){}.getType();
* @param <E>
* @param url
* @param type
* @param nameValuePairs
* @return
* @throws BitlyException
*/
public <E> E pullData(String url, Type type, NameValuePair[] nameValuePairs) throws BitlyException {
// APICaller would retrieve the json string object from facebook by making a https call
// Once the json string object is obtaind, it is passed to obj transformer and the right object
// is retrieved
return JSONToObjectTransformer.<E>getObject(caller.getData(url, nameValuePairs), type);
}


}
@@ -1,4 +1,4 @@
package com.bit4j.facebook;
package com.bit4j.bitly;

public class Client {

Expand Down
@@ -1,23 +1,22 @@
package com.bit4j.facebook;
package com.bit4j.bitly;

import java.io.Serializable;

public class OAuthAccessToken implements Serializable {

private static final long serialVersionUID = 359116283414855580L;

private String accessToken;
private String login;
private String apiKey;
public OAuthAccessToken(String accessToken, String login, String apiKey){

public OAuthAccessToken(String accessToken) {
this.accessToken = accessToken;
this.login = login;
this.apiKey = apiKey;
}

/**
* The access token as obtained from bitly.
* The access token as obtained from bitly.
*
* @return
*/
public String getAccessToken() {
Expand All @@ -31,7 +30,17 @@ public String getLogin() {
public String getApiKey() {
return apiKey;
}



public void setLogin(String login) {
this.login = login;
}

public void setApiKey(String apiKey) {
this.apiKey = apiKey;
}

public void setAccessToken(String accessToken) {
this.accessToken = accessToken;
}

}
35 changes: 35 additions & 0 deletions src/com/bit4j/bitly/entity/bitly/Data.java
@@ -0,0 +1,35 @@
package com.bit4j.bitly.entity.bitly;

import java.io.Serializable;

public class Data implements Serializable {

private static final long serialVersionUID = -418298335839738052L;

private String globalHash;
private String hash;
private String longUrl;
private String newHash;
private String url;

public String getGlobalHash() {
return globalHash;
}

public String getHash() {
return hash;
}

public String getLongUrl() {
return longUrl;
}

public String getNewHash() {
return newHash;
}

public String getUrl() {
return url;
}

}
26 changes: 26 additions & 0 deletions src/com/bit4j/bitly/entity/bitly/ShortURL.java
@@ -0,0 +1,26 @@
package com.bit4j.bitly.entity.bitly;

import java.io.Serializable;

public class ShortURL implements Serializable {

private static final long serialVersionUID = -3686610455246570530L;

private Data data;

private int statusCode;
private String statusTxt;

public Data getData() {
return data;
}

public int getStatusCode() {
return statusCode;
}

public String getStatusTxt() {
return statusTxt;
}

}
@@ -1,4 +1,4 @@
package com.bit4j.facebook.enums;
package com.bit4j.bitly.enums;

public enum HttpClientType {

Expand Down
@@ -1,8 +1,8 @@
package com.bit4j.facebook.exception;
package com.bit4j.bitly.exception;

import java.io.Serializable;

public class FacebookError implements Serializable {
public class BitlyError implements Serializable {

private static final long serialVersionUID = -1069090066662240990L;

Expand All @@ -11,11 +11,11 @@ public class FacebookError implements Serializable {
private RequestArg[] requestArgs;

/* Keeping a no args constructor*/
private FacebookError() {
private BitlyError() {
super();
}

public FacebookError(int errorCode, String errorMsg, RequestArg[] requestArgs) {
public BitlyError(int errorCode, String errorMsg, RequestArg[] requestArgs) {
super();
this.errorCode = errorCode;
this.errorMsg = errorMsg;
Expand Down
@@ -1,24 +1,24 @@
package com.bit4j.facebook.exception;
package com.bit4j.bitly.exception;

public class FacebookException extends Exception {
public class BitlyException extends Exception {

private static final long serialVersionUID = -3433466538055836949L;
private FacebookError error;
private BitlyError error;

public FacebookException(String msg, Exception exception) {
public BitlyException(String msg, Exception exception) {
super(msg, exception);
}

public FacebookException(FacebookError error) {
public BitlyException(BitlyError error) {
super();
this.error = error;
}

public FacebookError getError() {
public BitlyError getError() {
return error;
}

public void setError(FacebookError error) {
public void setError(BitlyError error) {
this.error = error;
}

Expand Down
@@ -1,4 +1,4 @@
package com.bit4j.facebook.exception;
package com.bit4j.bitly.exception;

import java.io.Serializable;

Expand Down
@@ -1,4 +1,4 @@
package com.bit4j.facebook.exception;
package com.bit4j.bitly.exception;

import java.io.Serializable;

Expand Down
@@ -1,4 +1,4 @@
package com.bit4j.facebook.exception;
package com.bit4j.bitly.exception;

import java.io.Serializable;

Expand Down

0 comments on commit e895c41

Please sign in to comment.