diff --git a/LICENSE-apache.txt b/LICENSE-apache.txt deleted file mode 100644 index 183b52c0..00000000 --- a/LICENSE-apache.txt +++ /dev/null @@ -1,56 +0,0 @@ -/* - * The Apache Software License, Version 1.1 - * - * - * Copyright (c) 1999 The Apache Software Foundation. All rights - * reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. - * - * 4. The names "Xerces" and "Apache Software Foundation" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache", - * nor may "Apache" appear in their name, without prior written - * permission of the Apache Software Foundation. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation and was - * originally based on software copyright (c) 1999, International - * Business Machines, Inc., http://www.ibm.com. For more - * information on the Apache Software Foundation, please see - * . - */ diff --git a/LICENSE.txt b/LICENSE.txt index bbe3905f..82b96c85 100644 --- a/LICENSE.txt +++ b/LICENSE.txt @@ -1,45 +1,25 @@ -/*-- - - Copyright (C) 2005 Aetrion LLC. - All rights reserved. - - Redistribution and use in source and binary forms, with or without - modification, are permitted provided that the following conditions - are met: - - 1. Redistributions of source code must retain the above copyright - notice, this list of conditions, and the following disclaimer. - - 2. Redistributions in binary form must reproduce the above copyright - notice, this list of conditions, and the disclaimer that follows - these conditions in the documentation and/or other materials - provided with the distribution. - - 3. The name "flickrj" must not be used to endorse or promote products - derived from this software without prior written permission. For - written permission, please contact me@anthonyeden.com. - - 4. Products derived from this software may not be called "flickrj", nor - may "flickrj" appear in their name, without prior written permission - from Aetrion LLC (license@aetrion.com). - - In addition, I request (but do not require) that you include in the - end-user documentation provided with the redistribution and/or in the - software itself an acknowledgement equivalent to the following: - "This product includes software developed by Aetrion LLC." - - THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - DISCLAIMED. IN NO EVENT SHALL THE AUTHOR(S) BE LIABLE FOR ANY DIRECT, - INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES - (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR - SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) - HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, - STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING - IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE - POSSIBILITY OF SUCH DAMAGE. - - For more information on flickrj, please see . - - */ \ No newline at end of file +BSD 2-Clause License + +Copyright (c) 2004, Anthony Eden (@aeden), Darren Greaves (@boncey), Allan (@callmeal) and contributors. +All rights reserved. + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" +AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR +SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER +CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, +OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. \ No newline at end of file diff --git a/README.md b/README.md index dde7936b..9910c01a 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,7 @@ __Note:__ This API has been forked from [FlickrJ at Sourceforge](http://flickrj. This is a Java API which wraps the [REST-based Flickr API](http://www.flickr.com/services/api/). -Comments and questions should be sent to the [GitHub Repo](https://github.com/callmeal/Flickr4Java). +Comments and questions should be raised on the [GitHub Repo issue tracker](https://github.com/boncey/Flickr4Java/issues). ### Usage @@ -18,13 +18,8 @@ For example, to send a test ping to the Flickr service: Flickr f = new Flickr(apiKey, sharedSecret, new REST()); TestInterface testInterface = f.getTestInterface(); Collection results = testInterface.echo(Collections.EMPTY_MAP); - -__Please note:__ this library is not thread safe. - -###Setup for gradle -```gradle - compile 'com.aetrion.flickr:flickrapi:1.1' - ``` + +See `/src/examples/java` for more. ### Requirements @@ -34,23 +29,27 @@ An API key is required to use this API. You can [request one on Flickr](http:// #### Required libraries -- [scribe-java (v.1.3.2 onwards)](https://github.com/fernandezpablo85/scribe-java/wiki/Getting-Started) (required for the OAuth functionality). -- [log4j](http://www.apache.org/dyn/closer.cgi/logging/log4j/1.2.17/log4j-1.2.17.zip) (runtime dependency for logging) +- [scribejava-api (v 6.2.0 onwards)](https://github.com/scribejava/scribejava) (required for the OAuth functionality). +- [slf4j](https://www.slf4j.org) (runtime dependency for logging) ### Download [Download the latest version from bintray](https://bintray.com/boncey/Flickr4Java/Flickr4Java). +### Gradle + + compile 'com.flickr4java:flickr4java:3.0.0' + ### Maven com.flickr4java flickr4java - 2.19 + 3.0.0 -Flickr4Java is now available on Maven Central so the above settings should be all you need (it used to only be available on [JCenter](https://bintray.com/bintray/jcenter). +Flickr4Java is available on Maven Central so the above settings should be all you need. ### Testing The tests now run against captured responses from Flickr (see `src/test/resources/payloads`) and don't contact the Flickr API at all. diff --git a/build.gradle b/build.gradle index b831994d..d761cb7c 100644 --- a/build.gradle +++ b/build.gradle @@ -4,7 +4,7 @@ apply plugin: 'idea' apply plugin: 'maven' apply plugin: 'application' -version = '2.19' +version = '3.0.0' group = 'com.flickr4java.flickr' sourceCompatibility = '1.5' @@ -27,9 +27,8 @@ sourceSets { } dependencies { - compile 'org.scribe:scribe:1.3.3' - compile 'axis:axis:1.4' - compile 'log4j:log4j:1.2.17' + compile 'com.github.scribejava:scribejava-apis:6.2.0' + compile 'org.slf4j:slf4j-api:1.7.25' testCompile group: 'junit', name: 'junit', version: '4.11' } diff --git a/pom.xml b/pom.xml index bd36ceda..7f96718a 100644 --- a/pom.xml +++ b/pom.xml @@ -4,7 +4,7 @@ com.flickr4java flickr4java - 2.20-SNAPSHOT + 3.0.0-SNAPSHOT jar flickr4java Java API For Flickr. Fork of FlickrJ. @@ -16,14 +16,6 @@ 7 - - - Apache License, Version 1.1 - http://www.apache.org/licenses/LICENSE-1.1.txt - repo - - - boncey @@ -45,19 +37,26 @@ 1.8 4.11 1.7.25 + 6.2.0 - org.scribe - scribe - 1.3.5 + com.github.scribejava + scribejava-apis + ${scribe.version} org.slf4j slf4j-api ${slf4j.version} + + org.slf4j + slf4j-simple + ${slf4j.version} + test + junit junit diff --git a/src/examples/java/AuthExample.java b/src/examples/java/AuthExample.java index 226a5151..b76802bb 100644 --- a/src/examples/java/AuthExample.java +++ b/src/examples/java/AuthExample.java @@ -5,14 +5,14 @@ import com.flickr4java.flickr.auth.AuthInterface; import com.flickr4java.flickr.auth.Permission; import com.flickr4java.flickr.util.IOUtilities; - -import org.scribe.model.Token; -import org.scribe.model.Verifier; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuth1Token; import java.io.IOException; import java.io.InputStream; import java.util.Properties; import java.util.Scanner; +import java.util.concurrent.ExecutionException; /** * Demonstrates the authentication-process. @@ -26,7 +26,7 @@ */ public class AuthExample { - public static void auth() throws IOException, FlickrException { + public static void auth() throws IOException, FlickrException, ExecutionException, InterruptedException { Properties properties; InputStream in = null; try { @@ -43,10 +43,10 @@ public static void auth() throws IOException, FlickrException { Scanner scanner = new Scanner(System.in); - Token token = authInterface.getRequestToken(); - System.out.println("token: " + token); + OAuth1RequestToken requestToken = authInterface.getRequestToken(); + System.out.println("token: " + requestToken); - String url = authInterface.getAuthorizationUrl(token, Permission.DELETE); + String url = authInterface.getAuthorizationUrl(requestToken, Permission.DELETE); System.out.println("Follow this URL to authorise yourself on Flickr"); System.out.println(url); System.out.println("Paste in the token it gives you:"); @@ -55,14 +55,14 @@ public static void auth() throws IOException, FlickrException { String tokenKey = scanner.nextLine(); scanner.close(); - Token requestToken = authInterface.getAccessToken(token, new Verifier(tokenKey)); + OAuth1Token accessToken = authInterface.getAccessToken(requestToken, tokenKey); System.out.println("Authentication success"); - Auth auth = authInterface.checkToken(requestToken); + Auth auth = authInterface.checkToken(accessToken); // This token can be used until the user revokes it. - System.out.println("Token: " + requestToken.getToken()); - System.out.println("Secret: " + requestToken.getSecret()); + System.out.println("Token: " + accessToken.getToken()); + System.out.println("Secret: " + accessToken.getTokenSecret()); System.out.println("nsid: " + auth.getUser().getId()); System.out.println("Realname: " + auth.getUser().getRealName()); System.out.println("Username: " + auth.getUser().getUsername()); diff --git a/src/examples/java/Backup.java b/src/examples/java/Backup.java index cf888ebe..e86f418f 100644 --- a/src/examples/java/Backup.java +++ b/src/examples/java/Backup.java @@ -13,10 +13,8 @@ import com.flickr4java.flickr.photosets.PhotosetsInterface; import com.flickr4java.flickr.util.AuthStore; import com.flickr4java.flickr.util.FileAuthStore; - -import org.scribe.model.Token; -import org.scribe.model.Verifier; -import org.xml.sax.SAXException; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuth1Token; import java.io.BufferedInputStream; import java.io.File; @@ -57,11 +55,11 @@ public Backup(String apiKey, String nsid, String sharedSecret, File authsDir) th } } - private void authorize() throws IOException, SAXException, FlickrException { + private void authorize() throws IOException, FlickrException { AuthInterface authInterface = flickr.getAuthInterface(); - Token accessToken = authInterface.getRequestToken(); + OAuth1RequestToken requestToken = authInterface.getRequestToken(); - String url = authInterface.getAuthorizationUrl(accessToken, Permission.READ); + String url = authInterface.getAuthorizationUrl(requestToken, Permission.READ); System.out.println("Follow this URL to authorise yourself on Flickr"); System.out.println(url); System.out.println("Paste in the token it gives you:"); @@ -69,9 +67,9 @@ private void authorize() throws IOException, SAXException, FlickrException { String tokenKey = new Scanner(System.in).nextLine(); - Token requestToken = authInterface.getAccessToken(accessToken, new Verifier(tokenKey)); + OAuth1Token accessToken = authInterface.getAccessToken(requestToken, tokenKey); - Auth auth = authInterface.checkToken(requestToken); + Auth auth = authInterface.checkToken(accessToken); RequestContext.getRequestContext().setAuth(auth); this.authStore.store(auth); System.out.println("Thanks. You probably will not have to do this every time. Now starting backup."); diff --git a/src/examples/java/UploadPhoto.java b/src/examples/java/UploadPhoto.java index a1ccb88b..bd1181c9 100644 --- a/src/examples/java/UploadPhoto.java +++ b/src/examples/java/UploadPhoto.java @@ -18,11 +18,10 @@ import com.flickr4java.flickr.uploader.Uploader; import com.flickr4java.flickr.util.AuthStore; import com.flickr4java.flickr.util.FileAuthStore; -import org.scribe.model.Token; -import org.scribe.model.Verifier; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuth1Token; import org.slf4j.Logger; import org.slf4j.LoggerFactory; -import org.xml.sax.SAXException; import java.io.File; import java.io.FilenameFilter; @@ -163,9 +162,9 @@ private void setNsid() throws FlickrException { } } - private void authorize() throws IOException, SAXException, FlickrException { + private void authorize() throws IOException, FlickrException { AuthInterface authInterface = flickr.getAuthInterface(); - Token accessToken = authInterface.getRequestToken(); + OAuth1RequestToken accessToken = authInterface.getRequestToken(); // Try with DELETE permission. At least need write permission for upload and add-to-set. String url = authInterface.getAuthorizationUrl(accessToken, Permission.DELETE); @@ -177,7 +176,7 @@ private void authorize() throws IOException, SAXException, FlickrException { Scanner scanner = new Scanner(System.in); String tokenKey = scanner.nextLine(); - Token requestToken = authInterface.getAccessToken(accessToken, new Verifier(tokenKey)); + OAuth1Token requestToken = authInterface.getAccessToken(accessToken, tokenKey); Auth auth = authInterface.checkToken(requestToken); RequestContext.getRequestContext().setAuth(auth); @@ -217,7 +216,7 @@ private Auth constructAuth(String authToken, String tokenSecret, String username return auth; } - public void setAuth(String authToken, String username, String tokenSecret) throws IOException, SAXException, FlickrException { + public void setAuth(String authToken, String username, String tokenSecret) throws IOException, FlickrException { RequestContext rc = RequestContext.getRequestContext(); Auth auth = null; diff --git a/src/main/java/com/flickr4java/flickr/Flickr.java b/src/main/java/com/flickr4java/flickr/Flickr.java index 4daf8c78..ad4c2a17 100644 --- a/src/main/java/com/flickr4java/flickr/Flickr.java +++ b/src/main/java/com/flickr4java/flickr/Flickr.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; import com.flickr4java.flickr.activity.ActivityInterface; diff --git a/src/main/java/com/flickr4java/flickr/FlickrException.java b/src/main/java/com/flickr4java/flickr/FlickrException.java index 40a2b243..56d13c43 100644 --- a/src/main/java/com/flickr4java/flickr/FlickrException.java +++ b/src/main/java/com/flickr4java/flickr/FlickrException.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; /** diff --git a/src/main/java/com/flickr4java/flickr/Parameter.java b/src/main/java/com/flickr4java/flickr/Parameter.java index 54dd321f..7d0b62d3 100644 --- a/src/main/java/com/flickr4java/flickr/Parameter.java +++ b/src/main/java/com/flickr4java/flickr/Parameter.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; /** diff --git a/src/main/java/com/flickr4java/flickr/REST.java b/src/main/java/com/flickr4java/flickr/REST.java index f42beabd..5734d240 100644 --- a/src/main/java/com/flickr4java/flickr/REST.java +++ b/src/main/java/com/flickr4java/flickr/REST.java @@ -1,19 +1,19 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; import com.flickr4java.flickr.auth.Auth; -import com.flickr4java.flickr.util.Base64; +import com.flickr4java.flickr.uploader.Payload; +import com.flickr4java.flickr.uploader.UploadMetaData; import com.flickr4java.flickr.util.DebugInputStream; import com.flickr4java.flickr.util.IOUtilities; import com.flickr4java.flickr.util.UrlUtilities; -import org.scribe.builder.ServiceBuilder; -import org.scribe.builder.api.FlickrApi; -import org.scribe.model.OAuthRequest; -import org.scribe.model.Token; -import org.scribe.model.Verb; -import org.scribe.oauth.OAuthService; +import com.github.scribejava.apis.FlickrApi; +import com.github.scribejava.core.builder.ServiceBuilder; +import com.github.scribejava.core.httpclient.jdk.JDKHttpClientConfig; +import com.github.scribejava.core.model.OAuth1AccessToken; +import com.github.scribejava.core.model.OAuthRequest; +import com.github.scribejava.core.model.Verb; +import com.github.scribejava.core.oauth.OAuth10aService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Document; @@ -23,19 +23,20 @@ import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; -import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; import java.io.StringReader; import java.net.HttpURLConnection; import java.net.URL; +import java.util.Base64; +import java.util.HashMap; import java.util.Map; -import java.util.Map.Entry; -import java.util.concurrent.TimeUnit; +import java.util.UUID; +import java.util.concurrent.ExecutionException; /** * 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 $ */ @@ -43,9 +44,7 @@ public class REST extends Transport { private static final Logger logger = LoggerFactory.getLogger(REST.class); - public static final String PATH = "/services/rest/"; - - private static final String CHARSET_NAME = "UTF-8"; + private static final String PATH = "/services/rest/"; private boolean proxyAuth = false; @@ -53,10 +52,6 @@ public class REST extends Transport { private String proxyPassword = ""; - private final DocumentBuilder builder; - - private static Object mutex = new Object(); - private Integer connectTimeoutMs; private Integer readTimeoutMs; @@ -70,19 +65,12 @@ public REST() { setPath(PATH); setScheme(DEFAULT_SCHEME); setResponseClass(RESTResponse.class); - DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); - try { - builder = builderFactory.newDocumentBuilder(); - } catch (ParserConfigurationException e) { - throw new FlickrRuntimeException(e); - } } /** * Construct a new REST transport instance using the specified host endpoint. - * - * @param host - * The host endpoint + * + * @param host The host endpoint */ public REST(String host) { this(); @@ -91,11 +79,9 @@ public REST(String host) { /** * Construct a new REST transport instance using the specified host and port endpoint. - * - * @param host - * The host endpoint - * @param port - * The port + * + * @param host The host endpoint + * @param port The port */ public REST(String host, int port) { this(); @@ -105,7 +91,7 @@ public REST(String host, int port) { /** * Set a proxy for REST-requests. - * + * * @param proxyHost * @param proxyPort */ @@ -119,7 +105,7 @@ public void setProxy(String proxyHost, int proxyPort) { /** * Set a proxy with authentication for REST-requests. - * + * * @param proxyHost * @param proxyPort * @param username @@ -134,17 +120,15 @@ 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 - * The parameters (collection of Parameter objects) + * + * @param path The request path + * @param parameters The parameters (collection of Parameter objects) * @return The Response */ @Override - public com.flickr4java.flickr.Response get(String path, Map parameters, String apiKey, String sharedSecret) throws FlickrException { + public com.flickr4java.flickr.Response get(String path, Map parameters, String apiKey, String sharedSecret) { - OAuthRequest request = new OAuthRequest(Verb.GET, getScheme() + "://" + getHost() + path); + OAuthRequest request = new OAuthRequest(Verb.GET, buildUrl(path)); for (Map.Entry entry : parameters.entrySet()) { request.addQuerystringParameter(entry.getKey(), String.valueOf(entry.getValue())); } @@ -155,9 +139,9 @@ public com.flickr4java.flickr.Response get(String path, Map para RequestContext requestContext = RequestContext.getRequestContext(); Auth auth = requestContext.getAuth(); + OAuth10aService service = createOAuthService(apiKey, sharedSecret); if (auth != null) { - Token requestToken = new Token(auth.getToken(), auth.getTokenSecret()); - OAuthService service = createOAuthService(parameters, apiKey, sharedSecret); + OAuth1AccessToken requestToken = new OAuth1AccessToken(auth.getToken(), auth.getTokenSecret()); service.signRequest(requestToken, request); } else { // For calls that do not require authorization e.g. flickr.people.findByUsername which could be the @@ -170,123 +154,78 @@ public com.flickr4java.flickr.Response get(String path, Map para if (Flickr.debugRequest) { logger.debug("GET: " + request.getCompleteUrl()); } - setTimeouts(request); - org.scribe.model.Response scribeResponse = request.send(); try { - - com.flickr4java.flickr.Response response = null; - synchronized (mutex) { - String strXml = scribeResponse.getBody().trim(); - 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) { + return handleResponse(request, service); + } catch (IllegalAccessException | InstantiationException | SAXException | IOException | InterruptedException | ExecutionException | ParserConfigurationException 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 - * The parameters - * @return The Response + * Invoke an HTTP POST request on a remote host. + * + * @param path The request path + * @param parameters The parameters (collection of Parameter objects) + * @return The Response object */ @Override - public Response getNonOAuth(String path, Map parameters) throws FlickrException { - InputStream in = null; - try { - URL url = UrlUtilities.buildUrl(getScheme(), getHost(), getPort(), path, parameters); - if (Flickr.debugRequest) { - logger.debug("GET: " + url); - } - HttpURLConnection conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - if (proxyAuth) { - conn.setRequestProperty("Proxy-Authorization", "Basic " + getProxyCredentials()); - } - setTimeouts(conn); - conn.connect(); + public com.flickr4java.flickr.Response post(String path, Map parameters, String apiKey, String sharedSecret) { - if (Flickr.debugStream) { - in = new DebugInputStream(conn.getInputStream(), System.out); - } else { - in = conn.getInputStream(); - } + OAuthRequest request = new OAuthRequest(Verb.POST, buildUrl(path)); - Response response = null; - synchronized (mutex) { - Document document = builder.parse(in); - response = (Response) responseClass.newInstance(); - response.parse(document); - } - return response; - } catch (IllegalAccessException e) { - throw new FlickrRuntimeException(e); - } catch (InstantiationException e) { - throw new FlickrRuntimeException(e); - } catch (IOException e) { - throw new FlickrRuntimeException(e); - } catch (SAXException e) { + buildNormalPostRequest(parameters, request); + + OAuth10aService service = createAndSignRequest(apiKey, sharedSecret, request); + + try { + return handleResponse(request, service); + } catch (IllegalAccessException | InterruptedException | ExecutionException | InstantiationException | IOException | SAXException | ParserConfigurationException e) { throw new FlickrRuntimeException(e); - } finally { - IOUtilities.close(in); } } /** * Invoke an HTTP POST request on a remote host. - * - * @param path - * The request path - * @param parameters - * The parameters (collection of Parameter objects) + * + * @param path The request path + * @param metaData The parameters (collection of Parameter objects) + * @param payload * @return The Response object */ @Override - public com.flickr4java.flickr.Response post(String path, Map parameters, String apiKey, String sharedSecret, boolean multipart) throws FlickrException { + public com.flickr4java.flickr.Response postMultiPart(String path, UploadMetaData metaData, Payload payload, String apiKey, String sharedSecret) { - OAuthRequest request = new OAuthRequest(Verb.POST, getScheme() + "://" + getHost() + path); + OAuthRequest request = new OAuthRequest(Verb.POST, buildUrl(path)); + Map uploadParameters = new HashMap<>(metaData.getUploadParameters()); - if (multipart) { - buildMultipartRequest(parameters, request); - } else { - buildNormalPostRequest(parameters, request); + buildMultipartRequest(uploadParameters, request); + + OAuth10aService service = createAndSignRequest(apiKey, sharedSecret, request); + + // Ensure all parameters (including oauth) are added to payload so signature matches + uploadParameters.putAll(request.getOauthParameters()); + + request.addMultipartPayload(String.format("form-data; name=\"photo\"; filename=\"%s\"", metaData.getFilename()), metaData.getFilemimetype(), payload.getPayload()); + uploadParameters.entrySet().forEach(e -> + request.addMultipartPayload(String.format("form-data; name=\"%s\"", e.getKey()), null, e.getValue().getBytes())); + + try { + return handleResponse(request, service); + } catch (IllegalAccessException | InterruptedException | ExecutionException | InstantiationException | IOException | SAXException | ParserConfigurationException e) { + throw new FlickrRuntimeException(e); } + } + private OAuth10aService createAndSignRequest(String apiKey, String sharedSecret, OAuthRequest request) { RequestContext requestContext = RequestContext.getRequestContext(); Auth auth = requestContext.getAuth(); + OAuth10aService service = createOAuthService(apiKey, sharedSecret); if (auth != null) { - Token requestToken = new Token(auth.getToken(), auth.getTokenSecret()); - OAuthService service = createOAuthService(parameters, apiKey, sharedSecret); + OAuth1AccessToken requestToken = new OAuth1AccessToken(auth.getToken(), auth.getTokenSecret()); service.signRequest(requestToken, request); } - if (multipart) { - // Ensure all parameters (including oauth) are added to payload so signature matches - parameters.putAll(request.getOauthParameters()); - request.addPayload(buildMultipartBody(parameters, getMultipartBoundary())); - } - if (proxyAuth) { request.addHeader("Proxy-Authorization", "Basic " + getProxyCredentials()); } @@ -295,51 +234,100 @@ public com.flickr4java.flickr.Response post(String path, Map par logger.debug("POST: " + request.getCompleteUrl()); } - org.scribe.model.Response scribeResponse = request.send(); + return service; + } + + private String buildUrl(String path) { + return String.format("%s://%s%s", getScheme(), getHost(), path); + } + + private Response handleResponse(OAuthRequest request, OAuth10aService service) throws InterruptedException, ExecutionException, IOException, SAXException, InstantiationException, IllegalAccessException, ParserConfigurationException { + com.github.scribejava.core.model.Response scribeResponse = service.execute(request); + + Response f4jResponse; + String strXml = scribeResponse.getBody().trim(); + if (Flickr.debugStream) { + logger.debug(strXml); + } + if (strXml.startsWith("oauth_problem=")) { + throw new FlickrRuntimeException(strXml); + } + + DocumentBuilder builder = getDocumentBuilder(); + Document document = builder.parse(new InputSource(new StringReader(strXml))); + f4jResponse = (Response) responseClass.newInstance(); + f4jResponse.parse(document); + return f4jResponse; + } + + /** + * 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 The parameters + * @return The Response + */ + @Override + public Response getNonOAuth(String path, Map parameters) { + InputStream in = null; try { - com.flickr4java.flickr.Response response = null; - synchronized (mutex) { - String strXml = scribeResponse.getBody().trim(); - 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); + URL url = UrlUtilities.buildUrl(getScheme(), getHost(), getPort(), path, parameters); + if (Flickr.debugRequest) { + logger.debug("GET: " + url); } + HttpURLConnection conn = (HttpURLConnection) url.openConnection(); + conn.setRequestMethod("GET"); + if (proxyAuth) { + conn.setRequestProperty("Proxy-Authorization", "Basic " + getProxyCredentials()); + } + setTimeouts(conn); + conn.connect(); + + if (Flickr.debugStream) { + in = new DebugInputStream(conn.getInputStream(), System.out); + } else { + in = conn.getInputStream(); + } + + Response response; + DocumentBuilder builder = getDocumentBuilder(); + Document document = builder.parse(in); + response = (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) { + } catch (IllegalAccessException | SAXException | IOException | InstantiationException | ParserConfigurationException e) { throw new FlickrRuntimeException(e); + } finally { + IOUtilities.close(in); } } /** - * - * @param parameters * @param sharedSecret * @return */ - private OAuthService createOAuthService(Map parameters, String apiKey, String sharedSecret) { - ServiceBuilder serviceBuilder = new ServiceBuilder().provider(FlickrApi.class).apiKey(apiKey).apiSecret(sharedSecret); + private OAuth10aService createOAuthService(String apiKey, String sharedSecret) { + JDKHttpClientConfig config = JDKHttpClientConfig.defaultConfig(); + if (connectTimeoutMs != null) { + config.setConnectTimeout(connectTimeoutMs); + } + if (readTimeoutMs != null) { + config.setReadTimeout(readTimeoutMs); + } + ServiceBuilder serviceBuilder = new ServiceBuilder(apiKey).apiKey(apiKey).apiSecret(sharedSecret).httpClientConfig(config); + if (Flickr.debugRequest) { serviceBuilder = serviceBuilder.debug(); } - return serviceBuilder.build(); + return serviceBuilder.build(FlickrApi.instance()); } /** - * * @param parameters * @param request */ @@ -350,26 +338,21 @@ private void buildNormalPostRequest(Map parameters, OAuthRequest } /** - * * @param parameters * @param request */ - private void buildMultipartRequest(Map parameters, OAuthRequest request) { - request.addHeader("Content-Type", "multipart/form-data; boundary=" + getMultipartBoundary()); - for (Map.Entry entry : parameters.entrySet()) { - String key = entry.getKey(); - if (!key.equals("photo") && !key.equals("filename") && !key.equals("filemimetype")) { - request.addQuerystringParameter(key, String.valueOf(entry.getValue())); - } + private void buildMultipartRequest(Map parameters, OAuthRequest request) { + String multipartBoundary = getMultipartBoundary(); + request.initMultipartBoundary(multipartBoundary); + + request.addHeader("Content-Type", "multipart/form-data; boundary=" + multipartBoundary); + for (Map.Entry entry : parameters.entrySet()) { + request.addQuerystringParameter(entry.getKey(), entry.getValue()); } } - /** - * - * @return - */ private String getMultipartBoundary() { - return "---------------------------7d273f7a0d3"; + return "---------------------------" + UUID.randomUUID(); } public boolean isProxyAuth() { @@ -378,64 +361,11 @@ public boolean isProxyAuth() { /** * Generates Base64-encoded credentials from locally stored username and password. - * + * * @return credentials */ public String getProxyCredentials() { - return new String(Base64.encode((proxyUser + ":" + proxyPassword).getBytes())); - } - - private byte[] buildMultipartBody(Map parameters, String boundary) { - - 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"; - - buffer.write(("--" + boundary + "\r\n").getBytes(CHARSET_NAME)); - for (Entry entry : parameters.entrySet()) { - String key = entry.getKey(); - if (!key.equals("filename") && !key.equals("filemimetype")) - writeParam(key, entry.getValue(), buffer, boundary, filename, fileMimeType); - } - } catch (IOException e) { - throw new FlickrRuntimeException(e); - } - - if (Flickr.debugRequest) { - String output = new String(buffer.toByteArray()); - logger.debug("Multipart body:\n" + output); - } - return buffer.toByteArray(); - } - - private void writeParam(String name, Object value, ByteArrayOutputStream buffer, String boundary, String filename, String fileMimeType) throws IOException { - if (value instanceof InputStream) { - buffer.write(("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\";\r\n").getBytes(CHARSET_NAME)); - buffer.write(("Content-Type: " + fileMimeType + "\r\n\r\n").getBytes(CHARSET_NAME)); - InputStream in = (InputStream) value; - byte[] buf = new byte[512]; - - int res = -1; - while ((res = in.read(buf)) != -1) { - buffer.write(buf, 0, res); - } - buffer.write(("\r\n" + "--" + boundary + "\r\n").getBytes(CHARSET_NAME)); - } else if (value instanceof byte[]) { - buffer.write(("Content-Disposition: form-data; name=\"" + name + "\"; filename=\"" + filename + "\";\r\n").getBytes(CHARSET_NAME)); - buffer.write(("Content-Type: " + fileMimeType + "\r\n\r\n").getBytes(CHARSET_NAME)); - buffer.write((byte[]) value); - buffer.write(("\r\n" + "--" + boundary + "\r\n").getBytes(CHARSET_NAME)); - } else { - buffer.write(("Content-Disposition: form-data; name=\"" + name + "\"\r\n\r\n").getBytes(CHARSET_NAME)); - buffer.write(((String) value).getBytes(CHARSET_NAME)); - buffer.write(("\r\n" + "--" + boundary + "\r\n").getBytes(CHARSET_NAME)); - } + return new String(Base64.getEncoder().encode((proxyUser + ":" + proxyPassword).getBytes())); } private void setTimeouts(HttpURLConnection conn) { @@ -447,15 +377,6 @@ private void setTimeouts(HttpURLConnection conn) { } } - private void setTimeouts(OAuthRequest request) { - if (connectTimeoutMs != null) { - request.setConnectTimeout(connectTimeoutMs, TimeUnit.MILLISECONDS); - } - if (readTimeoutMs != null) { - request.setReadTimeout(readTimeoutMs, TimeUnit.MILLISECONDS); - } - } - public void setConnectTimeoutMs(Integer connectTimeoutMs) { this.connectTimeoutMs = connectTimeoutMs; } @@ -464,6 +385,11 @@ public void setReadTimeoutMs(Integer readTimeoutMs) { this.readTimeoutMs = readTimeoutMs; } + private DocumentBuilder getDocumentBuilder() throws ParserConfigurationException { + DocumentBuilderFactory builderFactory = DocumentBuilderFactory.newInstance(); + return builderFactory.newDocumentBuilder(); + } + // Generate responses for offline tests // private void dumpResponseToFile(Object flickrMethod, String strXml, String httpMethod) throws IOException { // String filename = String.format("%s.xml", flickrMethod); diff --git a/src/main/java/com/flickr4java/flickr/RESTResponse.java b/src/main/java/com/flickr4java/flickr/RESTResponse.java index 1a2f91b2..e35da74d 100644 --- a/src/main/java/com/flickr4java/flickr/RESTResponse.java +++ b/src/main/java/com/flickr4java/flickr/RESTResponse.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; import com.flickr4java.flickr.util.XMLUtilities; diff --git a/src/main/java/com/flickr4java/flickr/RequestContext.java b/src/main/java/com/flickr4java/flickr/RequestContext.java index aa685997..030b0b63 100644 --- a/src/main/java/com/flickr4java/flickr/RequestContext.java +++ b/src/main/java/com/flickr4java/flickr/RequestContext.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; diff --git a/src/main/java/com/flickr4java/flickr/Response.java b/src/main/java/com/flickr4java/flickr/Response.java index cfc26a6d..6b37a11b 100644 --- a/src/main/java/com/flickr4java/flickr/Response.java +++ b/src/main/java/com/flickr4java/flickr/Response.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; diff --git a/src/main/java/com/flickr4java/flickr/SearchResultList.java b/src/main/java/com/flickr4java/flickr/SearchResultList.java index b5ec5e21..61af8d2e 100644 --- a/src/main/java/com/flickr4java/flickr/SearchResultList.java +++ b/src/main/java/com/flickr4java/flickr/SearchResultList.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; diff --git a/src/main/java/com/flickr4java/flickr/Transport.java b/src/main/java/com/flickr4java/flickr/Transport.java index 63f3c96f..f26e881f 100644 --- a/src/main/java/com/flickr4java/flickr/Transport.java +++ b/src/main/java/com/flickr4java/flickr/Transport.java @@ -1,13 +1,15 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr; +import com.flickr4java.flickr.uploader.Payload; +import com.flickr4java.flickr.uploader.UploadMetaData; + import java.util.Map; /** - * 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. + * 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. * * @author Matt Ray * @author Anthony Eden @@ -103,24 +105,22 @@ public void setScheme(String scheme) { * @return The Response object * @throws FlickrException */ - public abstract Response post(String path, Map parameters, String apiKey, String sharedSecret, boolean multipart) throws FlickrException; + public abstract Response post(String path, Map parameters, String apiKey, String sharedSecret) throws FlickrException; /** - * Invoke an HTTP POST request on a remote host. - * + * Invoke an HTTP POST multipart request on a remote host. + * * @param path * The request path * @param parameters * The parameters (List of Parameter objects) + * @param payload * @param apiKey * @param sharedSecret * @return The Response object * @throws FlickrException */ - public Response post(String path, Map parameters, String apiKey, String sharedSecret) throws FlickrException { - - return post(path, parameters, apiKey, sharedSecret, false); - } + public abstract Response postMultiPart(String path, UploadMetaData parameters, Payload payload, String apiKey, String sharedSecret) throws FlickrException; /** * Invoke a non OAuth HTTP GET request on a remote host. @@ -134,7 +134,7 @@ public Response post(String path, Map parameters, String apiKey, * @return The Response * @throws FlickrException */ - public abstract Response getNonOAuth(String path, Map parameters) throws FlickrException; + public abstract Response getNonOAuth(String path, Map parameters) throws FlickrRuntimeException; public void setResponseClass(Class responseClass) { if (responseClass == null) { diff --git a/src/main/java/com/flickr4java/flickr/auth/Auth.java b/src/main/java/com/flickr4java/flickr/auth/Auth.java index 32441bbf..44ea9e94 100644 --- a/src/main/java/com/flickr4java/flickr/auth/Auth.java +++ b/src/main/java/com/flickr4java/flickr/auth/Auth.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.auth; diff --git a/src/main/java/com/flickr4java/flickr/auth/AuthInterface.java b/src/main/java/com/flickr4java/flickr/auth/AuthInterface.java index 955d1838..747be969 100644 --- a/src/main/java/com/flickr4java/flickr/auth/AuthInterface.java +++ b/src/main/java/com/flickr4java/flickr/auth/AuthInterface.java @@ -1,31 +1,32 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.auth; import com.flickr4java.flickr.Flickr; import com.flickr4java.flickr.FlickrException; +import com.flickr4java.flickr.FlickrRuntimeException; import com.flickr4java.flickr.Response; import com.flickr4java.flickr.Transport; import com.flickr4java.flickr.people.User; import com.flickr4java.flickr.util.ByteUtilities; import com.flickr4java.flickr.util.XMLUtilities; -import org.scribe.builder.ServiceBuilder; -import org.scribe.builder.api.FlickrApi; -import org.scribe.exceptions.OAuthException; -import org.scribe.model.Token; -import org.scribe.model.Verifier; -import org.scribe.oauth.OAuthService; +import com.github.scribejava.apis.FlickrApi; +import com.github.scribejava.core.builder.ServiceBuilder; +import com.github.scribejava.core.exceptions.OAuthException; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuth1Token; +import com.github.scribejava.core.oauth.OAuth10aService; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.w3c.dom.Element; +import java.io.IOException; import java.io.UnsupportedEncodingException; import java.security.MessageDigest; import java.security.NoSuchAlgorithmException; import java.util.Map; import java.util.TreeMap; +import java.util.concurrent.ExecutionException; /** * Authentication interface. @@ -70,9 +71,9 @@ public AuthInterface(String apiKey, String sharedSecret, Transport transport) { /** * Get the OAuth request token - this is step one of authorization. * - * @return the {@link Token}, store this for when the user returns from the Flickr website. + * @return the {@link OAuth1RequestToken}, store this for when the user returns from the Flickr website. */ - public Token getRequestToken() { + public OAuth1RequestToken getRequestToken() { return getRequestToken(null); } @@ -82,15 +83,21 @@ public Token getRequestToken() { * * @param callbackUrl * optional callback URL - required for web auth flow, will be set to "oob" if not specified. - * @return the {@link Token}, store this for when the user returns from the Flickr website. + * @return the {@link OAuth1RequestToken}, store this for when the user returns from the Flickr website. */ - public Token getRequestToken(String callbackUrl) { - + public OAuth1RequestToken getRequestToken(String callbackUrl) { String callback = (callbackUrl != null) ? callbackUrl : OUT_OF_BOUND_AUTH_METHOD; - OAuthService service = new ServiceBuilder().provider(FlickrApi.class).apiKey(apiKey).apiSecret(sharedSecret).callback(callback).build(); + OAuth10aService service = new ServiceBuilder(apiKey) + .apiSecret(sharedSecret) + .callback(callback) + .build(FlickrApi.instance()); - return service.getRequestToken(); + try { + return service.getRequestToken(); + } catch (IOException | InterruptedException | ExecutionException e) { + throw new FlickrRuntimeException(e); + } } /** @@ -99,38 +106,39 @@ public Token getRequestToken(String callbackUrl) { * @param oAuthRequestToken * the token from a {@link AuthInterface#getRequestToken} call. */ - public String getAuthorizationUrl(Token oAuthRequestToken, Permission permission) { - - OAuthService service = new ServiceBuilder().provider(FlickrApi.class).apiKey(apiKey).apiSecret(sharedSecret).build(); + public String getAuthorizationUrl(OAuth1RequestToken oAuthRequestToken, Permission permission) { + OAuth10aService service = new ServiceBuilder(apiKey) + .apiSecret(sharedSecret) + .build(FlickrApi.instance()); String authorizationUrl = service.getAuthorizationUrl(oAuthRequestToken); return String.format("%s&perms=%s", authorizationUrl, permission.toString()); } /** * Trade the request token for an access token, this is step three of authorization. - * - * @param oAuthRequestToken + * @param oAuthRequestToken * this is the token returned by the {@link AuthInterface#getRequestToken} call. * @param verifier - * the Verifier created from the code entered by a user or passed back to a callback URL. */ @SuppressWarnings("boxing") - public Token getAccessToken(Token oAuthRequestToken, Verifier verifier) { - OAuthService service = new ServiceBuilder().provider(FlickrApi.class).apiKey(apiKey).apiSecret(sharedSecret).build(); + public OAuth1Token getAccessToken(OAuth1RequestToken oAuthRequestToken, String verifier) { + OAuth10aService service = new ServiceBuilder(apiKey) + .apiSecret(sharedSecret) + .build(FlickrApi.instance()); // Flickr seems to return invalid token sometimes so retry a few times. // See http://www.flickr.com/groups/api/discuss/72157628028927244/ - Token accessToken = null; + OAuth1Token accessToken = null; boolean success = false; for (int i = 0; i < maxGetTokenRetries && !success; i++) { try { accessToken = service.getAccessToken(oAuthRequestToken, verifier); success = true; - } catch (OAuthException e) { + } catch (OAuthException | IOException | InterruptedException | ExecutionException e) { if (i == maxGetTokenRetries - 1) { logger.error(String.format("OAuthService.getAccessToken failing after %d tries, re-throwing exception", i), e); - throw e; + throw new FlickrRuntimeException(e); } else { logger.warn(String.format("OAuthService.getAccessToken failed, try number %d: %s", i, e.getMessage())); try { @@ -142,9 +150,6 @@ public Token getAccessToken(Token oAuthRequestToken, Verifier verifier) { } } - assert accessToken != null; - assert success; - return accessToken; } @@ -156,9 +161,8 @@ public Token getAccessToken(Token oAuthRequestToken, Verifier verifier) { * @return The Auth object * @throws FlickrException */ - public Auth checkToken(Token accessToken) throws FlickrException { - - return checkToken(accessToken.getToken(), accessToken.getSecret()); + public Auth checkToken(OAuth1Token accessToken) throws FlickrException { + return checkToken(accessToken.getToken(), accessToken.getTokenSecret()); } /** @@ -198,7 +202,7 @@ public Auth checkToken(String authToken, String tokenSecret) throws FlickrExcept * @throws FlickrException * @see "http://www.flickr.com/services/api/flickr.auth.oauth.getAccessToken.html" */ - public Token exchangeAuthToken(String authToken) throws FlickrException { + public OAuth1RequestToken exchangeAuthToken(String authToken) throws FlickrException { // Use TreeMap so keys are automatically sorted alphabetically Map parameters = new TreeMap(); @@ -212,7 +216,7 @@ public Token exchangeAuthToken(String authToken) throws FlickrException { throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); } - Token accessToken = constructToken(response); + OAuth1RequestToken accessToken = constructToken(response); return accessToken; } @@ -244,12 +248,12 @@ private Auth constructAuth(Response response, String authToken, String tokenSecr * * @param response */ - private Token constructToken(Response response) { + private OAuth1RequestToken constructToken(Response response) { Element authElement = response.getPayload(); String oauthToken = XMLUtilities.getChildValue(authElement, "oauth_token"); String oauthTokenSecret = XMLUtilities.getChildValue(authElement, "oauth_token_secret"); - Token token = new Token(oauthToken, oauthTokenSecret); + OAuth1RequestToken token = new OAuth1RequestToken(oauthToken, oauthTokenSecret); return token; } diff --git a/src/main/java/com/flickr4java/flickr/auth/Permission.java b/src/main/java/com/flickr4java/flickr/auth/Permission.java index 993c43d4..da29fae3 100644 --- a/src/main/java/com/flickr4java/flickr/auth/Permission.java +++ b/src/main/java/com/flickr4java/flickr/auth/Permission.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.auth; diff --git a/src/main/java/com/flickr4java/flickr/blogs/Blog.java b/src/main/java/com/flickr4java/flickr/blogs/Blog.java index fc93f4e5..80eabc84 100644 --- a/src/main/java/com/flickr4java/flickr/blogs/Blog.java +++ b/src/main/java/com/flickr4java/flickr/blogs/Blog.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.blogs; import java.math.BigDecimal; diff --git a/src/main/java/com/flickr4java/flickr/blogs/BlogsInterface.java b/src/main/java/com/flickr4java/flickr/blogs/BlogsInterface.java index e42c0a69..e97642e4 100644 --- a/src/main/java/com/flickr4java/flickr/blogs/BlogsInterface.java +++ b/src/main/java/com/flickr4java/flickr/blogs/BlogsInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.blogs; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/cameras/CamerasInterface.java b/src/main/java/com/flickr4java/flickr/cameras/CamerasInterface.java index ea125885..b9f87aab 100644 --- a/src/main/java/com/flickr4java/flickr/cameras/CamerasInterface.java +++ b/src/main/java/com/flickr4java/flickr/cameras/CamerasInterface.java @@ -29,7 +29,7 @@ public class CamerasInterface { private static final String METHOD_GET_BRAND_MODELS = "flickr.cameras.getBrandModels"; /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(CamerasInterface.class); diff --git a/src/main/java/com/flickr4java/flickr/collections/Collection.java b/src/main/java/com/flickr4java/flickr/collections/Collection.java index c28384a8..2a54c18f 100644 --- a/src/main/java/com/flickr4java/flickr/collections/Collection.java +++ b/src/main/java/com/flickr4java/flickr/collections/Collection.java @@ -18,7 +18,7 @@ public class Collection { /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(Collection.class); diff --git a/src/main/java/com/flickr4java/flickr/collections/CollectionsInterface.java b/src/main/java/com/flickr4java/flickr/collections/CollectionsInterface.java index 790afaa7..ca6cc101 100644 --- a/src/main/java/com/flickr4java/flickr/collections/CollectionsInterface.java +++ b/src/main/java/com/flickr4java/flickr/collections/CollectionsInterface.java @@ -29,7 +29,7 @@ public class CollectionsInterface { private static final String METHOD_GET_TREE = "flickr.collections.getTree"; /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(CollectionsInterface.class); diff --git a/src/main/java/com/flickr4java/flickr/contacts/Contact.java b/src/main/java/com/flickr4java/flickr/contacts/Contact.java index 0df98fd1..d73fe40f 100644 --- a/src/main/java/com/flickr4java/flickr/contacts/Contact.java +++ b/src/main/java/com/flickr4java/flickr/contacts/Contact.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.contacts; import com.flickr4java.flickr.util.BuddyIconable; diff --git a/src/main/java/com/flickr4java/flickr/contacts/ContactsInterface.java b/src/main/java/com/flickr4java/flickr/contacts/ContactsInterface.java index d899cbb7..1e93346a 100644 --- a/src/main/java/com/flickr4java/flickr/contacts/ContactsInterface.java +++ b/src/main/java/com/flickr4java/flickr/contacts/ContactsInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.contacts; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/contacts/OnlineStatus.java b/src/main/java/com/flickr4java/flickr/contacts/OnlineStatus.java index fa62f54a..3b55706f 100644 --- a/src/main/java/com/flickr4java/flickr/contacts/OnlineStatus.java +++ b/src/main/java/com/flickr4java/flickr/contacts/OnlineStatus.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.contacts; import java.io.Serializable; diff --git a/src/main/java/com/flickr4java/flickr/favorites/FavoritesInterface.java b/src/main/java/com/flickr4java/flickr/favorites/FavoritesInterface.java index f0bc58ef..8ea40e46 100644 --- a/src/main/java/com/flickr4java/flickr/favorites/FavoritesInterface.java +++ b/src/main/java/com/flickr4java/flickr/favorites/FavoritesInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.favorites; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/groups/Category.java b/src/main/java/com/flickr4java/flickr/groups/Category.java index c0c32125..7a765271 100644 --- a/src/main/java/com/flickr4java/flickr/groups/Category.java +++ b/src/main/java/com/flickr4java/flickr/groups/Category.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.groups; import java.util.Collection; diff --git a/src/main/java/com/flickr4java/flickr/groups/Group.java b/src/main/java/com/flickr4java/flickr/groups/Group.java index 534b0107..3be27cc6 100644 --- a/src/main/java/com/flickr4java/flickr/groups/Group.java +++ b/src/main/java/com/flickr4java/flickr/groups/Group.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.groups; import com.flickr4java.flickr.util.BuddyIconable; diff --git a/src/main/java/com/flickr4java/flickr/groups/GroupsInterface.java b/src/main/java/com/flickr4java/flickr/groups/GroupsInterface.java index 29cc6eea..181b3898 100644 --- a/src/main/java/com/flickr4java/flickr/groups/GroupsInterface.java +++ b/src/main/java/com/flickr4java/flickr/groups/GroupsInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.groups; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/groups/Subcategory.java b/src/main/java/com/flickr4java/flickr/groups/Subcategory.java index 4ea067f3..987852c6 100644 --- a/src/main/java/com/flickr4java/flickr/groups/Subcategory.java +++ b/src/main/java/com/flickr4java/flickr/groups/Subcategory.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.groups; /** diff --git a/src/main/java/com/flickr4java/flickr/groups/pools/PoolsInterface.java b/src/main/java/com/flickr4java/flickr/groups/pools/PoolsInterface.java index 9f6ebec5..a32b14b1 100644 --- a/src/main/java/com/flickr4java/flickr/groups/pools/PoolsInterface.java +++ b/src/main/java/com/flickr4java/flickr/groups/pools/PoolsInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.groups.pools; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/people/PeopleInterface.java b/src/main/java/com/flickr4java/flickr/people/PeopleInterface.java index a2522cfc..efb9293e 100644 --- a/src/main/java/com/flickr4java/flickr/people/PeopleInterface.java +++ b/src/main/java/com/flickr4java/flickr/people/PeopleInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.people; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/people/User.java b/src/main/java/com/flickr4java/flickr/people/User.java index 762e4cf4..aaf3fbfb 100644 --- a/src/main/java/com/flickr4java/flickr/people/User.java +++ b/src/main/java/com/flickr4java/flickr/people/User.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.people; import com.flickr4java.flickr.contacts.OnlineStatus; diff --git a/src/main/java/com/flickr4java/flickr/photos/Editability.java b/src/main/java/com/flickr4java/flickr/photos/Editability.java index c378657e..618229df 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Editability.java +++ b/src/main/java/com/flickr4java/flickr/photos/Editability.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; /** diff --git a/src/main/java/com/flickr4java/flickr/photos/Exif.java b/src/main/java/com/flickr4java/flickr/photos/Exif.java index 6d56dcb4..97f5ae4b 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Exif.java +++ b/src/main/java/com/flickr4java/flickr/photos/Exif.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; diff --git a/src/main/java/com/flickr4java/flickr/photos/Extras.java b/src/main/java/com/flickr4java/flickr/photos/Extras.java index 154bdee0..58c63426 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Extras.java +++ b/src/main/java/com/flickr4java/flickr/photos/Extras.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; import java.util.HashSet; diff --git a/src/main/java/com/flickr4java/flickr/photos/Note.java b/src/main/java/com/flickr4java/flickr/photos/Note.java index 53252e11..a4ec3202 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Note.java +++ b/src/main/java/com/flickr4java/flickr/photos/Note.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; import java.awt.Rectangle; diff --git a/src/main/java/com/flickr4java/flickr/photos/Permissions.java b/src/main/java/com/flickr4java/flickr/photos/Permissions.java index 6cf6ed03..06dbe5cf 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Permissions.java +++ b/src/main/java/com/flickr4java/flickr/photos/Permissions.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; /** diff --git a/src/main/java/com/flickr4java/flickr/photos/Photo.java b/src/main/java/com/flickr4java/flickr/photos/Photo.java index f814d80c..bd34981a 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Photo.java +++ b/src/main/java/com/flickr4java/flickr/photos/Photo.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/photos/PhotoContext.java b/src/main/java/com/flickr4java/flickr/photos/PhotoContext.java index 03622a6a..9ff3bb04 100644 --- a/src/main/java/com/flickr4java/flickr/photos/PhotoContext.java +++ b/src/main/java/com/flickr4java/flickr/photos/PhotoContext.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; /** diff --git a/src/main/java/com/flickr4java/flickr/photos/PhotoList.java b/src/main/java/com/flickr4java/flickr/photos/PhotoList.java index 2367e7eb..194f94eb 100644 --- a/src/main/java/com/flickr4java/flickr/photos/PhotoList.java +++ b/src/main/java/com/flickr4java/flickr/photos/PhotoList.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; diff --git a/src/main/java/com/flickr4java/flickr/photos/PhotoUrl.java b/src/main/java/com/flickr4java/flickr/photos/PhotoUrl.java index d4376963..1dfee81c 100644 --- a/src/main/java/com/flickr4java/flickr/photos/PhotoUrl.java +++ b/src/main/java/com/flickr4java/flickr/photos/PhotoUrl.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.photos; diff --git a/src/main/java/com/flickr4java/flickr/photos/Photocount.java b/src/main/java/com/flickr4java/flickr/photos/Photocount.java index 4e95e7b3..a4a16d2f 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Photocount.java +++ b/src/main/java/com/flickr4java/flickr/photos/Photocount.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; import java.util.Date; diff --git a/src/main/java/com/flickr4java/flickr/photos/PhotosInterface.java b/src/main/java/com/flickr4java/flickr/photos/PhotosInterface.java index 008ae835..e21bac88 100644 --- a/src/main/java/com/flickr4java/flickr/photos/PhotosInterface.java +++ b/src/main/java/com/flickr4java/flickr/photos/PhotosInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/photos/SearchParameters.java b/src/main/java/com/flickr4java/flickr/photos/SearchParameters.java index 9be4ae8a..4125584d 100644 --- a/src/main/java/com/flickr4java/flickr/photos/SearchParameters.java +++ b/src/main/java/com/flickr4java/flickr/photos/SearchParameters.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/photos/Size.java b/src/main/java/com/flickr4java/flickr/photos/Size.java index 77660e01..38af87e1 100644 --- a/src/main/java/com/flickr4java/flickr/photos/Size.java +++ b/src/main/java/com/flickr4java/flickr/photos/Size.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos; import java.util.Arrays; diff --git a/src/main/java/com/flickr4java/flickr/photos/licenses/License.java b/src/main/java/com/flickr4java/flickr/photos/licenses/License.java index 6ee46b8f..15983df1 100644 --- a/src/main/java/com/flickr4java/flickr/photos/licenses/License.java +++ b/src/main/java/com/flickr4java/flickr/photos/licenses/License.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos.licenses; diff --git a/src/main/java/com/flickr4java/flickr/photos/licenses/LicensesInterface.java b/src/main/java/com/flickr4java/flickr/photos/licenses/LicensesInterface.java index 3c34198e..adaefe2c 100644 --- a/src/main/java/com/flickr4java/flickr/photos/licenses/LicensesInterface.java +++ b/src/main/java/com/flickr4java/flickr/photos/licenses/LicensesInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos.licenses; diff --git a/src/main/java/com/flickr4java/flickr/photos/notes/NotesInterface.java b/src/main/java/com/flickr4java/flickr/photos/notes/NotesInterface.java index e29c23f2..d1bfb140 100644 --- a/src/main/java/com/flickr4java/flickr/photos/notes/NotesInterface.java +++ b/src/main/java/com/flickr4java/flickr/photos/notes/NotesInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos.notes; diff --git a/src/main/java/com/flickr4java/flickr/photos/transform/TransformInterface.java b/src/main/java/com/flickr4java/flickr/photos/transform/TransformInterface.java index 18c3ff01..3fc72030 100644 --- a/src/main/java/com/flickr4java/flickr/photos/transform/TransformInterface.java +++ b/src/main/java/com/flickr4java/flickr/photos/transform/TransformInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photos.transform; diff --git a/src/main/java/com/flickr4java/flickr/photosets/Photoset.java b/src/main/java/com/flickr4java/flickr/photosets/Photoset.java index b0066d78..93e1a1fb 100644 --- a/src/main/java/com/flickr4java/flickr/photosets/Photoset.java +++ b/src/main/java/com/flickr4java/flickr/photosets/Photoset.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photosets; import com.flickr4java.flickr.people.User; diff --git a/src/main/java/com/flickr4java/flickr/photosets/Photosets.java b/src/main/java/com/flickr4java/flickr/photosets/Photosets.java index 74f995ac..0551c8a4 100644 --- a/src/main/java/com/flickr4java/flickr/photosets/Photosets.java +++ b/src/main/java/com/flickr4java/flickr/photosets/Photosets.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photosets; import java.util.Collection; diff --git a/src/main/java/com/flickr4java/flickr/photosets/PhotosetsInterface.java b/src/main/java/com/flickr4java/flickr/photosets/PhotosetsInterface.java index 4509d193..d8cb62ef 100644 --- a/src/main/java/com/flickr4java/flickr/photosets/PhotosetsInterface.java +++ b/src/main/java/com/flickr4java/flickr/photosets/PhotosetsInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.photosets; import com.flickr4java.flickr.Flickr; diff --git a/src/main/java/com/flickr4java/flickr/reflection/Argument.java b/src/main/java/com/flickr4java/flickr/reflection/Argument.java index 74ccda22..1e57efda 100644 --- a/src/main/java/com/flickr4java/flickr/reflection/Argument.java +++ b/src/main/java/com/flickr4java/flickr/reflection/Argument.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.reflection; diff --git a/src/main/java/com/flickr4java/flickr/reflection/Error.java b/src/main/java/com/flickr4java/flickr/reflection/Error.java index 75a151ae..1ecef1ef 100644 --- a/src/main/java/com/flickr4java/flickr/reflection/Error.java +++ b/src/main/java/com/flickr4java/flickr/reflection/Error.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.reflection; diff --git a/src/main/java/com/flickr4java/flickr/reflection/Method.java b/src/main/java/com/flickr4java/flickr/reflection/Method.java index 0e28dd3c..00d437af 100644 --- a/src/main/java/com/flickr4java/flickr/reflection/Method.java +++ b/src/main/java/com/flickr4java/flickr/reflection/Method.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.reflection; diff --git a/src/main/java/com/flickr4java/flickr/reflection/ReflectionInterface.java b/src/main/java/com/flickr4java/flickr/reflection/ReflectionInterface.java index cd68ae7a..cb5a840d 100644 --- a/src/main/java/com/flickr4java/flickr/reflection/ReflectionInterface.java +++ b/src/main/java/com/flickr4java/flickr/reflection/ReflectionInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.reflection; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/stats/Csv.java b/src/main/java/com/flickr4java/flickr/stats/Csv.java index 1248e83d..9bfeb51f 100644 --- a/src/main/java/com/flickr4java/flickr/stats/Csv.java +++ b/src/main/java/com/flickr4java/flickr/stats/Csv.java @@ -14,7 +14,7 @@ public class Csv { /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(Csv.class); diff --git a/src/main/java/com/flickr4java/flickr/stats/Domain.java b/src/main/java/com/flickr4java/flickr/stats/Domain.java index db107626..299f1edb 100644 --- a/src/main/java/com/flickr4java/flickr/stats/Domain.java +++ b/src/main/java/com/flickr4java/flickr/stats/Domain.java @@ -12,7 +12,7 @@ public class Domain { /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(Domain.class); diff --git a/src/main/java/com/flickr4java/flickr/stats/Referrer.java b/src/main/java/com/flickr4java/flickr/stats/Referrer.java index 62b9f47b..e64ac8c8 100644 --- a/src/main/java/com/flickr4java/flickr/stats/Referrer.java +++ b/src/main/java/com/flickr4java/flickr/stats/Referrer.java @@ -12,7 +12,7 @@ public class Referrer { /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(Referrer.class); diff --git a/src/main/java/com/flickr4java/flickr/stats/Stats.java b/src/main/java/com/flickr4java/flickr/stats/Stats.java index 4ce1b6cd..cd66ddc8 100644 --- a/src/main/java/com/flickr4java/flickr/stats/Stats.java +++ b/src/main/java/com/flickr4java/flickr/stats/Stats.java @@ -12,7 +12,7 @@ public class Stats { /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(Stats.class); diff --git a/src/main/java/com/flickr4java/flickr/stats/StatsInterface.java b/src/main/java/com/flickr4java/flickr/stats/StatsInterface.java index 3fd9e928..d1abbf9d 100644 --- a/src/main/java/com/flickr4java/flickr/stats/StatsInterface.java +++ b/src/main/java/com/flickr4java/flickr/stats/StatsInterface.java @@ -31,7 +31,7 @@ public class StatsInterface { /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(StatsInterface.class); diff --git a/src/main/java/com/flickr4java/flickr/stats/Totals.java b/src/main/java/com/flickr4java/flickr/stats/Totals.java index 4130d0be..d1f8cbd4 100644 --- a/src/main/java/com/flickr4java/flickr/stats/Totals.java +++ b/src/main/java/com/flickr4java/flickr/stats/Totals.java @@ -12,7 +12,7 @@ public class Totals { /** - * Logger for log4j. + * Logger. */ @SuppressWarnings("unused") private static Logger _log = LoggerFactory.getLogger(Totals.class); diff --git a/src/main/java/com/flickr4java/flickr/tags/RelatedTagsList.java b/src/main/java/com/flickr4java/flickr/tags/RelatedTagsList.java index 8cd0e984..113d0f4c 100644 --- a/src/main/java/com/flickr4java/flickr/tags/RelatedTagsList.java +++ b/src/main/java/com/flickr4java/flickr/tags/RelatedTagsList.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.tags; diff --git a/src/main/java/com/flickr4java/flickr/tags/Tag.java b/src/main/java/com/flickr4java/flickr/tags/Tag.java index ae389c5c..81354c33 100644 --- a/src/main/java/com/flickr4java/flickr/tags/Tag.java +++ b/src/main/java/com/flickr4java/flickr/tags/Tag.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.tags; /** diff --git a/src/main/java/com/flickr4java/flickr/tags/TagsInterface.java b/src/main/java/com/flickr4java/flickr/tags/TagsInterface.java index 74062ae1..20916600 100644 --- a/src/main/java/com/flickr4java/flickr/tags/TagsInterface.java +++ b/src/main/java/com/flickr4java/flickr/tags/TagsInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.tags; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/test/TestInterface.java b/src/main/java/com/flickr4java/flickr/test/TestInterface.java index 7dc67ae6..a51b4270 100644 --- a/src/main/java/com/flickr4java/flickr/test/TestInterface.java +++ b/src/main/java/com/flickr4java/flickr/test/TestInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.test; import com.flickr4java.flickr.Flickr; diff --git a/src/main/java/com/flickr4java/flickr/uploader/Payload.java b/src/main/java/com/flickr4java/flickr/uploader/Payload.java new file mode 100644 index 00000000..231f78d6 --- /dev/null +++ b/src/main/java/com/flickr4java/flickr/uploader/Payload.java @@ -0,0 +1,66 @@ +package com.flickr4java.flickr.uploader; + +import com.flickr4java.flickr.FlickrRuntimeException; + +import java.io.ByteArrayOutputStream; +import java.io.File; +import java.io.IOException; +import java.io.InputStream; +import java.nio.file.Files; + +public class Payload { + + private byte[] payload; + + private String photoId; + + public Payload(byte[] payload) { + this.payload = payload; + } + + public Payload(File file) { + try { + payload = Files.readAllBytes(file.toPath()); + } catch (IOException e) { + throw new FlickrRuntimeException(e); + } + } + + public Payload(InputStream inputStream) { + try (ByteArrayOutputStream buffer = new ByteArrayOutputStream()) { + int nRead; + byte[] data = new byte[16384]; + + while ((nRead = inputStream.read(data, 0, data.length)) != -1) { + buffer.write(data, 0, nRead); + } + + payload = buffer.toByteArray(); + } catch (IOException e) { + throw new FlickrRuntimeException(e); + } + } + + public Payload(InputStream inputStream, String photoId) { + this(inputStream); + this.photoId = photoId; + } + + public Payload(byte[] payload, String photoId) { + this(payload); + this.photoId = photoId; + } + + public Payload(File file, String photoId) { + this(file); + this.photoId = photoId; + } + + public byte[] getPayload() { + return payload; + } + + public String getPhotoId() { + return photoId; + } +} diff --git a/src/main/java/com/flickr4java/flickr/uploader/UploadMetaData.java b/src/main/java/com/flickr4java/flickr/uploader/UploadMetaData.java index 9cd0e64f..65859260 100644 --- a/src/main/java/com/flickr4java/flickr/uploader/UploadMetaData.java +++ b/src/main/java/com/flickr4java/flickr/uploader/UploadMetaData.java @@ -1,10 +1,12 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.uploader; +import com.flickr4java.flickr.util.StringUtilities; + import java.util.Collection; +import java.util.Map; +import java.util.TreeMap; /** * Metadata that describe a photo. @@ -14,9 +16,9 @@ */ public class UploadMetaData { - private String filename = null; + private String filename = "image.jpg"; - private String fileMimeType = null; + private String fileMimeType = "image/jpeg"; private String title; @@ -30,7 +32,7 @@ public class UploadMetaData { private boolean familyFlag; - private boolean async = false; + private boolean async; private Boolean hidden; @@ -38,6 +40,8 @@ public class UploadMetaData { private String contentType; + private String photoId; + public String getTitle() { return title; } @@ -190,7 +194,7 @@ public boolean isAsync() { } /** - * Switch the Uploader behaviour - sychronous or asyncrounous. + * Switch the Uploader behaviour - synchronous or asynchronous. *

* * The default is sychronous. @@ -204,4 +208,70 @@ public UploadMetaData setAsync(boolean async) { return this; } + public String getPhotoId() { + return photoId; + } + + /** + * Set the existing photo id for a replace operation. + * @param photoId + */ + public void setPhotoId(String photoId) { + this.photoId = photoId; + } + + /** + * Get the upload parameters. + * @return + */ + public Map getUploadParameters() { + Map parameters = new TreeMap<>(); + + + String title = getTitle(); + if (title != null) { + parameters.put("title", title); + } + + String description = getDescription(); + if (description != null) { + parameters.put("description", description); + } + + Collection tags = getTags(); + if (tags != null) { + parameters.put("tags", StringUtilities.join(tags, " ")); + } + + if (isHidden() != null) { + parameters.put("hidden", isHidden().booleanValue() ? "1" : "0"); + } + + if (getSafetyLevel() != null) { + parameters.put("safety_level", getSafetyLevel()); + } + + if (getContentType() != null) { + parameters.put("content_type", getContentType()); + } + + if (getPhotoId() != null) { + parameters.put("photo_id", getPhotoId()); + } + + parameters.put("is_public", isPublicFlag() ? "1" : "0"); + parameters.put("is_family", isFamilyFlag() ? "1" : "0"); + parameters.put("is_friend", isFriendFlag() ? "1" : "0"); + parameters.put("async", isAsync() ? "1" : "0"); + + return parameters; + } + + public static UploadMetaData replace(boolean async, String photoId) { + UploadMetaData metaData = new UploadMetaData(); + metaData.async = async; + metaData.photoId = photoId; + return metaData; + } + } diff --git a/src/main/java/com/flickr4java/flickr/uploader/Uploader.java b/src/main/java/com/flickr4java/flickr/uploader/Uploader.java index 2bca5f7c..b13a53ff 100644 --- a/src/main/java/com/flickr4java/flickr/uploader/Uploader.java +++ b/src/main/java/com/flickr4java/flickr/uploader/Uploader.java @@ -1,25 +1,14 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.uploader; import com.flickr4java.flickr.FlickrException; -import com.flickr4java.flickr.FlickrRuntimeException; import com.flickr4java.flickr.REST; import com.flickr4java.flickr.Transport; -import com.flickr4java.flickr.util.IOUtilities; -import com.flickr4java.flickr.util.StringUtilities; import java.io.File; -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.IOException; import java.io.InputStream; -import java.util.Collection; -import java.util.Map; import java.util.Set; -import java.util.TreeMap; /** * Upload a photo. @@ -37,13 +26,9 @@ * @version $Id: Uploader.java,v 1.12 2009/12/15 20:57:49 x-mago Exp $ */ public class Uploader implements IUploader { - /** - * - */ + private static final String SERVICES_REPLACE_PATH = "/services/replace/"; - /** - * - */ + private static final String SERVICES_UPLOAD_PATH = "/services/upload/"; private final String apiKey; @@ -88,12 +73,8 @@ public Uploader(String apiKey, String sharedSecret, Transport transport) { */ @Override public String upload(byte[] data, UploadMetaData metaData) throws FlickrException { - Map parameters = setUploadParameters(metaData); - parameters.put("photo", data); - - UploaderResponse response = postPhoto(parameters, SERVICES_UPLOAD_PATH); - - return getResponseString(metaData.isAsync(), response); + Payload payload = new Payload(data); + return sendUploadRequest(metaData, payload); } /** @@ -108,16 +89,8 @@ public String upload(byte[] data, UploadMetaData metaData) throws FlickrExceptio */ @Override public String upload(File file, UploadMetaData metaData) throws FlickrException { - InputStream in = null; - - try { - in = new FileInputStream(file); - return upload(in, metaData); - } catch (IOException e) { - throw new FlickrRuntimeException(e); - } finally { - IOUtilities.close(in); - } + Payload payload = new Payload(file); + return sendUploadRequest(metaData, payload); } /** @@ -130,12 +103,8 @@ public String upload(File file, UploadMetaData metaData) throws FlickrException */ @Override public String upload(InputStream in, UploadMetaData metaData) throws FlickrException { - Map parameters = setUploadParameters(metaData); - parameters.put("photo", in); - - UploaderResponse response = postPhoto(parameters, SERVICES_UPLOAD_PATH); - - return getResponseString(metaData.isAsync(), response); + Payload payload = new Payload(in); + return sendUploadRequest(metaData, payload); } /** @@ -147,12 +116,8 @@ public String upload(InputStream in, UploadMetaData metaData) throws FlickrExcep */ @Override public String replace(InputStream in, String flickrId, boolean async) throws FlickrException { - Map parameters = setReplaceParameters(flickrId, async); - parameters.put("photo", in); - - UploaderResponse response = postPhoto(parameters, SERVICES_REPLACE_PATH); - - return getResponseString(async, response); + Payload payload = new Payload(in, flickrId); + return sendReplaceRequest(async, payload); } /** @@ -166,13 +131,8 @@ public String replace(InputStream in, String flickrId, boolean async) throws Fli */ @Override public String replace(byte[] data, String flickrId, boolean async) throws FlickrException { - Map parameters = setReplaceParameters(flickrId, async); - - parameters.put("photo", data); - - UploaderResponse response = postPhoto(parameters, SERVICES_REPLACE_PATH); - - return getResponseString(async, response); + Payload payload = new Payload(data, flickrId); + return sendReplaceRequest(async, payload); } /** @@ -186,118 +146,37 @@ public String replace(byte[] data, String flickrId, boolean async) throws Flickr */ @Override public String replace(File file, String flickrId, boolean async) throws FlickrException { - InputStream in = null; - - try { - in = new FileInputStream(file); - return replace(in, flickrId, async); - } catch (FileNotFoundException e) { - throw new FlickrRuntimeException(e); - } finally { - IOUtilities.close(in); - } + Payload payload = new Payload(file, flickrId); + return sendReplaceRequest(async, payload); } - /** - * Call the post multipart end point. - * - * @param parameters - * @param path - * @return - * @throws FlickrException - */ - private UploaderResponse postPhoto(Map parameters, String path) throws FlickrException { - UploaderResponse response = (UploaderResponse) transport.post(path, parameters, apiKey, sharedSecret, true); + private String sendUploadRequest(UploadMetaData metaData, Payload payload) throws FlickrException { + UploaderResponse response = (UploaderResponse) transport.postMultiPart(SERVICES_UPLOAD_PATH, metaData, payload, apiKey, sharedSecret); if (response.isError()) { throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); } - return response; - } - /** - * Get the photo or ticket id from the response. - * - * @param async - * @param response - * @return - */ - private String getResponseString(boolean async, UploaderResponse response) { - String id = ""; - if (async) { - id = response.getTicketId(); - } else { - id = response.getPhotoId(); - } - return id; + return getResponseString(metaData.isAsync(), response); } - /** - * - * @param metaData - * @return - */ - private Map setUploadParameters(UploadMetaData metaData) { - Map parameters = new TreeMap(); - - String filename = metaData.getFilename(); - if(filename == null || filename.equals("")) - filename = "image.jpg"; // Will NOT work for videos, filename must be passed. - parameters.put("filename", filename); - - String fileMimeType = metaData.getFilemimetype(); - if(fileMimeType == null || fileMimeType.equals("")) - fileMimeType = "image/jpeg"; - - parameters.put("filemimetype", fileMimeType); - - String title = metaData.getTitle(); - if (title != null) { - parameters.put("title", title); - } - - String description = metaData.getDescription(); - if (description != null) { - parameters.put("description", description); - } - - Collection tags = metaData.getTags(); - if (tags != null) { - parameters.put("tags", StringUtilities.join(tags, " ")); - } - - if (metaData.isHidden() != null) { - parameters.put("hidden", metaData.isHidden().booleanValue() ? "1" : "0"); - } - - if (metaData.getSafetyLevel() != null) { - parameters.put("safety_level", metaData.getSafetyLevel()); - } - - if (metaData.getContentType() != null) { - parameters.put("content_type", metaData.getContentType()); + private String sendReplaceRequest(boolean async, Payload payload) throws FlickrException { + UploaderResponse response = (UploaderResponse) transport.postMultiPart(SERVICES_REPLACE_PATH, UploadMetaData.replace(async, payload.getPhotoId()), payload, apiKey, sharedSecret); + if (response.isError()) { + throw new FlickrException(response.getErrorCode(), response.getErrorMessage()); } - parameters.put("is_public", metaData.isPublicFlag() ? "1" : "0"); - parameters.put("is_family", metaData.isFamilyFlag() ? "1" : "0"); - parameters.put("is_friend", metaData.isFriendFlag() ? "1" : "0"); - parameters.put("async", metaData.isAsync() ? "1" : "0"); - - return parameters; + return getResponseString(async, response); } /** + * Get the photo or ticket id from the response. * - * @param flickrId * @param async + * @param response * @return */ - private Map setReplaceParameters(String flickrId, boolean async) { - Map parameters = new TreeMap(); - - parameters.put("async", async ? "1" : "0"); - parameters.put("photo_id", flickrId); - - return parameters; + private String getResponseString(boolean async, UploaderResponse response) { + return async ? response.getTicketId() : response.getPhotoId(); } /** diff --git a/src/main/java/com/flickr4java/flickr/uploader/UploaderResponse.java b/src/main/java/com/flickr4java/flickr/uploader/UploaderResponse.java index 5d00cf85..07ce5988 100644 --- a/src/main/java/com/flickr4java/flickr/uploader/UploaderResponse.java +++ b/src/main/java/com/flickr4java/flickr/uploader/UploaderResponse.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.uploader; diff --git a/src/main/java/com/flickr4java/flickr/urls/UrlsInterface.java b/src/main/java/com/flickr4java/flickr/urls/UrlsInterface.java index 93a39bb1..5ba2b45e 100644 --- a/src/main/java/com/flickr4java/flickr/urls/UrlsInterface.java +++ b/src/main/java/com/flickr4java/flickr/urls/UrlsInterface.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.urls; import com.flickr4java.flickr.FlickrException; diff --git a/src/main/java/com/flickr4java/flickr/util/Base64.java b/src/main/java/com/flickr4java/flickr/util/Base64.java deleted file mode 100644 index 707fb8ae..00000000 --- a/src/main/java/com/flickr4java/flickr/util/Base64.java +++ /dev/null @@ -1,404 +0,0 @@ -/* - * The Apache Software License, Version 1.1 - * - * - * Copyright (c) 1999 The Apache Software Foundation. All rights - * reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in - * the documentation and/or other materials provided with the - * distribution. - * - * 3. The end-user documentation included with the redistribution, - * if any, must include the following acknowledgment: - * "This product includes software developed by the - * Apache Software Foundation (http://www.apache.org/)." - * Alternately, this acknowledgment may appear in the software itself, - * if and wherever such third-party acknowledgments normally appear. - * - * 4. The names "Xerces" and "Apache Software Foundation" must - * not be used to endorse or promote products derived from this - * software without prior written permission. For written - * permission, please contact apache@apache.org. - * - * 5. Products derived from this software may not be called "Apache", - * nor may "Apache" appear in their name, without prior written - * permission of the Apache Software Foundation. - * - * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED - * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES - * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE - * DISCLAIMED. IN NO EVENT SHALL THE APACHE SOFTWARE FOUNDATION OR - * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, - * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT - * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF - * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND - * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, - * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT - * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF - * SUCH DAMAGE. - * ==================================================================== - * - * This software consists of voluntary contributions made by many - * individuals on behalf of the Apache Software Foundation and was - * originally based on software copyright (c) 1999, International - * Business Machines, Inc., http://www.apache.org. For more - * information on the Apache Software Foundation, please see - * . - */ - -package com.flickr4java.flickr.util; - -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -/** - * This class provides encode/decode for RFC 2045 Base64 as defined by RFC 2045, N. Freed and N. Borenstein. RFC 2045: Multipurpose Internet Mail Extensions - * (MIME) Part One: Format of Internet Message Bodies. Reference 1996 Available at: http://www.ietf.org/rfc/rfc2045.txt This class is used by XML Schema binary - * format validation. - *

- * - * This implementation does not encode/decode streaming data. You need the data that you will encode/decode already on a byte array. - *

- * - * This is the latest pre commons-codec version of Base64. - * - * @author Jeffrey Rodriguez - * @author Sandy Gao - * @version Base64.java,v 1.8 2001/05/29 22:19:01 neilg Exp - */ -public final class Base64 { - - private static Logger _log = LoggerFactory.getLogger(Base64.class); - - static private final int BASELENGTH = 255; - - static private final int LOOKUPLENGTH = 64; - - static private final int TWENTYFOURBITGROUP = 24; - - static private final int EIGHTBIT = 8; - - static private final int SIXTEENBIT = 16; - - static private final int SIXBIT = 6; - - static private final int FOURBYTE = 4; - - static private final int SIGN = -128; - - static private final byte PAD = (byte) '='; - - static private byte[] base64Alphabet = new byte[BASELENGTH]; - - static private byte[] lookUpBase64Alphabet = new byte[LOOKUPLENGTH]; - - static { - - for (int i = 0; i < BASELENGTH; i++) { - base64Alphabet[i] = -1; - } - for (int i = 'Z'; i >= 'A'; i--) { - base64Alphabet[i] = (byte) (i - 'A'); - } - for (int i = 'z'; i >= 'a'; i--) { - base64Alphabet[i] = (byte) (i - 'a' + 26); - } - - for (int i = '9'; i >= '0'; i--) { - base64Alphabet[i] = (byte) (i - '0' + 52); - } - - base64Alphabet['+'] = 62; - base64Alphabet['/'] = 63; - - for (int i = 0; i <= 25; i++) - lookUpBase64Alphabet[i] = (byte) ('A' + i); - - for (int i = 26, j = 0; i <= 51; i++, j++) - lookUpBase64Alphabet[i] = (byte) ('a' + j); - - for (int i = 52, j = 0; i <= 61; i++, j++) - lookUpBase64Alphabet[i] = (byte) ('0' + j); - lookUpBase64Alphabet[62] = (byte) '+'; - lookUpBase64Alphabet[63] = (byte) '/'; - - } - - protected static boolean isWhiteSpace(byte octect) { - return (octect == 0x20 || octect == 0xd || octect == 0xa || octect == 0x9); - } - - protected static boolean isPad(byte octect) { - return (octect == PAD); - } - - protected static boolean isData(byte octect) { - return (base64Alphabet[octect] != -1); - } - - public static boolean isBase64(String isValidString) { - if (isValidString == null) - return false; - return (isArrayByteBase64(isValidString.getBytes())); - } - - public static boolean isBase64(byte octect) { - return (isWhiteSpace(octect) || isPad(octect) || isData(octect)); - } - - /** - * remove WhiteSpace from MIME containing encoded Base64 data. e.g. " sdffferererrereresfsdfsdfsdff\n\r iiiiiiiiierejrlkwjerklwjerwerwr==\n\r" - * - * @param data - * @return stripped data - */ - public static synchronized byte[] removeWhiteSpace(byte[] data) { - if (data == null) - return null; - - int newSize = 0; - int len = data.length; - int i = 0; - for (; i < len; i++) { - if (!isWhiteSpace(data[i])) - newSize++; - } - - if (newSize == len) - return data;// return input array since no whiteSpace - - byte[] arrayWithoutSpaces = new byte[newSize];// Allocate new array without whiteSpace - - int j = 0; - for (i = 0; i < len; i++) { - if (isWhiteSpace(data[i])) - continue; - else - arrayWithoutSpaces[j++] = data[i];// copy non-WhiteSpace - } - return arrayWithoutSpaces; - - } - - public static synchronized boolean isArrayByteBase64(byte[] arrayOctect) { - return (getDecodedDataLength(arrayOctect) >= 0); - } - - /** - * Encodes hex octects into Base64 - * - * @param binaryData - * Array containing binaryData - * @return Encoded Base64 array - */ - public static synchronized byte[] encode(byte[] binaryData) { - if (binaryData == null) - return null; - - int lengthDataBits = binaryData.length * EIGHTBIT; - int fewerThan24bits = lengthDataBits % TWENTYFOURBITGROUP; - int numberTriplets = lengthDataBits / TWENTYFOURBITGROUP; - byte encodedData[] = null; - - if (fewerThan24bits != 0) // data not divisible by 24 bit - encodedData = new byte[(numberTriplets + 1) * 4]; - else - // 16 or 8 bit - encodedData = new byte[numberTriplets * 4]; - - byte k = 0, l = 0, b1 = 0, b2 = 0, b3 = 0; - - int encodedIndex = 0; - int dataIndex = 0; - int i = 0; - if (_log.isDebugEnabled()) { - _log.debug("number of triplets = " + numberTriplets); - } - for (i = 0; i < numberTriplets; i++) { - - dataIndex = i * 3; - b1 = binaryData[dataIndex]; - b2 = binaryData[dataIndex + 1]; - b3 = binaryData[dataIndex + 2]; - - if (_log.isDebugEnabled()) { - _log.debug("b1= " + b1 + ", b2= " + b2 + ", b3= " + b3); - } - - l = (byte) (b2 & 0x0f); - k = (byte) (b1 & 0x03); - - encodedIndex = i * 4; - byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); - - byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); - byte val3 = ((b3 & SIGN) == 0) ? (byte) (b3 >> 6) : (byte) ((b3) >> 6 ^ 0xfc); - - encodedData[encodedIndex] = lookUpBase64Alphabet[val1]; - if (_log.isDebugEnabled()) { - _log.debug("val2 = " + val2); - _log.debug("k4 = " + (k << 4)); - _log.debug("vak = " + (val2 | (k << 4))); - } - - encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)]; - encodedData[encodedIndex + 2] = lookUpBase64Alphabet[(l << 2) | val3]; - encodedData[encodedIndex + 3] = lookUpBase64Alphabet[b3 & 0x3f]; - } - - // form integral number of 6-bit groups - dataIndex = i * 3; - encodedIndex = i * 4; - if (fewerThan24bits == EIGHTBIT) { - b1 = binaryData[dataIndex]; - k = (byte) (b1 & 0x03); - if (_log.isDebugEnabled()) { - _log.debug("b1=" + b1); - _log.debug("b1<<2 = " + (b1 >> 2)); - } - byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); - encodedData[encodedIndex] = lookUpBase64Alphabet[val1]; - encodedData[encodedIndex + 1] = lookUpBase64Alphabet[k << 4]; - encodedData[encodedIndex + 2] = PAD; - encodedData[encodedIndex + 3] = PAD; - } else if (fewerThan24bits == SIXTEENBIT) { - - b1 = binaryData[dataIndex]; - b2 = binaryData[dataIndex + 1]; - l = (byte) (b2 & 0x0f); - k = (byte) (b1 & 0x03); - - byte val1 = ((b1 & SIGN) == 0) ? (byte) (b1 >> 2) : (byte) ((b1) >> 2 ^ 0xc0); - byte val2 = ((b2 & SIGN) == 0) ? (byte) (b2 >> 4) : (byte) ((b2) >> 4 ^ 0xf0); - - encodedData[encodedIndex] = lookUpBase64Alphabet[val1]; - encodedData[encodedIndex + 1] = lookUpBase64Alphabet[val2 | (k << 4)]; - encodedData[encodedIndex + 2] = lookUpBase64Alphabet[l << 2]; - encodedData[encodedIndex + 3] = PAD; - } - return encodedData; - } - - /** - * Decodes Base64 data into octects - * - * @param base64Data - * Byte array containing Base64 data - * @return Array containind decoded data. - */ - public static synchronized byte[] decode(byte[] base64Data) { - - if (base64Data == null) - return null; - - byte[] normalizedBase64Data = removeWhiteSpace(base64Data); - - if (normalizedBase64Data.length % FOURBYTE != 0) { - return null;// should be divisible by four - } - - int numberQuadruple = (normalizedBase64Data.length / FOURBYTE); - - if (numberQuadruple == 0) - return new byte[0]; - - byte decodedData[] = null; - byte b1 = 0, b2 = 0, b3 = 0, b4 = 0, marker0 = 0, marker1 = 0; - byte d1 = 0, d2 = 0, d3 = 0, d4 = 0; - - // Throw away anything not in normalizedBase64Data - // Adjust size - int i = 0; - int encodedIndex = 0; - int dataIndex = 0; - decodedData = new byte[(numberQuadruple) * 3]; - - for (; i < numberQuadruple - 1; i++) { - - if (!isData((d1 = normalizedBase64Data[dataIndex++])) || !isData((d2 = normalizedBase64Data[dataIndex++])) - || !isData((d3 = normalizedBase64Data[dataIndex++])) || !isData((d4 = normalizedBase64Data[dataIndex++]))) - return null;// if found "no data" just return null - - b1 = base64Alphabet[d1]; - b2 = base64Alphabet[d2]; - b3 = base64Alphabet[d3]; - b4 = base64Alphabet[d4]; - - decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); - decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); - decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); - } - - if (!isData((d1 = normalizedBase64Data[dataIndex++])) || !isData((d2 = normalizedBase64Data[dataIndex++]))) { - return null;// if found "no data" just return null - } - - b1 = base64Alphabet[d1]; - b2 = base64Alphabet[d2]; - - d3 = normalizedBase64Data[dataIndex++]; - d4 = normalizedBase64Data[dataIndex++]; - if (!isData((d3)) || !isData((d4))) {// Check if they are PAD characters - if (isPad(d3) && isPad(d4)) { // Two PAD e.g. 3c[Pad][Pad] - if ((b2 & 0xf) != 0)// last 4 bits should be zero - return null; - byte[] tmp = new byte[i * 3 + 1]; - System.arraycopy(decodedData, 0, tmp, 0, i * 3); - tmp[encodedIndex] = (byte) (b1 << 2 | b2 >> 4); - return tmp; - } else if (!isPad(d3) && isPad(d4)) { // One PAD e.g. 3cQ[Pad] - b3 = base64Alphabet[d3]; - if ((b3 & 0x3) != 0)// last 2 bits should be zero - return null; - byte[] tmp = new byte[i * 3 + 2]; - System.arraycopy(decodedData, 0, tmp, 0, i * 3); - tmp[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); - tmp[encodedIndex] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); - return tmp; - } else { - return null;// an error like "3c[Pad]r", "3cdX", "3cXd", "3cXX" where X is non data - } - } else { // No PAD e.g 3cQl - b3 = base64Alphabet[d3]; - b4 = base64Alphabet[d4]; - decodedData[encodedIndex++] = (byte) (b1 << 2 | b2 >> 4); - decodedData[encodedIndex++] = (byte) (((b2 & 0xf) << 4) | ((b3 >> 2) & 0xf)); - decodedData[encodedIndex++] = (byte) (b3 << 6 | b4); - - } - - return decodedData; - } - - /** - * returns length of decoded data given an array containing encoded data. WhiteSpace removing is done if data array not valid. - * - * @param base64Data - * @return a -1 would be return if not - */ - static public synchronized int getDecodedDataLength(byte[] base64Data) { - - if (base64Data == null) - return -1; - - if (base64Data.length == 0) - return 0; - - // byte[] normalizedBase64Data = removeWhiteSpace( base64Data );//Remove any whiteSpace - byte[] decodedData = null; - - if ((decodedData = decode(base64Data)) == null)// decode could return a null byte array - return -1; - - return decodedData.length; - } -} diff --git a/src/main/java/com/flickr4java/flickr/util/ByteUtilities.java b/src/main/java/com/flickr4java/flickr/util/ByteUtilities.java index 05d37ef1..256426e0 100644 --- a/src/main/java/com/flickr4java/flickr/util/ByteUtilities.java +++ b/src/main/java/com/flickr4java/flickr/util/ByteUtilities.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; diff --git a/src/main/java/com/flickr4java/flickr/util/DebugInputStream.java b/src/main/java/com/flickr4java/flickr/util/DebugInputStream.java index 08207446..b4e7829d 100644 --- a/src/main/java/com/flickr4java/flickr/util/DebugInputStream.java +++ b/src/main/java/com/flickr4java/flickr/util/DebugInputStream.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; diff --git a/src/main/java/com/flickr4java/flickr/util/DebugOutputStream.java b/src/main/java/com/flickr4java/flickr/util/DebugOutputStream.java index 12e163b2..65b7cd39 100644 --- a/src/main/java/com/flickr4java/flickr/util/DebugOutputStream.java +++ b/src/main/java/com/flickr4java/flickr/util/DebugOutputStream.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; diff --git a/src/main/java/com/flickr4java/flickr/util/IOUtilities.java b/src/main/java/com/flickr4java/flickr/util/IOUtilities.java index adb5f146..7c475f2d 100644 --- a/src/main/java/com/flickr4java/flickr/util/IOUtilities.java +++ b/src/main/java/com/flickr4java/flickr/util/IOUtilities.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; import java.io.IOException; diff --git a/src/main/java/com/flickr4java/flickr/util/ImageUtilities.java b/src/main/java/com/flickr4java/flickr/util/ImageUtilities.java index 6adfd9f2..1808ff82 100644 --- a/src/main/java/com/flickr4java/flickr/util/ImageUtilities.java +++ b/src/main/java/com/flickr4java/flickr/util/ImageUtilities.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; diff --git a/src/main/java/com/flickr4java/flickr/util/StringUtilities.java b/src/main/java/com/flickr4java/flickr/util/StringUtilities.java index 2abb1417..d10296d0 100644 --- a/src/main/java/com/flickr4java/flickr/util/StringUtilities.java +++ b/src/main/java/com/flickr4java/flickr/util/StringUtilities.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; import java.util.Arrays; diff --git a/src/main/java/com/flickr4java/flickr/util/UrlUtilities.java b/src/main/java/com/flickr4java/flickr/util/UrlUtilities.java index 13687b43..37baf300 100644 --- a/src/main/java/com/flickr4java/flickr/util/UrlUtilities.java +++ b/src/main/java/com/flickr4java/flickr/util/UrlUtilities.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; diff --git a/src/main/java/com/flickr4java/flickr/util/XMLUtilities.java b/src/main/java/com/flickr4java/flickr/util/XMLUtilities.java index bf1da72b..fb104ff6 100644 --- a/src/main/java/com/flickr4java/flickr/util/XMLUtilities.java +++ b/src/main/java/com/flickr4java/flickr/util/XMLUtilities.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.util; import org.w3c.dom.Element; diff --git a/src/test/java/com/flickr4java/flickr/test/AuthInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/AuthInterfaceTest.java index 89acc27b..21be6830 100644 --- a/src/test/java/com/flickr4java/flickr/test/AuthInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/AuthInterfaceTest.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.test; @@ -13,11 +11,12 @@ import java.net.URI; import java.net.URISyntaxException; import java.util.Scanner; +import java.util.concurrent.ExecutionException; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuth1Token; import org.junit.Ignore; import org.junit.Test; -import org.scribe.model.Token; -import org.scribe.model.Verifier; import com.flickr4java.flickr.FlickrException; import com.flickr4java.flickr.auth.Auth; @@ -32,15 +31,15 @@ public class AuthInterfaceTest extends Flickr4JavaTest { @Test @Ignore // Ignored as test is interactive so would fail a build - public void testAuthFlow() throws FlickrException, IOException, URISyntaxException { + public void testAuthFlow() throws IOException, URISyntaxException, ExecutionException, InterruptedException, FlickrException { AuthInterface authInterface = flickr.getAuthInterface(); - Token requestToken = authInterface.getRequestToken(); + OAuth1RequestToken requestToken = authInterface.getRequestToken(); assertNotNull(requestToken); assertNotNull(requestToken.getToken()); - assertNotNull(requestToken.getSecret()); + assertNotNull(requestToken.getTokenSecret()); assertTrue(requestToken.getRawResponse().contains("oauth_callback_confirmed=true")); String url = authInterface.getAuthorizationUrl(requestToken, Permission.READ); @@ -66,17 +65,16 @@ public void testAuthFlow() throws FlickrException, IOException, URISyntaxExcepti assertNotNull(code); - Verifier verifier = new Verifier(code); - Token accessToken = authInterface.getAccessToken(requestToken, verifier); + OAuth1Token accessToken = authInterface.getAccessToken(requestToken, code); assertNotNull(accessToken); assertNotNull(accessToken.getToken()); - assertNotNull(accessToken.getSecret()); + assertNotNull(accessToken.getTokenSecret()); Auth checkedAuth = authInterface.checkToken(accessToken); assertNotNull(checkedAuth); assertEquals(accessToken.getToken(), checkedAuth.getToken()); - assertEquals(accessToken.getSecret(), checkedAuth.getTokenSecret()); + assertEquals(accessToken.getTokenSecret(), checkedAuth.getTokenSecret()); assertEquals(Permission.READ, checkedAuth.getPermission()); assertNotNull(checkedAuth.getUser()); assertNotNull(checkedAuth.getUser().getUsername()); @@ -94,10 +92,10 @@ public void testExchangeToken() throws FlickrException { System.out.print(">>"); String flickrAuthToken = in.nextLine(); - Token oAuthToken = authInterface.exchangeAuthToken(flickrAuthToken); + OAuth1RequestToken oAuthToken = authInterface.exchangeAuthToken(flickrAuthToken); assertNotNull(oAuthToken); assertNotNull(oAuthToken.getToken()); - assertNotNull(oAuthToken.getSecret()); + assertNotNull(oAuthToken.getTokenSecret()); } } diff --git a/src/test/java/com/flickr4java/flickr/test/BlogsInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/BlogsInterfaceTest.java index 8d800f43..e615da54 100644 --- a/src/test/java/com/flickr4java/flickr/test/BlogsInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/BlogsInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/CommonsInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/CommonsInterfaceTest.java index 3caeca96..96b58f10 100644 --- a/src/test/java/com/flickr4java/flickr/test/CommonsInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/CommonsInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/ContactsInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/ContactsInterfaceTest.java index 1b4e77fe..9e564a1c 100644 --- a/src/test/java/com/flickr4java/flickr/test/ContactsInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/ContactsInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/FavoritesInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/FavoritesInterfaceTest.java index fb0d8d7f..6a1880b7 100644 --- a/src/test/java/com/flickr4java/flickr/test/FavoritesInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/FavoritesInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/GroupsInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/GroupsInterfaceTest.java index 8246a4b1..362e8b05 100644 --- a/src/test/java/com/flickr4java/flickr/test/GroupsInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/GroupsInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/MembersInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/MembersInterfaceTest.java index 480b4bb1..5a90905b 100644 --- a/src/test/java/com/flickr4java/flickr/test/MembersInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/MembersInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/PandaInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/PandaInterfaceTest.java index 537c57df..c013392f 100644 --- a/src/test/java/com/flickr4java/flickr/test/PandaInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/PandaInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/PeopleInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/PeopleInterfaceTest.java index 2952ff75..3c2aeca2 100644 --- a/src/test/java/com/flickr4java/flickr/test/PeopleInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/PeopleInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/PhotosInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/PhotosInterfaceTest.java index fa95e5ae..f147b223 100644 --- a/src/test/java/com/flickr4java/flickr/test/PhotosInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/PhotosInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/PhotosetsInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/PhotosetsInterfaceTest.java index a33f4175..8de7e511 100644 --- a/src/test/java/com/flickr4java/flickr/test/PhotosetsInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/PhotosetsInterfaceTest.java @@ -74,13 +74,12 @@ public void testEditPhotos() throws FlickrException { PhotosetsInterface iface = flickr.getPhotosetsInterface(); - iface.addPhoto(testSet.getId(), testProperties.getPhotoId()); iface.editPhotos(testSet.getId(), testProperties.getPhotoId(), setPics.toArray(new String[setPics.size()])); Photoset ps = iface.getInfo(testSet.getId()); assertNotNull(ps); assertEquals(testProperties.getPhotoId(), ps.getPrimaryPhoto().getId()); - assertTrue(ps.getPhotoCount() > 2); + assertTrue(ps.getPhotoCount() >= 2); } @Test diff --git a/src/test/java/com/flickr4java/flickr/test/PoolsInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/PoolsInterfaceTest.java index 9ad6e8d2..32eb42cf 100644 --- a/src/test/java/com/flickr4java/flickr/test/PoolsInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/PoolsInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/TestInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/TestInterfaceTest.java index 9e040837..0245df1e 100644 --- a/src/test/java/com/flickr4java/flickr/test/TestInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/TestInterfaceTest.java @@ -1,4 +1,4 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ + package com.flickr4java.flickr.test; diff --git a/src/test/java/com/flickr4java/flickr/test/UploaderTest.java b/src/test/java/com/flickr4java/flickr/test/UploaderTest.java index 2b53823c..9687e4a8 100644 --- a/src/test/java/com/flickr4java/flickr/test/UploaderTest.java +++ b/src/test/java/com/flickr4java/flickr/test/UploaderTest.java @@ -1,27 +1,23 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ - package com.flickr4java.flickr.test; -import static org.junit.Assert.assertNotNull; - import com.flickr4java.flickr.FlickrException; import com.flickr4java.flickr.photos.PhotosInterface; import com.flickr4java.flickr.uploader.UploadMetaData; - import com.flickr4java.flickr.uploader.Uploader; import org.junit.Test; -import java.io.ByteArrayOutputStream; -import java.io.File; -import java.io.FileInputStream; -import java.io.IOException; -import java.io.InputStream; +import java.io.*; +import java.nio.file.Files; + +import static org.junit.Assert.assertNotNull; /** * @author Anthony Eden */ public class UploaderTest extends Flickr4JavaTest { + private static final String DUMMY_PHOTO_ID = "1234567890"; + /** * Test photo uploading using a byte array. * @@ -35,25 +31,21 @@ public void testUploadByteArray() throws IOException, FlickrException { PhotosInterface pint = flickr.getPhotosInterface(); if (testProperties.isRealFlickr()) { - try (InputStream in = new FileInputStream(imageFile); ByteArrayOutputStream out = new ByteArrayOutputStream()) { - int b; - while ((b = in.read()) != -1) { - out.write((byte) b); - } - UploadMetaData metaData = buildPrivatePhotoMetadata(); - // check correct handling of escaped value - metaData.setTitle("óöä"); - String photoId = uploader.upload(out.toByteArray(), metaData); - assertNotNull(photoId); - try { - pint.delete(photoId); - } catch (FlickrException e) { - // Ignore if user doesn't have delete permissions - // This will leave a *private* photo in the test account's photostream! - if (!e.getErrorCode().equals("99")) { - throw e; - } + + UploadMetaData metaData = buildPrivatePhotoMetadata(); + // check correct handling of escaped value + metaData.setTitle("óöä"); + String photoId = uploader.upload(Files.readAllBytes(imageFile.toPath()), metaData); + assertNotNull(photoId); + try { + pint.delete(photoId); + } catch (FlickrException e) { + // Ignore if user doesn't have delete permissions + // This will leave a *private* photo in the test account's photostream! + if (!e.getErrorCode().equals("99")) { + throw e; } + } } else { UploadMetaData metaData = buildPrivatePhotoMetadata(); @@ -142,9 +134,10 @@ public void testReplaceInputStream() throws IOException, FlickrException { } } } else { - UploadMetaData metaData = buildPrivatePhotoMetadata(); - String photoId = uploader.upload("".getBytes(), metaData); - assertNotNull(photoId); + try (InputStream replaceIS = new ByteArrayInputStream("".getBytes())) { + String photoId = uploader.replace(replaceIS, DUMMY_PHOTO_ID, false); + assertNotNull(photoId); + } } } @@ -161,39 +154,32 @@ public void testReplaceByteArray() throws IOException, FlickrException { PhotosInterface pint = flickr.getPhotosInterface(); if (testProperties.isRealFlickr()) { - try (InputStream in = new FileInputStream(imageFile); ByteArrayOutputStream out = new ByteArrayOutputStream()) { - int b; - while ((b = in.read()) != -1) { - out.write((byte) b); - } - // Upload a photo, which we'll replace, then delete - UploadMetaData metaData = buildPrivatePhotoMetadata(); - String photoId = uploader.upload(out.toByteArray(), metaData); + // Upload a photo, which we'll replace, then delete + UploadMetaData metaData = buildPrivatePhotoMetadata(); + String photoId = uploader.upload(Files.readAllBytes(imageFile.toPath()), metaData); - try { - photoId = uploader.replace(out.toByteArray(), photoId, false); - assertNotNull(photoId); - } catch (FlickrException e) { - // Error code 1 means test account is not pro so don't fail test because of that - if (!e.getErrorCode().equals("1")) { - throw e; - } + try { + photoId = uploader.replace(Files.readAllBytes(imageFile.toPath()), photoId, false); + assertNotNull(photoId); + } catch (FlickrException e) { + // Error code 1 means test account is not pro so don't fail test because of that + if (!e.getErrorCode().equals("1")) { + throw e; } + } - try { - pint.delete(photoId); - } catch (FlickrException e) { - // Ignore if user doesn't have delete permissions - // This will leave a *private* photo in the test account's photostream! - if (!e.getErrorCode().equals("99")) { - throw e; - } + try { + pint.delete(photoId); + } catch (FlickrException e) { + // Ignore if user doesn't have delete permissions + // This will leave a *private* photo in the test account's photostream! + if (!e.getErrorCode().equals("99")) { + throw e; } } } else { - UploadMetaData metaData = buildPrivatePhotoMetadata(); - String photoId = uploader.upload("".getBytes(), metaData); + String photoId = uploader.replace("".getBytes(), DUMMY_PHOTO_ID, false); assertNotNull(photoId); } } diff --git a/src/test/java/com/flickr4java/flickr/test/UrlsInterfaceTest.java b/src/test/java/com/flickr4java/flickr/test/UrlsInterfaceTest.java index f6cb3cc2..a1ca91f7 100644 --- a/src/test/java/com/flickr4java/flickr/test/UrlsInterfaceTest.java +++ b/src/test/java/com/flickr4java/flickr/test/UrlsInterfaceTest.java @@ -1,5 +1,3 @@ -/* Copyright 2004, Aetrion LLC. All Rights Reserved. */ - package com.flickr4java.flickr.test; import static org.junit.Assert.assertEquals; diff --git a/src/test/java/com/flickr4java/flickr/test/util/FileTestProperties.java b/src/test/java/com/flickr4java/flickr/test/util/FileTestProperties.java index bc39b9de..31470ac2 100644 --- a/src/test/java/com/flickr4java/flickr/test/util/FileTestProperties.java +++ b/src/test/java/com/flickr4java/flickr/test/util/FileTestProperties.java @@ -21,7 +21,7 @@ public class FileTestProperties implements TestProperties { /** - * Logger for log4j. + * Logger. */ private static Logger _log = LoggerFactory.getLogger(FileTestProperties.class); diff --git a/src/test/java/com/flickr4java/flickr/test/util/FlickrStub.java b/src/test/java/com/flickr4java/flickr/test/util/FlickrStub.java index febd6ac9..9db0bc58 100644 --- a/src/test/java/com/flickr4java/flickr/test/util/FlickrStub.java +++ b/src/test/java/com/flickr4java/flickr/test/util/FlickrStub.java @@ -1,6 +1,4 @@ -/* - * Copyright (c) 2005 Aetrion LLC. - */ + package com.flickr4java.flickr.test.util; import com.flickr4java.flickr.IFlickr; @@ -236,7 +234,7 @@ public ActivityInterface getActivityInterface() { return activityInterface; } - public synchronized BlogsInterface getBlogsInterface() { + public BlogsInterface getBlogsInterface() { if (blogsInterface == null) { blogsInterface = new BlogsInterface(apiKey, sharedSecret, transport); } @@ -288,7 +286,7 @@ public GroupsInterface getGroupsInterface() { /** * @return the interface to the flickr.interestingness methods */ - public synchronized InterestingnessInterface getInterestingnessInterface() { + public InterestingnessInterface getInterestingnessInterface() { if (interestingnessInterface == null) { interestingnessInterface = new InterestingnessInterface(apiKey, sharedSecret, transport); } diff --git a/src/test/java/com/flickr4java/flickr/test/util/Setup.java b/src/test/java/com/flickr4java/flickr/test/util/Setup.java index 9c543b86..3db87122 100644 --- a/src/test/java/com/flickr4java/flickr/test/util/Setup.java +++ b/src/test/java/com/flickr4java/flickr/test/util/Setup.java @@ -6,13 +6,14 @@ import com.flickr4java.flickr.auth.Auth; import com.flickr4java.flickr.auth.AuthInterface; import com.flickr4java.flickr.auth.Permission; +import com.github.scribejava.core.model.OAuth1RequestToken; +import com.github.scribejava.core.model.OAuth1Token; + import java.io.FileInputStream; import java.io.FileOutputStream; import java.io.IOException; import java.util.Properties; import java.util.Scanner; -import org.scribe.model.Token; -import org.scribe.model.Verifier; public class Setup { @@ -20,9 +21,7 @@ public static void main(String[] args) { System.out.println("Flickr4Java: Set up integration test environment"); try { new Setup(); - } catch (IOException e) { - e.printStackTrace(); - } catch (FlickrException e) { + } catch (IOException | FlickrException e) { e.printStackTrace(); } } @@ -38,7 +37,7 @@ public Setup() throws IOException, FlickrException { Scanner scanner = new Scanner(System.in); - Token requestToken = authInterface.getRequestToken(); + OAuth1RequestToken requestToken = authInterface.getRequestToken(); String url = authInterface.getAuthorizationUrl(requestToken, Permission.DELETE); System.out.println("Follow this URL to authorise yourself on Flickr"); @@ -48,18 +47,18 @@ public Setup() throws IOException, FlickrException { String tokenKey = scanner.nextLine().trim(); - Token accessToken = authInterface.getAccessToken(requestToken, new Verifier(tokenKey)); + OAuth1Token accessToken = authInterface.getAccessToken(requestToken, tokenKey); System.out.println("Authentication success"); Auth auth = authInterface.checkToken(accessToken); properties.setProperty("token", accessToken.getToken()); - properties.setProperty("tokensecret", accessToken.getSecret()); + properties.setProperty("tokensecret", accessToken.getTokenSecret()); properties.store(new FileOutputStream(propertiesFile), ""); // This token can be used until the user revokes it. System.out.println("Access token - token = " + accessToken.getToken()); - System.out.println(" - secret = " + accessToken.getSecret()); + System.out.println(" - secret = " + accessToken.getTokenSecret()); System.out.println("(These have been saved to the properties file.)"); System.out.println("Realname: " + auth.getUser().getRealName()); System.out.println("Username: " + auth.getUser().getUsername()); diff --git a/src/test/java/com/flickr4java/flickr/test/util/TestPropertiesFactory.java b/src/test/java/com/flickr4java/flickr/test/util/TestPropertiesFactory.java index 94da3764..36a29188 100644 --- a/src/test/java/com/flickr4java/flickr/test/util/TestPropertiesFactory.java +++ b/src/test/java/com/flickr4java/flickr/test/util/TestPropertiesFactory.java @@ -4,13 +4,14 @@ import org.slf4j.LoggerFactory; import java.io.File; +import java.net.URL; import java.nio.file.Paths; import java.util.Optional; public class TestPropertiesFactory { /** - * Logger for log4j. + * Logger. */ private static Logger _log = LoggerFactory.getLogger(TestPropertiesFactory.class); @@ -31,18 +32,20 @@ public static TestProperties getTestProperties() { } private Optional findPropertyFile() { - Optional fileOptional; + Optional fileOptional = Optional.empty(); String setupPropertiesPath = System.getenv("SETUP_PROPERTIES_PATH"); try { - if (setupPropertiesPath != null) { - _log.debug("Using properties file at " + setupPropertiesPath); + if (setupPropertiesPath != null && setupPropertiesPath.length() > 0) { + _log.info("Using properties file at '{}'", setupPropertiesPath); File properties = new File(setupPropertiesPath); fileOptional = properties.exists() ? Optional.of(properties) : Optional.empty(); } else { - _log.debug("Using properties file /setup.properties from classpath"); - File properties = Paths.get(this.getClass().getResource("/setup.properties").toURI()).toFile(); - fileOptional = properties.exists() ? Optional.of(properties) : Optional.empty(); + URL resource = this.getClass().getResource("/setup.properties"); + if (resource != null) { + fileOptional = Optional.of(Paths.get(resource.toURI()).toFile()); + _log.info("Using properties file /setup.properties from classpath"); + } } } catch (Exception e) { _log.warn("Unable to load properties file", e); diff --git a/src/test/java/com/flickr4java/flickr/test/util/TransportStub.java b/src/test/java/com/flickr4java/flickr/test/util/TransportStub.java index c5c91887..0a8a54f6 100644 --- a/src/test/java/com/flickr4java/flickr/test/util/TransportStub.java +++ b/src/test/java/com/flickr4java/flickr/test/util/TransportStub.java @@ -1,9 +1,8 @@ package com.flickr4java.flickr.test.util; -import com.flickr4java.flickr.FlickrRuntimeException; -import com.flickr4java.flickr.RESTResponse; -import com.flickr4java.flickr.Response; -import com.flickr4java.flickr.Transport; +import com.flickr4java.flickr.*; +import com.flickr4java.flickr.uploader.Payload; +import com.flickr4java.flickr.uploader.UploadMetaData; import com.flickr4java.flickr.uploader.UploaderResponse; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -18,6 +17,7 @@ import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.HashMap; import java.util.Map; public class TransportStub extends Transport { @@ -41,12 +41,13 @@ public Response get(String path, Map parameters, String apiKey, } @Override - public Response post(String path, Map parameters, String apiKey, String sharedSecret, boolean multipart) { - if (multipart) { - return loadMulitpartResponseFromFile("upload", "post"); - } else { - return loadResponseFromFile(parameters.get("method"), "post"); - } + public Response post(String path, Map parameters, String apiKey, String sharedSecret) { + return loadResponseFromFile(parameters.get("method"), "post"); + } + + @Override + public Response postMultiPart(String path, UploadMetaData parameters, Payload payload, String apiKey, String sharedSecret) { + return loadMulitpartResponseFromFile("upload", "post"); } @Override