1 package fr.in2p3.jsaga.adaptor.data;
2
3 import fr.in2p3.jsaga.adaptor.base.defaults.Default;
4 import fr.in2p3.jsaga.adaptor.base.usage.UOptional;
5 import fr.in2p3.jsaga.adaptor.base.usage.Usage;
6 import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
7 import fr.in2p3.jsaga.helpers.EntryPath;
8 import org.globus.ftp.FileInfo;
9 import org.globus.ftp.GridFTPSession;
10 import org.globus.ftp.exception.UnexpectedReplyCodeException;
11 import org.ogf.saga.error.*;
12
13 import java.util.Map;
14 import java.util.Vector;
15
16
17
18
19
20
21
22
23
24
25
26
27
28 public class Gsiftp1DataAdaptor extends GsiftpDataAdaptorAbstract {
29 public String getType() {
30 return "gsiftp-v1";
31 }
32
33
34 public Usage getUsage() {
35 return new UOptional(TCP_BUFFER_SIZE);
36 }
37
38
39 public Default[] getDefaults(Map attributes) throws IncorrectStateException {
40 return null;
41 }
42
43
44 public FileAttributes getAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
45 EntryPath path = new EntryPath(absolutePath);
46 String entryName = path.getEntryName();
47 FileAttributes[] list = this.listAttributes(path.getBaseDir(), additionalArgs);
48 for (int i=0; i<list.length; i++) {
49 if (list[i].getName().equals(entryName)) {
50 return list[i];
51 }
52 }
53 throw new DoesNotExistException("Entry does not exist: "+entryName);
54 }
55
56
57 public FileAttributes[] listAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
58 Vector v;
59 try {
60 m_client.setMode(GridFTPSession.MODE_STREAM);
61 m_client.setPassiveMode(true);
62 String sav = m_client.getCurrentDir();
63 m_client.changeDir(absolutePath);
64 v = m_client.list();
65 m_client.changeDir(sav);
66 } catch (Exception e) {
67 try {
68 throw rethrowException(e);
69 } catch (BadParameterException badParameter) {
70 throw new NoSuccessException("Unexpected exception", e);
71 }
72 }
73 FileAttributes[] ret = new FileAttributes[v.size()];
74 for (int i=0; i<v.size(); i++) {
75 FileInfo entry = (FileInfo) v.get(i);
76 ret[i] = new Gsiftp1FileAttributes(entry);
77 }
78 return ret;
79 }
80
81 protected void rethrowParsedException(UnexpectedReplyCodeException e) throws DoesNotExistException, AlreadyExistsException, PermissionDeniedException, NoSuccessException {
82 String message = e.getReply().getMessage();
83 if (message.indexOf("not a plain file") > -1) {
84 throw new DoesNotExistException(e);
85 } else if (message.indexOf("exists") > -1) {
86 throw new AlreadyExistsException(e);
87 } else if (message.indexOf("Permission denied") > -1) {
88 throw new PermissionDeniedException(e);
89 } else {
90 throw new NoSuccessException(e);
91 }
92 }
93 }