1 16 package org.apache.cocoon.acting; 17 18 import java.io.InputStream ; 19 import java.io.OutputStream ; 20 import java.util.Map ; 21 22 import org.apache.avalon.framework.parameters.Parameters; 23 import org.apache.avalon.framework.service.ServiceException; 24 import org.apache.avalon.framework.service.ServiceManager; 25 import org.apache.avalon.framework.thread.ThreadSafe; 26 import org.apache.cocoon.components.source.impl.PartSource; 27 import org.apache.cocoon.environment.Redirector; 28 import org.apache.excalibur.source.ModifiableSource; 29 import org.apache.excalibur.source.Source; 30 import org.apache.excalibur.source.SourceResolver; 31 import org.apache.excalibur.source.TraversableSource; 32 33 48 public class CopySourceAction extends ServiceableAction implements ThreadSafe 49 { 50 51 private SourceResolver resolver; 52 53 public void service(ServiceManager manager) throws ServiceException { 54 super.service(manager); 55 this.resolver = (SourceResolver)manager.lookup(SourceResolver.ROLE); 56 } 57 58 public Map act(Redirector redirector, org.apache.cocoon.environment.SourceResolver oldResolver, Map objectModel, String source, Parameters par) 59 throws Exception { 60 61 Source src = resolver.resolveURI(source); 63 Source dest = resolver.resolveURI(par.getParameter("dest")); 64 65 if (! (dest instanceof ModifiableSource)) { 67 throw new IllegalArgumentException ("Non-writeable URI : " + dest.getURI()); 68 } 69 70 if (dest instanceof TraversableSource) { 71 TraversableSource trDest = (TraversableSource) dest; 72 if (trDest.isCollection()) { 73 if (src instanceof TraversableSource) { 74 dest = trDest.getChild(((TraversableSource)src).getName()); 75 } else if (src instanceof PartSource){ 76 dest = trDest.getChild(((PartSource)src).getPart().getFileName()); 80 } 81 } 82 } 83 84 ModifiableSource wdest = (ModifiableSource)dest; 85 86 InputStream is = src.getInputStream(); 88 OutputStream os = wdest.getOutputStream(); 89 90 try { 92 byte[] buffer = new byte[1024]; 93 int len; 94 while ((len = is.read(buffer, 0, buffer.length)) > 0) { 95 os.write(buffer, 0, len); 96 } 97 os.close(); 98 } catch(Exception e) { 99 if (wdest.canCancel(os)) { 100 wdest.cancel(os); 101 } 102 } finally { 103 is.close(); 104 } 105 return EMPTY_MAP; 107 } 108 } 109 | Popular Tags |