2 * Copyright 2009 Electronic Business Systems Ltd.
4 * This file is part of GSS.
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.
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.
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/>.
19 package gr.ebs.gss.client.rest;
21 import gr.ebs.gss.client.GSS;
22 import gr.ebs.gss.client.SessionExpiredDialog;
24 import com.google.gwt.http.client.RequestBuilder;
25 import com.google.gwt.user.client.IncrementalCommand;
30 public abstract class RestCommand implements IncrementalCommand {
31 protected boolean showLoadingIndicator = true;
33 protected void handleHeaders(String username, RequestBuilder requestBuilder, String path) {
34 String date = getDate();
35 requestBuilder.setHeader("X-GSS-Date", date);
38 String token = app.getToken();
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");
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);
52 GSS.get().displayError("no username");
55 public static native String getDate()/*-{
56 return (new Date()).toUTCString();
59 public static native String getDate(Long ms)/*-{
60 return (new Date(ms)).toUTCString();
63 public static native String calculateSig(String method, String date, String resource, String token)/*-{
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);
72 public static native String base64decode(String encStr)/*-{
73 if (typeof atob === 'function') {
76 var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
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);
84 if(encStr.charCodeAt(i -2) == 61){
85 return(decOut.substring(0, decOut.length -2));
87 else if(encStr.charCodeAt(i -1) == 61){
88 return(decOut.substring(0, decOut.length -1));
95 public void onComplete() {}
97 public abstract void onError(Throwable t);
99 public String fixPath(String pathToFix) {
100 if(pathToFix.endsWith("/"))
102 return pathToFix+"/";
106 * Retrieve the showLoadingIndicator.
108 * @return the showLoadingIndicator
110 public boolean isShowLoadingIndicator() {
111 return showLoadingIndicator;
115 * Modify the showLoadingIndicator.
117 * @param newShowLoadingIndicator the showLoadingIndicator to set
119 public void setShowLoadingIndicator(boolean newShowLoadingIndicator) {
120 showLoadingIndicator = newShowLoadingIndicator;
123 static void sessionExpired() {
124 SessionExpiredDialog dlg = new SessionExpiredDialog();