Skip to content

Commit

Permalink
Merge pull request #2 from NatriumTrioksida/master
Browse files Browse the repository at this point in the history
Add some functionality
  • Loading branch information
bane73 committed Oct 26, 2015
2 parents 56f6308 + 02dd6f4 commit a4d6b10
Show file tree
Hide file tree
Showing 4 changed files with 164 additions and 13 deletions.
Binary file added libs/commons-logging-1.1.3.jar
Binary file not shown.
Binary file added libs/httpclient-4.3.6.jar
Binary file not shown.
Binary file added libs/httpcore-4.3.3.jar
Binary file not shown.
177 changes: 164 additions & 13 deletions src/net/thegreshams/firebase4j/service/Firebase.java
Expand Up @@ -5,7 +5,12 @@
import java.io.InputStreamReader;
import java.io.Reader;
import java.io.StringWriter;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import net.thegreshams.firebase4j.error.FirebaseException;
Expand All @@ -15,14 +20,17 @@

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpDelete;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.client.methods.HttpRequestBase;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.log4j.Logger;


Expand All @@ -43,6 +51,8 @@ public class Firebase {


private final String baseUrl;
private String secureToken = null;
private List<NameValuePair> query;


public Firebase( String baseUrl ) throws FirebaseException {
Expand All @@ -53,6 +63,19 @@ public Firebase( String baseUrl ) throws FirebaseException {
throw new FirebaseException( msg );
}
this.baseUrl = baseUrl.trim();
query = new ArrayList<NameValuePair>();
LOGGER.info( "intialized with base-url: " + this.baseUrl );
}

public Firebase(String baseUrl, String secureToken) throws FirebaseException {
if( baseUrl == null || baseUrl.trim().isEmpty() ) {
String msg = "baseUrl cannot be null or empty; was: '" + baseUrl + "'";
LOGGER.error( msg );
throw new FirebaseException( msg );
}
this.secureToken = secureToken;
this.baseUrl = baseUrl.trim();
query = new ArrayList<NameValuePair>();
LOGGER.info( "intialized with base-url: " + this.baseUrl );
}

Expand All @@ -70,9 +93,10 @@ public Firebase( String baseUrl ) throws FirebaseException {
* GETs data from the base-url.
*
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse get() throws FirebaseException {
public FirebaseResponse get() throws FirebaseException, UnsupportedEncodingException {
return this.get( null );
}

Expand All @@ -81,9 +105,10 @@ public FirebaseResponse get() throws FirebaseException {
*
* @param path -- if null/empty, refers to the base-url
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse get( String path ) throws FirebaseException {
public FirebaseResponse get( String path ) throws FirebaseException, UnsupportedEncodingException {

// make the request
String url = this.buildFullUrlFromRelativePath( path );
Expand All @@ -96,17 +121,91 @@ public FirebaseResponse get( String path ) throws FirebaseException {
return response;
}

/**
* PATCHs data to the base-url
*
* @param data -- can be null/empty
* @return
* @throws {@link FirebaseException}
* @throws {@link JacksonUtilityException}
* @throws UnsupportedEncodingException
*/

public FirebaseResponse patch(Map<String, Object> data) throws FirebaseException, JacksonUtilityException, UnsupportedEncodingException {
return this.patch(null, data);
}

/**
* PATCHs data on the provided-path relative to the base-url.
*
* @param path -- if null/empty, refers to the base-url
* @param data -- can be null/empty
* @return {@link FirebaseResponse}
* @throws {@link FirebaseException}
* @throws {@link JacksonUtilityException}
* @throws UnsupportedEncodingException
*/

public FirebaseResponse patch(String path, Map<String, Object> data) throws FirebaseException, JacksonUtilityException, UnsupportedEncodingException {
// make the request
String url = this.buildFullUrlFromRelativePath( path );
//HttpPut request = new HttpPut( url );
HttpPatch request = new HttpPatch(url);
request.setEntity( this.buildEntityFromDataMap( data ) );
HttpResponse httpResponse = this.makeRequest( request );

// process the response
FirebaseResponse response = this.processResponse( FirebaseRestMethod.PATCH, httpResponse );

return response;
}

/**
*
* @param jsonData
* @return
* @throws UnsupportedEncodingException
* @throws FirebaseException
*/

public FirebaseResponse patch(String jsonData) throws UnsupportedEncodingException, FirebaseException {
return this.patch(null, jsonData);
}

/**
*
* @param path
* @param jsonData
* @return
* @throws UnsupportedEncodingException
* @throws FirebaseException
*/

public FirebaseResponse patch(String path, String jsonData) throws UnsupportedEncodingException, FirebaseException {
// make the request
String url = this.buildFullUrlFromRelativePath( path );
HttpPatch request = new HttpPatch( url );
request.setEntity( this.buildEntityFromJsonData( jsonData ) );
HttpResponse httpResponse = this.makeRequest( request );

// process the response
FirebaseResponse response = this.processResponse( FirebaseRestMethod.PATCH, httpResponse );

return response;
}

/**
* PUTs data to the base-url (ie: creates or overwrites).
* If there is already data at the base-url, this data overwrites it.
* If data is null/empty, any data existing at the base-url is deleted.
*
* @param data -- can be null/empty
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link JacksonUtilityException}
* @throws {@link FirebaseException}
*/
public FirebaseResponse put( Map<String, Object> data ) throws JacksonUtilityException, FirebaseException {
public FirebaseResponse put( Map<String, Object> data ) throws JacksonUtilityException, FirebaseException, UnsupportedEncodingException {
return this.put( null, data );
}

Expand All @@ -118,10 +217,11 @@ public FirebaseResponse put( Map<String, Object> data ) throws JacksonUtilityExc
* @param path -- if null/empty, refers to base-url
* @param data -- can be null/empty
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link JacksonUtilityException}
* @throws {@link FirebaseException}
*/
public FirebaseResponse put( String path, Map<String, Object> data ) throws JacksonUtilityException, FirebaseException {
public FirebaseResponse put( String path, Map<String, Object> data ) throws JacksonUtilityException, FirebaseException, UnsupportedEncodingException {

// make the request
String url = this.buildFullUrlFromRelativePath( path );
Expand All @@ -142,9 +242,10 @@ public FirebaseResponse put( String path, Map<String, Object> data ) throws Jack
*
* @param jsonData -- can be null/empty
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse put( String jsonData ) throws FirebaseException {
public FirebaseResponse put( String jsonData ) throws FirebaseException, UnsupportedEncodingException {
return this.put( null, jsonData );
}

Expand All @@ -156,9 +257,10 @@ public FirebaseResponse put( String jsonData ) throws FirebaseException {
* @param path -- if null/empty, refers to base-url
* @param jsonData -- can be null/empty
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse put( String path, String jsonData ) throws FirebaseException {
public FirebaseResponse put( String path, String jsonData ) throws FirebaseException, UnsupportedEncodingException {

// make the request
String url = this.buildFullUrlFromRelativePath( path );
Expand All @@ -182,10 +284,11 @@ public FirebaseResponse put( String path, String jsonData ) throws FirebaseExcep
*
* @param data -- can be null/empty but will result in no data being POSTed
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link JacksonUtilityException}
* @throws {@link FirebaseException}
*/
public FirebaseResponse post( Map<String, Object> data ) throws JacksonUtilityException, FirebaseException {
public FirebaseResponse post( Map<String, Object> data ) throws JacksonUtilityException, FirebaseException, UnsupportedEncodingException {
return this.post( null, data );
}

Expand All @@ -200,10 +303,11 @@ public FirebaseResponse post( Map<String, Object> data ) throws JacksonUtilityEx
* @param path -- if null/empty, refers to base-url
* @param data -- can be null/empty but will result in no data being POSTed
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link JacksonUtilityException}
* @throws {@link FirebaseException}
*/
public FirebaseResponse post( String path, Map<String, Object> data ) throws JacksonUtilityException, FirebaseException {
public FirebaseResponse post( String path, Map<String, Object> data ) throws JacksonUtilityException, FirebaseException, UnsupportedEncodingException {

// make the request
String url = this.buildFullUrlFromRelativePath( path );
Expand All @@ -227,9 +331,10 @@ public FirebaseResponse post( String path, Map<String, Object> data ) throws Jac
*
* @param jsonData -- can be null/empty but will result in no data being POSTed
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse post( String jsonData ) throws FirebaseException {
public FirebaseResponse post( String jsonData ) throws FirebaseException, UnsupportedEncodingException {
return this.post( null, jsonData );
}

Expand All @@ -244,9 +349,10 @@ public FirebaseResponse post( String jsonData ) throws FirebaseException {
* @param path -- if null/empty, refers to base-url
* @param jsonData -- can be null/empty but will result in no data being POSTed
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse post( String path, String jsonData ) throws FirebaseException {
public FirebaseResponse post( String path, String jsonData ) throws FirebaseException, UnsupportedEncodingException {

// make the request
String url = this.buildFullUrlFromRelativePath( path );
Expand All @@ -260,13 +366,27 @@ public FirebaseResponse post( String path, String jsonData ) throws FirebaseExce
return response;
}

/**
* Append a query to the request.
*
* @param query -- Query string based on Firebase REST API
* @param parameter -- Query parameter
* @return Firebase -- return this Firebase object
*/

public Firebase addQuery(String query, String parameter) {
this.query.add(new BasicNameValuePair(query, parameter));
return this;
}

/**
* DELETEs data from the base-url.
*
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse delete() throws FirebaseException {
public FirebaseResponse delete() throws FirebaseException, UnsupportedEncodingException {
return this.delete( null );
}

Expand All @@ -275,9 +395,10 @@ public FirebaseResponse delete() throws FirebaseException {
*
* @param path -- if null/empty, refers to the base-url
* @return {@link FirebaseResponse}
* @throws UnsupportedEncodingException
* @throws {@link FirebaseException}
*/
public FirebaseResponse delete( String path ) throws FirebaseException {
public FirebaseResponse delete( String path ) throws FirebaseException, UnsupportedEncodingException {

// make the request
String url = this.buildFullUrlFromRelativePath( path );
Expand Down Expand Up @@ -324,7 +445,7 @@ private StringEntity buildEntityFromJsonData( String jsonData ) throws FirebaseE
return result;
}

private String buildFullUrlFromRelativePath( String path ) {
private String buildFullUrlFromRelativePath( String path ) throws UnsupportedEncodingException {

// massage the path (whether it's null, empty, or not) into a full URL
if( path == null ) {
Expand All @@ -336,11 +457,36 @@ private String buildFullUrlFromRelativePath( String path ) {
}
String url = this.baseUrl + path + Firebase.FIREBASE_API_JSON_EXTENSION;

if(query != null) {
url += "?";
Iterator<NameValuePair> it = query.iterator();
NameValuePair e;
while(it.hasNext()) {
e = it.next();
url += e.getName() + "=" + URLEncoder.encode(e.getValue(), "UTF-8") + "&";
}
}

if(secureToken != null) {
if(query != null) {
url += "auth=" + secureToken;
} else {
url += "?auth=" + secureToken;
}
}

if(url.lastIndexOf("&") == url.length()) {
StringBuilder str = new StringBuilder(url);
str.deleteCharAt(str.length());
url = str.toString();
}

LOGGER.info( "built full url to '" + url + "' using relative-path of '" + path + "'" );

return url;
}


private HttpResponse makeRequest( HttpRequestBase request ) throws FirebaseException {

HttpResponse response = null;
Expand Down Expand Up @@ -403,6 +549,7 @@ private FirebaseResponse processResponse( FirebaseRestMethod method, HttpRespons
success = true;
}
break;
case PATCH:
case PUT:
case POST:
case GET:
Expand Down Expand Up @@ -457,6 +604,9 @@ private FirebaseResponse processResponse( FirebaseRestMethod method, HttpRespons
// build the response
response = new FirebaseResponse( success, code, body, writer.toString() );

//clear the query
query = null;

return response;
}

Expand All @@ -472,6 +622,7 @@ private FirebaseResponse processResponse( FirebaseRestMethod method, HttpRespons
public enum FirebaseRestMethod {

GET,
PATCH,
PUT,
POST,
DELETE;
Expand Down

0 comments on commit a4d6b10

Please sign in to comment.