Move authentication to a separate login servlet.
[online-glom:gwt-glom.git] / src / main / java / org / glom / web / client / activity / DocumentLoginActivity.java
1 /*
2  * Copyright (C) 2011 Openismus GmbH
3  *
4  * This file is part of GWT-Glom.
5  *
6  * GWT-Glom is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU Lesser General Public License as published by the
8  * Free Software Foundation, either version 3 of the License, or (at your
9  * option) any later version.
10  *
11  * GWT-Glom is distributed in the hope that it will be useful, but WITHOUT
12  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
13  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU Lesser General Public License
14  * for more details.
15  *
16  * You should have received a copy of the GNU Lesser General Public License
17  * along with GWT-Glom.  If not, see <http://www.gnu.org/licenses/>.
18  */
19
20 package org.glom.web.client.activity;
21
22 import org.glom.web.client.ClientFactory;
23 import org.glom.web.client.OnlineGlomLoginServiceAsync;
24 import org.glom.web.client.PlaceControllerExt;
25 import org.glom.web.client.StringUtils;
26 import org.glom.web.client.event.TableChangeEvent;
27 import org.glom.web.client.place.HasDocumentPlace;
28 import org.glom.web.client.ui.DocumentLoginView;
29 import com.google.gwt.core.client.GWT;
30 import com.google.gwt.event.dom.client.ClickEvent;
31 import com.google.gwt.event.dom.client.ClickHandler;
32 import com.google.gwt.event.shared.EventBus;
33 import com.google.gwt.place.shared.PlaceController;
34 import com.google.gwt.user.client.rpc.AsyncCallback;
35 import com.google.gwt.user.client.ui.AcceptsOneWidget;
36
37 public class DocumentLoginActivity extends HasDocumentActivity {
38
39         private final DocumentLoginView view;
40
41         public DocumentLoginActivity(final HasDocumentPlace place, final ClientFactory clientFactory) {
42                 super(place, clientFactory);
43                 this.view = clientFactory.getDocumentLoginView();
44         }
45         
46         private void goToPrevious() {
47                 final PlaceController placeController = clientFactory.getPlaceController();
48                 if(placeController instanceof PlaceControllerExt) {
49                         final PlaceControllerExt ext = (PlaceControllerExt)placeController;
50                         ext.previous();
51                 } else {
52                         GWT.log("The PlaceController was not a PlaceControllerExt.");
53                 }
54         }
55         
56         private void goToDefault() {
57                 final PlaceController placeController = clientFactory.getPlaceController();
58                 if(placeController instanceof PlaceControllerExt) {
59                         final PlaceControllerExt ext = (PlaceControllerExt)placeController;
60                         goTo(ext.getDefaultPlace());
61                 } else {
62                         GWT.log("The PlaceController was not a PlaceControllerExt.");
63                 }
64         }
65
66         @Override
67         public void start(final AcceptsOneWidget panel, final EventBus eventBus) {
68                 if (StringUtils.isEmpty(documentID)) {
69                         goToPrevious();
70                 }
71
72                 // register this class as the presenter
73                 view.setPresenter(this);
74
75                 //Find out if there is any need for authentication,
76                 //asking for the credentials if necessary:
77                 checkAuthentication(eventBus);
78
79                 // indicate that the view is ready to be displayed
80                 panel.setWidget(view.asWidget());
81         }
82
83         @Override
84         protected void clearView() {
85                 super.clearView();
86                 view.clear();
87         }
88
89         /*
90          * (non-Javadoc)
91          * 
92          * @see com.google.gwt.activity.shared.AbstractActivity#onCancel()
93          */
94         @Override
95         public void onCancel() {
96                 clearView();
97         }
98
99         /*
100          * (non-Javadoc)
101          * 
102          * @see com.google.gwt.activity.shared.AbstractActivity#onStop()
103          */
104         @Override
105         public void onStop() {
106                 clearView();
107         }
108         
109         //TODO: Remove or modify this in the base class.
110         /**
111          * @param eventBus
112          */
113         protected void checkAuthentication(final EventBus eventBus) {
114                 if(StringUtils.isEmpty(documentID)) {
115                         //TODO: Show that no document was chosen?
116                         return;
117                 }
118
119                 // Check if the authentication info has been set for the document
120                 final AsyncCallback<Boolean> isAuthCallback = new AsyncCallback<Boolean>() {
121                         @Override
122                         public void onFailure(final Throwable caught) {
123                                 // TODO: create a way to notify users of asynchronous callback failures
124                                 GWT.log("AsyncCallback Failed: OnlineGlomService.isAuthenticated(): " + caught.getMessage());
125                         }
126         
127                         @Override
128                         public void onSuccess(final Boolean result) {
129                                 if (!result) {
130                                         //If the user is not already authenticated,
131                                         //then attempt that:
132                                         setUpAuthClickHandlers(eventBus);
133                                 } else {
134                                         // The user was already authenticated, so go to the previous (or default) page:
135                                         goToPrevious();
136                                         eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
137                                                         .getSelectedTableName()));
138                                 }
139                         }
140                 };
141                 OnlineGlomLoginServiceAsync.Util.getInstance().isAuthenticated(documentID, isAuthCallback);
142         }
143
144         private void setUpAuthClickHandlers(final EventBus eventBus) {
145                 
146                 //The login button:
147                 view.setClickLoginHandler(new ClickHandler() {
148                         @Override
149                         public void onClick(final ClickEvent event) {
150                                 view.setTextFieldsEnabled(false);
151         
152                                 final AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {
153                                         @Override
154                                         public void onFailure(final Throwable caught) {
155                                                 // TODO: create a way to notify users of asynchronous callback failures
156                                                 GWT.log("AsyncCallback Failed: OnlineGlomService.checkAuthentication(): " + caught.getMessage());
157                                         }
158         
159                                         @Override
160                                         public void onSuccess(final Boolean result) {
161                                                 if (result) {
162                                                         // If authentication succeeded, take us to the requested table:
163                                                         goToPrevious();
164                                                         //eventBus.fireEvent(new TableChangeEvent(clientFactory.getTableSelectionView()
165                                                         //              .getSelectedTableName()));
166                                                 } else {
167                                                         // If authentication failed, tell the user:
168                                                         view.setTextFieldsEnabled(true);
169                                                         view.setError();
170                                                 }
171                                         }
172                                 };
173                                 OnlineGlomLoginServiceAsync.Util.getInstance().checkAuthentication(documentID,
174                                                 view.getUsername(), view.getPassword(), callback);
175                         }
176                 });
177                 
178                 //The cancel button:
179                 view.setClickCancelHandler(new ClickHandler() {
180                         @Override
181                         public void onClick(final ClickEvent event) {
182                                 goToDefault();
183                         }
184                 });
185         }
186
187 }