Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Updated headers to reflect that Robert Drysdale contributed (a lot) t…

…o those files.
  • Loading branch information...
commit 65bcd8f2a8ad709b006b5a69d47747976a5826a3 1 parent 18318b9
@vbfischer authored
View
49 src/main/java/org/springframework/social/dropbox/api/impl/DropboxModule.java
@@ -1,25 +1,24 @@
-package org.springframework.social.dropbox.api.impl;
-
-import org.codehaus.jackson.Version;
-import org.codehaus.jackson.map.module.SimpleModule;
-import org.springframework.social.dropbox.api.DropboxUserProfile;
-import org.springframework.social.dropbox.api.FileUrl;
-import org.springframework.social.dropbox.api.Metadata;
-
-/**
- * User: Bryce Fischer
- * Date: 5/17/11
- * Time: 11:06 AM
- */
-public class DropboxModule extends SimpleModule {
- public DropboxModule() {
- super("DropboxModule", new Version(1, 0, 0, null));
- }
-
- @Override
- public void setupModule(SetupContext context) {
- context.setMixInAnnotations(DropboxUserProfile.class, DropboxUserProfileMixin.class);
- context.setMixInAnnotations(Metadata.class, MetadataMixin.class);
- context.setMixInAnnotations(FileUrl.class, FileUrlMixin.class);
- }
-}
+package org.springframework.social.dropbox.api.impl;
+
+import org.codehaus.jackson.Version;
+import org.codehaus.jackson.map.module.SimpleModule;
+import org.springframework.social.dropbox.api.DropboxUserProfile;
+import org.springframework.social.dropbox.api.FileUrl;
+import org.springframework.social.dropbox.api.Metadata;
+
+/**
+ * @author Bryce Fischer
+ * @author Robert Drysdale
+ */
+public class DropboxModule extends SimpleModule {
+ public DropboxModule() {
+ super("DropboxModule", new Version(1, 0, 0, null));
+ }
+
+ @Override
+ public void setupModule(SetupContext context) {
+ context.setMixInAnnotations(DropboxUserProfile.class, DropboxUserProfileMixin.class);
+ context.setMixInAnnotations(Metadata.class, MetadataMixin.class);
+ context.setMixInAnnotations(FileUrl.class, FileUrlMixin.class);
+ }
+}
View
415 src/main/java/org/springframework/social/dropbox/api/impl/DropboxTemplate.java
@@ -1,210 +1,205 @@
-package org.springframework.social.dropbox.api.impl;
-
-import java.io.InputStream;
-import java.net.URI;
-import java.util.List;
-
-import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.map.ObjectMapper;
-import org.codehaus.jackson.type.TypeReference;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.HttpMethod;
-import org.springframework.http.client.ClientHttpRequest;
-import org.springframework.http.client.ClientHttpResponse;
-import org.springframework.http.converter.HttpMessageConverter;
-import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
-import org.springframework.social.dropbox.api.Dropbox;
-import org.springframework.social.dropbox.api.DropboxFile;
-import org.springframework.social.dropbox.api.DropboxUserProfile;
-import org.springframework.social.dropbox.api.FileUrl;
-import org.springframework.social.dropbox.api.Metadata;
-import org.springframework.social.oauth1.AbstractOAuth1ApiBinding;
-import org.springframework.util.LinkedMultiValueMap;
-import org.springframework.util.MultiValueMap;
-import org.springframework.web.client.DefaultResponseErrorHandler;
-import org.springframework.web.client.ResponseErrorHandler;
-import org.springframework.web.client.RestTemplate;
-import org.springframework.web.util.UriTemplate;
-
-/**
- * User: Bryce Fischer
- * Date: 5/17/11
- * Time: 9:22 AM
- */
-public class DropboxTemplate extends AbstractOAuth1ApiBinding implements Dropbox {
- private final String appFolderUrl;
- private ObjectMapper objectMapper;
- public DropboxTemplate(String appKey, String appSecret, String accessToken, String accessTokenSecret, boolean appFolder) {
- super(appKey, appSecret, accessToken, accessTokenSecret);
- appFolderUrl = appFolder ? "sandbox" : "dropbox";
- registerDropboxJsonModule(getRestTemplate());
- }
-
- public DropboxTemplate() {
- super();
- appFolderUrl = null;
- }
-
- public DropboxUserProfile getUserProfile() {
- return getRestTemplate().getForObject(ACCOUNT_INFO_URL, DropboxUserProfile.class);
- }
-
- public Metadata getItemMetadata(String path) {
- return getRestTemplate().getForObject(METADATA_URL, Metadata.class, appFolderUrl, path);
- }
-
- public Metadata restore(String path, String rev) {
- return getRestTemplate().getForObject(RESTORE_URL + "?rev=" + rev, Metadata.class, appFolderUrl, path);
- }
-
- public Metadata copy(String fromPath, String toPath) {
- MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
- vars.add("root", appFolderUrl);
- vars.add("from_path", fromPath);
- vars.add("to_path", toPath);
- return getRestTemplate().postForObject(COPY_URL, vars, Metadata.class);
- }
-
- public Metadata createFolder(String folder) {
- MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
- vars.add("root", appFolderUrl);
- vars.add("path", folder);
- return getRestTemplate().postForObject(CREATE_FOLDER_URL, vars, Metadata.class);
- }
-
- public Metadata delete(String path) {
- MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
- vars.add("root", appFolderUrl);
- vars.add("path", path);
- return getRestTemplate().postForObject(DELETE_URL, vars, Metadata.class);
- }
-
- public Metadata move(String fromPath, String toPath) {
- MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
- vars.add("root", appFolderUrl);
- vars.add("from_path", fromPath);
- vars.add("to_path", toPath);
- return getRestTemplate().postForObject(MOVE_URL, vars, Metadata.class);
- }
-
- public List<Metadata> getRevisions(String path) {
- JsonNode node = getRestTemplate().getForObject(REVISIONS_URL, JsonNode.class, appFolderUrl, path);
-
- try {
- return objectMapper.readValue(node, new TypeReference<List<Metadata>>() {});
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public List<Metadata> search(String path, String query) {
- JsonNode node = getRestTemplate().getForObject(SEARCH_URL + "?query=" + query, JsonNode.class, appFolderUrl, path);
-
- try {
- return objectMapper.readValue(node, new TypeReference<List<Metadata>>() {});
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public DropboxFile getThumbnail(String path) {
- try {
- UriTemplate uriTemplate = new UriTemplate(THUMBNAILS_URL);
- URI uri = uriTemplate.expand(appFolderUrl, path);
- ClientHttpResponse response = getRestTemplate().getRequestFactory().createRequest(uri, HttpMethod.GET).execute();
- HttpHeaders headers = response.getHeaders();
-
- return new DropboxFile(
- headers.getContentType().toString(),
- headers.getContentLength(),
- response.getBody());
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public DropboxFile getFile(String path) {
- try {
- UriTemplate uriTemplate = new UriTemplate(FILE_URL);
- URI uri = uriTemplate.expand(appFolderUrl, path);
- ClientHttpResponse response = getRestTemplate().getRequestFactory().createRequest(uri, HttpMethod.GET).execute();
- HttpHeaders headers = response.getHeaders();
-
- return new DropboxFile(
- headers.getContentType().toString(),
- headers.getContentLength(),
- response.getBody());
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- public FileUrl getMedia(String path) {
- return getRestTemplate().getForObject(MEDIA_URL, FileUrl.class, appFolderUrl, path);
- }
-
- public FileUrl getShare(String path) {
- return getRestTemplate().getForObject(SHARES_URL, FileUrl.class, appFolderUrl, path);
- }
-
- public Metadata putFile(String path, byte[] file) {
- UriTemplate uriTemplate = new UriTemplate(FILE_PUT_URL);
- URI uri = uriTemplate.expand(appFolderUrl, path);
-
- try {
- ClientHttpRequest request = getRestTemplate().getRequestFactory().createRequest(uri, HttpMethod.PUT);
- request.getBody().write(file);
-
- ClientHttpResponse response = request.execute();
- ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
- if (errorHandler.hasError(response)) {
- errorHandler.handleError(response);
- return null;
- }
- else {
- InputStream stream = response.getBody();
- return objectMapper.readValue(stream, Metadata.class);
- }
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-
- private void registerDropboxJsonModule(RestTemplate restTemplate) {
- List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
-
- for (HttpMessageConverter<?> converter : converters) {
- if (converter instanceof MappingJacksonHttpMessageConverter) {
- MappingJacksonHttpMessageConverter jsonConverter = (MappingJacksonHttpMessageConverter) converter;
-
- objectMapper = new ObjectMapper();
- objectMapper.registerModule(new DropboxModule());
- jsonConverter.setObjectMapper(objectMapper);
- }
- }
- }
-
- public static final String BASE_URL = "https://api.dropbox.com/1/";
- public static final String BASE_CONTENT_URL = "https://api-content.dropbox.com/1/";
-
- public static final String ACCOUNT_INFO_URL = BASE_URL + "account/info";
- public static final String COPY_URL = BASE_URL + "fileops/copy";
- public static final String CREATE_FOLDER_URL = BASE_URL + "fileops/create_folder";
- public static final String DELETE_URL = BASE_URL + "fileops/delete";
- public static final String FILE_URL = BASE_CONTENT_URL + "files/{appFolderUrl}/{path}";
- public static final String FILE_POST_URL = BASE_CONTENT_URL + "files/{appFolderUrl}/{path}";
- public static final String FILE_PUT_URL = BASE_CONTENT_URL + "files_put/{appFolderUrl}/{path}";
- public static final String MEDIA_URL = BASE_URL + "media/{appFolderUrl}/{path}";
- public static final String METADATA_URL = BASE_URL + "metadata/{appFolderUrl}/{path}";
- public static final String MOVE_URL = BASE_URL + "fileops/move";
- public static final String RESTORE_URL = BASE_URL + "restore/{appFolderUrl}/{path}";
- public static final String REVISIONS_URL = BASE_URL + "revisions/{appFolderUrl}/{path}";
- public static final String SEARCH_URL = BASE_URL + "search/{appFolderUrl}/{path}";
- public static final String SHARES_URL = BASE_URL + "shares/{appFolderUrl}/{path}";
- public static final String THUMBNAILS_URL = BASE_CONTENT_URL + "thumbnails/{appFolderUrl}/{path}";
-}
+package org.springframework.social.dropbox.api.impl;
+
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.map.ObjectMapper;
+import org.codehaus.jackson.type.TypeReference;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.HttpMethod;
+import org.springframework.http.client.ClientHttpRequest;
+import org.springframework.http.client.ClientHttpResponse;
+import org.springframework.http.converter.HttpMessageConverter;
+import org.springframework.http.converter.json.MappingJacksonHttpMessageConverter;
+import org.springframework.social.dropbox.api.*;
+import org.springframework.social.oauth1.AbstractOAuth1ApiBinding;
+import org.springframework.util.LinkedMultiValueMap;
+import org.springframework.util.MultiValueMap;
+import org.springframework.web.client.DefaultResponseErrorHandler;
+import org.springframework.web.client.ResponseErrorHandler;
+import org.springframework.web.client.RestTemplate;
+import org.springframework.web.util.UriTemplate;
+
+import java.io.InputStream;
+import java.net.URI;
+import java.util.List;
+
+/**
+ * @author Bryce Fischer
+ * @author Robert Drysdale
+ */
+public class DropboxTemplate extends AbstractOAuth1ApiBinding implements Dropbox {
+ private final String appFolderUrl;
+ private ObjectMapper objectMapper;
+ public DropboxTemplate(String appKey, String appSecret, String accessToken, String accessTokenSecret, boolean appFolder) {
+ super(appKey, appSecret, accessToken, accessTokenSecret);
+ appFolderUrl = appFolder ? "sandbox" : "dropbox";
+ registerDropboxJsonModule(getRestTemplate());
+ }
+
+ public DropboxTemplate() {
+ super();
+ appFolderUrl = null;
+ }
+
+ public DropboxUserProfile getUserProfile() {
+ return getRestTemplate().getForObject(ACCOUNT_INFO_URL, DropboxUserProfile.class);
+ }
+
+ public Metadata getItemMetadata(String path) {
+ return getRestTemplate().getForObject(METADATA_URL, Metadata.class, appFolderUrl, path);
+ }
+
+ public Metadata restore(String path, String rev) {
+ return getRestTemplate().getForObject(RESTORE_URL + "?rev=" + rev, Metadata.class, appFolderUrl, path);
+ }
+
+ public Metadata copy(String fromPath, String toPath) {
+ MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
+ vars.add("root", appFolderUrl);
+ vars.add("from_path", fromPath);
+ vars.add("to_path", toPath);
+ return getRestTemplate().postForObject(COPY_URL, vars, Metadata.class);
+ }
+
+ public Metadata createFolder(String folder) {
+ MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
+ vars.add("root", appFolderUrl);
+ vars.add("path", folder);
+ return getRestTemplate().postForObject(CREATE_FOLDER_URL, vars, Metadata.class);
+ }
+
+ public Metadata delete(String path) {
+ MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
+ vars.add("root", appFolderUrl);
+ vars.add("path", path);
+ return getRestTemplate().postForObject(DELETE_URL, vars, Metadata.class);
+ }
+
+ public Metadata move(String fromPath, String toPath) {
+ MultiValueMap<String, String> vars = new LinkedMultiValueMap<String, String>();
+ vars.add("root", appFolderUrl);
+ vars.add("from_path", fromPath);
+ vars.add("to_path", toPath);
+ return getRestTemplate().postForObject(MOVE_URL, vars, Metadata.class);
+ }
+
+ public List<Metadata> getRevisions(String path) {
+ JsonNode node = getRestTemplate().getForObject(REVISIONS_URL, JsonNode.class, appFolderUrl, path);
+
+ try {
+ return objectMapper.readValue(node, new TypeReference<List<Metadata>>() {});
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public List<Metadata> search(String path, String query) {
+ JsonNode node = getRestTemplate().getForObject(SEARCH_URL + "?query=" + query, JsonNode.class, appFolderUrl, path);
+
+ try {
+ return objectMapper.readValue(node, new TypeReference<List<Metadata>>() {});
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public DropboxFile getThumbnail(String path) {
+ try {
+ UriTemplate uriTemplate = new UriTemplate(THUMBNAILS_URL);
+ URI uri = uriTemplate.expand(appFolderUrl, path);
+ ClientHttpResponse response = getRestTemplate().getRequestFactory().createRequest(uri, HttpMethod.GET).execute();
+ HttpHeaders headers = response.getHeaders();
+
+ return new DropboxFile(
+ headers.getContentType().toString(),
+ headers.getContentLength(),
+ response.getBody());
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public DropboxFile getFile(String path) {
+ try {
+ UriTemplate uriTemplate = new UriTemplate(FILE_URL);
+ URI uri = uriTemplate.expand(appFolderUrl, path);
+ ClientHttpResponse response = getRestTemplate().getRequestFactory().createRequest(uri, HttpMethod.GET).execute();
+ HttpHeaders headers = response.getHeaders();
+
+ return new DropboxFile(
+ headers.getContentType().toString(),
+ headers.getContentLength(),
+ response.getBody());
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ public FileUrl getMedia(String path) {
+ return getRestTemplate().getForObject(MEDIA_URL, FileUrl.class, appFolderUrl, path);
+ }
+
+ public FileUrl getShare(String path) {
+ return getRestTemplate().getForObject(SHARES_URL, FileUrl.class, appFolderUrl, path);
+ }
+
+ public Metadata putFile(String path, byte[] file) {
+ UriTemplate uriTemplate = new UriTemplate(FILE_PUT_URL);
+ URI uri = uriTemplate.expand(appFolderUrl, path);
+
+ try {
+ ClientHttpRequest request = getRestTemplate().getRequestFactory().createRequest(uri, HttpMethod.PUT);
+ request.getBody().write(file);
+
+ ClientHttpResponse response = request.execute();
+ ResponseErrorHandler errorHandler = new DefaultResponseErrorHandler();
+ if (errorHandler.hasError(response)) {
+ errorHandler.handleError(response);
+ return null;
+ }
+ else {
+ InputStream stream = response.getBody();
+ return objectMapper.readValue(stream, Metadata.class);
+ }
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+
+ private void registerDropboxJsonModule(RestTemplate restTemplate) {
+ List<HttpMessageConverter<?>> converters = restTemplate.getMessageConverters();
+
+ for (HttpMessageConverter<?> converter : converters) {
+ if (converter instanceof MappingJacksonHttpMessageConverter) {
+ MappingJacksonHttpMessageConverter jsonConverter = (MappingJacksonHttpMessageConverter) converter;
+
+ objectMapper = new ObjectMapper();
+ objectMapper.registerModule(new DropboxModule());
+ jsonConverter.setObjectMapper(objectMapper);
+ }
+ }
+ }
+
+ public static final String BASE_URL = "https://api.dropbox.com/1/";
+ public static final String BASE_CONTENT_URL = "https://api-content.dropbox.com/1/";
+
+ public static final String ACCOUNT_INFO_URL = BASE_URL + "account/info";
+ public static final String COPY_URL = BASE_URL + "fileops/copy";
+ public static final String CREATE_FOLDER_URL = BASE_URL + "fileops/create_folder";
+ public static final String DELETE_URL = BASE_URL + "fileops/delete";
+ public static final String FILE_URL = BASE_CONTENT_URL + "files/{appFolderUrl}/{path}";
+ public static final String FILE_POST_URL = BASE_CONTENT_URL + "files/{appFolderUrl}/{path}";
+ public static final String FILE_PUT_URL = BASE_CONTENT_URL + "files_put/{appFolderUrl}/{path}";
+ public static final String MEDIA_URL = BASE_URL + "media/{appFolderUrl}/{path}";
+ public static final String METADATA_URL = BASE_URL + "metadata/{appFolderUrl}/{path}";
+ public static final String MOVE_URL = BASE_URL + "fileops/move";
+ public static final String RESTORE_URL = BASE_URL + "restore/{appFolderUrl}/{path}";
+ public static final String REVISIONS_URL = BASE_URL + "revisions/{appFolderUrl}/{path}";
+ public static final String SEARCH_URL = BASE_URL + "search/{appFolderUrl}/{path}";
+ public static final String SHARES_URL = BASE_URL + "shares/{appFolderUrl}/{path}";
+ public static final String THUMBNAILS_URL = BASE_CONTENT_URL + "thumbnails/{appFolderUrl}/{path}";
+}
View
80 src/main/java/org/springframework/social/dropbox/api/impl/DropboxUserProfileMixin.java
@@ -1,41 +1,39 @@
-package org.springframework.social.dropbox.api.impl;
-
-import org.codehaus.jackson.JsonNode;
-import org.codehaus.jackson.JsonParser;
-import org.codehaus.jackson.annotate.JsonIgnoreProperties;
-import org.codehaus.jackson.map.DeserializationContext;
-import org.codehaus.jackson.map.JsonDeserializer;
-import org.codehaus.jackson.map.annotate.JsonDeserialize;
-import org.springframework.social.dropbox.api.DropboxUserProfile;
-
-import java.io.IOException;
-import java.math.BigInteger;
-
-/**
- * User: Bryce Fischer
- * Date: 5/17/11
- * Time: 11:07 AM
- */
-@JsonIgnoreProperties(ignoreUnknown = true)
-@JsonDeserialize(using = DropboxUserProfileMixin.DropboxUserProfileDeserializer.class)
-public class DropboxUserProfileMixin {
- static class DropboxUserProfileDeserializer extends JsonDeserializer<DropboxUserProfile>{
- @Override
- public DropboxUserProfile deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
- JsonNode tree = jp.readValueAsTree();
-
- String referralLink = tree.get("referral_link").getValueAsText();
- String country = tree.get("country").getValueAsText();
- String displayName = tree.get("display_name").getValueAsText();
- String email = tree.get("email").getValueAsText();
- BigInteger uid = tree.get("uid").getBigIntegerValue();
-
- JsonNode quotaNode = tree.get("quota_info");
- BigInteger sharedQuota = quotaNode.get("shared").getBigIntegerValue();
- BigInteger quota = quotaNode.get("quota").getBigIntegerValue();
- BigInteger normalQuota = quotaNode.get("normal").getBigIntegerValue();
-
- return new DropboxUserProfile(uid, displayName, email, country, referralLink, sharedQuota, quota, normalQuota);
- }
- }
-}
+package org.springframework.social.dropbox.api.impl;
+
+import org.codehaus.jackson.JsonNode;
+import org.codehaus.jackson.JsonParser;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.map.DeserializationContext;
+import org.codehaus.jackson.map.JsonDeserializer;
+import org.codehaus.jackson.map.annotate.JsonDeserialize;
+import org.springframework.social.dropbox.api.DropboxUserProfile;
+
+import java.io.IOException;
+import java.math.BigInteger;
+
+/**
+ * @author Bryce Fischer
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+@JsonDeserialize(using = DropboxUserProfileMixin.DropboxUserProfileDeserializer.class)
+public class DropboxUserProfileMixin {
+ static class DropboxUserProfileDeserializer extends JsonDeserializer<DropboxUserProfile>{
+ @Override
+ public DropboxUserProfile deserialize(JsonParser jp, DeserializationContext ctxt) throws IOException {
+ JsonNode tree = jp.readValueAsTree();
+
+ String referralLink = tree.get("referral_link").getValueAsText();
+ String country = tree.get("country").getValueAsText();
+ String displayName = tree.get("display_name").getValueAsText();
+ String email = tree.get("email").getValueAsText();
+ BigInteger uid = tree.get("uid").getBigIntegerValue();
+
+ JsonNode quotaNode = tree.get("quota_info");
+ BigInteger sharedQuota = quotaNode.get("shared").getBigIntegerValue();
+ BigInteger quota = quotaNode.get("quota").getBigIntegerValue();
+ BigInteger normalQuota = quotaNode.get("normal").getBigIntegerValue();
+
+ return new DropboxUserProfile(uid, displayName, email, country, referralLink, sharedQuota, quota, normalQuota);
+ }
+ }
+}
View
34 src/main/java/org/springframework/social/dropbox/api/impl/FileUrlMixin.java
@@ -1,15 +1,19 @@
-package org.springframework.social.dropbox.api.impl;
-
-import java.util.Date;
-
-import org.codehaus.jackson.annotate.JsonCreator;
-import org.codehaus.jackson.annotate.JsonIgnoreProperties;
-import org.codehaus.jackson.annotate.JsonProperty;
-
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class FileUrlMixin {
- @JsonCreator
- public FileUrlMixin(
- @JsonProperty("url") String url,
- @JsonProperty("expires") Date expires) {}
-}
+package org.springframework.social.dropbox.api.impl;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+import java.util.Date;
+
+/**
+ * @author Robert Drysdale
+ *
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class FileUrlMixin {
+ @JsonCreator
+ public FileUrlMixin(
+ @JsonProperty("url") String url,
+ @JsonProperty("expires") Date expires) {}
+}
View
59 src/main/java/org/springframework/social/dropbox/api/impl/MetadataMixin.java
@@ -1,28 +1,31 @@
-package org.springframework.social.dropbox.api.impl;
-
-import java.util.Date;
-import java.util.List;
-
-import org.codehaus.jackson.annotate.JsonCreator;
-import org.codehaus.jackson.annotate.JsonIgnoreProperties;
-import org.codehaus.jackson.annotate.JsonProperty;
-import org.springframework.social.dropbox.api.Metadata;
-
-@JsonIgnoreProperties(ignoreUnknown = true)
-public class MetadataMixin {
- @JsonCreator
- public MetadataMixin(
- @JsonProperty("size") String size,
- @JsonProperty("bytes") int bytes,
- @JsonProperty("is_dir") boolean isDir,
- @JsonProperty("is_deleted") boolean isDeleted,
- @JsonProperty("rev") String rev,
- @JsonProperty("hash") String hash,
- @JsonProperty("thumb_exists") boolean thumbExists,
- @JsonProperty("icon") String icon,
- @JsonProperty("modified") Date modified,
- @JsonProperty("root") String root,
- @JsonProperty("path") String path,
- @JsonProperty("mime_type") String mimeType,
- @JsonProperty("contents") List<Metadata> contents) {}
-}
+package org.springframework.social.dropbox.api.impl;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.springframework.social.dropbox.api.Metadata;
+
+import java.util.Date;
+import java.util.List;
+
+/**
+ * @author Robert Drysdale
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class MetadataMixin {
+ @JsonCreator
+ public MetadataMixin(
+ @JsonProperty("size") String size,
+ @JsonProperty("bytes") int bytes,
+ @JsonProperty("is_dir") boolean isDir,
+ @JsonProperty("is_deleted") boolean isDeleted,
+ @JsonProperty("rev") String rev,
+ @JsonProperty("hash") String hash,
+ @JsonProperty("thumb_exists") boolean thumbExists,
+ @JsonProperty("icon") String icon,
+ @JsonProperty("modified") Date modified,
+ @JsonProperty("root") String root,
+ @JsonProperty("path") String path,
+ @JsonProperty("mime_type") String mimeType,
+ @JsonProperty("contents") List<Metadata> contents) {}
+}
View
89 src/main/java/org/springframework/social/dropbox/connect/DropboxAdapter.java
@@ -1,45 +1,44 @@
-package org.springframework.social.dropbox.connect;
-
-import org.springframework.social.connect.ApiAdapter;
-import org.springframework.social.connect.ConnectionValues;
-import org.springframework.social.connect.UserProfile;
-import org.springframework.social.connect.UserProfileBuilder;
-import org.springframework.social.dropbox.api.Dropbox;
-import org.springframework.social.dropbox.api.DropboxUserProfile;
-import org.springframework.web.client.HttpClientErrorException;
-
-/**
- * User: Bryce Fischer
- * Date: 5/17/11
- * Time: 11:33 AM
- */
-public class DropboxAdapter implements ApiAdapter<Dropbox> {
- @Override
- public boolean test(Dropbox dropboxApi) {
- try {
- dropboxApi.getUserProfile();
- return true;
- } catch (HttpClientErrorException e) {
- return false;
- }
- }
-
- @Override
- public void setConnectionValues(Dropbox dropboxApi, ConnectionValues values) {
- DropboxUserProfile profile = dropboxApi.getUserProfile();
- values.setProviderUserId(profile.getUid().toString());
- values.setDisplayName(profile.getDisplayName());
- values.setProfileUrl(profile.getReferralLink());
- }
-
- @Override
- public UserProfile fetchUserProfile(Dropbox dropboxApi) {
- DropboxUserProfile profile = dropboxApi.getUserProfile();
- return new UserProfileBuilder().setName(profile.getDisplayName()).setUsername(profile.getEmail()).setEmail(profile.getEmail()).build();
- }
-
- @Override
- public void updateStatus(Dropbox dropboxApi, String s) {
- // Not Supported
- }
-}
+package org.springframework.social.dropbox.connect;
+
+import org.springframework.social.connect.ApiAdapter;
+import org.springframework.social.connect.ConnectionValues;
+import org.springframework.social.connect.UserProfile;
+import org.springframework.social.connect.UserProfileBuilder;
+import org.springframework.social.dropbox.api.Dropbox;
+import org.springframework.social.dropbox.api.DropboxUserProfile;
+import org.springframework.web.client.HttpClientErrorException;
+
+/**
+ * @author Bryce Fischer
+ * @author Robert Drysdale
+ */
+public class DropboxAdapter implements ApiAdapter<Dropbox> {
+ @Override
+ public boolean test(Dropbox dropboxApi) {
+ try {
+ dropboxApi.getUserProfile();
+ return true;
+ } catch (HttpClientErrorException e) {
+ return false;
+ }
+ }
+
+ @Override
+ public void setConnectionValues(Dropbox dropboxApi, ConnectionValues values) {
+ DropboxUserProfile profile = dropboxApi.getUserProfile();
+ values.setProviderUserId(profile.getUid().toString());
+ values.setDisplayName(profile.getDisplayName());
+ values.setProfileUrl(profile.getReferralLink());
+ }
+
+ @Override
+ public UserProfile fetchUserProfile(Dropbox dropboxApi) {
+ DropboxUserProfile profile = dropboxApi.getUserProfile();
+ return new UserProfileBuilder().setName(profile.getDisplayName()).setUsername(profile.getEmail()).setEmail(profile.getEmail()).build();
+ }
+
+ @Override
+ public void updateStatus(Dropbox dropboxApi, String s) {
+ // Not Supported
+ }
+}
View
59 src/main/java/org/springframework/social/dropbox/connect/DropboxServiceProvider.java
@@ -1,30 +1,29 @@
-package org.springframework.social.dropbox.connect;
-
-import org.springframework.social.dropbox.api.Dropbox;
-import org.springframework.social.dropbox.api.impl.DropboxTemplate;
-import org.springframework.social.oauth1.AbstractOAuth1ServiceProvider;
-import org.springframework.social.oauth1.OAuth1Template;
-import org.springframework.social.oauth1.OAuth1Version;
-
-/**
- * User: Bryce Fischer
- * Date: 5/17/11
- * Time: 11:29 AM
- */
-public class DropboxServiceProvider extends AbstractOAuth1ServiceProvider<Dropbox> {
- private final boolean appFolder;
-
- public DropboxServiceProvider(String consumerKey, String consumerSecret, boolean appFolder) {
- super(consumerKey, consumerSecret, new OAuth1Template(consumerKey, consumerSecret,
- "https://api.dropbox.com/1/oauth/request_token",
- "https://www.dropbox.com/1/oauth/authorize",
- "https://api.dropbox.com/1/oauth/access_token",
- OAuth1Version.CORE_10));
- this.appFolder = appFolder;
- }
-
- @Override
- public Dropbox getApi(String accessToken, String secret) {
- return new DropboxTemplate(getConsumerKey(), getConsumerSecret(), accessToken, secret, appFolder);
- }
-}
+package org.springframework.social.dropbox.connect;
+
+import org.springframework.social.dropbox.api.Dropbox;
+import org.springframework.social.dropbox.api.impl.DropboxTemplate;
+import org.springframework.social.oauth1.AbstractOAuth1ServiceProvider;
+import org.springframework.social.oauth1.OAuth1Template;
+import org.springframework.social.oauth1.OAuth1Version;
+
+/**
+ * @author Bryce Fischer
+ * @author Robert Drysdale
+ */
+public class DropboxServiceProvider extends AbstractOAuth1ServiceProvider<Dropbox> {
+ private final boolean appFolder;
+
+ public DropboxServiceProvider(String consumerKey, String consumerSecret, boolean appFolder) {
+ super(consumerKey, consumerSecret, new OAuth1Template(consumerKey, consumerSecret,
+ "https://api.dropbox.com/1/oauth/request_token",
+ "https://www.dropbox.com/1/oauth/authorize",
+ "https://api.dropbox.com/1/oauth/access_token",
+ OAuth1Version.CORE_10));
+ this.appFolder = appFolder;
+ }
+
+ @Override
+ public Dropbox getApi(String accessToken, String secret) {
+ return new DropboxTemplate(getConsumerKey(), getConsumerSecret(), accessToken, secret, appFolder);
+ }
+}
View
87 src/test/java/org/springframework/social/dropbox/api/impl/AbstractDropboxApiTest.java
@@ -1,44 +1,43 @@
-package org.springframework.social.dropbox.api.impl;
-
-import java.text.SimpleDateFormat;
-import java.util.Date;
-
-import org.junit.Before;
-import org.springframework.core.io.ClassPathResource;
-import org.springframework.core.io.Resource;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
-import org.springframework.social.test.client.MockRestServiceServer;
-
-
-/**
- * User: Bryce Fischer
- * Date: 7/7/11
- * Time: 1:01 PM
- */
-public abstract class AbstractDropboxApiTest {
- protected DropboxTemplate dropbox;
- protected MockRestServiceServer mockServer;
- protected HttpHeaders responseHeaders;
-
- @Before
- public void setUp() throws Exception {
- dropbox = new DropboxTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET", false);
- mockServer = MockRestServiceServer.createServer(dropbox.getRestTemplate());
- responseHeaders = new HttpHeaders();
- responseHeaders.setContentType(MediaType.APPLICATION_JSON);
- }
-
- protected Resource jsonResource(String filename) {
- return new ClassPathResource(filename + ".json");
- }
-
- protected Date fromDropboxDate(String date) {
- try {
- return new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").parse(date);
- }
- catch (Exception e) {
- throw new RuntimeException(e);
- }
- }
-}
+package org.springframework.social.dropbox.api.impl;
+
+import org.junit.Before;
+import org.springframework.core.io.ClassPathResource;
+import org.springframework.core.io.Resource;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.social.test.client.MockRestServiceServer;
+
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
+
+/**
+ * @author Bryce Fischer
+ * @author Robert Drysdale
+ */
+public abstract class AbstractDropboxApiTest {
+ protected DropboxTemplate dropbox;
+ protected MockRestServiceServer mockServer;
+ protected HttpHeaders responseHeaders;
+
+ @Before
+ public void setUp() throws Exception {
+ dropbox = new DropboxTemplate("API_KEY", "API_SECRET", "ACCESS_TOKEN", "ACCESS_TOKEN_SECRET", false);
+ mockServer = MockRestServiceServer.createServer(dropbox.getRestTemplate());
+ responseHeaders = new HttpHeaders();
+ responseHeaders.setContentType(MediaType.APPLICATION_JSON);
+ }
+
+ protected Resource jsonResource(String filename) {
+ return new ClassPathResource(filename + ".json");
+ }
+
+ protected Date fromDropboxDate(String date) {
+ try {
+ return new SimpleDateFormat("EEE, d MMM yyyy HH:mm:ss Z").parse(date);
+ }
+ catch (Exception e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
View
653 src/test/java/org/springframework/social/dropbox/api/impl/DropboxTemplateTest.java
@@ -1,328 +1,325 @@
-package org.springframework.social.dropbox.api.impl;
-
-import static junit.framework.Assert.assertEquals;
-import static org.springframework.http.HttpMethod.GET;
-import static org.springframework.http.HttpMethod.POST;
-import static org.springframework.http.HttpMethod.PUT;
-import static org.springframework.social.test.client.RequestMatchers.method;
-import static org.springframework.social.test.client.RequestMatchers.requestTo;
-import static org.springframework.social.test.client.ResponseCreators.withResponse;
-
-import java.io.ByteArrayOutputStream;
-import java.io.FileInputStream;
-import java.math.BigInteger;
-import java.util.List;
-
-import org.junit.Test;
-import org.springframework.http.HttpHeaders;
-import org.springframework.http.MediaType;
-import org.springframework.social.dropbox.api.DropboxFile;
-import org.springframework.social.dropbox.api.DropboxUserProfile;
-import org.springframework.social.dropbox.api.FileUrl;
-import org.springframework.social.dropbox.api.Metadata;
-
-/**
- * User: Bryce Fischer
- * Date: 5/17/11
- * Time: 9:41 AM
- */
-public class DropboxTemplateTest extends AbstractDropboxApiTest {
- @Test
- public void getUserProfileValid() throws Exception {
- mockServer
- .expect(requestTo("https://api.dropbox.com/1/account/info"))
- .andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/profileValid"), responseHeaders));
-
- DropboxUserProfile profile = dropbox.getUserProfile();
- assertEquals("US", profile.getCountry());
- assertEquals("Some User", profile.getDisplayName());
- assertEquals("some email", profile.getEmail());
- assertEquals(BigInteger.valueOf(10), profile.getUid());
- assertEquals("referralLink", profile.getReferralLink());
- assertEquals(BigInteger.valueOf(108179488768L), profile.getQuota());
- assertEquals(BigInteger.valueOf(44962990383L), profile.getSharedQuota());
- assertEquals(BigInteger.valueOf(46970996076L), profile.getNormalQuota());
- }
-
- @Test
- public void putFile() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.FILE_PUT_URL
- .replaceFirst("\\{appFolderUrl\\}", "dropbox")
- .replaceFirst("\\{path\\}", "file.json")))
- .andExpect(method(PUT))
- .andRespond(withResponse(jsonResource("/file_put_metadata"), responseHeaders));
-
- FileInputStream stream = new FileInputStream(jsonResource("metadata").getFile());
- ByteArrayOutputStream bytes = new ByteArrayOutputStream();
- while(true) {
- int b = stream.read();
- if (b == -1) {
- break;
- }
- bytes.write(b);
- }
-
- Metadata metadata = dropbox.putFile("file.json", bytes.toByteArray());
- assertEquals("7702a9405f", metadata.getRev());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(12265, metadata.getBytes());
- assertEquals(fromDropboxDate("Fri, 02 Dec 2011 11:27:27 +0000"), metadata.getModified());
- assertEquals("/file.json", metadata.getPath());
- assertEquals(false, metadata.isDir());
- assertEquals("page_white", metadata.getIcon());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("application/octet-stream", metadata.getMimeType());
- assertEquals("12KB", metadata.getSize());
- }
-
- @Test
- public void copyFile() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.COPY_URL))
- .andExpect(method(POST))
- .andRespond(withResponse(jsonResource("/copy"), responseHeaders));
-
- Metadata metadata = dropbox.copy("file.json", "file3.json");
- assertEquals("8e02a9405f", metadata.getRev());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(12265, metadata.getBytes());
- assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:24:07 +0000"), metadata.getModified());
- assertEquals("/file3.json", metadata.getPath());
- assertEquals(false, metadata.isDir());
- assertEquals("page_white", metadata.getIcon());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("application/octet-stream", metadata.getMimeType());
- assertEquals("12KB", metadata.getSize());
- }
-
- @Test
- public void moveFile() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.MOVE_URL))
- .andExpect(method(POST))
- .andRespond(withResponse(jsonResource("/file_moved"), responseHeaders));
-
- Metadata metadata = dropbox.move("file.json", "file_moved.json");
- assertEquals("9202a9405f", metadata.getRev());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(12265, metadata.getBytes());
- assertEquals(fromDropboxDate("Fri, 16 Dec 2011 14:39:42 +0000"), metadata.getModified());
- assertEquals("/file_moved.json", metadata.getPath());
- assertEquals(false, metadata.isDir());
- assertEquals("page_white", metadata.getIcon());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("application/octet-stream", metadata.getMimeType());
- assertEquals("12KB", metadata.getSize());
- }
-
- @Test
- public void restoreFile() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.RESTORE_URL
- .replaceFirst("\\{appFolderUrl\\}", "dropbox")
- .replaceFirst("\\{path\\}", "file3.json")
- + "?rev=8e02a9405f"))
- .andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/restored"), responseHeaders));
-
- Metadata metadata = dropbox.restore("file3.json", "8e02a9405f");
- assertEquals("9302a9405f", metadata.getRev());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(12265, metadata.getBytes());
- assertEquals(fromDropboxDate("Fri, 16 Dec 2011 14:48:58 +0000"), metadata.getModified());
- assertEquals("/file3.json", metadata.getPath());
- assertEquals(false, metadata.isDir());
- assertEquals("page_white", metadata.getIcon());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("application/octet-stream", metadata.getMimeType());
- assertEquals("12KB", metadata.getSize());
- }
-
- @Test
- public void getMedia() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.MEDIA_URL
- .replaceFirst("\\{appFolderUrl\\}", "dropbox")
- .replaceFirst("\\{path\\}", "file3.json")))
- .andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/media"), responseHeaders));
-
- FileUrl url = dropbox.getMedia("file3.json");
- assertEquals("https://dl.dropbox.com/0/view/6rcp09bdfz1kxfv/file3.json", url.getUrl());
- assertEquals(fromDropboxDate("Sun, 15 Jan 2012 15:24:47 +0000"), url.getExpires());
- }
-
- @Test
- public void getShare() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.SHARES_URL
- .replaceFirst("\\{appFolderUrl\\}", "dropbox")
- .replaceFirst("\\{path\\}", "file3.json")))
- .andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/share"), responseHeaders));
-
- FileUrl url = dropbox.getShare("file3.json");
- assertEquals("http://db.tt/LnS1qL1q", url.getUrl());
- assertEquals(fromDropboxDate("Sun, 15 Jan 2012 15:24:47 +0000"), url.getExpires());
- }
-
- @Test
- public void searchFile() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.SEARCH_URL
- .replaceFirst("\\{appFolderUrl\\}", "dropbox")
- .replaceFirst("\\{path\\}", "")
- + "?query=json"))
- .andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/search"), responseHeaders));
-
- List<Metadata> list = dropbox.search("", "json");
- assertEquals(3, list.size());
- Metadata metadata = list.get(0);
-
- assertEquals("8d02a9405f", metadata.getRev());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(12265, metadata.getBytes());
- assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:23:30 +0000"), metadata.getModified());
- assertEquals("/file2.json", metadata.getPath());
- assertEquals(false, metadata.isDir());
- assertEquals("page_white", metadata.getIcon());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("application/octet-stream", metadata.getMimeType());
- assertEquals("12KB", metadata.getSize());
- }
-
- @Test
- public void createFolder() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.CREATE_FOLDER_URL))
- .andExpect(method(POST))
- .andRespond(withResponse(jsonResource("/create_folder"), responseHeaders));
-
- Metadata metadata = dropbox.createFolder("test");
- assertEquals("8f02a9405f", metadata.getRev());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(0, metadata.getBytes());
- assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:35:37 +0000"), metadata.getModified());
- assertEquals("/test", metadata.getPath());
- assertEquals(true, metadata.isDir());
- assertEquals("folder", metadata.getIcon());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("0 bytes", metadata.getSize());
- }
-
- @Test
- public void delete() throws Exception {
- mockServer
- .expect(requestTo(DropboxTemplate.DELETE_URL))
- .andExpect(method(POST))
- .andRespond(withResponse(jsonResource("/delete"), responseHeaders));
-
- Metadata metadata = dropbox.delete("file3.json");
- assertEquals("9002a9405f", metadata.getRev());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(0, metadata.getBytes());
- assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:57:00 +0000"), metadata.getModified());
- assertEquals("/file3.json", metadata.getPath());
- assertEquals(false, metadata.isDir());
- assertEquals("page_white", metadata.getIcon());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("0 bytes", metadata.getSize());
- assertEquals(true, metadata.isDeleted());
- assertEquals("application/octet-stream", metadata.getMimeType());
- }
-
- @Test
- public void getFile() throws Exception {
-
- HttpHeaders h = new HttpHeaders();
- h.setContentType(MediaType.APPLICATION_JSON);
- h.setContentLength(1234);
-
- mockServer
- .expect(requestTo(DropboxTemplate.FILE_URL
- .replaceFirst("\\{appFolderUrl\\}", "dropbox")
- .replaceFirst("\\{path\\}", "Getting%20Started.pdf")))
- .andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/metadata"), h));
-
- DropboxFile file = dropbox.getFile("Getting Started.pdf");
- byte[] bytes = file.getBytes();
- assertEquals(1234, bytes.length);
- }
-
- @Test
- public void getRevisions() throws Exception {
- mockServer.expect(requestTo(DropboxTemplate.REVISIONS_URL.replaceFirst("\\{appFolderUrl\\}", "dropbox").replaceFirst("\\{path\\}", "file.json"))).andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/revisions"), responseHeaders));
- List<Metadata> revisions = dropbox.getRevisions("file.json");
-
- Metadata file = revisions.get(0);
- assertEquals("7702a9405f", file.getRev());
- assertEquals(false, file.isThumbExists());
- assertEquals(12265, file.getBytes());
- assertEquals(fromDropboxDate("Fri, 02 Dec 2011 11:27:27 +0000"), file.getModified());
- assertEquals("/file.json", file.getPath());
- assertEquals(false, file.isDir());
- assertEquals("12KB", file.getSize());
- assertEquals("dropbox", file.getRoot());
- assertEquals("page_white", file.getIcon());
- assertEquals("application/octet-stream", file.getMimeType());
-
- file = revisions.get(1);
- assertEquals("7602a9405f", file.getRev());
- assertEquals(false, file.isThumbExists());
- assertEquals(556035, file.getBytes());
- assertEquals(fromDropboxDate("Fri, 02 Dec 2011 11:24:16 +0000"), file.getModified());
- assertEquals("/file.json", file.getPath());
- assertEquals(false, file.isDir());
- assertEquals("543KB", file.getSize());
- assertEquals("dropbox", file.getRoot());
- assertEquals("page_white", file.getIcon());
- assertEquals("application/octet-stream", file.getMimeType());
- }
-
- @Test
- public void getMetadata() throws Exception {
- mockServer.expect(requestTo(DropboxTemplate.METADATA_URL.replaceFirst("\\{appFolderUrl\\}", "dropbox").replaceFirst("\\{path\\}", ""))).andExpect(method(GET))
- .andRespond(withResponse(jsonResource("/metadata"), responseHeaders));
- Metadata metadata = dropbox.getItemMetadata("");
-
- assertEquals("0881bfe7f09e0fe856cf9a27000ac00c", metadata.getHash());
- assertEquals(false, metadata.isThumbExists());
- assertEquals(0, metadata.getBytes());
- assertEquals("/", metadata.getPath());
- assertEquals(true, metadata.isDir());
- assertEquals("0 bytes", metadata.getSize());
- assertEquals("dropbox", metadata.getRoot());
- assertEquals("folder", metadata.getIcon());
-
- List<Metadata> contents = metadata.getContents();
- assertEquals(9, contents.size());
-
- Metadata folder = contents.get(0);
- assertEquals("1202a9405f", folder.getRev());
- assertEquals(false, folder.isThumbExists());
- assertEquals(0, folder.getBytes());
- assertEquals(fromDropboxDate("Wed, 08 Jun 2011 20:58:29 +0000"), folder.getModified());
- assertEquals("/cv", folder.getPath());
- assertEquals(true, folder.isDir());
- assertEquals("0 bytes", folder.getSize());
- assertEquals("dropbox", folder.getRoot());
- assertEquals("folder", folder.getIcon());
-
- Metadata file = contents.get(1);
- assertEquals("702a9405f", file.getRev());
- assertEquals(false, file.isThumbExists());
- assertEquals(230783, file.getBytes());
- assertEquals(fromDropboxDate("Thu, 05 May 2011 14:28:24 +0000"), file.getModified());
- assertEquals("/Getting Started.pdf", file.getPath());
- assertEquals(false, file.isDir());
- assertEquals("225.4KB", file.getSize());
- assertEquals("dropbox", file.getRoot());
- assertEquals("page_white_acrobat", file.getIcon());
- assertEquals("application/pdf", file.getMimeType());
- }
-}
+package org.springframework.social.dropbox.api.impl;
+
+import org.junit.Test;
+import org.springframework.http.HttpHeaders;
+import org.springframework.http.MediaType;
+import org.springframework.social.dropbox.api.DropboxFile;
+import org.springframework.social.dropbox.api.DropboxUserProfile;
+import org.springframework.social.dropbox.api.FileUrl;
+import org.springframework.social.dropbox.api.Metadata;
+
+import java.io.ByteArrayOutputStream;
+import java.io.FileInputStream;
+import java.math.BigInteger;
+import java.util.List;
+
+import static junit.framework.Assert.assertEquals;
+import static org.springframework.http.HttpMethod.*;
+import static org.springframework.social.test.client.RequestMatchers.method;
+import static org.springframework.social.test.client.RequestMatchers.requestTo;
+import static org.springframework.social.test.client.ResponseCreators.withResponse;
+
+/**
+ * @author Bryce Fischer
+ * @author Robert Drysdale
+ */
+public class DropboxTemplateTest extends AbstractDropboxApiTest {
+ @Test
+ public void getUserProfileValid() throws Exception {
+ mockServer
+ .expect(requestTo("https://api.dropbox.com/1/account/info"))
+ .andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/profileValid"), responseHeaders));
+
+ DropboxUserProfile profile = dropbox.getUserProfile();
+ assertEquals("US", profile.getCountry());
+ assertEquals("Some User", profile.getDisplayName());
+ assertEquals("some email", profile.getEmail());
+ assertEquals(BigInteger.valueOf(10), profile.getUid());
+ assertEquals("referralLink", profile.getReferralLink());
+ assertEquals(BigInteger.valueOf(108179488768L), profile.getQuota());
+ assertEquals(BigInteger.valueOf(44962990383L), profile.getSharedQuota());
+ assertEquals(BigInteger.valueOf(46970996076L), profile.getNormalQuota());
+ }
+
+ @Test
+ public void putFile() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.FILE_PUT_URL
+ .replaceFirst("\\{appFolderUrl\\}", "dropbox")
+ .replaceFirst("\\{path\\}", "file.json")))
+ .andExpect(method(PUT))
+ .andRespond(withResponse(jsonResource("/file_put_metadata"), responseHeaders));
+
+ FileInputStream stream = new FileInputStream(jsonResource("metadata").getFile());
+ ByteArrayOutputStream bytes = new ByteArrayOutputStream();
+ while(true) {
+ int b = stream.read();
+ if (b == -1) {
+ break;
+ }
+ bytes.write(b);
+ }
+
+ Metadata metadata = dropbox.putFile("file.json", bytes.toByteArray());
+ assertEquals("7702a9405f", metadata.getRev());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(12265, metadata.getBytes());
+ assertEquals(fromDropboxDate("Fri, 02 Dec 2011 11:27:27 +0000"), metadata.getModified());
+ assertEquals("/file.json", metadata.getPath());
+ assertEquals(false, metadata.isDir());
+ assertEquals("page_white", metadata.getIcon());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("application/octet-stream", metadata.getMimeType());
+ assertEquals("12KB", metadata.getSize());
+ }
+
+ @Test
+ public void copyFile() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.COPY_URL))
+ .andExpect(method(POST))
+ .andRespond(withResponse(jsonResource("/copy"), responseHeaders));
+
+ Metadata metadata = dropbox.copy("file.json", "file3.json");
+ assertEquals("8e02a9405f", metadata.getRev());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(12265, metadata.getBytes());
+ assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:24:07 +0000"), metadata.getModified());
+ assertEquals("/file3.json", metadata.getPath());
+ assertEquals(false, metadata.isDir());
+ assertEquals("page_white", metadata.getIcon());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("application/octet-stream", metadata.getMimeType());
+ assertEquals("12KB", metadata.getSize());
+ }
+
+ @Test
+ public void moveFile() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.MOVE_URL))
+ .andExpect(method(POST))
+ .andRespond(withResponse(jsonResource("/file_moved"), responseHeaders));
+
+ Metadata metadata = dropbox.move("file.json", "file_moved.json");
+ assertEquals("9202a9405f", metadata.getRev());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(12265, metadata.getBytes());
+ assertEquals(fromDropboxDate("Fri, 16 Dec 2011 14:39:42 +0000"), metadata.getModified());
+ assertEquals("/file_moved.json", metadata.getPath());
+ assertEquals(false, metadata.isDir());
+ assertEquals("page_white", metadata.getIcon());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("application/octet-stream", metadata.getMimeType());
+ assertEquals("12KB", metadata.getSize());
+ }
+
+ @Test
+ public void restoreFile() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.RESTORE_URL
+ .replaceFirst("\\{appFolderUrl\\}", "dropbox")
+ .replaceFirst("\\{path\\}", "file3.json")
+ + "?rev=8e02a9405f"))
+ .andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/restored"), responseHeaders));
+
+ Metadata metadata = dropbox.restore("file3.json", "8e02a9405f");
+ assertEquals("9302a9405f", metadata.getRev());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(12265, metadata.getBytes());
+ assertEquals(fromDropboxDate("Fri, 16 Dec 2011 14:48:58 +0000"), metadata.getModified());
+ assertEquals("/file3.json", metadata.getPath());
+ assertEquals(false, metadata.isDir());
+ assertEquals("page_white", metadata.getIcon());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("application/octet-stream", metadata.getMimeType());
+ assertEquals("12KB", metadata.getSize());
+ }
+
+ @Test
+ public void getMedia() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.MEDIA_URL
+ .replaceFirst("\\{appFolderUrl\\}", "dropbox")
+ .replaceFirst("\\{path\\}", "file3.json")))
+ .andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/media"), responseHeaders));
+
+ FileUrl url = dropbox.getMedia("file3.json");
+ assertEquals("https://dl.dropbox.com/0/view/6rcp09bdfz1kxfv/file3.json", url.getUrl());
+ assertEquals(fromDropboxDate("Sun, 15 Jan 2012 15:24:47 +0000"), url.getExpires());
+ }
+
+ @Test
+ public void getShare() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.SHARES_URL
+ .replaceFirst("\\{appFolderUrl\\}", "dropbox")
+ .replaceFirst("\\{path\\}", "file3.json")))
+ .andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/share"), responseHeaders));
+
+ FileUrl url = dropbox.getShare("file3.json");
+ assertEquals("http://db.tt/LnS1qL1q", url.getUrl());
+ assertEquals(fromDropboxDate("Sun, 15 Jan 2012 15:24:47 +0000"), url.getExpires());
+ }
+
+ @Test
+ public void searchFile() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.SEARCH_URL
+ .replaceFirst("\\{appFolderUrl\\}", "dropbox")
+ .replaceFirst("\\{path\\}", "")
+ + "?query=json"))
+ .andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/search"), responseHeaders));
+
+ List<Metadata> list = dropbox.search("", "json");
+ assertEquals(3, list.size());
+ Metadata metadata = list.get(0);
+
+ assertEquals("8d02a9405f", metadata.getRev());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(12265, metadata.getBytes());
+ assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:23:30 +0000"), metadata.getModified());
+ assertEquals("/file2.json", metadata.getPath());
+ assertEquals(false, metadata.isDir());
+ assertEquals("page_white", metadata.getIcon());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("application/octet-stream", metadata.getMimeType());
+ assertEquals("12KB", metadata.getSize());
+ }
+
+ @Test
+ public void createFolder() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.CREATE_FOLDER_URL))
+ .andExpect(method(POST))
+ .andRespond(withResponse(jsonResource("/create_folder"), responseHeaders));
+
+ Metadata metadata = dropbox.createFolder("test");
+ assertEquals("8f02a9405f", metadata.getRev());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(0, metadata.getBytes());
+ assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:35:37 +0000"), metadata.getModified());
+ assertEquals("/test", metadata.getPath());
+ assertEquals(true, metadata.isDir());
+ assertEquals("folder", metadata.getIcon());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("0 bytes", metadata.getSize());
+ }
+
+ @Test
+ public void delete() throws Exception {
+ mockServer
+ .expect(requestTo(DropboxTemplate.DELETE_URL))
+ .andExpect(method(POST))
+ .andRespond(withResponse(jsonResource("/delete"), responseHeaders));
+
+ Metadata metadata = dropbox.delete("file3.json");
+ assertEquals("9002a9405f", metadata.getRev());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(0, metadata.getBytes());
+ assertEquals(fromDropboxDate("Fri, 16 Dec 2011 12:57:00 +0000"), metadata.getModified());
+ assertEquals("/file3.json", metadata.getPath());
+ assertEquals(false, metadata.isDir());
+ assertEquals("page_white", metadata.getIcon());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("0 bytes", metadata.getSize());
+ assertEquals(true, metadata.isDeleted());
+ assertEquals("application/octet-stream", metadata.getMimeType());
+ }
+
+ @Test
+ public void getFile() throws Exception {
+
+ HttpHeaders h = new HttpHeaders();
+ h.setContentType(MediaType.APPLICATION_JSON);
+ h.setContentLength(1234);
+
+ mockServer
+ .expect(requestTo(DropboxTemplate.FILE_URL
+ .replaceFirst("\\{appFolderUrl\\}", "dropbox")
+ .replaceFirst("\\{path\\}", "Getting%20Started.pdf")))
+ .andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/metadata"), h));
+
+ DropboxFile file = dropbox.getFile("Getting Started.pdf");
+ byte[] bytes = file.getBytes();
+ assertEquals(1234, bytes.length);
+ }
+
+ @Test
+ public void getRevisions() throws Exception {
+ mockServer.expect(requestTo(DropboxTemplate.REVISIONS_URL.replaceFirst("\\{appFolderUrl\\}", "dropbox").replaceFirst("\\{path\\}", "file.json"))).andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/revisions"), responseHeaders));
+ List<Metadata> revisions = dropbox.getRevisions("file.json");
+
+ Metadata file = revisions.get(0);
+ assertEquals("7702a9405f", file.getRev());
+ assertEquals(false, file.isThumbExists());
+ assertEquals(12265, file.getBytes());
+ assertEquals(fromDropboxDate("Fri, 02 Dec 2011 11:27:27 +0000"), file.getModified());
+ assertEquals("/file.json", file.getPath());
+ assertEquals(false, file.isDir());
+ assertEquals("12KB", file.getSize());
+ assertEquals("dropbox", file.getRoot());
+ assertEquals("page_white", file.getIcon());
+ assertEquals("application/octet-stream", file.getMimeType());
+
+ file = revisions.get(1);
+ assertEquals("7602a9405f", file.getRev());
+ assertEquals(false, file.isThumbExists());
+ assertEquals(556035, file.getBytes());
+ assertEquals(fromDropboxDate("Fri, 02 Dec 2011 11:24:16 +0000"), file.getModified());
+ assertEquals("/file.json", file.getPath());
+ assertEquals(false, file.isDir());
+ assertEquals("543KB", file.getSize());
+ assertEquals("dropbox", file.getRoot());
+ assertEquals("page_white", file.getIcon());
+ assertEquals("application/octet-stream", file.getMimeType());
+ }
+
+ @Test
+ public void getMetadata() throws Exception {
+ mockServer.expect(requestTo(DropboxTemplate.METADATA_URL.replaceFirst("\\{appFolderUrl\\}", "dropbox").replaceFirst("\\{path\\}", ""))).andExpect(method(GET))
+ .andRespond(withResponse(jsonResource("/metadata"), responseHeaders));
+ Metadata metadata = dropbox.getItemMetadata("");
+
+ assertEquals("0881bfe7f09e0fe856cf9a27000ac00c", metadata.getHash());
+ assertEquals(false, metadata.isThumbExists());
+ assertEquals(0, metadata.getBytes());
+ assertEquals("/", metadata.getPath());
+ assertEquals(true, metadata.isDir());
+ assertEquals("0 bytes", metadata.getSize());
+ assertEquals("dropbox", metadata.getRoot());
+ assertEquals("folder", metadata.getIcon());
+
+ List<Metadata> contents = metadata.getContents();
+ assertEquals(9, contents.size());
+
+ Metadata folder = contents.get(0);
+ assertEquals("1202a9405f", folder.getRev());
+ assertEquals(false, folder.isThumbExists());
+ assertEquals(0, folder.getBytes());
+ assertEquals(fromDropboxDate("Wed, 08 Jun 2011 20:58:29 +0000"), folder.getModified());
+ assertEquals("/cv", folder.getPath());
+ assertEquals(true, folder.isDir());
+ assertEquals("0 bytes", folder.getSize());
+ assertEquals("dropbox", folder.getRoot());
+ assertEquals("folder", folder.getIcon());
+
+ Metadata file = contents.get(1);
+ assertEquals("702a9405f", file.getRev());
+ assertEquals(false, file.isThumbExists());
+ assertEquals(230783, file.getBytes());
+ assertEquals(fromDropboxDate("Thu, 05 May 2011 14:28:24 +0000"), file.getModified());
+ assertEquals("/Getting Started.pdf", file.getPath());
+ assertEquals(false, file.isDir());
+ assertEquals("225.4KB", file.getSize());
+ assertEquals("dropbox", file.getRoot());
+ assertEquals("page_white_acrobat", file.getIcon());
+ assertEquals("application/pdf", file.getMimeType());
+ }
+}
View
79 src/test/java/org/springframework/social/dropbox/connect/DropboxAdapterTest.java
@@ -1,40 +1,39 @@
-package org.springframework.social.dropbox.connect;
-
-import org.junit.Test;
-import org.mockito.Mockito;
-import org.springframework.social.connect.UserProfile;
-import org.springframework.social.dropbox.api.Dropbox;
-import org.springframework.social.dropbox.api.DropboxUserProfile;
-
-import java.math.BigInteger;
-
-import static junit.framework.Assert.assertEquals;
-
-/**
- * User: Bryce Fischer
- * Date: 5/17/11
- * Time: 11:40 AM
- */
-public class DropboxAdapterTest {
- private DropboxAdapter adapter = new DropboxAdapter();
- private Dropbox dropboxApi = Mockito.mock(Dropbox.class);
-
- @Test
- public void fetchProfile() throws Exception {
- final String country = "USA";
- final String displayName = "DisplayName";
- final BigInteger uid = BigInteger.valueOf(1);
- final BigInteger sharedQuota = BigInteger.valueOf(123);
- final BigInteger quota = new BigInteger("62277025792");
- final BigInteger normalQuota = BigInteger.valueOf(323);
- String email = "emailaddress";
- String referralLink = "referralLink";
-
- Mockito.when(dropboxApi.getUserProfile())
- .thenReturn(new DropboxUserProfile(uid, displayName, email, country, referralLink, sharedQuota, quota, normalQuota));
-
- UserProfile profile = adapter.fetchUserProfile(dropboxApi);
-
- assertEquals(displayName, profile.getName());
- }
-}
+package org.springframework.social.dropbox.connect;
+
+import org.junit.Test;
+import org.mockito.Mockito;
+import org.springframework.social.connect.UserProfile;
+import org.springframework.social.dropbox.api.Dropbox;
+import org.springframework.social.dropbox.api.DropboxUserProfile;
+
+import java.math.BigInteger;
+
+import static junit.framework.Assert.assertEquals;
+
+/**
+ * @author Bryce Fischer
+ * @author Robert Drysdale
+ */
+public class DropboxAdapterTest {
+ private DropboxAdapter adapter = new DropboxAdapter();
+ private Dropbox dropboxApi = Mockito.mock(Dropbox.class);
+
+ @Test
+ public void fetchProfile() throws Exception {
+ final String country = "USA";
+ final String displayName = "DisplayName";
+ final BigInteger uid = BigInteger.valueOf(1);
+ final BigInteger sharedQuota = BigInteger.valueOf(123);
+ final BigInteger quota = new BigInteger("62277025792");
+ final BigInteger normalQuota = BigInteger.valueOf(323);
+ String email = "emailaddress";
+ String referralLink = "referralLink";
+
+ Mockito.when(dropboxApi.getUserProfile())
+ .thenReturn(new DropboxUserProfile(uid, displayName, email, country, referralLink, sharedQuota, quota, normalQuota));
+
+ UserProfile profile = adapter.fetchUserProfile(dropboxApi);
+
+ assertEquals(displayName, profile.getName());
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.