Remove the redundant gss top-level directory.
[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                 requestBuilder.setHeader("Accept-Charset", "utf-8");
46         }
47
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);
52                 } else
53                         GSS.get().displayError("no username");
54         }
55
56         public static native String getDate()/*-{
57                 return (new Date()).toUTCString();
58         }-*/;
59
60         public static native String calculateSig(String method, String date, String resource, String token)/*-{
61                 $wnd.b64pad = "=";
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);
66                 return sig;
67         }-*/;
68
69         public static native String base64decode(String encStr)/*-{
70                 if (typeof atob === 'function') {
71            return atob(encStr);
72         }
73         var base64s = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
74         var bits;
75         var decOut = "";
76         var i = 0;
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);
80         }
81         if(encStr.charCodeAt(i -2) == 61){
82             return(decOut.substring(0, decOut.length -2));
83         }
84         else if(encStr.charCodeAt(i -1) == 61){
85             return(decOut.substring(0, decOut.length -1));
86         }
87         else {
88             return(decOut);
89         }
90         }-*/;
91
92         /**
93          * An overridden RequestBuilder that allows us to use DELETE, HEAD and PUT methods.
94          */
95         protected class RestRequestBuilder extends RequestBuilder {
96                 public RestRequestBuilder(String httpMethod, String url) {
97                         super(httpMethod, url);
98                 }
99         }
100
101         public void onComplete(){}
102
103         public abstract void onError(Throwable t);
104
105         public String fixPath(String pathToFix){
106                 if(pathToFix.endsWith("/"))
107                         return pathToFix;
108                 return pathToFix+"/";
109         }
110
111         /**
112          * Retrieve the showLoadingIndicator.
113          *
114          * @return the showLoadingIndicator
115          */
116         public boolean isShowLoadingIndicator() {
117                 return showLoadingIndicator;
118         }
119
120         /**
121          * Modify the showLoadingIndicator.
122          *
123          * @param newShowLoadingIndicator the showLoadingIndicator to set
124          */
125         public void setShowLoadingIndicator(boolean newShowLoadingIndicator) {
126                 showLoadingIndicator = newShowLoadingIndicator;
127         }
128
129         static void sessionExpired(){
130                 SessionExpiredDialog dlg = new SessionExpiredDialog();
131                 dlg.center();
132         }
133
134 }