X-Git-Url: http://git.argeo.org/?a=blobdiff_plain;f=runtime%2Forg.argeo.slc.support.simple%2Fsrc%2Fmain%2Fjava%2Forg%2Fargeo%2Fslc%2Fjsch%2FScpTo.java;h=5da5f79e7ba2511ee774f8e4b62498d17fb65b4e;hb=0e203ff53b8339d966c24687fe335bbbb7192641;hp=2a6da62de8048b60607c7aee17d0994999151036;hpb=7cb867319d0f8b1b0af3e7eb3f9833b091281320;p=gpl%2Fargeo-slc.git diff --git a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpTo.java b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpTo.java index 2a6da62de..5da5f79e7 100644 --- a/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpTo.java +++ b/runtime/org.argeo.slc.support.simple/src/main/java/org/argeo/slc/jsch/ScpTo.java @@ -15,6 +15,7 @@ import org.apache.commons.io.IOUtils; import org.apache.commons.logging.Log; import org.apache.commons.logging.LogFactory; import org.argeo.slc.SlcException; +import org.springframework.core.io.ByteArrayResource; import org.springframework.core.io.Resource; import org.springframework.util.AntPathMatcher; import org.springframework.util.PathMatcher; @@ -56,7 +57,7 @@ public class ScpTo extends AbstractJschTask { } if (localResource != null) { - uploadResource(session, localResource, remoteDir); + uploadResource(session, localResource); } } @@ -108,35 +109,45 @@ public class ScpTo extends AbstractJschTask { protected void uploadFile(Session session, File file, String remoteFile) { try { - uploadFile(session, new FileInputStream(file), file.length(), file + upload(session, new FileInputStream(file), file.length(), file .getPath(), file.toString(), remoteFile); } catch (FileNotFoundException e) { throw new SlcException("Cannot upload " + file, e); } } - protected void uploadResource(Session session, Resource resource, - String remoteFile) { + protected void uploadResource(Session session, Resource resource) { + String targetPath = remotePath != null ? remotePath : remoteDir + '/' + + resource.getFilename(); try { File lFile = resource.getFile(); - uploadFile(session, lFile, remotePath); + uploadFile(session, lFile, targetPath); } catch (IOException e) { // no underlying file found // load the resource in memory before transferring it InputStream in = null; try { - in = resource.getInputStream(); - ByteArrayOutputStream out = new ByteArrayOutputStream(); - IOUtils.copy(in, out); - byte[] arr = out.toByteArray(); + byte[] arr; + String path; + if (resource instanceof ByteArrayResource) { + arr = ((ByteArrayResource) resource).getByteArray(); + path = "bytearray"; + } else { + in = resource.getInputStream(); + ByteArrayOutputStream out = new ByteArrayOutputStream(); + IOUtils.copy(in, out); + arr = out.toByteArray(); + path = resource.getURL().getPath(); + if (path.startsWith("/")) + path = path.substring(1); + } ByteArrayInputStream content = new ByteArrayInputStream(arr); - uploadFile(session, content, arr.length, resource.getURL() - .getPath(), resource.toString(), remotePath); + upload(session, content, arr.length, path, resource.toString(), + targetPath); arr = null; } catch (IOException e1) { - throw new SlcException("Can neither interpret resource " - + localResource - + " as file, nor create a temporary file", e1); + throw new SlcException("Can not interpret resource " + + localResource, e1); } finally { IOUtils.closeQuietly(in); // no need to close byte arrays streams @@ -144,7 +155,7 @@ public class ScpTo extends AbstractJschTask { } } - protected void uploadFile(Session session, InputStream in, long size, + protected void upload(Session session, InputStream in, long size, String path, String sourceDesc, String remoteFile) { OutputStream channelOut; InputStream channelIn;