Hardcoded pithos site url
[pithos] / web_client / src / gr / grnet / pithos / web / client / CredentialsDialog.java
1 /*
2  *  Copyright (c) 2011 Greek Research and Technology Network
3  */
4 package gr.grnet.pithos.web.client;
5
6 import com.google.gwt.user.client.Window;
7 import gr.grnet.pithos.web.client.rest.PostCommand;
8 import gr.grnet.pithos.web.client.rest.RestException;
9
10 import com.google.gwt.core.client.GWT;
11 import com.google.gwt.dom.client.NativeEvent;
12 import com.google.gwt.event.dom.client.ClickEvent;
13 import com.google.gwt.event.dom.client.ClickHandler;
14 import com.google.gwt.event.dom.client.KeyCodes;
15 import com.google.gwt.user.client.DeferredCommand;
16 import com.google.gwt.user.client.Event.NativePreviewEvent;
17 import com.google.gwt.user.client.ui.AbstractImagePrototype;
18 import com.google.gwt.user.client.ui.Button;
19 import com.google.gwt.user.client.ui.DialogBox;
20 import com.google.gwt.user.client.ui.FlexTable;
21 import com.google.gwt.user.client.ui.HTML;
22 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
23 import com.google.gwt.user.client.ui.HorizontalPanel;
24 import com.google.gwt.user.client.ui.TextBox;
25 import com.google.gwt.user.client.ui.VerticalPanel;
26
27
28 /**
29  * A dialog box that displays the user credentials for use in other client
30  * applications, such as WebDAV clients.
31  */
32 public class CredentialsDialog extends DialogBox {
33
34         private final String WIDTH_FIELD = "35em";
35         private final String WIDTH_TEXT = "42em";
36
37         private TextBox passwordBox;
38
39         /**
40          * The 'confirm reset password' dialog box.
41          */
42         private class ConfirmResetPasswordDialog extends DialogBox {
43
44                 /**
45                  * The widget's constructor.
46                  *
47                  * @param images the supplied images
48                  */
49                 private ConfirmResetPasswordDialog(MessagePanel.Images images) {
50                         // Set the dialog's caption.
51                         setText("Confirmation");
52                         setAnimationEnabled(true);
53                         // Create a VerticalPanel to contain the label and the buttons.
54                         VerticalPanel outer = new VerticalPanel();
55                         HorizontalPanel buttons = new HorizontalPanel();
56
57                         HTML text;
58                         text = new HTML("<table><tr><td>" +
59                                         AbstractImagePrototype.create(images.warn()).getHTML() +
60                                         "</td><td>" + "Are you sure you want to create a new " +
61                                         "WebDAV password?</td></tr></table>");
62                         text.setStyleName("pithos-warnMessage");
63                         outer.add(text);
64
65                         // Create the 'Yes' button, along with a listener that hides the
66                         // dialog when the button is clicked and resets the password.
67                         Button ok = new Button("Yes", new ClickHandler() {
68                                 @Override
69                                 public void onClick(ClickEvent event) {
70                                         resetPassword(GSS.get().getCurrentUserResource().getUri());
71                                         hide();
72                                 }
73                         });
74                         buttons.add(ok);
75                         buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
76                         // Create the 'No' button, along with a listener that hides the
77                         // dialog when the button is clicked.
78                         Button cancel = new Button("No", new ClickHandler() {
79                                 @Override
80                                 public void onClick(ClickEvent event) {
81                                         hide();
82                                 }
83                         });
84                         buttons.add(cancel);
85                         buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
86                         buttons.setSpacing(8);
87                         buttons.setStyleName("pithos-warnMessage");
88                         outer.setStyleName("pithos-warnMessage");
89                         outer.add(buttons);
90                         outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
91                         outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
92                         setWidget(outer);
93                 }
94
95                 @Override
96                 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
97                         super.onPreviewNativeEvent(preview);
98                         NativeEvent evt = preview.getNativeEvent();
99                         if (evt.getType().equals("keydown"))
100                                 // Use the popup's key preview hooks to close the dialog when either
101                                 // enter or escape is pressed.
102                                 switch (evt.getKeyCode()) {
103                                         case KeyCodes.KEY_ENTER:
104                                         case KeyCodes.KEY_ESCAPE:
105                                                 hide();
106                                                 break;
107                                 }
108                 }
109
110         }
111
112         private class ReauthenticateDialog extends DialogBox {
113                 /**
114                  * The widget constructor.
115                  */
116                 public ReauthenticateDialog() {
117                         // Set the dialog's caption.
118                         setText("New Password Created");
119                         setAnimationEnabled(true);
120                         VerticalPanel outer = new VerticalPanel();
121
122                         // Create the text and set a style name so we can style it with CSS.
123                         HTML text = new HTML("<p>A new WebDAV password has been created." +
124                                         "</p><p>You will now be redirected to the initial screen" +
125                                         " for the changes to take effect. Choose \"Show " +
126                                         "Credentials\" again afterwards to see the new password.</p>");
127                         text.setStyleName("pithos-AboutText");
128                         outer.add(text);
129
130                         // Create the 'OK' button, along with a listener that hides the
131                         // dialog when the button is clicked.
132                         Button confirm = new Button("Proceed", new ClickHandler() {
133                                 @Override
134                                 public void onClick(ClickEvent event) {
135                                         GSS.get().authenticateUser();
136                                         hide();
137                                 }
138                         });
139                         outer.add(confirm);
140                         outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
141                         outer.setSpacing(8);
142                         setWidget(outer);
143                 }
144
145                 @Override
146                 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
147                         super.onPreviewNativeEvent(preview);
148                         NativeEvent evt = preview.getNativeEvent();
149                         if (evt.getType().equals("keydown"))
150                                 // Use the popup's key preview hooks to close the dialog when
151                                 // either enter or escape is pressed.
152                                 switch (evt.getKeyCode()) {
153                                         case KeyCodes.KEY_ENTER:
154                                                 GSS.get().authenticateUser();
155                                                 hide();
156                                                 break;
157                                         case KeyCodes.KEY_ESCAPE:
158                                                 hide();
159                                                 break;
160                                 }
161                 }
162
163         }
164
165         /**
166          * The widget constructor.
167          */
168         public CredentialsDialog(final MessagePanel.Images images) {
169                 // Set the dialog's caption.
170                 setText("User Credentials");
171                 setAnimationEnabled(true);
172                 // A VerticalPanel that contains the 'about' label and the 'OK' button.
173                 VerticalPanel outer = new VerticalPanel();
174                 Configuration conf = (Configuration) GWT.create(Configuration.class);
175                 String service = conf.serviceName();
176         String path = Window.Location.getPath();
177         String baseUrl = GWT.getModuleBaseURL();
178         String homeUrl = baseUrl.substring(0, baseUrl.indexOf(path));
179                 String webdavUrl = homeUrl + conf.webdavUrl();
180                 String tokenNote = conf.tokenTTLNote();
181                 // Create the text and set a style name so we can style it with CSS.
182                 HTML text = new HTML("<p>These are the user credentials that are " +
183                                 "required for interacting with " + service + ". You can copy" +
184                                 " and paste the username and password in the WebDAV client " +
185                                 "in order to use " + service + " through the WebDAV " +
186                                 "interface, at:<br/> " + webdavUrl + "<br/>" + tokenNote +
187                                 "</p>");
188                 text.setStyleName("pithos-AboutText");
189                 text.setWidth(WIDTH_TEXT);
190                 outer.add(text);
191                 FlexTable table = new FlexTable();
192                 table.setText(0, 0, "Username");
193                 table.setText(1, 0, "Password");
194                 table.setText(2, 0, "Token");
195                 TextBox username = new TextBox();
196                 final GSS app = GSS.get();
197                 username.setText(app.getCurrentUserResource().getUsername());
198                 username.setReadOnly(true);
199                 username.setWidth(WIDTH_FIELD);
200                 username.addClickHandler(new ClickHandler() {
201                         @Override
202                         public void onClick(ClickEvent event) {
203                                 GSS.enableIESelection();
204                                 ((TextBox) event.getSource()).selectAll();
205                                 GSS.preventIESelection();
206                         }
207
208                 });
209                 table.setWidget(0, 1, username);
210                 passwordBox = new TextBox();
211                 passwordBox.setText(app.getWebDAVPassword());
212                 passwordBox.setReadOnly(true);
213                 passwordBox.setWidth(WIDTH_FIELD);
214                 passwordBox.addClickHandler(new  ClickHandler() {
215                         @Override
216                         public void onClick(ClickEvent event) {
217                                 GSS.enableIESelection();
218                                 ((TextBox) event.getSource()).selectAll();
219                                 GSS.preventIESelection();
220                         }
221
222                 });
223                 table.setWidget(1, 1, passwordBox);
224
225                 TextBox tokenBox = new TextBox();
226                 tokenBox.setText(app.getToken());
227                 tokenBox.setReadOnly(true);
228                 tokenBox.setWidth(WIDTH_FIELD);
229                 tokenBox.addClickHandler(new ClickHandler() {
230                         @Override
231                         public void onClick(ClickEvent event) {
232                                 GSS.enableIESelection();
233                                 ((TextBox) event.getSource()).selectAll();
234                                 GSS.preventIESelection();
235                         }
236
237                 });
238                 table.setWidget(2, 1, tokenBox);
239
240                 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
241                 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
242                 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
243                 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
244                 table.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
245                 table.getFlexCellFormatter().setStyleName(2, 1, "props-values");
246                 outer.add(table);
247
248                 // Create the 'OK' button, along with a listener that hides the dialog
249                 // when the button is clicked.
250                 Button confirm = new Button("Close", new ClickHandler() {
251                         @Override
252                         public void onClick(ClickEvent event) {
253                                 hide();
254                         }
255                 });
256                 outer.add(confirm);
257                 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
258
259                 // Create the 'Reset password' button, along with a listener that hides
260                 // the dialog when the button is clicked.
261                 Button resetPassword = new Button("Reset Password", new ClickHandler() {
262                         @Override
263                         public void onClick(ClickEvent event) {
264                                 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
265                                 dlg.center();
266                         }
267                 });
268                 outer.add(resetPassword);
269                 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
270
271                 outer.setSpacing(8);
272                 setWidget(outer);
273         }
274
275         @Override
276         protected void onPreviewNativeEvent(NativePreviewEvent preview) {
277                 super.onPreviewNativeEvent(preview);
278                 NativeEvent evt = preview.getNativeEvent();
279                 if (evt.getType().equals("keydown"))
280                         // Use the popup's key preview hooks to close the dialog when
281                         // either enter or escape is pressed.
282                         switch (evt.getKeyCode()) {
283                                 case KeyCodes.KEY_ENTER:
284                                 case KeyCodes.KEY_ESCAPE:
285                                         hide();
286                                         break;
287                         }
288         }
289
290
291         /**
292          * Generate an RPC request to reset WebDAV password.
293          *
294          * @param userId the Uri of the user whose password will be reset
295          */
296         private void resetPassword(String userUri) {
297
298                 if (userUri == null || userUri.length() == 0) {
299                         GSS.get().displayError("Empty user Uri!");
300                         return;
301                 }
302                 GWT.log("resetPassword(" + userUri + ")", null);
303                 PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
304
305                         @Override
306                         public void onComplete() {
307                                 ReauthenticateDialog dlg = new ReauthenticateDialog();
308                                 dlg.center();
309                         }
310
311                         @Override
312                         public void onError(Throwable t) {
313                                 GWT.log("", t);
314                                 if(t instanceof RestException){
315                                         int statusCode = ((RestException)t).getHttpStatusCode();
316                                         if(statusCode == 405)
317                                                 GSS.get().displayError("You don't have the necessary" +
318                                                                 " permissions");
319                                         else if(statusCode == 404)
320                                                 GSS.get().displayError("Resource does not exist");
321                                         else
322                                                 GSS.get().displayError("Unable to reset password:" +
323                                                                         ((RestException)t).getHttpStatusText());
324                                 }
325                                 else
326                                         GSS.get().displayError("System error resetting password:" +
327                                                                 t.getMessage());
328                         }
329                 };
330                 DeferredCommand.addCommand(cg);
331         }
332
333 }