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