2 * Copyright (c) 2011 Greek Research and Technology Network
4 package gr.grnet.pithos.web.client;
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;
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;
29 * A dialog box that displays the user credentials for use in other client
30 * applications, such as WebDAV clients.
32 public class CredentialsDialog extends DialogBox {
34 private final String WIDTH_FIELD = "35em";
35 private final String WIDTH_TEXT = "42em";
37 private TextBox passwordBox;
40 * The 'confirm reset password' dialog box.
42 private class ConfirmResetPasswordDialog extends DialogBox {
45 * The widget's constructor.
47 * @param images the supplied images
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();
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");
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() {
69 public void onClick(ClickEvent event) {
70 resetPassword(GSS.get().getCurrentUserResource().getUri());
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() {
80 public void onClick(ClickEvent event) {
85 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
86 buttons.setSpacing(8);
87 buttons.setStyleName("pithos-warnMessage");
88 outer.setStyleName("pithos-warnMessage");
90 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
91 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
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:
112 private class ReauthenticateDialog extends DialogBox {
114 * The widget constructor.
116 public ReauthenticateDialog() {
117 // Set the dialog's caption.
118 setText("New Password Created");
119 setAnimationEnabled(true);
120 VerticalPanel outer = new VerticalPanel();
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");
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() {
134 public void onClick(ClickEvent event) {
135 GSS.get().authenticateUser();
140 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
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();
157 case KeyCodes.KEY_ESCAPE:
166 * The widget constructor.
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 +
188 text.setStyleName("pithos-AboutText");
189 text.setWidth(WIDTH_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() {
202 public void onClick(ClickEvent event) {
203 GSS.enableIESelection();
204 ((TextBox) event.getSource()).selectAll();
205 GSS.preventIESelection();
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() {
216 public void onClick(ClickEvent event) {
217 GSS.enableIESelection();
218 ((TextBox) event.getSource()).selectAll();
219 GSS.preventIESelection();
223 table.setWidget(1, 1, passwordBox);
225 TextBox tokenBox = new TextBox();
226 tokenBox.setText(app.getToken());
227 tokenBox.setReadOnly(true);
228 tokenBox.setWidth(WIDTH_FIELD);
229 tokenBox.addClickHandler(new ClickHandler() {
231 public void onClick(ClickEvent event) {
232 GSS.enableIESelection();
233 ((TextBox) event.getSource()).selectAll();
234 GSS.preventIESelection();
238 table.setWidget(2, 1, tokenBox);
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");
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() {
252 public void onClick(ClickEvent event) {
257 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
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() {
263 public void onClick(ClickEvent event) {
264 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
268 outer.add(resetPassword);
269 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
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:
292 * Generate an RPC request to reset WebDAV password.
294 * @param userId the Uri of the user whose password will be reset
296 private void resetPassword(String userUri) {
298 if (userUri == null || userUri.length() == 0) {
299 GSS.get().displayError("Empty user Uri!");
302 GWT.log("resetPassword(" + userUri + ")", null);
303 PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
306 public void onComplete() {
307 ReauthenticateDialog dlg = new ReauthenticateDialog();
312 public void onError(Throwable 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" +
319 else if(statusCode == 404)
320 GSS.get().displayError("Resource does not exist");
322 GSS.get().displayError("Unable to reset password:" +
323 ((RestException)t).getHttpStatusText());
326 GSS.get().displayError("System error resetting password:" +
330 DeferredCommand.addCommand(cg);