diff --git a/pom.xml b/pom.xml index cfbd703..6547be0 100644 --- a/pom.xml +++ b/pom.xml @@ -49,11 +49,7 @@ decompress-archive-plugin [1.0.0-SNAPSHOT,2.0.0-SNAPSHOT) - - org.gcube.data.transfer - sis-geotk-plugin - LATEST - + diff --git a/src/main/java/org/gcube/data/transfer/service/transfers/engine/impl/AbstractTicketHandler.java b/src/main/java/org/gcube/data/transfer/service/transfers/engine/impl/AbstractTicketHandler.java index f1be9ab..67eeccf 100644 --- a/src/main/java/org/gcube/data/transfer/service/transfers/engine/impl/AbstractTicketHandler.java +++ b/src/main/java/org/gcube/data/transfer/service/transfers/engine/impl/AbstractTicketHandler.java @@ -9,6 +9,10 @@ import java.io.InputStream; import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; +import java.nio.file.Files; +import java.nio.file.StandardCopyOption; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; import java.util.HashMap; import java.util.Map.Entry; import java.util.regex.Matcher; @@ -40,11 +44,19 @@ public abstract class AbstractTicketHandler { PersistenceProvider persistenceProvider; PluginManager pluginManager; + + private MessageDigest md; + public AbstractTicketHandler(PersistenceProvider persProv,PluginManager plugMan, TransferTicket ticket) { this.persistenceProvider=persProv; this.pluginManager=plugMan; this.ticket=ticket; + try { + md = MessageDigest.getInstance("SHA1"); + } catch (NoSuchAlgorithmException e) { + throw new RuntimeException("Unable to initialize",e); + } } protected void onStep(String msg,double progress,Status status,long transferredBytes){ @@ -120,13 +132,16 @@ public abstract class AbstractTicketHandler { throw new ManagedException("Cannot save file in host"); } - transferStream(is, out); + String checksum=transferStream(is, out); + completedTransfer=true; // IF TRANSFER FAILS, EXCEPTIONS AR THROWN - log.debug("Completed transfer to {} , moving to destination {} ",tempFile.getAbsolutePath(),destination.getAbsolutePath()); - tempFile.renameTo(destination); - + log.debug("Completed transfer to {} [ SHA1 : {}]. moving to destination {} ",tempFile.getAbsolutePath(),checksum,destination.getAbsolutePath()); + Files.copy(tempFile.toPath(), destination.toPath(),StandardCopyOption.REPLACE_EXISTING); + Files.deleteIfExists(tempFile.toPath()); + log.debug("Moved. Size is [temp : {} , dest : {}] ",tempFile.length(),destination.length()); + //Plugin execution if(ticket.getPluginInvocations()!=null){ for(PluginInvocation invocation:ticket.getPluginInvocations()){ @@ -185,25 +200,37 @@ public abstract class AbstractTicketHandler { return getTicket(); } - - private void transferStream(InputStream in, OutputStream out) throws ManagedException{ - + + + private String transferStream(InputStream in, OutputStream out) throws ManagedException{ + md.reset(); + long receivedTotal=0l; try{ byte[] internalBuf=new byte[1024]; int received=0; while ((received=in.read(internalBuf))!=-1){ + md.update(internalBuf, 0, received); out.write(internalBuf,0,received); receivedTotal+=received; onStep("Transferring",0d,Status.TRANSFERRING,receivedTotal); } out.flush(); + + byte[] mdbytes = md.digest(); + + //convert the byte to hex format + StringBuffer sb = new StringBuffer(""); + for (int i = 0; i < mdbytes.length; i++) { + sb.append(Integer.toString((mdbytes[i] & 0xff) + 0x100, 16).substring(1)); + } + log.debug("Completed transfer phase for ticket ID {}. Transferred {} bytes. ",ticket.getId(),receivedTotal); + return sb.toString(); }catch(IOException e){ log.debug("Unable to read from source",e); throw new ManagedException("Unable to read from source."); } - log.debug("Completed transfer phase for ticket ID {}. Transferred {} bytes. ",ticket.getId(),receivedTotal); } private InputStream getInputStream() throws ManagedException{