root / web_client / src / gr / grnet / pithos / web / client / rest / RestCommand.java @ 58777026
History | View | Annotate | Download (4.9 kB)
1 |
/*
|
---|---|
2 |
* Copyright 2011 GRNET S.A. All rights reserved.
|
3 |
*
|
4 |
* Redistribution and use in source and binary forms, with or
|
5 |
* without modification, are permitted provided that the following
|
6 |
* conditions are met:
|
7 |
*
|
8 |
* 1. Redistributions of source code must retain the above
|
9 |
* copyright notice, this list of conditions and the following
|
10 |
* disclaimer.
|
11 |
*
|
12 |
* 2. Redistributions in binary form must reproduce the above
|
13 |
* copyright notice, this list of conditions and the following
|
14 |
* disclaimer in the documentation and/or other materials
|
15 |
* provided with the distribution.
|
16 |
*
|
17 |
* THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
|
18 |
* OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
|
19 |
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
|
20 |
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
|
21 |
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
22 |
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
23 |
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
|
24 |
* USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
25 |
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
|
26 |
* LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
|
27 |
* ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
|
28 |
* POSSIBILITY OF SUCH DAMAGE.
|
29 |
*
|
30 |
* The views and conclusions contained in the software and
|
31 |
* documentation are those of the authors and should not be
|
32 |
* interpreted as representing official policies, either expressed
|
33 |
* or implied, of GRNET S.A.
|
34 |
*/
|
35 |
package gr.grnet.pithos.web.client.rest; |
36 |
|
37 |
import gr.grnet.pithos.web.client.GSS; |
38 |
import gr.grnet.pithos.web.client.SessionExpiredDialog; |
39 |
|
40 |
import com.google.gwt.http.client.RequestBuilder; |
41 |
import com.google.gwt.user.client.IncrementalCommand; |
42 |
|
43 |
public abstract class RestCommand implements IncrementalCommand { |
44 |
protected boolean showLoadingIndicator = true; |
45 |
|
46 |
protected void handleHeaders(String username, RequestBuilder requestBuilder, String path) { |
47 |
String date = getDate();
|
48 |
requestBuilder.setHeader("X-GSS-Date", date);
|
49 |
|
50 |
GSS app = GSS.get(); |
51 |
String token = app.getToken();
|
52 |
if (token == null) |
53 |
token = "aa";
|
54 |
String resource = path.substring(app.getApiPath().length()-1,path.length()); |
55 |
String sig = calculateSig(requestBuilder.getHTTPMethod(), date, resource, base64decode(token));
|
56 |
requestBuilder.setHeader("Authorization", username + " " + sig); |
57 |
requestBuilder.setHeader("Accept", "application/json; charset=utf-8"); |
58 |
} |
59 |
|
60 |
protected void handleHeaders(RequestBuilder requestBuilder, String path) { |
61 |
if (GSS.get().getCurrentUserResource() != null) { |
62 |
String username = GSS.get().getCurrentUserResource().getUsername();
|
63 |
handleHeaders(username, requestBuilder, path); |
64 |
} else
|
65 |
GSS.get().displayError("no username");
|
66 |
} |
67 |
|
68 |
public static native String getDate()/*-{ |
69 |
return (new Date()).toUTCString();
|
70 |
}-*/;
|
71 |
|
72 |
public static native String getDate(Long ms)/*-{ |
73 |
return (new Date(ms)).toUTCString();
|
74 |
}-*/;
|
75 |
|
76 |
public static native String calculateSig(String method, String date, String resource, String token)/*-{ |
77 |
$wnd.b64pad = "=";
|
78 |
var q = resource.indexOf('?');
|
79 |
var res = q == -1? resource: resource.substring(0, q);
|
80 |
var data = method + date + res;
|
81 |
var sig = $wnd.b64_hmac_sha1(token, data);
|
82 |
return sig;
|
83 |
}-*/;
|
84 |
|
85 |
public static native String base64decode(String encStr)/*-{ |
86 |
if (typeof atob === 'function') {
|
87 |
return atob(encStr);
|
88 |
}
|
89 |
var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
|
90 |
var bits;
|
91 |
var decOut = "";
|
92 |
var i = 0;
|
93 |
for(; i<encStr.length; i += 4){
|
94 |
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;
|
95 |
decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
|
96 |
}
|
97 |
if(encStr.charCodeAt(i -2) == 61){
|
98 |
return(decOut.substring(0, decOut.length -2));
|
99 |
}
|
100 |
else if(encStr.charCodeAt(i -1) == 61){
|
101 |
return(decOut.substring(0, decOut.length -1));
|
102 |
}
|
103 |
else {
|
104 |
return(decOut);
|
105 |
}
|
106 |
}-*/;
|
107 |
|
108 |
public void onComplete() {} |
109 |
|
110 |
public abstract void onError(Throwable t); |
111 |
|
112 |
public String fixPath(String pathToFix) { |
113 |
if(pathToFix.endsWith("/")) |
114 |
return pathToFix;
|
115 |
return pathToFix+"/"; |
116 |
} |
117 |
|
118 |
/**
|
119 |
* Retrieve the showLoadingIndicator.
|
120 |
*
|
121 |
* @return the showLoadingIndicator
|
122 |
*/
|
123 |
public boolean isShowLoadingIndicator() { |
124 |
return showLoadingIndicator;
|
125 |
} |
126 |
|
127 |
/**
|
128 |
* Modify the showLoadingIndicator.
|
129 |
*
|
130 |
* @param newShowLoadingIndicator the showLoadingIndicator to set
|
131 |
*/
|
132 |
public void setShowLoadingIndicator(boolean newShowLoadingIndicator) { |
133 |
showLoadingIndicator = newShowLoadingIndicator; |
134 |
} |
135 |
|
136 |
static void sessionExpired() { |
137 |
SessionExpiredDialog dlg = new SessionExpiredDialog();
|
138 |
dlg.center(); |
139 |
} |
140 |
|
141 |
} |