root / web_client / src / gr / grnet / pithos / web / client / rest / RestCommand.java @ fbff60ff
History | View | Annotate | Download (3.4 kB)
1 |
/*
|
---|---|
2 |
* Copyright (c) 2011 Greek Research and Technology Network
|
3 |
*/
|
4 |
package gr.grnet.pithos.web.client.rest; |
5 |
|
6 |
import gr.grnet.pithos.web.client.GSS; |
7 |
import gr.grnet.pithos.web.client.SessionExpiredDialog; |
8 |
|
9 |
import com.google.gwt.http.client.RequestBuilder; |
10 |
import com.google.gwt.user.client.IncrementalCommand; |
11 |
|
12 |
public abstract class RestCommand implements IncrementalCommand { |
13 |
protected boolean showLoadingIndicator = true; |
14 |
|
15 |
protected void handleHeaders(String username, RequestBuilder requestBuilder, String path) { |
16 |
String date = getDate();
|
17 |
requestBuilder.setHeader("X-GSS-Date", date);
|
18 |
|
19 |
GSS app = GSS.get(); |
20 |
String token = app.getToken();
|
21 |
if (token == null) |
22 |
token = "aa";
|
23 |
String resource = path.substring(app.getApiPath().length()-1,path.length()); |
24 |
String sig = calculateSig(requestBuilder.getHTTPMethod(), date, resource, base64decode(token));
|
25 |
requestBuilder.setHeader("Authorization", username + " " + sig); |
26 |
requestBuilder.setHeader("Accept", "application/json; charset=utf-8"); |
27 |
} |
28 |
|
29 |
protected void handleHeaders(RequestBuilder requestBuilder, String path) { |
30 |
if (GSS.get().getCurrentUserResource() != null) { |
31 |
String username = GSS.get().getCurrentUserResource().getUsername();
|
32 |
handleHeaders(username, requestBuilder, path); |
33 |
} else
|
34 |
GSS.get().displayError("no username");
|
35 |
} |
36 |
|
37 |
public static native String getDate()/*-{ |
38 |
return (new Date()).toUTCString();
|
39 |
}-*/;
|
40 |
|
41 |
public static native String getDate(Long ms)/*-{ |
42 |
return (new Date(ms)).toUTCString();
|
43 |
}-*/;
|
44 |
|
45 |
public static native String calculateSig(String method, String date, String resource, String token)/*-{ |
46 |
$wnd.b64pad = "=";
|
47 |
var q = resource.indexOf('?');
|
48 |
var res = q == -1? resource: resource.substring(0, q);
|
49 |
var data = method + date + res;
|
50 |
var sig = $wnd.b64_hmac_sha1(token, data);
|
51 |
return sig;
|
52 |
}-*/;
|
53 |
|
54 |
public static native String base64decode(String encStr)/*-{ |
55 |
if (typeof atob === 'function') {
|
56 |
return atob(encStr);
|
57 |
}
|
58 |
var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
59 |
var bits;
|
60 |
var decOut = "";
|
61 |
var i = 0;
|
62 |
for(; i<encStr.length; i += 4){
|
63 |
bits = (base64s.indexOf(encStr.charAt(i)) & 0xff) <<18 | (base64s.indexOf(encStr.charAt(i +1)) & 0xff) <<12 | (base64s.indexOf(encStr.charAt(i +2)) & 0xff) << 6 | base64s.indexOf(encStr.charAt(i +3)) & 0xff;
|
64 |
decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
|
65 |
}
|
66 |
if(encStr.charCodeAt(i -2) == 61){
|
67 |
return(decOut.substring(0, decOut.length -2));
|
68 |
}
|
69 |
else if(encStr.charCodeAt(i -1) == 61){
|
70 |
return(decOut.substring(0, decOut.length -1));
|
71 |
}
|
72 |
else {
|
73 |
return(decOut);
|
74 |
}
|
75 |
}-*/;
|
76 |
|
77 |
public void onComplete() {} |
78 |
|
79 |
public abstract void onError(Throwable t); |
80 |
|
81 |
public String fixPath(String pathToFix) { |
82 |
if(pathToFix.endsWith("/")) |
83 |
return pathToFix;
|
84 |
return pathToFix+"/"; |
85 |
} |
86 |
|
87 |
/**
|
88 |
* Retrieve the showLoadingIndicator.
|
89 |
*
|
90 |
* @return the showLoadingIndicator
|
91 |
*/
|
92 |
public boolean isShowLoadingIndicator() { |
93 |
return showLoadingIndicator;
|
94 |
} |
95 |
|
96 |
/**
|
97 |
* Modify the showLoadingIndicator.
|
98 |
*
|
99 |
* @param newShowLoadingIndicator the showLoadingIndicator to set
|
100 |
*/
|
101 |
public void setShowLoadingIndicator(boolean newShowLoadingIndicator) { |
102 |
showLoadingIndicator = newShowLoadingIndicator; |
103 |
} |
104 |
|
105 |
static void sessionExpired() { |
106 |
SessionExpiredDialog dlg = new SessionExpiredDialog();
|
107 |
dlg.center(); |
108 |
} |
109 |
|
110 |
} |