Restore check for atob, since it works fine in every combination tried: Windows/Linux...
[pithos] / src / gr / ebs / gss / client / rest / RestCommand.java
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 calculateSig(String method, String date, String resource, String token)/*-{
60                 $wnd.b64pad = "=";
61                 var q = resource.indexOf('?');
62                 var res = q == -1? resource: resource.substring(0, q);
63                 var data = method + date + res;
64                 var sig = $wnd.b64_hmac_sha1(token, data);
65                 return sig;
66         }-*/;
67
68         public static native String base64decode(String encStr)/*-{
69         if (typeof atob === 'function') {
70             return atob(encStr);
71         }
72         var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
73         var bits;
74         var decOut = "";
75         var i = 0;
76         for(; i<encStr.length; i += 4){
77             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;
78             decOut += String.fromCharCode((bits & 0xff0000) >>16, (bits & 0xff00) >>8, bits & 0xff);
79         }
80         if(encStr.charCodeAt(i -2) == 61){
81             return(decOut.substring(0, decOut.length -2));
82         }
83         else if(encStr.charCodeAt(i -1) == 61){
84             return(decOut.substring(0, decOut.length -1));
85         }
86         else {
87             return(decOut);
88         }
89         }-*/;
90
91         /**
92          * An overridden RequestBuilder that allows us to use DELETE, HEAD and PUT methods.
93          */
94         protected class RestRequestBuilder extends RequestBuilder {
95                 public RestRequestBuilder(String httpMethod, String url) {
96                         super(httpMethod, url);
97                 }
98         }
99
100         public void onComplete(){}
101
102         public abstract void onError(Throwable t);
103
104         public String fixPath(String pathToFix){
105                 if(pathToFix.endsWith("/"))
106                         return pathToFix;
107                 return pathToFix+"/";
108         }
109
110         /**
111          * Retrieve the showLoadingIndicator.
112          *
113          * @return the showLoadingIndicator
114          */
115         public boolean isShowLoadingIndicator() {
116                 return showLoadingIndicator;
117         }
118
119         /**
120          * Modify the showLoadingIndicator.
121          *
122          * @param newShowLoadingIndicator the showLoadingIndicator to set
123          */
124         public void setShowLoadingIndicator(boolean newShowLoadingIndicator) {
125                 showLoadingIndicator = newShowLoadingIndicator;
126         }
127
128         static void sessionExpired(){
129                 SessionExpiredDialog dlg = new SessionExpiredDialog();
130                 dlg.center();
131         }
132
133 }