public RPCEnvelope invoke(RPCEnvelope req) throws Exception {

		RPCStreamEnvelope request = (RPCStreamEnvelope)req;
		if(request.getRpcType()==RPCEnvelope.RPC_TYPE_REQUEST){			
			File f = new File(request.getSource());
			if(f.exists() && f.isFile() && f.canRead() )
			{
				if(log.isDebugEnabled()) log.debug("Sending file to client.");
				RPCStreamEnvelope response = new RPCStreamEnvelope();
				response.setRpcType(RPCEnvelope.RPC_TYPE_RESPONSE);
				response.setDestination(request.getDestination());
				response.setSource(request.getSource());						
				return response;				
			}else{
				RPCShellEnvelope response = new RPCShellEnvelope();
				response.addRpcObjects(1).addRpcObjects("Remote file not found:" + request.getSource());
				return response;
			}
			
		}else{
			RPCShellEnvelope response = new RPCShellEnvelope();
			File f = new File(request.getDestination());
			if(!f.getParentFile().exists()){
				boolean mkdir = f.getParentFile().mkdirs();
				if(mkdir==false){
					response.addRpcObjects(1).addRpcObjects("Failed to create directory");					
				}else{
					FileOutputStream fout = new FileOutputStream(f);
					try{
						StreamCopier.copy(request.getStream(), fout);
						response.addRpcObjects(0).addRpcObjects(request.getLength()).addRpcObjects("Content saved to " + request.getDestination());	
					}finally{
						if(fout!=null) fout.close();
					}					
				}
			}	
			return response;
			
		}
		
	}