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