Statistics
| Branch: | Revision:

root / java / gssclient / src / gssclient / HttpMethodGenerator.java @ 18:7a2e7c4355bf

History | View | Annotate | Download (3.8 kB)

1
package gssclient;
2

    
3
import java.io.File;
4
import java.io.UnsupportedEncodingException;
5
import java.security.InvalidKeyException;
6
import java.security.NoSuchAlgorithmException;
7
import java.text.SimpleDateFormat;
8
import java.util.Date;
9
import java.util.Locale;
10
import java.util.TimeZone;
11

    
12
import javax.crypto.Mac;
13
import javax.crypto.spec.SecretKeySpec;
14

    
15
import org.apache.commons.codec.binary.Base64;
16
import org.apache.commons.httpclient.HttpMethod;
17
import org.apache.commons.httpclient.methods.FileRequestEntity;
18
import org.apache.commons.httpclient.methods.GetMethod;
19
import org.apache.commons.httpclient.methods.PutMethod;
20

    
21
public class HttpMethodGenerator {
22

    
23
    public static final String HOST = "http://pithos.grnet.gr";
24
    public static final String REST_PREFIX = "/pithos/rest";
25
    public static final String PATH_PREFIX = HOST + REST_PREFIX;
26
    private static final String HMAC_SHA1 = "HmacSHA1";
27
    private static final String AUTHORIZATION_HEADER = "Authorization";
28
    private static final String DATE_HEADER = "X-GSS-Date";
29
    private static final String DATE_FORMAT = "EEE, d MMM yyyy HH:mm:ss z";
30
    private static final SimpleDateFormat DATE_FORMATER = new SimpleDateFormat(
31
            DATE_FORMAT, Locale.US);
32

    
33
    static {
34
        DATE_FORMATER.setTimeZone(TimeZone.getTimeZone("GMT"));
35
    }
36

    
37
    protected static String sign(String httpMethod, String timestamp, String path,
38
            String token) {
39

    
40
        String input = httpMethod + timestamp + path;
41
        String signed = null;
42

    
43
        try {
44
            System.err.println("Token:" + token);
45
            // Get an HMAC-SHA1 key from the authentication token.
46
            System.err.println("Input: " + input);
47
            byte[] unencodedToken = Base64.decodeBase64(token.getBytes());
48
            SecretKeySpec signingKey = new SecretKeySpec(unencodedToken,
49
                    HMAC_SHA1);
50

    
51
            // Get an HMAC-SHA1 Mac instance and initialize with the signing
52
            // key.
53
            Mac hmac = Mac.getInstance(HMAC_SHA1);
54
            hmac.init(signingKey);
55

    
56
            // Compute the HMAC on the input data bytes.
57
            byte[] rawMac = hmac.doFinal(input.getBytes());
58

    
59
            // Do base 64 encoding.
60
            signed = new String(Base64.encodeBase64(rawMac), "US-ASCII");
61

    
62
        } catch (InvalidKeyException ikex) {
63
            System.err.println("Fatal key exception: " + ikex.getMessage());
64
            ikex.printStackTrace();
65
        } catch (UnsupportedEncodingException ueex) {
66
            System.err
67
                    .println("Fatal encoding exception: " + ueex.getMessage());
68
        } catch (NoSuchAlgorithmException nsaex) {
69
            System.err.println("Fatal algorithm exception: "
70
                    + nsaex.getMessage());
71
            nsaex.printStackTrace();
72
        }
73

    
74
        if (signed == null) {
75
            System.exit(-1);
76
        }
77
        System.err.println("Signed: " + signed);
78
        return signed;
79
    }
80

    
81
    protected static void fillRequestHeaders(HttpMethod request, Date timestamp,
82
                    UserInfo userInfo) {
83
        
84
        String path = request.getPath().substring(REST_PREFIX.length());
85
        String strTimestamp = DATE_FORMATER.format(timestamp);
86
        String signature = sign(request.getName(), strTimestamp, path, userInfo
87
                .getToken());
88
        request.addRequestHeader(AUTHORIZATION_HEADER, userInfo.getUsername()
89
                + " " + signature);
90
        request.addRequestHeader(DATE_HEADER, strTimestamp);
91
    }
92
  
93
    public static GetMethod generateGetMethod(String uri, Date timestamp, 
94
            UserInfo userInfo) {
95

    
96
        GetMethod request = new GetMethod(uri);
97
        fillRequestHeaders(request, timestamp, userInfo);
98
        return request;
99
    }
100
    
101
    public static PutMethod generatePutMethod(String uri, Date timestamp, 
102
            UserInfo userInfo, String filename, String path) {
103

    
104
        File file = new File(filename);
105
        PutMethod request = new PutMethod(uri + "files/" + path + file.getName());
106
        fillRequestHeaders(request, timestamp, userInfo);
107
        request.setRequestEntity(new FileRequestEntity(file, ""));
108
        return request;
109
    }
110

    
111
}