Statistics
| Branch: | Tag: | Revision:

root / web_client / src / gr / grnet / pithos / web / client / CredentialsDialog.java @ f55cf326

History | View | Annotate | Download (11 kB)

1
/*
2
 * Copyright 2011 GRNET S.A. All rights reserved.
3
 *
4
 * Redistribution and use in source and binary forms, with or
5
 * without modification, are permitted provided that the following
6
 * conditions are met:
7
 *
8
 *   1. Redistributions of source code must retain the above
9
 *      copyright notice, this list of conditions and the following
10
 *      disclaimer.
11
 *
12
 *   2. Redistributions in binary form must reproduce the above
13
 *      copyright notice, this list of conditions and the following
14
 *      disclaimer in the documentation and/or other materials
15
 *      provided with the distribution.
16
 *
17
 * THIS SOFTWARE IS PROVIDED BY GRNET S.A. ``AS IS'' AND ANY EXPRESS
18
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
19
 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
20
 * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL GRNET S.A OR
21
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23
 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
24
 * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
25
 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN
27
 * ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28
 * POSSIBILITY OF SUCH DAMAGE.
29
 *
30
 * The views and conclusions contained in the software and
31
 * documentation are those of the authors and should not be
32
 * interpreted as representing official policies, either expressed
33
 * or implied, of GRNET S.A.
34
 */
35
package gr.grnet.pithos.web.client;
36

    
37
import gr.grnet.pithos.web.client.rest.PostCommand;
38
import gr.grnet.pithos.web.client.rest.RestException;
39

    
40
import com.google.gwt.core.client.GWT;
41
import com.google.gwt.dom.client.NativeEvent;
42
import com.google.gwt.event.dom.client.ClickEvent;
43
import com.google.gwt.event.dom.client.ClickHandler;
44
import com.google.gwt.event.dom.client.KeyCodes;
45
import com.google.gwt.user.client.DeferredCommand;
46
import com.google.gwt.user.client.Event.NativePreviewEvent;
47
import com.google.gwt.user.client.ui.AbstractImagePrototype;
48
import com.google.gwt.user.client.ui.Button;
49
import com.google.gwt.user.client.ui.DialogBox;
50
import com.google.gwt.user.client.ui.FlexTable;
51
import com.google.gwt.user.client.ui.HTML;
52
import com.google.gwt.user.client.ui.HasHorizontalAlignment;
53
import com.google.gwt.user.client.ui.HorizontalPanel;
54
import com.google.gwt.user.client.ui.TextBox;
55
import com.google.gwt.user.client.ui.VerticalPanel;
56

    
57

    
58
/**
59
 * A dialog box that displays the user credentials for use in other client
60
 * applications, such as WebDAV clients.
61
 */
62
public class CredentialsDialog extends DialogBox {
63

    
64
        private final String WIDTH_FIELD = "35em";
65
        private final String WIDTH_TEXT = "42em";
66

    
67
        /**
68
         * The 'confirm reset password' dialog box.
69
         */
70
        private class ConfirmResetPasswordDialog extends DialogBox {
71

    
72
                /**
73
                 * The widget's constructor.
74
                 *
75
                 * @param images the supplied images
76
                 */
77
                private ConfirmResetPasswordDialog(MessagePanel.Images images) {
78
                        // Set the dialog's caption.
79
                        setText("Confirmation");
80
                        setAnimationEnabled(true);
81
                        // Create a VerticalPanel to contain the label and the buttons.
82
                        VerticalPanel outer = new VerticalPanel();
83
                        HorizontalPanel buttons = new HorizontalPanel();
84

    
85
                        HTML text;
86
                        text = new HTML("<table><tr><td>" +
87
                                        AbstractImagePrototype.create(images.warn()).getHTML() +
88
                                        "</td><td>" + "Are you sure you want to create a new " +
89
                                        "WebDAV password?</td></tr></table>");
90
                        text.setStyleName("pithos-warnMessage");
91
                        outer.add(text);
92

    
93
                        // Create the 'Yes' button, along with a listener that hides the
94
                        // dialog when the button is clicked and resets the password.
95
                        Button ok = new Button("Yes", new ClickHandler() {
96
                                @Override
97
                                public void onClick(ClickEvent event) {
98
                                        resetPassword(Pithos.get().getCurrentUserResource().getUri());
99
                                        hide();
100
                                }
101
                        });
102
                        buttons.add(ok);
103
                        buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
104
                        // Create the 'No' button, along with a listener that hides the
105
                        // dialog when the button is clicked.
106
                        Button cancel = new Button("No", new ClickHandler() {
107
                                @Override
108
                                public void onClick(ClickEvent event) {
109
                                        hide();
110
                                }
111
                        });
112
                        buttons.add(cancel);
113
                        buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
114
                        buttons.setSpacing(8);
115
                        buttons.setStyleName("pithos-warnMessage");
116
                        outer.setStyleName("pithos-warnMessage");
117
                        outer.add(buttons);
118
                        outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
119
                        outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
120
                        setWidget(outer);
121
                }
122

    
123
                @Override
124
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
125
                        super.onPreviewNativeEvent(preview);
126
                        NativeEvent evt = preview.getNativeEvent();
127
                        if (evt.getType().equals("keydown"))
128
                                // Use the popup's key preview hooks to close the dialog when either
129
                                // enter or escape is pressed.
130
                                switch (evt.getKeyCode()) {
131
                                        case KeyCodes.KEY_ENTER:
132
                                        case KeyCodes.KEY_ESCAPE:
133
                                                hide();
134
                                                break;
135
                                }
136
                }
137

    
138
        }
139

    
140
        private class ReauthenticateDialog extends DialogBox {
141
                /**
142
                 * The widget constructor.
143
                 */
144
                public ReauthenticateDialog() {
145
                        // Set the dialog's caption.
146
                        setText("New Password Created");
147
                        setAnimationEnabled(true);
148
                        VerticalPanel outer = new VerticalPanel();
149

    
150
                        // Create the text and set a style name so we can style it with CSS.
151
                        HTML text = new HTML("<p>A new WebDAV password has been created." +
152
                                        "</p><p>You will now be redirected to the initial screen" +
153
                                        " for the changes to take effect. Choose \"Show " +
154
                                        "Credentials\" again afterwards to see the new password.</p>");
155
                        text.setStyleName("pithos-AboutText");
156
                        outer.add(text);
157

    
158
                        // Create the 'OK' button, along with a listener that hides the
159
                        // dialog when the button is clicked.
160
                        Button confirm = new Button("Proceed", new ClickHandler() {
161
                                @Override
162
                                public void onClick(ClickEvent event) {
163
                                        Pithos.get().authenticateUser();
164
                                        hide();
165
                                }
166
                        });
167
                        outer.add(confirm);
168
                        outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
169
                        outer.setSpacing(8);
170
                        setWidget(outer);
171
                }
172

    
173
                @Override
174
                protected void onPreviewNativeEvent(NativePreviewEvent preview) {
175
                        super.onPreviewNativeEvent(preview);
176
                        NativeEvent evt = preview.getNativeEvent();
177
                        if (evt.getType().equals("keydown"))
178
                                // Use the popup's key preview hooks to close the dialog when
179
                                // either enter or escape is pressed.
180
                                switch (evt.getKeyCode()) {
181
                                        case KeyCodes.KEY_ENTER:
182
                                                Pithos.get().authenticateUser();
183
                                                hide();
184
                                                break;
185
                                        case KeyCodes.KEY_ESCAPE:
186
                                                hide();
187
                                                break;
188
                                }
189
                }
190

    
191
        }
192

    
193
        /**
194
         * The widget constructor.
195
         */
196
        public CredentialsDialog(final MessagePanel.Images images) {
197
                // Set the dialog's caption.
198
                setText("User Credentials");
199
                setAnimationEnabled(true);
200
                // A VerticalPanel that contains the 'about' label and the 'OK' button.
201
                VerticalPanel outer = new VerticalPanel();
202
                Configuration conf = (Configuration) GWT.create(Configuration.class);
203
                String service = conf.serviceName();
204
                // Create the text and set a style name so we can style it with CSS.
205
                HTML text = new HTML("<p>These are the user credentials that are " +
206
                                "required for interacting with " + service + ".");
207
                text.setStyleName("pithos-AboutText");
208
                text.setWidth(WIDTH_TEXT);
209
                outer.add(text);
210
                FlexTable table = new FlexTable();
211
                table.setText(0, 0, "Username");
212
                table.setText(1, 0, "Token");
213
                TextBox username = new TextBox();
214
                final Pithos app = Pithos.get();
215
                username.setText(app.getCurrentUserResource().getUsername());
216
                username.setReadOnly(true);
217
                username.setWidth(WIDTH_FIELD);
218
                username.addClickHandler(new ClickHandler() {
219
                        @Override
220
                        public void onClick(ClickEvent event) {
221
                                Pithos.enableIESelection();
222
                                ((TextBox) event.getSource()).selectAll();
223
                                Pithos.preventIESelection();
224
                        }
225

    
226
                });
227
                table.setWidget(0, 1, username);
228

    
229
                TextBox tokenBox = new TextBox();
230
                tokenBox.setText(app.getToken());
231
                tokenBox.setReadOnly(true);
232
                tokenBox.setWidth(WIDTH_FIELD);
233
                tokenBox.addClickHandler(new ClickHandler() {
234
                        @Override
235
                        public void onClick(ClickEvent event) {
236
                                Pithos.enableIESelection();
237
                                ((TextBox) event.getSource()).selectAll();
238
                                Pithos.preventIESelection();
239
                        }
240

    
241
                });
242
                table.setWidget(1, 1, tokenBox);
243

    
244
                table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
245
                table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
246
                table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
247
                table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
248
                outer.add(table);
249

    
250
                // Create the 'OK' button, along with a listener that hides the dialog
251
                // when the button is clicked.
252
                Button confirm = new Button("Close", new ClickHandler() {
253
                        @Override
254
                        public void onClick(ClickEvent event) {
255
                                hide();
256
                        }
257
                });
258
                outer.add(confirm);
259
                outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
260

    
261
                // Create the 'Reset password' button, along with a listener that hides
262
                // the dialog when the button is clicked.
263
                Button resetPassword = new Button("Reset Password", new ClickHandler() {
264
                        @Override
265
                        public void onClick(ClickEvent event) {
266
                                ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
267
                                dlg.center();
268
                        }
269
                });
270
                outer.add(resetPassword);
271
                outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
272

    
273
                outer.setSpacing(8);
274
                setWidget(outer);
275
        }
276

    
277
        @Override
278
        protected void onPreviewNativeEvent(NativePreviewEvent preview) {
279
                super.onPreviewNativeEvent(preview);
280
                NativeEvent evt = preview.getNativeEvent();
281
                if (evt.getType().equals("keydown"))
282
                        // Use the popup's key preview hooks to close the dialog when
283
                        // either enter or escape is pressed.
284
                        switch (evt.getKeyCode()) {
285
                                case KeyCodes.KEY_ENTER:
286
                                case KeyCodes.KEY_ESCAPE:
287
                                        hide();
288
                                        break;
289
                        }
290
        }
291

    
292

    
293
        /**
294
         * Generate an RPC request to reset WebDAV password.
295
         *
296
         */
297
        private void resetPassword(String userUri) {
298

    
299
                if (userUri == null || userUri.length() == 0) {
300
                        Pithos.get().displayError("Empty user Uri!");
301
                        return;
302
                }
303
                GWT.log("resetPassword(" + userUri + ")", null);
304
                PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
305

    
306
                        @Override
307
                        public void onComplete() {
308
                                ReauthenticateDialog dlg = new ReauthenticateDialog();
309
                                dlg.center();
310
                        }
311

    
312
                        @Override
313
                        public void onError(Throwable t) {
314
                                GWT.log("", t);
315
                                if(t instanceof RestException){
316
                                        int statusCode = ((RestException)t).getHttpStatusCode();
317
                                        if(statusCode == 405)
318
                                                Pithos.get().displayError("You don't have the necessary" +
319
                                                                " permissions");
320
                                        else if(statusCode == 404)
321
                                                Pithos.get().displayError("Resource does not exist");
322
                                        else
323
                                                Pithos.get().displayError("Unable to reset password:" +
324
                                                                        ((RestException)t).getHttpStatusText());
325
                                }
326
                                else
327
                                        Pithos.get().displayError("System error resetting password:" +
328
                                                                t.getMessage());
329
                        }
330
                };
331
                DeferredCommand.addCommand(cg);
332
        }
333

    
334
}