Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow fetching json response from Flickr (for a few methods) #112

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Flickr4Java/.gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,4 @@ Flickr4Java.iws
.project
.settings/
bin/
**rebel.xml
13 changes: 13 additions & 0 deletions Flickr4Java/src/main/java/com/flickr4java/flickr/JSONResponse.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
package com.flickr4java.flickr;

/**
* An interface for a response object which carries json
*/
public interface JSONResponse {

/**
* Returns json as String
*/
StringBuilder getBody();

}
150 changes: 101 additions & 49 deletions Flickr4Java/src/main/java/com/flickr4java/flickr/REST.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@
package com.flickr4java.flickr;

import com.flickr4java.flickr.auth.Auth;
import com.flickr4java.flickr.json.JSONResponseImpl;
import com.flickr4java.flickr.util.Base64;
import com.flickr4java.flickr.util.DebugInputStream;
import com.flickr4java.flickr.util.IOUtilities;
import com.flickr4java.flickr.util.UrlUtilities;

import org.apache.log4j.Logger;
import org.scribe.builder.ServiceBuilder;

Expand Down Expand Up @@ -37,7 +37,7 @@

/**
* Transport implementation using the REST interface.
*
*
* @author Anthony Eden
* @version $Id: REST.java,v 1.26 2009/07/01 22:07:08 x-mago Exp $
*/
Expand Down Expand Up @@ -82,7 +82,7 @@ public REST() {

/**
* Construct a new REST transport instance using the specified host endpoint.
*
*
* @param host
* The host endpoint
*/
Expand All @@ -93,7 +93,7 @@ public REST(String host) {

/**
* Construct a new REST transport instance using the specified host and port endpoint.
*
*
* @param host
* The host endpoint
* @param port
Expand All @@ -107,7 +107,7 @@ public REST(String host, int port) {

/**
* Set a proxy for REST-requests.
*
*
* @param proxyHost
* @param proxyPort
*/
Expand All @@ -119,7 +119,7 @@ public void setProxy(String proxyHost, int proxyPort) {

/**
* Set a proxy with authentication for REST-requests.
*
*
* @param proxyHost
* @param proxyPort
* @param username
Expand All @@ -134,7 +134,7 @@ public void setProxy(String proxyHost, int proxyPort, String username, String pa

/**
* Invoke an HTTP GET request on a remote host. You must close the InputStream after you are done with.
*
*
* @param path
* The request path
* @param parameters
Expand All @@ -144,6 +144,81 @@ public void setProxy(String proxyHost, int proxyPort, String username, String pa
@Override
public com.flickr4java.flickr.Response get(String path, Map<String, Object> parameters, String apiKey, String sharedSecret) {

// OAuthRequest request = new OAuthRequest(Verb.GET, getScheme() + "://" + getHost() + path);
// for (Map.Entry<String, Object> entry : parameters.entrySet()) {
// request.addQuerystringParameter(entry.getKey(), String.valueOf(entry.getValue()));
// }
//
// if (proxyAuth) {
// request.addHeader("Proxy-Authorization", "Basic " + getProxyCredentials());
// }
//
// RequestContext requestContext = RequestContext.getRequestContext();
// Auth auth = requestContext.getAuth();
// if (auth != null) {
// Token requestToken = new Token(auth.getToken(), auth.getTokenSecret());
// OAuthService service = createOAuthService(parameters, apiKey, sharedSecret);
// service.signRequest(requestToken, request);
// } else {
// // For calls that do not require authorization e.g. flickr.people.findByUsername which could be the
// // first call if the user did not supply the user-id (i.e. nsid).
// if (!parameters.containsKey(Flickr.API_KEY)) {
// request.addQuerystringParameter(Flickr.API_KEY, apiKey);
// }
// }
//
// if (Flickr.debugRequest) {
// logger.debug("GET: " + request.getCompleteUrl());
// }
// setTimeouts(request);
// org.scribe.model.Response scribeResponse = request.send();

org.scribe.model.Response scribeResponse = getScribeResponse(path, parameters, apiKey, sharedSecret, false, false);

try {

com.flickr4java.flickr.Response response = null;
synchronized (mutex) {
String strXml = scribeResponse.getBody();
if (Flickr.debugStream) {
logger.debug(strXml);
}
if (strXml.startsWith("oauth_problem=")) {
throw new FlickrRuntimeException(strXml);
}
Document document = builder.parse(new InputSource(new StringReader(strXml)));
response = (com.flickr4java.flickr.Response) responseClass.newInstance();
response.parse(document);
}
return response;
} catch (IllegalAccessException e) {
throw new FlickrRuntimeException(e);
} catch (InstantiationException e) {
throw new FlickrRuntimeException(e);
} catch (SAXException e) {
throw new FlickrRuntimeException(e);
} catch (IOException e) {
throw new FlickrRuntimeException(e);
}
}


@Override
public JSONResponse getJson(String path, Map<String, Object> parameters, String apiKey, String sharedSecret, boolean noJsonCallback) throws FlickrException{
JSONResponse json = null;
parameters.put("format","json");
if(noJsonCallback){
parameters.put("nojsoncallback", "1");
}

org.scribe.model.Response response = getScribeResponse(path,parameters,apiKey,sharedSecret,true, true);
synchronized (mutex){
json = new JSONResponseImpl(response);
}
return json;
}

private org.scribe.model.Response getScribeResponse(String path, Map<String, Object> parameters, String apiKey, String sharedSecret, boolean isJson, boolean noJsonCallback){
OAuthRequest request = new OAuthRequest(Verb.GET, getScheme() + "://" + getHost() + path);
for (Map.Entry<String, Object> entry : parameters.entrySet()) {
request.addQuerystringParameter(entry.getKey(), String.valueOf(entry.getValue()));
Expand Down Expand Up @@ -172,39 +247,16 @@ public com.flickr4java.flickr.Response get(String path, Map<String, Object> para
}
setTimeouts(request);
org.scribe.model.Response scribeResponse = request.send();
return scribeResponse;
}

try {

com.flickr4java.flickr.Response response = null;
synchronized (mutex) {
String strXml = scribeResponse.getBody();
if (Flickr.debugStream) {
logger.debug(strXml);
}
if (strXml.startsWith("oauth_problem=")) {
throw new FlickrRuntimeException(strXml);
}
Document document = builder.parse(new InputSource(new StringReader(strXml)));
response = (com.flickr4java.flickr.Response) responseClass.newInstance();
response.parse(document);
}
return response;
} catch (IllegalAccessException e) {
throw new FlickrRuntimeException(e);
} catch (InstantiationException e) {
throw new FlickrRuntimeException(e);
} catch (SAXException e) {
throw new FlickrRuntimeException(e);
} catch (IOException e) {
throw new FlickrRuntimeException(e);
}
}

/**
* Invoke a non OAuth HTTP GET request on a remote host.
*
*
* This is only used for the Flickr OAuth methods checkToken and getAccessToken.
*
*
* @param path
* The request path
* @param parameters
Expand Down Expand Up @@ -255,7 +307,7 @@ public Response getNonOAuth(String path, Map<String, String> parameters) {

/**
* Invoke an HTTP POST request on a remote host.
*
*
* @param path
* The request path
* @param parameters
Expand Down Expand Up @@ -324,7 +376,7 @@ public com.flickr4java.flickr.Response post(String path, Map<String, Object> par
}

/**
*
*
* @param parameters
* @param sharedSecret
* @return
Expand All @@ -339,7 +391,7 @@ private OAuthService createOAuthService(Map<String, Object> parameters, String a
}

/**
*
*
* @param parameters
* @param request
*/
Expand All @@ -350,7 +402,7 @@ private void buildNormalPostRequest(Map<String, Object> parameters, OAuthRequest
}

/**
*
*
* @param parameters
* @param request
*/
Expand All @@ -365,7 +417,7 @@ private void buildMultipartRequest(Map<String, Object> parameters, OAuthRequest
}

/**
*
*
* @return
*/
private String getMultipartBoundary() {
Expand All @@ -378,7 +430,7 @@ public boolean isProxyAuth() {

/**
* Generates Base64-encoded credentials from locally stored username and password.
*
*
* @return credentials
*/
public String getProxyCredentials() {
Expand All @@ -389,19 +441,19 @@ private byte[] buildMultipartBody(Map<String, Object> parameters, String boundar

ByteArrayOutputStream buffer = new ByteArrayOutputStream();
try {
String filename = (String) parameters.get("filename");
if(filename == null)
filename = "image.jpg";

String fileMimeType = (String) parameters.get("filemimetype");
if(fileMimeType == null)
fileMimeType = "image/jpeg";
String filename = (String) parameters.get("filename");
if(filename == null)
filename = "image.jpg";

String fileMimeType = (String) parameters.get("filemimetype");
if(fileMimeType == null)
fileMimeType = "image/jpeg";

buffer.write(("--" + boundary + "\r\n").getBytes(CHARSET_NAME));
for (Entry<String, Object> entry : parameters.entrySet()) {
String key = entry.getKey();
if(!key.equals("filename") && !key.equals("filemimetype"))
writeParam(key, entry.getValue(), buffer, boundary, filename, fileMimeType);
writeParam(key, entry.getValue(), buffer, boundary, filename, fileMimeType);
}
} catch (IOException e) {
throw new FlickrRuntimeException(e);
Expand Down
35 changes: 27 additions & 8 deletions Flickr4Java/src/main/java/com/flickr4java/flickr/Transport.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@
/**
* The abstract Transport class provides a common interface for transporting requests to the Flickr servers. Flickr offers several transport methods including
* REST, SOAP and XML-RPC. Flickr4Java currently implements the REST transport and work is being done to include the SOAP transport.
*
*
* @author Matt Ray
* @author Anthony Eden
*/
Expand All @@ -26,7 +26,7 @@ public abstract class Transport {
public static final String UPLOAD_API_HOST = "up.flickr.com";

protected static final String DEFAULT_SCHEME = "https";

private String transportType;

protected Class<?> responseClass;
Expand All @@ -36,7 +36,7 @@ public abstract class Transport {
private String host;

private int port = 443;

private String scheme;

public String getHost() {
Expand Down Expand Up @@ -81,7 +81,7 @@ public void setScheme(String scheme) {

/**
* Invoke an HTTP GET request on a remote host. You must close the InputStream after you are done with.
*
*
* @param path
* The request path
* @param parameters
Expand All @@ -93,9 +93,28 @@ public void setScheme(String scheme) {
*/
public abstract Response get(String path, Map<String, Object> parameters, String apiKey, String sharedSecret) throws FlickrException;

/**
* Invoke an HTTP GET request on a remote host. You must close the InputStream after you are done with.
* While #get request for XML which is further processed to marshalize flickr-objects,
* this method makes sure the response contaisn json which could be passed to a client (JavaScript available).
*
* @param path
* The request path
* @param parameters
* The parameters (collection of Parameter objects)
* @param apiKey
* @param sharedSecret
* @return The Response
* @throws FlickrException
*/
public abstract JSONResponse getJson(String path, Map<String, Object> parameters, String apiKey, String sharedSecret,boolean noJsonCallback) throws FlickrException;




/**
* Invoke an HTTP POST request on a remote host.
*
*
* @param path
* The request path
* @param parameters
Expand All @@ -109,7 +128,7 @@ public void setScheme(String scheme) {

/**
* Invoke an HTTP POST request on a remote host.
*
*
* @param path
* The request path
* @param parameters
Expand All @@ -126,9 +145,9 @@ public Response post(String path, Map<String, Object> parameters, String apiKey,

/**
* Invoke a non OAuth HTTP GET request on a remote host.
*
*
* This is only used for the Flickr OAuth methods checkToken and getAccessToken.
*
*
* @param path
* The request path
* @param parameters
Expand Down
Loading