Commit d2fe7715 authored by Pedro Rocha's avatar Pedro Rocha
Browse files

Fragmentando o upload em chunks de 512kb.

[INFOGRD-3717]
parent e7a20c4c
......@@ -13,6 +13,7 @@ public class Algorithm {
private User whoCreated = null;
private AlgorithmVersion lastVersion = null;
private List<AlgorithmVersion> versions = new ArrayList<AlgorithmVersion>();
private Boolean hidden = null;
public Algorithm id(String id) {
this.id = id;
return this;
......@@ -56,6 +57,16 @@ public class Algorithm {
public void setWhoCreated(User whoCreated) {
this.whoCreated = whoCreated;
}
public Boolean getHidden() {
return hidden;
}
public void setHidden(Boolean hidden) {
this.hidden = hidden;
}
public Algorithm hidden(Boolean hidden) {
this.hidden = hidden;
return this;
}
public Algorithm lastVersion(AlgorithmVersion lastVersion) {
this.lastVersion = lastVersion;
return this;
......@@ -95,12 +106,13 @@ public class Algorithm {
Objects.equals(this.categories, algorithm.categories) &&
Objects.equals(this.whoCreated, algorithm.whoCreated) &&
Objects.equals(this.lastVersion, algorithm.lastVersion) &&
Objects.equals(this.versions, algorithm.versions);
Objects.equals(this.versions, algorithm.versions) &&
Objects.equals(this.hidden, algorithm.hidden);
}
@Override
public int hashCode() {
return Objects.hash(id, name, categories, whoCreated, lastVersion, versions);
return Objects.hash(id, name, categories, whoCreated, lastVersion, versions, hidden);
}
@Override
......@@ -113,6 +125,7 @@ public class Algorithm {
sb.append(" whoCreated: ").append(toIndentedString(whoCreated)).append("\n");
sb.append(" lastVersion: ").append(toIndentedString(lastVersion)).append("\n");
sb.append(" versions: ").append(toIndentedString(versions)).append("\n");
sb.append(" hidden: ").append(toIndentedString(hidden)).append("\n");
sb.append("}");
return sb.toString();
}
......
......@@ -52,6 +52,20 @@ public class JobInfo {
}
}
public enum JobType {
FLOW("flow"),
ALGORITHM("algorithm");
private String value;
JobType(String value) {
this.value = value;
}
@Override
public String toString() {
return String.valueOf(value);
}
}
private SubmitedByEnum submitedBy = null;
private String projectId = null;
......@@ -86,6 +100,8 @@ public class JobInfo {
private Integer exitCode = null;
private JobType jobType = null;
/**
* The status describing the end of the execution. It can be empty if the job
* have not already finished.
......@@ -520,6 +536,24 @@ public class JobInfo {
this.exitStatus = exitStatus;
}
/**
* The type of the job. It is either FLOW or ALGORITHM.
*
* @return jobType
*/
public JobType getJobType() {
return jobType;
}
public void setJobType(JobType jobType) {
this.jobType = jobType;
}
public JobInfo jobType(JobType jobType) {
this.jobType = jobType;
return this;
}
public JobInfo cpuTime(Double cpuTime) {
this.cpuTime = cpuTime;
return this;
......@@ -704,6 +738,7 @@ public class JobInfo {
Objects.equals(this.state, job.state) &&
Objects.equals(this.exitCode, job.exitCode) &&
Objects.equals(this.exitStatus, job.exitStatus) &&
Objects.equals(this.jobType, job.jobType) &&
Objects.equals(this.cpuTime, job.cpuTime) &&
Objects.equals(this.wallclockTime, job.wallclockTime) &&
Objects.equals(this.ramMemory, job.ramMemory) &&
......@@ -718,7 +753,7 @@ public class JobInfo {
public int hashCode() {
return Objects.hash(jobId, groupId, sessionId, submitedBy, projectId, algorithmId, algorithmVersion, algorithmName,
jobOwner, jobOwnerName, automaticallyMachineSelection, submissionMachine, submissionTime, executionMachine,
endTime, numberOfAttempts, description, priority, state, exitCode, exitStatus, cpuTime, wallclockTime, ramMemory,
endTime, numberOfAttempts, description, priority, state, exitCode, exitStatus, jobType, cpuTime, wallclockTime, ramMemory,
progressInfo, statusHistory, lastModifiedTime, monitoredFiles, specificData);
}
......@@ -749,6 +784,7 @@ public class JobInfo {
sb.append(" state: ").append(toIndentedString(state)).append("\n");
sb.append(" exitCode: ").append(toIndentedString(exitCode)).append("\n");
sb.append(" exitStatus: ").append(toIndentedString(exitStatus)).append("\n");
sb.append(" jobType: ").append(toIndentedString(jobType)).append("\n");
sb.append(" cpuTime: ").append(toIndentedString(cpuTime)).append("\n");
sb.append(" wallclockTime: ").append(toIndentedString(wallclockTime)).append("\n");
sb.append(" ramMemory: ").append(toIndentedString(ramMemory)).append("\n");
......
......@@ -2,17 +2,21 @@ package Requests.Projects;
import Requests.Authentication.InvalidLoginOrPasswordException;
import Requests.Authentication.Token;
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataMultiPart;
import org.glassfish.jersey.media.multipart.MultiPart;
import org.glassfish.jersey.media.multipart.MultiPartFeature;
import org.glassfish.jersey.media.multipart.file.FileDataBodyPart;
import org.glassfish.jersey.media.multipart.file.StreamDataBodyPart;
import javax.ws.rs.client.*;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
......@@ -32,30 +36,81 @@ public class FileExplorer {
public static FileUpload uploadFile(String host, Token token, Project project, String projectFolderPath,
String localFilePath, String fileName) throws ProjectOrFileNotFoundException, PermissionException {
FileDataBodyPart filePart = new FileDataBodyPart("file", new File(localFilePath));
MultiPart multipartEntity = null;
File file = new File(localFilePath);
Long size = file.length();
Integer rangeStart = 0;
Integer rangeEnd = null;
Integer chunkSize = 512*1024; // 512kb
FormDataMultiPart form = null;
FileInputStream fis = null;
try {
Client client = ClientBuilder.newClient();
WebTarget webTarget = client.target(host).register(MultiPartFeature.class);
filePart.setContentDisposition(FormDataContentDisposition.name("file")
.fileName(fileName).build());
form = new FormDataMultiPart();
form.field("uploadType", "multipart");
multipartEntity = form.bodyPart(filePart);
Response response = webTarget.path("projects").path(project.getId()).path("files").path
(projectFolderPath).request("application/json;charset=UTF-8").header(HttpHeaders
.AUTHORIZATION, "Bearer " + token.getAccessToken()).post(Entity.entity
(multipartEntity, MediaType.MULTIPART_FORM_DATA));
return response.readEntity(FileUpload.class);
} finally {
if (multipartEntity != null) {
try {
multipartEntity.close();
} catch (IOException e) {
// se o arquivo é menor que um chunk, pode enviar de uma vez só.
if(size <= chunkSize) {
FileDataBodyPart filePart = new FileDataBodyPart("file", file);
filePart.setContentDisposition(FormDataContentDisposition.name("file")
.fileName(fileName).build());
form.field("uploadType", "multipart");
form.bodyPart(filePart);
Response response = webTarget.path("projects").path(project.getId()).path("files").path
(projectFolderPath).request("application/json;charset=UTF-8").header(HttpHeaders
.AUTHORIZATION, "Bearer " + token.getAccessToken()).post(Entity.entity
(form, MediaType.MULTIPART_FORM_DATA));
return response.readEntity(FileUpload.class);
} else {
byte buffer[] = new byte[chunkSize];
fis = new FileInputStream(file);
rangeEnd = chunkSize;
form.field("uploadType", "resumable");
form.field("rangeStart", "0");
form.field("rangeEnd", rangeEnd.toString());
form.field("totalSize", size.toString());
fis.read(buffer);
InputStream is = new ByteArrayInputStream(buffer);
StreamDataBodyPart bodyPart = new StreamDataBodyPart("file", is, fileName, MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(bodyPart);
Response response = webTarget.path("projects").path(project.getId()).path("files").path
(projectFolderPath).request("application/json;charset=UTF-8").header(HttpHeaders
.AUTHORIZATION, "Bearer " + token.getAccessToken()).post(Entity.entity
(form, MediaType.MULTIPART_FORM_DATA));
form.getBodyParts().clear();
is.close();
FileUpload fileUpload = response.readEntity(FileUpload.class);
while (response.getStatus() == 200 && rangeEnd < size) {
rangeStart = rangeEnd;
rangeEnd += chunkSize;
rangeEnd = rangeEnd > size.intValue()? size.intValue(): rangeEnd;
form.field("uploadType", "resumable");
form.field("uploadId", fileUpload.getUploadId());
form.field("rangeStart", rangeStart.toString());
form.field("rangeEnd", rangeEnd.toString());
form.field("totalSize", size.toString());
fis.read(buffer);
is = new ByteArrayInputStream(buffer);
bodyPart = new StreamDataBodyPart("file", is, fileName, MediaType.APPLICATION_OCTET_STREAM_TYPE);
form.bodyPart(bodyPart);
response = webTarget.path("projects").path(project.getId()).path("files").path
(projectFolderPath).request("application/json;charset=UTF-8").header(HttpHeaders
.AUTHORIZATION, "Bearer " + token.getAccessToken()).post(Entity.entity
(form, MediaType.MULTIPART_FORM_DATA));
form.getBodyParts().clear();
is.close();
fileUpload = response.readEntity(FileUpload.class);
response.close();
}
multipartEntity = null;
return fileUpload;
}
} catch (FileNotFoundException e) {
e.printStackTrace();
throw new ProjectOrFileNotFoundException();
} catch (IOException e) {
e.printStackTrace();
throw new ProjectOrFileNotFoundException();
} finally {
if (form != null) {
try {
form.close();
......@@ -63,6 +118,13 @@ public class FileExplorer {
}
form = null;
}
if (fis != null ) {
try {
fis.close();
} catch(IOException e) {
}
fis = null;
}
}
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment