Skip to content

Commit

Permalink
Increase chunk size, fix date parsing, fix token response, tweak erro…
Browse files Browse the repository at this point in the history
…r exceptions
  • Loading branch information
rcgroot committed Nov 10, 2016
1 parent 45dbe1e commit 19aa075
Show file tree
Hide file tree
Showing 3 changed files with 83 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public class DropboxV2 implements DropboxAdapter {
private static final String PATH_SEPARATOR = "/";
private static final String VALUE_AUTHORIZATION_CODE = "authorization_code";
private static final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
private static final long FOUR_MEGA_BYTE = 4 * 1024;
private static final long FOUR_MEGA_BYTE = 4 * 1024 * 1024;
private static final String APPLICATION_JSON = "application/json";
private static final String APPLICATION_OCTET_STREAM = "application/octet-stream";

Expand Down Expand Up @@ -214,10 +214,10 @@ void delete(@Nonnull Metadata file) throws RestException {
}
}

public void pruneFolder(@Nonnull String folderPath, int pruneRootDays) throws RestException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss.SSSZ", Locale.US);
public void pruneFolder(@Nonnull String path, int pruneRootDays) throws RestException {
Date cutoff = new Date(System.currentTimeMillis() - pruneRootDays * MILLISECONDS_PER_DAY);
Metadata metadata = retrieveMetaData(folderPath);
String absolute = createAbsolutePath(path);
Metadata metadata = retrieveMetaData(absolute);
if (metadata.isDir() && metadata instanceof FolderMetadata) {
FolderMetadata folderMetadata = (FolderMetadata) metadata;
FolderContent contents = listFiles(folderMetadata);
Expand All @@ -229,11 +229,10 @@ public void pruneFolder(@Nonnull String folderPath, int pruneRootDays) throws Re
for (Metadata entry : contents.getEntries()) {
if (entry.isFile() && entry instanceof FileMetadata) {
FileMetadata fileMetadata = (FileMetadata) entry;
String serverModified = "";
String serverModified = fileMetadata.getServerModified();
Date lastModified;
try {
serverModified = fileMetadata.getServerModified();
lastModified = df.parse(serverModified);
lastModified = parseDate(serverModified);
} catch (ParseException e) {
throw new RestException(Messages.exception_dropbox_folder_prunedate(serverModified), e);
}
Expand All @@ -249,6 +248,13 @@ public void pruneFolder(@Nonnull String folderPath, int pruneRootDays) throws Re
}
}

@VisibleForTesting
Date parseDate(String serverModified) throws ParseException {
SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd'T'HH:mm:ss'Z'", Locale.US);

return df.parse(serverModified);
}

/**
* @param name name of the new file to store the content in
* @param content data stream of the content
Expand Down Expand Up @@ -487,6 +493,7 @@ private static String readAccessTokenFromWeb(String authorizationCode) throws Re
}
JsonObjectRequest<TokenResponse> request = new JsonObjectRequest.Builder<TokenResponse>()
.gson(new Gson())
.responseClass(TokenResponse.class)
.url(url)
.upload(formBuilder.build(), FormBuilder.CONTENT_TYPE)
.responseErrorClass(ErrorResponse.class)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@
package org.jenkinsci.plugins.publishoverdropbox.domain;

import com.google.gson.Gson;
import com.google.gson.JsonSyntaxException;
import org.apache.commons.io.IOUtils;
import org.jenkinsci.plugins.publishoverdropbox.domain.model.RestException;
import org.jenkinsci.plugins.publishoverdropbox.impl.Messages;
Expand Down Expand Up @@ -167,11 +168,15 @@ public T execute() throws RestException {
errorStream = connection.getErrorStream();
Object errorResponse;
if (classOfError != null) {
errorResponse = readModel(gson, errorStream, classOfError);
errorResponse = IOUtils.toString(errorStream);
try {
errorResponse = readModel(gson, (String) errorResponse, classOfError);
} catch (JsonSyntaxException exception) {
}
} else {
errorResponse = IOUtils.toString(errorStream);
}
throw new RestException(Messages.exception_rest_http(responseCode, responseMessage), errorResponse);
throw new RestException(errorResponse.toString(), new IOException(Messages.exception_rest_http(responseCode, responseMessage)));
}

// Download
Expand Down Expand Up @@ -215,7 +220,7 @@ private void signWithBearerToken(HttpURLConnection connection) {

private static <MODEL> MODEL readModel(Gson gson, InputStream inputStream, Class<MODEL> classOfModel) throws IOException {
MODEL model = null;
if (inputStream != null) {
if (inputStream != null && classOfModel != null) {
InputStreamReader reader = null;
try {
reader = new InputStreamReader(inputStream);
Expand All @@ -227,6 +232,14 @@ private static <MODEL> MODEL readModel(Gson gson, InputStream inputStream, Class
return model;
}

private static <MODEL> MODEL readModel(Gson gson, String jsonData, Class<MODEL> classOfModel) throws IOException {
MODEL model = null;
if (jsonData != null) {
model = gson.fromJson(jsonData, classOfModel);
}
return model;
}

private static void closeQuietly(Closeable closeable) {
if (closeable != null) {
try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,17 @@
import org.jenkinsci.plugins.publishoverdropbox.domain.model.FolderMetadata;
import org.jenkinsci.plugins.publishoverdropbox.domain.model.Metadata;
import org.jenkinsci.plugins.publishoverdropbox.domain.model.RestException;
import org.jfree.util.Log;
import org.junit.After;
import org.junit.Before;
import org.junit.Test;

import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.text.ParseException;
import java.util.Calendar;
import java.util.Date;

import static org.hamcrest.MatcherAssert.assertThat;
import static org.hamcrest.Matchers.is;
Expand All @@ -24,6 +28,7 @@
public class DropboxV2Test {

private final static String accessToken = "";
private final static String wrongAccessToken = "wrong";
private DropboxV2 sut;

@Before
Expand All @@ -41,9 +46,13 @@ public void tearDown() throws RestException {
if (sut == null) {
return;
}
boolean exists = sut.changeWorkingDirectory("/tests");
if (exists) {
sut.delete(sut.getWorkingFolder());
try {
boolean exists = sut.changeWorkingDirectory("/tests");
if (exists) {
sut.delete(sut.getWorkingFolder());
}
} catch (Exception e) {
Log.debug("Teardown failed", e);
}
}

Expand Down Expand Up @@ -192,4 +201,45 @@ public void testUploadFourChunks() throws RestException, UnsupportedEncodingExce
assertThat(metaData.getPathLower(), is("/tests/2chunks-file.txt"));
assertThat(metaData.getSize(), is((long) bytes.length));
}


@Test
public void testPruneFolderLeavesFiles() throws RestException, UnsupportedEncodingException {
// Arrange
sut.makeDirectory("tests");
sut.changeWorkingDirectory("tests");
final byte[] bytes = "Hello wo§rld".getBytes("UTF-8");
ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes);
sut.storeFile("simplefile.txt", inputStream, bytes.length);
// Act
sut.pruneFolder("/tests", 1);
// Assert
FolderMetadata metaData = (FolderMetadata) sut.retrieveMetaData("/tests");
assertThat(metaData.getName(), is("tests"));
FolderContent contents = sut.listFiles(metaData);
assertThat(contents.getEntries().size(), is(1));
}

@Test
public void testDateParsing() throws ParseException {
// Act
Date date = sut.parseDate("2016-11-04T07:42:22Z");
// Assert
Calendar cal = Calendar.getInstance();
cal.setTime(date);
assertThat(cal.get(Calendar.YEAR), is(2016));
assertThat(cal.get(Calendar.HOUR), is(7));
assertThat(cal.get(Calendar.SECOND), is(22));
}

@Test(expected = RestException.class)
public void testCantConnectWithWrongToken() throws IOException {
// Arrange
sut = new DropboxV2(wrongAccessToken);
// Act
sut.connect();
// Assert
assertThat(sut.isConnected(), is(false));
}

}

0 comments on commit 19aa075

Please sign in to comment.