Revision 41:f6c63a1edbca

b/java/gssclient/src/gssclient/cli/GSSClient.java
66 66
	    GetTokenCmd getTokenCmd = new GetTokenCmd();
67 67
	    userInfo = getTokenCmd.readUserInfo();
68 68
	}
69
	GSSCmd cmd = (GSSCmd) GSSCmdFactory.getInstance().getGSSCmdInstance(args, 
69
	GSSCmdFactory factory = GSSCmdFactory.getInstance();
70
	if (factory == null) {
71
	    System.err.println("Command not found");
72
	    return;
73
	}
74
	GSSCmd cmd = (GSSCmd) factory.getGSSCmdInstance(args, 
70 75
		client, userInfo);
71 76
	// The cmd may be null when the command line is wrong
72 77
	if (cmd != null) {
73 78
	    statusCode = cmd.execute();
74
	    System.out.println(HttpStatus.getStatusText(statusCode));
79
	    System.out.println(statusCode 
80
		    + " " + HttpStatus.getStatusText(statusCode));
81
	} else {
82
	    System.err.println("Command could not be created");
75 83
	}
76 84
    }
77 85
    
b/java/gssclient/src/gssclient/cli/GSSCmdFactory.java
4 4
import gssclient.commands.CompositeCmd;
5 5
import gssclient.commands.GSSCmd;
6 6
import gssclient.commands.GetFileCmd;
7
import gssclient.commands.MkDirCmd;
7 8
import gssclient.commands.PutFileCmd;
8 9

  
9 10
import java.io.File;
......
19 20
    
20 21
    private static final String GET_FILE = "get";
21 22
    private static final String PUT_FILE = "put";
23
    private static final String MKDIR = "mkdir";
22 24
    
23 25
    private static final Map<String, GSSCmdFactory> COMMANDS 
24 26
    	= new HashMap<String, GSSCmdFactory>();
......
26 28
    static {
27 29
	COMMANDS.put(GET_FILE, GetFileCmdFactory.getInstance());
28 30
	COMMANDS.put(PUT_FILE, PutFileCmdFactory.getInstance());
31
	COMMANDS.put(MKDIR, MkDirCmdFactory.getInstance());
29 32
    }
30 33

  
31 34
    private static GSSCmdFactory INSTANCE = new GSSCmdFactory();
......
39 42
    
40 43
    public GSSCmd getGSSCmdInstance(String args[], HttpClient client,
41 44
	    UserInfo userInfo) {
42
	return COMMANDS.get(args[0]).getGSSCmdInstance(args, 
43
		client, userInfo);
45
	GSSCmdFactory factory = COMMANDS.get(args[0]);
46
	if (factory != null) {
47
	    return factory.getGSSCmdInstance(args, 
48
		    client, userInfo);
49
	} else {
50
	    return null;
51
	}
44 52
    }
45 53
    
46 54
    /**
......
115 123
	    }
116 124
	}
117 125
    }
126

  
127
    /**
128
     * Class for creating MkDirCmd objects.
129
     */
130
    private static class MkDirCmdFactory extends GSSCmdFactory {
131
	private static final MkDirCmdFactory INSTANCE = 
132
	    new MkDirCmdFactory();
133

  
134
	private MkDirCmdFactory() { }
135
	
136
	public static MkDirCmdFactory getInstance() {
137
	    return INSTANCE;
138
	}
139
	
140
	public GSSCmd getGSSCmdInstance(String[] args, 
141
		HttpClient client, UserInfo userInfo) {
142
	    MkDirCmd cmd = new MkDirCmd(client, userInfo, new Date(), 
143
		    args[1], null);
144
	    return cmd;
145
	}
146
    }
147

  
118 148
 }
b/java/gssclient/src/gssclient/commands/MkDirCmd.java
1
package gssclient.commands;
2

  
3
import java.io.FileOutputStream;
4
import java.io.IOException;
5
import java.io.InputStream;
6
import java.util.Date;
7

  
8
import gssclient.NaiveTrustProvider;
9
import gssclient.ShibAuth;
10
import gssclient.ShibAuthException;
11
import gssclient.UserInfo;
12
import gssclient.httpmethods.HttpMethodFactory;
13
import gssclient.httpmethods.PostMethodFactory;
14

  
15
import org.apache.commons.httpclient.HttpClient;
16
import org.apache.commons.httpclient.HttpException;
17
import org.apache.commons.httpclient.HttpStatus;
18
import org.apache.commons.httpclient.methods.PostMethod;
19
import org.apache.commons.io.FilenameUtils;
20
import org.apache.commons.logging.Log;
21
import org.apache.commons.logging.LogFactory;
22

  
23
public class MkDirCmd implements GSSCmd {
24

  
25
    private HttpClient client;
26
    private UserInfo userInfo;
27
    private Date date;
28
    private String path;
29
    private String localPath;
30
    private String dir;
31
    private Log log;
32
    
33
    public MkDirCmd(HttpClient client, UserInfo userInfo, Date date, 
34
	    String path, String localPath) {
35
	this.log = LogFactory.getLog(this.getClass());
36
	this.client = client;
37
	this.userInfo = userInfo;
38
	this.date = date;
39
	this.path = FilenameUtils.getPath(path);
40
	this.dir = FilenameUtils.getName(path);
41
	this.localPath = localPath;
42
    }
43
    
44
    public int execute() {
45
	int statusCode = -1;
46
	
47
	PostMethod method = PostMethodFactory.getInstance()
48
		.generateMethod(HttpMethodFactory.Scheme.HTTP,
49
			GSSUtils.GSS_HOME,
50
			date, userInfo, path, localPath);
51
	method.addParameter("new", this.dir);
52
	
53
	try {
54
	    // Execute the method.
55
	    log.debug("Start method execution...");
56
	    log.debug("Creating directory " + method.getParameter("new"));
57
	    statusCode = client.executeMethod(method);
58
	    log.debug("End method execution.");
59

  
60
	    // Read the response body.
61
	    InputStream in = method.getResponseBodyAsStream();
62
	    if (localPath != null) {
63
		FileOutputStream out = new FileOutputStream(localPath);
64
		byte[] buffer = new byte[1024];
65
		int count = -1;
66
		while ((count = in.read(buffer)) != -1) {
67
		    out.write(buffer, 0, count);
68
		}	
69
		out.flush();
70
		out.close();
71
	    }
72
	} catch (HttpException e) {
73
	    System.err.println("Fatal protocol violation: " + e.getMessage());
74
	    e.printStackTrace();
75
	} catch (IOException e) {
76
	    System.err.println("Fatal transport error: " + e.getMessage());
77
	    e.printStackTrace();
78
	} finally {
79
	    // Release the connection.
80
	    method.releaseConnection();
81
	}
82
	
83
	return statusCode;
84
    }
85
    
86
    public static void main(String[] args) throws ShibAuthException {
87

  
88
	NaiveTrustProvider.setAlwaysTrust(Boolean.TRUE);
89
	ShibAuth shibAuth = new ShibAuth();
90
	UserInfo userInfo = shibAuth.open(args[0], args[1], args[2]);
91
   
92
        HttpClient client = new HttpClient();
93

  
94
	MkDirCmd cmd = new MkDirCmd(client, userInfo, new Date(), 
95
		"temp", null);
96

  
97
	int statusCode = cmd.execute();
98
	System.out.println(HttpStatus.getStatusText(statusCode));
99
    }
100

  
101
  
102
}
103

  
b/java/gssclient/src/gssclient/httpmethods/HttpMethodFactory.java
8 8
import java.security.InvalidKeyException;
9 9
import java.security.NoSuchAlgorithmException;
10 10
import java.text.SimpleDateFormat;
11
import java.util.Arrays;
12 11
import java.util.Date;
13 12
import java.util.Locale;
14 13
import java.util.TimeZone;
......
127 126
	    // In case the path contained directories, make sure they appear
128 127
	    // as such.
129 128
	    encodedPath = encodedPath.replace("%2F", "/");
129
	    encodedPath = encodedPath.replace("%3F", "?");
130
	    encodedPath = encodedPath.replace("%3D", "=");
130 131
	} catch (UnsupportedEncodingException uex) {
131 132
	    uex.printStackTrace();
132 133
	}
b/java/gssclient/src/gssclient/httpmethods/PostMethodFactory.java
1
package gssclient.httpmethods;
2

  
3
import java.io.File;
4

  
5
import org.apache.commons.httpclient.methods.FileRequestEntity;
6
import org.apache.commons.httpclient.methods.PostMethod;
7
import org.apache.commons.httpclient.HttpMethod;
8

  
9
public class PostMethodFactory extends HttpMethodFactory {
10
    
11
    private static PostMethodFactory INSTANCE = new PostMethodFactory(); 
12
	
13
    private PostMethodFactory() {
14
	super();
15
    }
16
    
17
    public static PostMethodFactory getInstance() {
18
	return INSTANCE;
19
    }
20
    
21
    /**
22
     * Generates a put method given the specified URI.
23
     * @param uri The method URI.
24
     * @param localPath The local path to the file implicated in the method;
25
     * it may be null.
26
     * @return The generated method. It can then be used to actually carry
27
     * out the call.
28
     */
29
    public <M extends HttpMethod> M generateSpecificMethod(String uri, 
30
	    String localPath) {
31

  
32
	PostMethod method = new PostMethod(uri);
33
	if (localPath != null) {
34
	    File file = new File(localPath);
35
	    method.setRequestEntity(new FileRequestEntity(file, ""));
36
	}
37
	// As we know that PutMethod is indeed a subclass of HttpMethod,
38
	// we can safely suppress the cast-related warning.
39
	@SuppressWarnings("unchecked")
40
	M returnVal = (M) method;
41
	return returnVal;
42
    }
43

  
44
}

Also available in: Unified diff