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