Revision 40:362278eba9aa

b/java/gssclient/src/gssclient/cli/GSSClient.java
45 45
    }
46 46

  
47 47
    private void processGSSCommands(String args[]) {
48
	if (args.length == 0)
49
	    return;
48 50
	int statusCode = -1;
49 51
	if (args[0].equals("get-token")) {
50 52
	    if (!(args.length == 4)) {
......
66 68
	}
67 69
	GSSCmd cmd = (GSSCmd) GSSCmdFactory.getInstance().getGSSCmdInstance(args, 
68 70
		client, userInfo);
69
	statusCode = cmd.execute();
70
	System.out.println(HttpStatus.getStatusText(statusCode));
71
//	for (int i = 1; i < args.length; i++) {
72
//	    GetFileCmd cmd = GetFileCmd.getInstance(client, 
73
//		    userInfo, 
74
//		    new Date(), 
75
//		    args[i]);
76
//	    statusCode = cmd.execute();
77
//	
78
//	}
71
	// The cmd may be null when the command line is wrong
72
	if (cmd != null) {
73
	    statusCode = cmd.execute();
74
	    System.out.println(HttpStatus.getStatusText(statusCode));
75
	}
79 76
    }
80 77
    
81 78
    private void processCmd(CommandLine line) {
b/java/gssclient/src/gssclient/cli/GSSCmdFactory.java
14 14

  
15 15
import org.apache.commons.httpclient.HttpClient;
16 16
import org.apache.commons.io.filefilter.WildcardFileFilter;
17
import org.apache.commons.logging.Log;
18
import org.apache.commons.logging.LogFactory;
19 17

  
20 18
public class GSSCmdFactory {
21 19
    
......
72 70
    private static class PutFileCmdFactory extends GSSCmdFactory {
73 71
	private static final PutFileCmdFactory INSTANCE = 
74 72
	    new PutFileCmdFactory();
75

  
76
	private Log log;
77 73
	
78 74
	private PutFileCmdFactory() { 
79
	    log = LogFactory.getLog(this.getClass());
80 75
	}
81 76
	
82 77
	public static PutFileCmdFactory getInstance() {
......
100 95
		    if (name == null) 
101 96
			continue;
102 97
		    File dir = file.getParentFile();
98
		    if (dir == null)
99
			continue;
103 100
		    FileFilter filter = new WildcardFileFilter(name);
104 101
		    File[] filteredFiles = dir.listFiles(filter);
102
		    if (filteredFiles == null)
103
			continue;
105 104
		    for (File filteredFile : filteredFiles) {
106 105
			cmd.add(new PutFileCmd(client, userInfo, new Date(),
107 106
				args[args.length - 1], 
......
109 108
		    }
110 109
		}
111 110
	    }
112
	    return cmd;
111
	    if (cmd.isEmpty()) {
112
		return null;
113
	    } else {
114
		return cmd;
115
	    }
113 116
	}
114 117
    }
115 118
 }
b/java/gssclient/src/gssclient/commands/CompositeCmd.java
1
package gssclient.commands;
2

  
3
import java.util.ArrayList;
4
import java.util.List;
5

  
6
/**
7
 * Composite command, allowing multiple commands to be executed together.
8
 * The return value of the group of commands will be the return value
9
 * of the command that executed last. 
10
 *
11
 */
12
public class CompositeCmd implements GSSCmd {
13

  
14
    // Collection of child commands
15
    private List<GSSCmd> childCommands = new ArrayList<GSSCmd>();
16
    
17
    public int execute() {
18
	int returnVal = -1;
19
	for (GSSCmd cmd : childCommands) {
20
	    returnVal = cmd.execute();
21
	}
22
	return returnVal;
23
    }
24
    
25
    public void add(GSSCmd cmd) {
26
	childCommands.add(cmd);
27
    }
28
    
29
    public void remove(GSSCmd cmd) {
30
	childCommands.remove(cmd);
31
    }
32
    
33
    public boolean isEmpty() {
34
	return childCommands.isEmpty();
35
    }
36
}
b/java/gssclient/src/gssclient/commands/GSSCmd.java
1 1
package gssclient.commands;
2 2

  
3
/**
4
 * Interface for GSS commands. A command is something that can be executed,
5
 * and returns a value depending on the execution outcome. 
6
 */
3 7
public interface GSSCmd {
4
    
5 8
    public int execute();
6

  
7 9
}
b/java/gssclient/src/gssclient/commands/GSSUtils.java
1
package gssclient.commands;
2

  
3
import java.io.IOException;
4
import java.util.Date;
5

  
6
import gssclient.UserInfo;
7
import gssclient.httpmethods.HeadMethodFactory;
8
import gssclient.httpmethods.HttpMethodFactory;
9

  
10
import org.apache.commons.httpclient.Header;
11
import org.apache.commons.httpclient.HttpClient;
12
import org.apache.commons.httpclient.HttpException;
13
import org.apache.commons.httpclient.HttpStatus;
14
import org.apache.commons.httpclient.methods.HeadMethod;
15
import org.apache.commons.logging.Log;
16
import org.apache.commons.logging.LogFactory;
17
import org.json.JSONException;
18
import org.json.JSONObject;
19

  
20
/**
21
 * General utility classes for GSS.
22
 *
23
 */
24
public class GSSUtils {
25

  
26
    private static final Log log = LogFactory.getLog(GSSUtils.class);
27

  
28
    public static final String X_GSS_METADATA_HEADER = "X-GSS-Metadata";
29

  
30
    public static final String GSS_HOME = "pithos.grnet.gr";
31
    
32
    /** The version JSON tag distinguishes between files and directories. */
33
    private static final String VERSION_HEADER = "version";
34
    
35
    /** The file path separator in GSS. */
36
    public static final String GSS_FILEPATH_SEPARATOR = "/";
37
    
38
    private static final GSSUtils INSTANCE = new GSSUtils();
39
    
40
    private int statusCode;
41
    
42
    private GSSUtils() {
43
	this.statusCode = -1;
44
    }
45

  
46
    
47
    public static GSSUtils getInstance() {
48
	return INSTANCE;
49
    }
50
    
51
    public int getStatusCode() {
52
	return this.statusCode;
53
    }
54
    
55
    /**
56
     * Returns true if the specified path represents a directory on GSS.
57
     * 
58
     * @param client the HTTP client object that will be used to execute
59
     * the required methods.
60
     * @param date the date time stamp that will be used in the HTTP methods.
61
     * @param userInfo the user information object.
62
     * @param path The path we want to check.
63
     * @param scratch A scratch pad passed by the calling method which will be
64
     * filled in with the JSON object that came back from the GSS method call.
65
     * It can be null if we are not interested in something like that.
66
     * @return true if the specified path represents a directory on GSS.
67
     */
68
    public boolean isDir(HttpClient client, Date date, UserInfo userInfo, 
69
	    String path, Scratch<JSONObject> scratch){
70

  
71
	boolean isDir = false;
72
	HeadMethod headMethod = HeadMethodFactory.getInstance()
73
		.generateMethod(HttpMethodFactory.Scheme.HTTP, GSS_HOME,
74
			date, userInfo, path, null);
75

  
76
	try {
77
	    // Execute the method.
78
	    log.debug("Start method execution " + headMethod.getName());
79
	    this.statusCode = client.executeMethod(headMethod);
80
	    log.debug("End method execution " + headMethod.getName() 
81
		    + " status code=" + this.statusCode);
82

  
83
	    if (this.statusCode != HttpStatus.SC_OK)
84
		return isDir;
85
	    Header gssMetaDataHeader = 
86
		headMethod.getResponseHeader(X_GSS_METADATA_HEADER);
87
	    JSONObject gssMetaData = 
88
		new JSONObject(gssMetaDataHeader.getValue());
89
	    if (scratch != null)
90
		scratch.setContents(gssMetaData);
91
	    if (!gssMetaData.has(VERSION_HEADER)) {
92
		isDir = true;
93
	    } else {
94
		isDir = false;
95
	    }
96
	} catch (HttpException e) {
97
	    System.err.println("Fatal protocol violation: " + e.getMessage());
98
	    e.printStackTrace();
99
	} catch (IOException e) {
100
	    System.err.println("Fatal transport error: " + e.getMessage());
101
	    e.printStackTrace();
102
	} catch (JSONException jex) {
103
	    System.err.println("Fatal JSON Exception: " + jex.getMessage());
104
	} finally {
105
	    // Release the connection.
106
	    headMethod.releaseConnection();
107
	}
108
	return isDir;
109
    }
110
    
111
    /**
112
     * A helper class that contains arbitrary data. To be used so that calling
113
     * methods can pass a scratch pad where called methods can put in data
114
     * other than the return value.
115
     *
116
     * @param <T> The type of content data
117
     */
118
    public static class Scratch<T> {
119
	T contents;
120
	
121
	void setContents(T scratch) {
122
	    this.contents = scratch;
123
	}
124
	
125
	T getContents() {
126
	    return this.contents;
127
	}
128
    }
129

  
130
}
b/java/gssclient/src/gssclient/commands/GetFileCmd.java
90 90
	int statusCode = cmd.execute();
91 91
	System.out.println(HttpStatus.getStatusText(statusCode));
92 92
    }
93

  
94
  
93 95
}
94 96

  
b/java/gssclient/src/gssclient/commands/GetTokenCmd.java
34 34
	this.username = username;
35 35
	this.passwd = passwd;
36 36
    }
37
    
37

  
38 38
    public int execute() {
39 39
	int statusCode = HttpStatus.SC_OK;
40 40
	NaiveTrustProvider.setAlwaysTrust(Boolean.TRUE);
b/java/gssclient/src/gssclient/commands/ListPropertiesCmd.java
5 5
import gssclient.ShibAuthException;
6 6
import gssclient.UserInfo;
7 7
import gssclient.httpmethods.GetMethodFactory;
8
import gssclient.httpmethods.HeadMethodFactory;
9 8
import gssclient.httpmethods.HttpMethodFactory;
10 9

  
11 10
import java.io.IOException;
12 11
import java.util.Date;
13 12

  
14
import org.apache.commons.httpclient.Header;
15 13
import org.apache.commons.httpclient.HttpClient;
16 14
import org.apache.commons.httpclient.HttpException;
17 15
import org.apache.commons.httpclient.HttpStatus;
18 16
import org.apache.commons.httpclient.methods.GetMethod;
19
import org.apache.commons.httpclient.methods.HeadMethod;
20 17
import org.apache.commons.logging.Log;
21 18
import org.apache.commons.logging.LogFactory;
22
import org.json.JSONException;
23 19
import org.json.JSONObject;
24 20

  
25 21
public class ListPropertiesCmd implements GSSCmd {
......
30 26
    private String path;
31 27
    private String localPath;
32 28
    private Log log;
33
    
34
    private static final String X_GSS_METADATA_HEADER = "X-GSS-Metadata";
35
    
29
    JSONObject gssMetaData;
30
        
36 31
    public ListPropertiesCmd(HttpClient client, UserInfo userInfo, Date date, 
37 32
	    String path, String localPath) {
38 33
	this.client = client;
......
48 43
	this.path = path;
49 44
    }
50 45
    
46
    public JSONObject getGSSMetaData() {
47
	return this.gssMetaData;
48
    }
49
    
51 50
    public int execute() {
52 51
	int statusCode = -1;
53 52
	
54
	HeadMethod headMethod = HeadMethodFactory.getInstance()
55
		.generateMethod(HttpMethodFactory.Scheme.HTTP,
56
		"pithos.grnet.gr",
57
		date, userInfo, path, localPath);
58

  
59
	JSONObject gssMetaData = null;
60

  
53
	GetMethod getMethod = null;
54
	
61 55
	try {
62
	    // Execute the method.
63
	    log.debug("Start method execution " + headMethod.getName());
64
	    statusCode = client.executeMethod(headMethod);
65
	    log.debug("End method execution " + headMethod.getName());
66

  
67
	    if (statusCode != HttpStatus.SC_OK)
68
		return statusCode;
69
	    // Read the response headers first, to determine if we
70
	    // are dealing with a file or a directory.
71
	    Header gssMetaDataHeader = 
72
		headMethod.getResponseHeader(X_GSS_METADATA_HEADER);
73
		gssMetaData = new JSONObject(gssMetaDataHeader.getValue());
74
		if (!gssMetaData.has("version")) { // Dir
75
		    GetMethod getMethod = GetMethodFactory.getInstance()
56
	    GSSUtils.Scratch<JSONObject> scratch = 
57
		new GSSUtils.Scratch<JSONObject>();
58
	    GSSUtils utils = GSSUtils.getInstance();
59
	    if (!utils.isDir(client, date, userInfo, path, scratch)) {
60
		statusCode = utils.getStatusCode();
61
		this.gssMetaData = scratch.getContents();
62
	    } else {
63
		getMethod = GetMethodFactory.getInstance()
76 64
		    	.generateMethod(HttpMethodFactory.Scheme.HTTP,
77
				"pithos.grnet.gr",
65
				GSSUtils.GSS_HOME,
78 66
				date, userInfo, path, localPath);
79
		    log.debug("Start method execution " + getMethod.getName());
80
		    statusCode = client.executeMethod(getMethod);
81
		    log.debug("End method execution " + getMethod.getName());
82
		    if (statusCode != HttpStatus.SC_OK)
83
			return statusCode;
84
		    gssMetaData = 
85
			new JSONObject(getMethod.getResponseBodyAsString());
67
		log.debug("Start method execution " + getMethod.getName());
68
		statusCode = client.executeMethod(getMethod);
69
		log.debug("End method execution " + getMethod.getName());
70
		if (statusCode != HttpStatus.SC_OK) {
71
		    return statusCode;
72
		} else {
73
		    this.gssMetaData = 
74
			new JSONObject(getMethod.getResponseHeader(GSSUtils.X_GSS_METADATA_HEADER));
86 75
		}
76
	    } 
87 77
	} catch (HttpException e) {
88 78
	    System.err.println("Fatal protocol violation: " + e.getMessage());
89 79
	    e.printStackTrace();
90 80
	} catch (IOException e) {
91 81
	    System.err.println("Fatal transport error: " + e.getMessage());
92 82
	    e.printStackTrace();
93
	} catch (JSONException jex) {
94
	    System.err.println("Fatal JSON Exception: " + jex.getMessage());
95 83
	} finally {
96
	    // Release the connection.
97
	    headMethod.releaseConnection();
84
	    // Release the connections.
85
	    if (getMethod != null)
86
		getMethod.releaseConnection();
98 87
	}
99
	
100
	System.out.println(gssMetaData);
101 88
	return statusCode;
102 89
    }
103 90
    
......
109 96
   
110 97
        HttpClient client = new HttpClient();
111 98

  
112
	ListPropertiesCmd cmd = new ListPropertiesCmd(client, userInfo, 
113
		new Date(), "test.txt", null);
99
	ListPropertiesCmd cmd = 
100
	    new ListPropertiesCmd(client, userInfo, new Date(), 
101
		    "test.txt", null);
114 102

  
115 103
	int statusCode = cmd.execute();
116 104
	System.out.println(HttpStatus.getStatusText(statusCode));
105
	System.out.println(cmd.getGSSMetaData());
117 106
	cmd.setPath("");
118 107
	statusCode = cmd.execute();
119 108
	System.out.println(HttpStatus.getStatusText(statusCode));
109
	System.out.println(cmd.getGSSMetaData());
120 110
    }
121 111
}
b/java/gssclient/src/gssclient/commands/PutFileCmd.java
19 19
import org.apache.commons.logging.Log;
20 20
import org.apache.commons.logging.LogFactory;
21 21

  
22
/**
23
 * Command to put (upload) files to GSS. Creates the necessary REST call and
24
 * executes it.
25
 * 
26
 * The target can be a fully specified filename, or a directory. When it is a  
27
 * directory, the file is uploaded in the target directory with the same name
28
 * as the local file.
29
 */
22 30
public class PutFileCmd implements GSSCmd {
23 31

  
24 32
    private HttpClient client;
......
50 58
    public int execute() {
51 59
	int statusCode = -1;
52 60
	
61
	GSSUtils utils = GSSUtils.getInstance();
62
	if (utils.isDir(client, date, userInfo, path, null)) {
63
	    File localFile = new File(localPath);
64
	    path = path 
65
	    	+ (path.endsWith(GSSUtils.GSS_FILEPATH_SEPARATOR) 
66
	    		? "" 
67
	    		: GSSUtils.GSS_FILEPATH_SEPARATOR)
68
	    	+ localFile.getName();
69
	}
53 70
	PutMethod method = PutMethodFactory.getInstance()
54 71
		.generateMethod(HttpMethodFactory.Scheme.HTTP,
55
			"pithos.grnet.gr",
72
			GSSUtils.GSS_HOME,
56 73
			date, userInfo, path, localPath);
57 74
	log.debug("PUT from=" + localPath + " to=" + path);
58 75
	
b/java/gssclient/src/gssclient/httpmethods/HttpMethodFactory.java
1 1
package gssclient.httpmethods;
2 2

  
3 3
import gssclient.UserInfo;
4
import gssclient.commands.GSSUtils;
4 5

  
5 6
import java.io.UnsupportedEncodingException;
6 7
import java.net.URLEncoder;
......
27 28
public abstract class HttpMethodFactory {
28 29

  
29 30
    public static enum Scheme { HTTP, HTTPS };
30
    public static final String HOST = "http://pithos.grnet.gr";
31
    public static final String HOST = "http://" + GSSUtils.GSS_HOME;
31 32
    public static final String REST_PREFIX = "/pithos/rest";
32 33
    public static final String PATH_PREFIX = HOST + REST_PREFIX;
33 34
    private static final String HMAC_SHA1 = "HmacSHA1";

Also available in: Unified diff