Permalink
Browse files

Add OverwriteOption and support File object uploads.

Add, update, and fix comments.
Change LiveConnectUtils from public to package visibility.
  • Loading branch information...
1 parent 72d604d commit 85d6d77c164e70e7ddcb170be1bba924f4530cd4 skrueger committed Aug 14, 2012
Showing with 452 additions and 92 deletions.
  1. +4 −1 src/internal/com/microsoft/live/Config.java
  2. +0 −1 src/internal/com/microsoft/live/ErrorMessages.java
  3. +1 −3 src/internal/com/microsoft/live/GetRequest.java
  4. +1 −1 src/internal/com/microsoft/live/HttpCopy.java
  5. +1 −1 src/internal/com/microsoft/live/HttpMove.java
  6. +0 −1 src/internal/com/microsoft/live/JsonEntity.java
  7. +1 −1 src/internal/com/microsoft/live/LiveConnectUtils.java
  8. +3 −3 src/internal/com/microsoft/live/OAuthErrorResponse.java
  9. +6 −3 src/internal/com/microsoft/live/OAuthRequestObserver.java
  10. +4 −2 src/internal/com/microsoft/live/OAuthResponseVisitor.java
  11. +8 −8 src/internal/com/microsoft/live/OAuthSuccessfulResponse.java
  12. +22 −8 src/internal/com/microsoft/live/UploadRequest.java
  13. +3 −4 src/src/com/microsoft/live/LiveAuthClient.java
  14. +9 −0 src/src/com/microsoft/live/LiveAuthException.java
  15. +21 −0 src/src/com/microsoft/live/LiveAuthListener.java
  16. +160 −45 src/src/com/microsoft/live/LiveConnectClient.java
  17. +54 −0 src/src/com/microsoft/live/LiveConnectSession.java
  18. +19 −1 src/src/com/microsoft/live/LiveDownloadOperation.java
  19. +19 −0 src/src/com/microsoft/live/LiveDownloadOperationListener.java
  20. +21 −2 src/src/com/microsoft/live/LiveOperation.java
  21. +4 −0 src/src/com/microsoft/live/LiveOperationException.java
  22. +16 −2 src/src/com/microsoft/live/LiveOperationListener.java
  23. +8 −0 src/src/com/microsoft/live/LiveStatus.java
  24. +22 −5 src/src/com/microsoft/live/LiveUploadOperationListener.java
  25. +45 −0 src/src/com/microsoft/live/OverwriteOption.java
@@ -9,7 +9,9 @@
import android.net.Uri;
import android.text.TextUtils;
-/** Config is a singleton class that contains the values used throughout the SDK. */
+/**
+ * Config is a singleton class that contains the values used throughout the SDK.
+ */
enum Config {
INSTANCE;
@@ -21,6 +23,7 @@
private Uri oAuthTokenUri;
Config() {
+ // initialize default values for constants
apiUri = Uri.parse("https://apis.live.net/v5.0");
apiVersion = "5.0";
oAuthAuthorizeUri = Uri.parse("https://login.live.com/oauth20_authorize.srf");
@@ -11,7 +11,6 @@
* used in for errors and exceptions.
*/
final class ErrorMessages {
-
public static final String ABSOLUTE_PARAMETER =
"Input parameter '%1$s' is invalid. '%1$s' cannot be absolute.";
public static final String CLIENT_ERROR =
@@ -25,9 +25,7 @@
* @param client to perform Http requests on
* @param path of the request
*/
- public GetRequest(LiveConnectSession session,
- HttpClient client,
- String path) {
+ public GetRequest(LiveConnectSession session, HttpClient client, String path) {
super(session, client, JsonResponseHandler.INSTANCE, path);
}
@@ -22,7 +22,7 @@
/**
* Constructs a new HttpCopy with the given uri and initializes its member variables.
- *
+ *
* @param uri of the request
*/
public HttpCopy(String uri) {
@@ -22,7 +22,7 @@
/**
* Constructs a new HttpMove with the given uri and initializes its member variables.
- *
+ *
* @param uri of the request
*/
public HttpMove(String uri) {
@@ -14,7 +14,6 @@
/**
* JsonEntity is an Entity that contains a Json body
- *
*/
class JsonEntity extends StringEntity {
@@ -13,7 +13,7 @@
* LiveConnectUtils is a non-instantiable utility class that contains various helper
* methods and constants.
*/
-public final class LiveConnectUtils {
+final class LiveConnectUtils {
/**
* Checks to see if the passed in Object is null, and throws a
@@ -22,7 +22,7 @@
* error_uri are optional
*/
public static class Builder {
- private ErrorType error;
+ private final ErrorType error;
private String errorDescription;
private String errorUri;
@@ -120,14 +120,14 @@ public static boolean validOAuthErrorResponse(JSONObject response) {
/**
* OPTIONAL. A URI identifying a human-readable web page with
* information about the error, used to provide the client
- * developer with additional information about the error.
+ * developer with additional information about the error.
*/
private final String errorUri;
/**
* OAuthErrorResponse constructor. It is private to enforce
* the use of the Builder.
- *
+ *
* @param builder to use to construct the object.
*/
private OAuthErrorResponse(Builder builder) {
@@ -6,17 +6,20 @@
package com.microsoft.live;
-/** An observer of an OAuth Request. It will be notified of an Exception or of a Response. */
+/**
+ * An observer of an OAuth Request. It will be notified of an Exception or of a Response.
+ */
interface OAuthRequestObserver {
/**
- * Callback used on an exception
+ * Callback used on an exception.
*
* @param exception
*/
public void onException(LiveAuthException exception);
/**
- * Callback used on a response
+ * Callback used on a response.
+ *
* @param response
*/
public void onResponse(OAuthResponse response);
@@ -12,13 +12,15 @@
interface OAuthResponseVisitor {
/**
- * Called when an OAuthSuccessfulResponse is visited
+ * Called when an OAuthSuccessfulResponse is visited.
+ *
* @param response being visited
*/
public void visit(OAuthSuccessfulResponse response);
/**
- * Called when an OAuthErrorResponse is being visited
+ * Called when an OAuthErrorResponse is being visited.
+ *
* @param response being visited
*/
public void visit(OAuthErrorResponse response);
@@ -75,29 +75,29 @@ public Builder scope(String scope) {
public static OAuthSuccessfulResponse createFromFragment(
Map<String, String> fragmentParameters) throws LiveAuthException {
- final String accessToken = fragmentParameters.get(OAuth.ACCESS_TOKEN);
- final String tokenTypeString = fragmentParameters.get(OAuth.TOKEN_TYPE);
+ String accessToken = fragmentParameters.get(OAuth.ACCESS_TOKEN);
+ String tokenTypeString = fragmentParameters.get(OAuth.TOKEN_TYPE);
// must have accessToken and tokenTypeString to be a valid OAuthSuccessfulResponse
assert accessToken != null;
assert tokenTypeString != null;
- final TokenType tokenType;
+ TokenType tokenType;
try {
tokenType = TokenType.valueOf(tokenTypeString.toUpperCase());
- } catch (final IllegalArgumentException e) {
+ } catch (IllegalArgumentException e) {
throw new LiveAuthException(ErrorMessages.SERVER_ERROR, e);
}
- final OAuthSuccessfulResponse.Builder builder =
+ OAuthSuccessfulResponse.Builder builder =
new OAuthSuccessfulResponse.Builder(accessToken, tokenType);
- final String authenticationToken = fragmentParameters.get(OAuth.AUTHENTICATION_TOKEN);
+ String authenticationToken = fragmentParameters.get(OAuth.AUTHENTICATION_TOKEN);
if (authenticationToken != null) {
builder.authenticationToken(authenticationToken);
}
- final String expiresInString = fragmentParameters.get(OAuth.EXPIRES_IN);
+ String expiresInString = fragmentParameters.get(OAuth.EXPIRES_IN);
if (expiresInString != null) {
final int expiresIn;
try {
@@ -109,7 +109,7 @@ public static OAuthSuccessfulResponse createFromFragment(
builder.expiresIn(expiresIn);
}
- final String scope = fragmentParameters.get(OAuth.SCOPE);
+ String scope = fragmentParameters.get(OAuth.SCOPE);
if (scope != null) {
builder.scope(scope);
}
@@ -21,20 +21,28 @@
public static final String METHOD = HttpPut.METHOD_NAME;
+ private static final String FILE_PATH = "file.";
private static final String ERROR_KEY = "error";
private static final String UPLOAD_LOCATION_KEY = "upload_location";
private HttpUriRequest currentRequest;
private final String filename;
private final boolean isRelativePath;
- private final boolean overwrite;
+
+ /**
+ * true if the given path refers to a File Object
+ * (i.e., the path begins with "/file").
+ */
+ private final boolean isFileUpload;
+
+ private final OverwriteOption overwrite;
public UploadRequest(LiveConnectSession session,
HttpClient client,
String path,
HttpEntity entity,
String filename,
- boolean overwrite) {
+ OverwriteOption overwrite) {
super(session,
client,
JsonResponseHandler.INSTANCE,
@@ -47,7 +55,11 @@ public UploadRequest(LiveConnectSession session,
this.filename = filename;
this.overwrite = overwrite;
- this.isRelativePath = Uri.parse(path).isRelative();
+ Uri uriPath = Uri.parse(path);
+ this.isRelativePath = uriPath.isRelative();
+
+ String lowerCasePath = uriPath.getPath().toLowerCase();
+ this.isFileUpload = lowerCasePath.indexOf(FILE_PATH) != -1;
}
@Override
@@ -87,11 +99,13 @@ public JSONObject execute() throws LiveOperationException {
uploadRequestUri = this.requestUri;
}
- // Add the file name to the upload location
- // and don't forget to set the overwrite query parameter
- uploadRequestUri.appendPath(this.filename);
- uploadRequestUri.appendQueryParameter(QueryParameters.OVERWRITE,
- Boolean.toString(this.overwrite));
+ if (!this.isFileUpload) {
+ // if it is not a file upload it is a folder upload and we must
+ // add the file name to the upload location
+ // and don't forget to set the overwrite query parameter
+ uploadRequestUri.appendPath(this.filename);
+ this.overwrite.appendQueryParameter(uploadRequestUri);
+ }
HttpPut uploadRequest = new HttpPut(uploadRequestUri.toString());
uploadRequest.setEntity(this.entity);
@@ -510,13 +510,12 @@ public void logout(LiveAuthListener listener, Object userState) {
for (String cookieKey : cookieKeys) {
String value = TextUtils.join("", new String[] {
cookieKey,
- "=; expires=Thu, 30-Oct-1980 16:00:00 GMT; domain=",
+ "=; expires=Thu, 30-Oct-1980 16:00:00 GMT;domain=",
domain,
- "; path=/; version=1"
+ ";path=/;version=1"
});
-
+
manager.setCookie(url, value);
- manager.setCookie(url, value + "; Secure");
}
cookieSyncManager.sync();
@@ -6,6 +6,9 @@
package com.microsoft.live;
+/**
+ * Indicates that an exception occurred during the Auth process.
+ */
public class LiveAuthException extends Exception {
private static final long serialVersionUID = 3368677530670470856L;
@@ -44,10 +47,16 @@
this.errorUri = errorUri;
}
+ /**
+ * @return Returns the authentication error.
+ */
public String getError() {
return this.error;
}
+ /**
+ * @return Returns the error URI.
+ */
public String getErrorUri() {
return this.errorUri;
}
@@ -6,7 +6,28 @@
package com.microsoft.live;
+/**
+ * Handles callback methods for LiveAuthClient init, login, and logout methods.
+ * Returns the * status of the operation when onAuthComplete is called. If there was an error
+ * during the operation, onAuthError is called with the exception that was thrown.
+ */
public interface LiveAuthListener {
+
+ /**
+ * Invoked when the operation completes successfully.
+ *
+ * @param status The {@link LiveStatus} for an operation. If successful, the status is
+ * CONNECTED. If unsuccessful, NOT_CONNECTED or UNKNOWN are returned.
+ * @param session The {@link LiveConnectSession} from the {@link LiveAuthClient}.
+ * @param userState An arbitrary object that is used to determine the caller of the method.
+ */
public void onAuthComplete(LiveStatus status, LiveConnectSession session, Object userState);
+
+ /**
+ * Invoked when the method call fails.
+ *
+ * @param exception The {@link LiveAuthException} error.
+ * @param userState An arbitrary object that is used to determine the caller of the method.
+ */
public void onAuthError(LiveAuthException exception, Object userState);
}
Oops, something went wrong.

0 comments on commit 85d6d77

Please sign in to comment.