2 * Copyright 2011 GRNET S.A. All rights reserved.
4 * Redistribution and use in source and binary forms, with or
5 * without modification, are permitted provided that the following
8 * 1. Redistributions of source code must retain the above
9 * copyright notice, this list of conditions and the following
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.
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.
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.
35 package gr.grnet.pithos.web.client;
37 import com.google.gwt.user.client.Window;
38 import gr.grnet.pithos.web.client.rest.PostCommand;
39 import gr.grnet.pithos.web.client.rest.RestException;
41 import com.google.gwt.core.client.GWT;
42 import com.google.gwt.dom.client.NativeEvent;
43 import com.google.gwt.event.dom.client.ClickEvent;
44 import com.google.gwt.event.dom.client.ClickHandler;
45 import com.google.gwt.event.dom.client.KeyCodes;
46 import com.google.gwt.user.client.DeferredCommand;
47 import com.google.gwt.user.client.Event.NativePreviewEvent;
48 import com.google.gwt.user.client.ui.AbstractImagePrototype;
49 import com.google.gwt.user.client.ui.Button;
50 import com.google.gwt.user.client.ui.DialogBox;
51 import com.google.gwt.user.client.ui.FlexTable;
52 import com.google.gwt.user.client.ui.HTML;
53 import com.google.gwt.user.client.ui.HasHorizontalAlignment;
54 import com.google.gwt.user.client.ui.HorizontalPanel;
55 import com.google.gwt.user.client.ui.TextBox;
56 import com.google.gwt.user.client.ui.VerticalPanel;
60 * A dialog box that displays the user credentials for use in other client
61 * applications, such as WebDAV clients.
63 public class CredentialsDialog extends DialogBox {
65 private final String WIDTH_FIELD = "35em";
66 private final String WIDTH_TEXT = "42em";
69 * The 'confirm reset password' dialog box.
71 private class ConfirmResetPasswordDialog extends DialogBox {
74 * The widget's constructor.
76 * @param images the supplied images
78 private ConfirmResetPasswordDialog(MessagePanel.Images images) {
79 // Set the dialog's caption.
80 setText("Confirmation");
81 setAnimationEnabled(true);
82 // Create a VerticalPanel to contain the label and the buttons.
83 VerticalPanel outer = new VerticalPanel();
84 HorizontalPanel buttons = new HorizontalPanel();
87 text = new HTML("<table><tr><td>" +
88 AbstractImagePrototype.create(images.warn()).getHTML() +
89 "</td><td>" + "Are you sure you want to create a new " +
90 "WebDAV password?</td></tr></table>");
91 text.setStyleName("pithos-warnMessage");
94 // Create the 'Yes' button, along with a listener that hides the
95 // dialog when the button is clicked and resets the password.
96 Button ok = new Button("Yes", new ClickHandler() {
98 public void onClick(ClickEvent event) {
99 resetPassword(GSS.get().getCurrentUserResource().getUri());
104 buttons.setCellHorizontalAlignment(ok, HasHorizontalAlignment.ALIGN_CENTER);
105 // Create the 'No' button, along with a listener that hides the
106 // dialog when the button is clicked.
107 Button cancel = new Button("No", new ClickHandler() {
109 public void onClick(ClickEvent event) {
114 buttons.setCellHorizontalAlignment(cancel, HasHorizontalAlignment.ALIGN_CENTER);
115 buttons.setSpacing(8);
116 buttons.setStyleName("pithos-warnMessage");
117 outer.setStyleName("pithos-warnMessage");
119 outer.setCellHorizontalAlignment(text, HasHorizontalAlignment.ALIGN_CENTER);
120 outer.setCellHorizontalAlignment(buttons, HasHorizontalAlignment.ALIGN_CENTER);
125 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
126 super.onPreviewNativeEvent(preview);
127 NativeEvent evt = preview.getNativeEvent();
128 if (evt.getType().equals("keydown"))
129 // Use the popup's key preview hooks to close the dialog when either
130 // enter or escape is pressed.
131 switch (evt.getKeyCode()) {
132 case KeyCodes.KEY_ENTER:
133 case KeyCodes.KEY_ESCAPE:
141 private class ReauthenticateDialog extends DialogBox {
143 * The widget constructor.
145 public ReauthenticateDialog() {
146 // Set the dialog's caption.
147 setText("New Password Created");
148 setAnimationEnabled(true);
149 VerticalPanel outer = new VerticalPanel();
151 // Create the text and set a style name so we can style it with CSS.
152 HTML text = new HTML("<p>A new WebDAV password has been created." +
153 "</p><p>You will now be redirected to the initial screen" +
154 " for the changes to take effect. Choose \"Show " +
155 "Credentials\" again afterwards to see the new password.</p>");
156 text.setStyleName("pithos-AboutText");
159 // Create the 'OK' button, along with a listener that hides the
160 // dialog when the button is clicked.
161 Button confirm = new Button("Proceed", new ClickHandler() {
163 public void onClick(ClickEvent event) {
164 GSS.get().authenticateUser();
169 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
175 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
176 super.onPreviewNativeEvent(preview);
177 NativeEvent evt = preview.getNativeEvent();
178 if (evt.getType().equals("keydown"))
179 // Use the popup's key preview hooks to close the dialog when
180 // either enter or escape is pressed.
181 switch (evt.getKeyCode()) {
182 case KeyCodes.KEY_ENTER:
183 GSS.get().authenticateUser();
186 case KeyCodes.KEY_ESCAPE:
195 * The widget constructor.
197 public CredentialsDialog(final MessagePanel.Images images) {
198 // Set the dialog's caption.
199 setText("User Credentials");
200 setAnimationEnabled(true);
201 // A VerticalPanel that contains the 'about' label and the 'OK' button.
202 VerticalPanel outer = new VerticalPanel();
203 Configuration conf = (Configuration) GWT.create(Configuration.class);
204 String service = conf.serviceName();
205 // Create the text and set a style name so we can style it with CSS.
206 HTML text = new HTML("<p>These are the user credentials that are " +
207 "required for interacting with " + service + ".");
208 text.setStyleName("pithos-AboutText");
209 text.setWidth(WIDTH_TEXT);
211 FlexTable table = new FlexTable();
212 table.setText(0, 0, "Username");
213 table.setText(1, 0, "Token");
214 TextBox username = new TextBox();
215 final GSS app = GSS.get();
216 username.setText(app.getCurrentUserResource().getUsername());
217 username.setReadOnly(true);
218 username.setWidth(WIDTH_FIELD);
219 username.addClickHandler(new ClickHandler() {
221 public void onClick(ClickEvent event) {
222 GSS.enableIESelection();
223 ((TextBox) event.getSource()).selectAll();
224 GSS.preventIESelection();
228 table.setWidget(0, 1, username);
230 TextBox tokenBox = new TextBox();
231 tokenBox.setText(app.getToken());
232 tokenBox.setReadOnly(true);
233 tokenBox.setWidth(WIDTH_FIELD);
234 tokenBox.addClickHandler(new ClickHandler() {
236 public void onClick(ClickEvent event) {
237 GSS.enableIESelection();
238 ((TextBox) event.getSource()).selectAll();
239 GSS.preventIESelection();
243 table.setWidget(1, 1, tokenBox);
245 table.getFlexCellFormatter().setStyleName(0, 0, "props-labels");
246 table.getFlexCellFormatter().setStyleName(0, 1, "props-values");
247 table.getFlexCellFormatter().setStyleName(1, 0, "props-labels");
248 table.getFlexCellFormatter().setStyleName(1, 1, "props-values");
251 // Create the 'OK' button, along with a listener that hides the dialog
252 // when the button is clicked.
253 Button confirm = new Button("Close", new ClickHandler() {
255 public void onClick(ClickEvent event) {
260 outer.setCellHorizontalAlignment(confirm, HasHorizontalAlignment.ALIGN_CENTER);
262 // Create the 'Reset password' button, along with a listener that hides
263 // the dialog when the button is clicked.
264 Button resetPassword = new Button("Reset Password", new ClickHandler() {
266 public void onClick(ClickEvent event) {
267 ConfirmResetPasswordDialog dlg = new ConfirmResetPasswordDialog(images);
271 outer.add(resetPassword);
272 outer.setCellHorizontalAlignment(resetPassword, HasHorizontalAlignment.ALIGN_CENTER);
279 protected void onPreviewNativeEvent(NativePreviewEvent preview) {
280 super.onPreviewNativeEvent(preview);
281 NativeEvent evt = preview.getNativeEvent();
282 if (evt.getType().equals("keydown"))
283 // Use the popup's key preview hooks to close the dialog when
284 // either enter or escape is pressed.
285 switch (evt.getKeyCode()) {
286 case KeyCodes.KEY_ENTER:
287 case KeyCodes.KEY_ESCAPE:
295 * Generate an RPC request to reset WebDAV password.
298 private void resetPassword(String userUri) {
300 if (userUri == null || userUri.length() == 0) {
301 GSS.get().displayError("Empty user Uri!");
304 GWT.log("resetPassword(" + userUri + ")", null);
305 PostCommand cg = new PostCommand(userUri + "?resetWebDAV", "", 200) {
308 public void onComplete() {
309 ReauthenticateDialog dlg = new ReauthenticateDialog();
314 public void onError(Throwable t) {
316 if(t instanceof RestException){
317 int statusCode = ((RestException)t).getHttpStatusCode();
318 if(statusCode == 405)
319 GSS.get().displayError("You don't have the necessary" +
321 else if(statusCode == 404)
322 GSS.get().displayError("Resource does not exist");
324 GSS.get().displayError("Unable to reset password:" +
325 ((RestException)t).getHttpStatusText());
328 GSS.get().displayError("System error resetting password:" +
332 DeferredCommand.addCommand(cg);