-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
9 changed files
with
648 additions
and
214 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
629 changes: 432 additions & 197 deletions
629
src/main/java/org/sead/uploader/dataverse/DVUploader.java
Large diffs are not rendered by default.
Oops, something went wrong.
107 changes: 107 additions & 0 deletions
107
src/main/java/org/sead/uploader/dataverse/HttpPartUploadJob.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package org.sead.uploader.dataverse; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.util.Map; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.apache.http.client.protocol.HttpClientContext; | ||
import org.apache.http.entity.InputStreamEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.util.EntityUtils; | ||
import static org.sead.uploader.AbstractUploader.println; | ||
import org.sead.uploader.util.Resource; | ||
|
||
/** | ||
* | ||
* @author Jim | ||
*/ | ||
public class HttpPartUploadJob implements Runnable { | ||
|
||
int partNo; | ||
long size; | ||
String signedUrl; | ||
Resource file; | ||
Map eTags; | ||
|
||
static private long partSize = -1; | ||
static private CloseableHttpClient httpClient = null; | ||
static private HttpClientContext localContext = null; | ||
|
||
public static void setHttpClient(CloseableHttpClient client) { | ||
httpClient = client; | ||
} | ||
|
||
public static void setHttpClientContext(HttpClientContext context) { | ||
localContext = context; | ||
} | ||
|
||
public static void setPartSize(long ps) { | ||
partSize=ps; | ||
} | ||
|
||
public HttpPartUploadJob(int partNo, String url, Resource file, long size, Map eTags) throws IllegalStateException { | ||
if ((size == -1) || (httpClient == null) || (localContext == null)) { | ||
throw new IllegalStateException("partSize not set"); | ||
} | ||
this.partNo = partNo; | ||
this.signedUrl = url; | ||
this.file = file; | ||
this.size=size; | ||
this.eTags = eTags; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see java.lang.Runnable#run() | ||
*/ | ||
public void run() { | ||
int retries = 3; | ||
//println("Starting upload of part: " + partNo); | ||
while (retries > 0) { | ||
try (InputStream is = file.getInputStream((partNo - 1) * partSize, size)) { | ||
|
||
HttpPut httpput = new HttpPut(signedUrl); | ||
httpput.setEntity(new InputStreamEntity(is, size)); | ||
CloseableHttpResponse putResponse = httpClient.execute(httpput); | ||
int putStatus = putResponse.getStatusLine().getStatusCode(); | ||
String putRes = null; | ||
HttpEntity putEntity = putResponse.getEntity(); | ||
if (putEntity != null) { | ||
putRes = EntityUtils.toString(putEntity); | ||
} | ||
if (putStatus == 200) { | ||
//Part successfully stored - parse the eTag from the response and it it to the Map | ||
String eTag = putResponse.getFirstHeader("ETag").getValue(); | ||
eTag= eTag.replace("\"",""); | ||
eTags.put(Integer.toString(partNo), eTag); | ||
retries = 0; | ||
//println("Completed upload of part: " + partNo); | ||
} else { | ||
if (putStatus >= 500) { | ||
println("Upload of part: " + partNo + " failed with status: " + putStatus + " (skipping)"); | ||
println("Error response: " + putResponse.getStatusLine() + " : " + putRes); | ||
retries--; | ||
} else { | ||
println("Upload of part: " + partNo + " failed with status: " + putStatus + " (retrying)"); | ||
println("Error response: " + putResponse.getStatusLine() + " : " + putRes); | ||
|
||
retries--; | ||
} | ||
} | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(System.out); | ||
println("Error uploading part: " + partNo + " : " + e.getMessage()); | ||
retries = 0; | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
/* | ||
* To change this license header, choose License Headers in Project Properties. | ||
* To change this template file, choose Tools | Templates | ||
* and open the template in the editor. | ||
*/ | ||
package org.sead.uploader.dataverse; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.DigestInputStream; | ||
import java.security.MessageDigest; | ||
import java.security.NoSuchAlgorithmException; | ||
import java.util.Map; | ||
import org.apache.commons.codec.binary.Hex; | ||
import org.apache.http.HttpEntity; | ||
import org.apache.http.client.methods.CloseableHttpResponse; | ||
import org.apache.http.client.methods.HttpPut; | ||
import org.apache.http.client.protocol.HttpClientContext; | ||
import org.apache.http.entity.InputStreamEntity; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.util.EntityUtils; | ||
import static org.sead.uploader.AbstractUploader.println; | ||
import org.sead.uploader.util.Resource; | ||
|
||
/** | ||
* | ||
* @author Jim | ||
*/ | ||
public class MD5Job implements Runnable { | ||
|
||
Resource file; | ||
Map infoMap; | ||
|
||
public MD5Job(Resource file, Map infoMap) throws IllegalStateException { | ||
this.file = file; | ||
this.infoMap = infoMap; | ||
} | ||
|
||
/* | ||
* (non-Javadoc) | ||
* | ||
* @see java.lang.Runnable#run() | ||
*/ | ||
public void run() { | ||
try { | ||
MessageDigest messageDigest = MessageDigest.getInstance("MD5"); | ||
|
||
try (InputStream inStream = file.getInputStream(); DigestInputStream digestInputStream = new DigestInputStream(inStream, messageDigest)) { | ||
byte[] bytes = new byte[64*1024]; | ||
while(digestInputStream.read(bytes) >= 0) { | ||
} | ||
String checksum = Hex.encodeHexString(digestInputStream.getMessageDigest().digest()); | ||
infoMap.put("md5", checksum); | ||
} catch (IOException e) { | ||
e.printStackTrace(System.out); | ||
println("Error calculating digest for: " + file.getAbsolutePath() + " : " + e.getMessage()); | ||
} | ||
} catch (NoSuchAlgorithmException nsae) { | ||
println("MD5 algorithm not found: " + nsae.getMessage()); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters