View Javadoc

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.*;
5   import fr.in2p3.jsaga.adaptor.data.read.FileAttributes;
6   import org.globus.ftp.GridFTPSession;
7   import org.globus.ftp.MlsxEntry;
8   import org.globus.ftp.exception.ServerException;
9   import org.globus.ftp.exception.UnexpectedReplyCodeException;
10  import org.ogf.saga.error.*;
11  
12  import java.io.IOException;
13  import java.util.*;
14  
15  /* ***************************************************
16  * *** Centre de Calcul de l'IN2P3 - Lyon (France) ***
17  * ***             http://cc.in2p3.fr/             ***
18  * ***************************************************
19  * File:   Gsiftp2DataAdaptor
20  * Author: Sylvain Reynaud (sreynaud@in2p3.fr)
21  * Date:   20 juil. 2007
22  * ***************************************************
23  * Description:                                      */
24  /**
25   *
26   */
27  public class Gsiftp2DataAdaptor extends GsiftpDataAdaptorAbstract {
28      private static final String PROTECTION = "Protection";
29  
30      @Override
31      public String getType() {
32          return "gsiftp-v2";
33      }
34  
35      @Override
36      public Usage getUsage() {
37          return new UAnd.Builder()
38                  .and(new UOptional(TCP_BUFFER_SIZE))
39                  .and(new UOptional(PROTECTION))
40                  .build();
41      }
42  
43      @Override
44      public Default[] getDefaults(Map attributes) throws IncorrectStateException {
45          return new Default[]{new Default(PROTECTION, "none"),
46                                  new Default(TCP_BUFFER_SIZE, "1048576")};
47      }
48  
49      /** override super.connect() to set data channel protection level */
50      @Override
51      public void connect(String userInfo, String host, int port, String basePath, Map attributes) throws AuthenticationFailedException, AuthorizationFailedException, BadParameterException, TimeoutException, NoSuccessException {
52          super.connect(userInfo, host, port, basePath, attributes);
53          if (attributes!=null && attributes.containsKey(PROTECTION)) {
54              String value = ((String) attributes.get(PROTECTION)).toLowerCase();
55  
56              // get protection level from attributes
57              int protection;
58              if (value.equalsIgnoreCase("none")) {
59                  protection = GridFTPSession.PROTECTION_CLEAR;
60              } else if (value.equalsIgnoreCase("integrity")) {
61                  protection = GridFTPSession.PROTECTION_SAFE;
62              } else if (value.equalsIgnoreCase("confidentiality")) {
63                  protection = GridFTPSession.PROTECTION_CONFIDENTIAL;
64              } else if (value.equalsIgnoreCase("privacy")) {
65                  protection = GridFTPSession.PROTECTION_PRIVATE;
66              } else {
67                  throw new BadParameterException("Attribute '"+PROTECTION+"' has unexpected value: "+value);
68              }
69  
70              // set protection level to data channel
71              try {
72                  m_client.setDataChannelProtection(protection);
73                  m_client.setProtectionBufferSize(16384);
74              } catch (IOException e) {
75                  throw new NoSuccessException(e);
76              } catch (ServerException e) {
77                  throw new NoSuccessException(e);
78              }
79          }
80      }
81  
82      public FileAttributes getAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
83          MlsxEntry entry;
84          try {
85              m_client.setPassiveMode(false);
86              entry = m_client.mlst(absolutePath);
87          } catch (Exception e) {
88              try {
89                  throw rethrowException(e);
90              } catch (BadParameterException badParameter) {
91                  throw new NoSuccessException("Unexpected exception", e);
92              }
93          }
94          return new Gsiftp2FileAttributes(entry);
95      }
96  
97      public FileAttributes[] listAttributes(String absolutePath, String additionalArgs) throws PermissionDeniedException, DoesNotExistException, TimeoutException, NoSuccessException {
98          Vector v;
99          try {
100             m_client.setMode(GridFTPSession.MODE_STREAM);
101             m_client.setPassiveMode(true);
102             v = m_client.mlsd(absolutePath);
103         } catch (Exception e) {
104             try {
105                 throw rethrowException(e);
106             } catch (BadParameterException badParameter) {
107                 throw new NoSuccessException("Unexpected exception", e);
108             }
109         }
110         List files = new ArrayList();
111         for (int i=0; i<v.size(); i++) {
112             MlsxEntry entry = (MlsxEntry) v.get(i);
113             // remove trailing '/'
114             if (absolutePath.matches(entry.getFileName() + "/*")) {
115                 // ignore this entry: absolutePath
116                 continue;
117             }
118             try {
119                 files.add(new Gsiftp2FileAttributes(entry));
120             } catch(DoesNotExistException e) {
121                 // ignore this entry: ., .. or null
122             }
123         }
124         return (FileAttributes[]) files.toArray(new FileAttributes[files.size()]);
125     }
126 
127     protected void rethrowParsedException(UnexpectedReplyCodeException e) throws DoesNotExistException, AlreadyExistsException, PermissionDeniedException, NoSuccessException {
128         String message = e.getReply().getMessage();
129         if (message.indexOf("No such") > -1) {
130             throw new DoesNotExistException(e);
131         } else if (message.indexOf("exists") > -1) {
132             throw new AlreadyExistsException(e);
133         } else if (message.indexOf("Permission denied") > -1) {
134             throw new PermissionDeniedException(e);
135         } else if (message.indexOf("Operation not permitted") > -1) {
136             throw new PermissionDeniedException(e);
137         } else {
138             throw new NoSuccessException(e);
139         }
140     }
141 }