Switch reset WebDAV password from GET to POST.
[pithos] / src / gr / ebs / gss / client / CredentialsDialog.java
1 /*
2  * Copyright 2008, 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;
20
21 import gr.ebs.gss.client.rest.PostCommand;
22 import gr.ebs.gss.client.rest.RestException;
23
24 import com.google.gwt.core.client.GWT;
25 import com.google.gwt.user.client.DeferredCommand;
26 import com.google.gwt.user.client.ui.Button;
27 import com.google.gwt.user.client.ui.ClickListener;
28 import com.google.gwt.user.client.ui.DialogBox;
29 import com.google.gwt.user.client.ui.FlexTable;
30 import com.google.gwt.user.client.ui.HTML;
31 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
32 import com.google.gwt.user.client.ui.HorizontalPanel;
33 import com.google.gwt.user.client.ui.KeyboardListener;
34 import com.google.gwt.user.client.ui.TextBox;
35 import com.google.gwt.user.client.ui.VerticalPanel;
36 import com.google.gwt.user.client.ui.Widget;
37
38
39 /**
40  * A dialog box that displays the user credentials for use in other client
41  * applications, such as WebDAV clients.
42  *
43  * @author kman
44  */
45 public class CredentialsDialog extends DialogBox {
46
47         private final String WIDTH_FIELD = "35em";
48         private final String WIDTH_TEXT = "42em";
49
50         private TextBox passwordBox;
51
52         /**
53          * The 'confirm reset password' dialog box.
54          */
55         public class ConfirmResetPasswordDialog extends DialogBox {
56
57                 /**
58                  * The widget's constructor.
59                  *
60                  * @param images the supplied images
61                  */
62                 private ConfirmResetPasswordDialog(MessagePanel.Images images) {
63                         // Set the dialog's caption.
64                         setText("Confirmation");
65                         setAnimationEnabled(true);
66                         // Create a VerticalPanel to contain the label and the buttons.
67                         VerticalPanel outer = new VerticalPanel();
68                         HorizontalPanel buttons = new HorizontalPanel();
69
70                         HTML text;
71                         text = new HTML("<table><tr><td>" + images.warn().getHTML() + "</td><td>" + "Are you sure you want to create a new WebDAV password?</td></tr></table>");
72                         text.setStyleName("gss-warnMessage");
73                         outer.add(text);
74
75                         // Create the 'Yes' button, along with a listener that hides the dialog
76                         // when the button is clicked and resets the password.
77                         Button ok = new Button("Yes", new ClickListener() {
78                                 public void onClick(Widget sender) {
79                                         resetPassword(GSS.get().getCurrentUserResource().getUri());
80                                         hide();
81                                 }
82                         });
83                         buttons.add(ok);
84                         buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
85                         // Create the 'No' button, along with a listener that hides the
86                         // dialog when the button is clicked.
87                         Button cancel = new Button("No", new ClickListener() {
88                                 public void onClick(Widget sender) {
89                                         hide();
90                                 }
91                         });
92                         buttons.add(cancel);
93                         buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
94                         buttons.setSpacing(8);
95                         buttons.setStyleName("gss-warnMessage");
96                         outer.setStyleName("gss-warnMessage");
97                         outer.add(buttons);
98                         outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
99                         outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
100                         setWidget(outer);
101                 }
102
103
104                 @Override
105                 public boolean onKeyDownPreview(final char key, final int modifiers) {
106                         // Use the popup's key preview hooks to close the dialog when
107                         // escape is pressed.
108                         switch (key) {
109                                 case KeyboardListener.KEY_ESCAPE:
110                                         hide();
111                                         break;
112                         }
113
114                         return true;
115                 }
116
117         }
118
119         /**
120          * The widget constructor.
121          */
122         public CredentialsDialog(final MessagePanel.Images images) {
123                 // Set the dialog's caption.
124                 setText("User Credentials");
125                 setAnimationEnabled(true);
126                 // Create a VerticalPanel to contain the 'about' label and the 'OK'
127                 // button.
128                 VerticalPanel outer = new VerticalPanel();
129                 Configuration conf = (Configuration) GWT.create(Configuration.class);
130                 String service = conf.serviceName();
131                 String webdavUrl = conf.serviceHome() + conf.webdavUrl();
132                 String tokenNote = conf.tokenTTLNote();
133                 // Create the text and set a style name so we can style it with CSS.
134                 HTML text = new HTML("<p>These are the user credentials that are required " +
135                                 "for interacting with " + service + ". " +
136                                 "You can copy and paste the username and password in the WebDAV client" +
137                                 " in order to use " + service + " through the WebDAV interface, at:<br/> " +
138                                 webdavUrl +
139                                 "<br/>" + tokenNote + "</p>");
140                 text.setStyleName("gss-AboutText");
141                 text.setWidth(WIDTH_TEXT);
142                 outer.add(text);
143                 FlexTable table = new FlexTable();
144                 table.setText(0, 0, "Username");
145                 table.setText(1, 0, "Password");
146                 table.setText(2, 0, "Token");
147                 TextBox username = new TextBox();
148                 final GSS app = GSS.get();
149                 username.setText(app.getCurrentUserResource().getUsername());
150                 username.setReadOnly(true);
151                 username.setWidth(WIDTH_FIELD);
152                 username.addClickListener(new ClickListener () {
153
154                         public void onClick(Widget sender) {
155                                 GSS.enableIESelection();
156                                 ((TextBox) sender).selectAll();
157                                 GSS.preventIESelection();
158                         }
159
160                 });
161                 table.setWidget(0, 1, username);
162                 passwordBox = new TextBox();
163                 passwordBox.setText(app.getWebDAVPassword());
164                 passwordBox.setReadOnly(true);
165                 passwordBox.setWidth(WIDTH_FIELD);
166                 passwordBox.addClickListener(new ClickListener () {
167
168                         public void onClick(Widget sender) {
169                                 GSS.enableIESelection();
170                                 ((TextBox) sender).selectAll();
171                                 GSS.preventIESelection();
172                         }
173
174                 });
175                 table.setWidget(1, 1, passwordBox);
176
177                 TextBox tokenBox = new TextBox();
178                 tokenBox.setText(app.getToken());
179                 tokenBox.setReadOnly(true);
180                 tokenBox.setWidth(WIDTH_FIELD);
181                 tokenBox.addClickListener(new ClickListener () {
182
183                         public void onClick(Widget sender) {
184                                 GSS.enableIESelection();
185                                 ((TextBox) sender).selectAll();
186                                 GSS.preventIESelection();
187                         }
188
189                 });
190                 table.setWidget(2, 1, tokenBox);
191
192                 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
193                 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
194                 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
195                 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
196                 table.getFlexCellFormatter().setStyleName(2, 0, "props-labels");
197                 table.getFlexCellFormatter().setStyleName(2, 1, "props-values");
198                 outer.add(table);
199
200                 // Create the 'OK' button, along with a listener that hides the dialog
201                 // when the button is clicked.
202                 Button confirm = new Button("Close", new ClickListener() {
203
204                         public void onClick(Widget sender) {
205                                 hide();
206                         }
207                 });
208                 outer.add(confirm);
209                 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
210
211                 // Create the 'Reset password' button, along with a listener that hides the dialog
212                 // when the button is clicked.
213                 Button resetPassword = new Button("Reset Password", new ClickListener() {
214
215                         public void onClick(Widget sender) {
216                                 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
217                                 dlg.center();
218                         }
219                 });
220                 outer.add(resetPassword);
221                 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
222
223                 outer.setSpacing(8);
224                 setWidget(outer);
225         }
226
227         @Override
228         public boolean onKeyDownPreview(char key, int modifiers) {
229                 // Use the popup's key preview hooks to close the dialog when either
230                 // enter or escape is pressed.
231                 switch (key) {
232                         case KeyboardListener.KEY_ENTER:
233                         case KeyboardListener.KEY_ESCAPE:
234                                 hide();
235                                 break;
236                 }
237                 return true;
238         }
239
240
241         /**
242          * Generate an RPC request to reset WebDAV password.
243          *
244          * @param userId the Uri of the user whose password will be reset
245          */
246         private void resetPassword(String userUri) {
247
248                 if (userUri == null || userUri.length() == 0) {
249                         GSS.get().displayError("Empty user Uri!");
250                         return;
251                 }
252                 GWT.log("resetPassword(" + userUri + ")", null);
253                 PostCommand cg = new PostCommand(userUri + "users?resetWebDAV", "", 200) {
254
255                         @Override
256                         public void onComplete() {
257                                 GSS.get().refreshWebDAVPassword();
258                                 passwordBox.setText(GSS.get().getWebDAVPassword());
259                         }
260
261                         @Override
262                         public void onError(Throwable t) {
263                                 GWT.log("", t);
264                                 if(t instanceof RestException){
265                                         int statusCode = ((RestException)t).getHttpStatusCode();
266                                         if(statusCode == 405)
267                                                 GSS.get().displayError("You don't have the necessary permissions");
268                                         else if(statusCode == 404)
269                                                 GSS.get().displayError("Resource does not exist");
270                                         else
271                                                 GSS.get().displayError("Unable to reset password:"+((RestException)t).getHttpStatusText());
272                                 }
273                                 else
274                                         GSS.get().displayError("System error resetting password:"+t.getMessage());
275                         }
276                 };
277                 DeferredCommand.addCommand(cg);
278
279         }
280
281 }