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");
45 requestBuilder.setHeader("Accept-Charset", "utf-8");
48 protected void handleHeaders(RequestBuilder requestBuilder, String path) {
49 if (GSS.get().getCurrentUserResource() != null) {
50 String username = GSS.get().getCurrentUserResource().getUsername();
51 handleHeaders(username, requestBuilder, path);
53 GSS.get().displayError("no username");
56 public static native String getDate()/*-{
57 return (new Date()).toUTCString();
60 public static native String calculateSig(String method, String date, String resource, String token)/*-{
62 var q = resource.indexOf('?');
63 var res = q == -1? resource: resource.substring(0, q);
64 var data = method + date + res;
65 var sig = $wnd.b64_hmac_sha1(token, data);
69 public static native String base64decode(String encStr)/*-{
70 if (typeof atob === 'function') {
73 var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
77 for(; i<encStr.length; i += 4){
78 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;
79 decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
81 if(encStr.charCodeAt(i -2) == 61){
82 return(decOut.substring(0, decOut.length -2));
84 else if(encStr.charCodeAt(i -1) == 61){
85 return(decOut.substring(0, decOut.length -1));
93 * An overridden RequestBuilder that allows us to use DELETE, HEAD and PUT methods.
95 protected class RestRequestBuilder extends RequestBuilder {
96 public RestRequestBuilder(String httpMethod, String url) {
97 super(httpMethod, url);
101 public void onComplete(){}
103 public abstract void onError(Throwable t);
105 public String fixPath(String pathToFix){
106 if(pathToFix.endsWith("/"))
108 return pathToFix+"/";
112 * Retrieve the showLoadingIndicator.
114 * @return the showLoadingIndicator
116 public boolean isShowLoadingIndicator() {
117 return showLoadingIndicator;
121 * Modify the showLoadingIndicator.
123 * @param newShowLoadingIndicator the showLoadingIndicator to set
125 public void setShowLoadingIndicator(boolean newShowLoadingIndicator) {
126 showLoadingIndicator = newShowLoadingIndicator;
129 static void sessionExpired(){
130 SessionExpiredDialog dlg = new SessionExpiredDialog();