Statistics
| Branch: | Tag: | Revision:

root / test / org / gss_project / gss / web / client / TestClient.java @ 83c3bc8e

History | View | Annotate | Download (4.4 kB)

1
/*
2
 * Copyright 2009 Electronic Business Systems Ltd.
3
 *
4
 * This file is part of GSS.
5
 *
6
 * GSS is free software: you can redistribute it and/or modify
7
 * it under the terms of the GNU General Public License as published by
8
 * the Free Software Foundation, either version 3 of the License, or
9
 * (at your option) any later version.
10
 *
11
 * GSS is distributed in the hope that it will be useful,
12
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
 * GNU General Public License for more details.
15
 *
16
 * You should have received a copy of the GNU General Public License
17
 * along with GSS.  If not, see <http://www.gnu.org/licenses/>.
18
 */
19
package org.gss_project.gss.web.client;
20

    
21
import java.io.IOException;
22
import java.io.UnsupportedEncodingException;
23
import java.security.InvalidKeyException;
24
import java.security.NoSuchAlgorithmException;
25
import java.util.ArrayList;
26
import java.util.Collection;
27
import java.util.Date;
28

    
29
import javax.crypto.Mac;
30
import javax.crypto.spec.SecretKeySpec;
31

    
32
import org.apache.commons.codec.binary.Base64;
33
import org.apache.commons.httpclient.Header;
34
import org.apache.commons.httpclient.HostConfiguration;
35
import org.apache.commons.httpclient.HttpClient;
36
import org.apache.commons.httpclient.HttpException;
37
import org.apache.commons.httpclient.HttpMethod;
38
import org.apache.commons.httpclient.HttpStatus;
39
import org.apache.commons.httpclient.methods.GetMethod;
40
import org.apache.commons.httpclient.util.DateUtil;
41

    
42

    
43
/**
44
 * @author past
45
 *
46
 */
47
public class TestClient {
48

    
49
        public static void main(String[] args) {
50
                String user = "ebstest@grnet-hq.admin.grnet.gr";
51
                String token="PcxaZ/4oIqCqIvCYgsUcKr1hAFcsW40G3kcWJSRPJV5GjzoNuo8RsA==";
52
                String host = "pithos.grnet.gr";
53
                String restPath = "/pithos/rest";
54
                String path = "/" + user + "/files/";
55
                String now = DateUtil.formatDate(new Date());
56
                String signature = sign("GET", now, path, token);
57
                HttpClient client = new HttpClient();
58
                HostConfiguration hostconfig = new HostConfiguration();
59
                hostconfig.setHost(host);
60
                HttpMethod method = new GetMethod(restPath + path);
61
                Collection<Header> headers = new ArrayList<Header>();
62
                Header auth = new Header("Authorization", user + " " + signature);
63
                headers.add(auth);
64
                Header date = new Header("X-GSS-Date", now);
65
                headers.add(date);
66
                System.out.println(headers.toString());
67
                hostconfig.getParams().setParameter("http.default-headers", headers);
68
                try {
69
                        // Execute the method.
70
                        int statusCode = client.executeMethod(hostconfig, method);
71

    
72
                        if (statusCode != HttpStatus.SC_OK)
73
                                System.err.println("Method failed: " + method.getStatusLine());
74

    
75
                        // Read the response body.
76
                        byte[] responseBody = method.getResponseBody();
77

    
78
                        // Deal with the response.
79
                        // Use caution: ensure correct character encoding and is not binary data
80
                        System.out.println(new String(responseBody));
81
                } catch (HttpException e) {
82
                        System.err.println("Fatal protocol violation: " + e.getMessage());
83
                        e.printStackTrace();
84
                } catch (IOException e) {
85
                        System.err.println("Fatal transport error: " + e.getMessage());
86
                        e.printStackTrace();
87
                } finally {
88
                        // Release the connection.
89
                        method.releaseConnection();
90
                }
91
        }
92

    
93
        public static String sign(String httpMethod, String timestamp,
94
                                String path, String token) {
95
                String input = httpMethod + timestamp + path;
96
                String signed = null;
97

    
98
                try {
99
                        System.err.println("Token:" + token);
100
                        // Get an HMAC-SHA1 key from the authentication token.
101
                        System.err.println("Input: " + input);
102
                        SecretKeySpec signingKey = new SecretKeySpec(Base64.decodeBase64(token.getBytes()), "HmacSHA1");
103

    
104
                        // Get an HMAC-SHA1 Mac instance and initialize with the signing key.
105
                        Mac hmac = Mac.getInstance("HmacSHA1");
106
                        hmac.init(signingKey);
107

    
108
                        // Compute the HMAC on the input data bytes.
109
                        byte[] rawMac = hmac.doFinal(input.getBytes());
110

    
111
                        // Do base 64 encoding.
112
                        signed = new String(Base64.encodeBase64(rawMac), "US-ASCII");
113

    
114
                } catch (InvalidKeyException ikex) {
115
                        System.err.println("Fatal key exception: " + ikex.getMessage());
116
                        ikex.printStackTrace();
117
                } catch (UnsupportedEncodingException ueex) {
118
                        System.err.println("Fatal encoding exception: "
119
                                                + ueex.getMessage());
120
                } catch (NoSuchAlgorithmException nsaex) {
121
                        System.err.println("Fatal algorithm exception: "
122
                                                + nsaex.getMessage());
123
                        nsaex.printStackTrace();
124
                }
125

    
126
                if (signed == null)
127
                        System.exit(-1);
128
                System.err.println("Signed: " + signed);
129
                return signed;
130
        }
131
}