001    /*
002      GRANITE DATA SERVICES
003      Copyright (C) 2012 GRANITE DATA SERVICES S.A.S.
004    
005      This file is part of Granite Data Services.
006    
007      Granite Data Services is free software; you can redistribute it and/or modify
008      it under the terms of the GNU Library General Public License as published by
009      the Free Software Foundation; either version 2 of the License, or (at your
010      option) any later version.
011    
012      Granite Data Services is distributed in the hope that it will be useful, but
013      WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
014      FITNESS FOR A PARTICULAR PURPOSE. See the GNU Library General Public License
015      for more details.
016    
017      You should have received a copy of the GNU Library General Public License
018      along with this library; if not, see <http://www.gnu.org/licenses/>.
019    */
020    
021    package org.granite.client.tide.javafx;
022    
023    import java.util.Observable;
024    import java.util.Observer;
025    import java.util.concurrent.Future;
026    
027    import javafx.beans.property.BooleanProperty;
028    import javafx.beans.property.ReadOnlyStringWrapper;
029    import javafx.beans.property.SimpleBooleanProperty;
030    import javafx.beans.property.StringProperty;
031    import javafx.beans.value.ChangeListener;
032    import javafx.beans.value.ObservableValue;
033    
034    import org.granite.client.messaging.messages.responses.FaultMessage;
035    import org.granite.client.messaging.messages.responses.FaultMessage.Code;
036    import org.granite.client.tide.Context;
037    import org.granite.client.tide.Identity;
038    import org.granite.client.tide.impl.ComponentImpl;
039    import org.granite.client.tide.server.ExceptionHandler;
040    import org.granite.client.tide.server.ServerSession;
041    import org.granite.client.tide.server.SimpleTideResponder;
042    import org.granite.client.tide.server.TideFaultEvent;
043    import org.granite.client.tide.server.TideResponder;
044    import org.granite.client.tide.server.TideResultEvent;
045    
046    
047    /**
048     * @author William DRAI
049     */
050    public abstract class BaseIdentity extends ComponentImpl implements Identity, ExceptionHandler {
051            
052            private BooleanProperty loggedIn = new SimpleBooleanProperty(this, "loggedIn");
053            private StringProperty username = new ReadOnlyStringWrapper(this, "username", null);
054            
055    
056            public BooleanProperty loggedInProperty() {
057                    return loggedIn;
058            }
059            
060            public boolean isLoggedIn() {
061                    return loggedIn.get();
062            }
063            
064            public void setLoggedIn(boolean loggedIn) {
065                    this.loggedIn.set(loggedIn);
066            }
067            
068            public StringProperty usernameProperty() {
069                    return username;
070            }
071            
072            public String getUsername() {
073                    return username.get();
074            }
075            
076            
077        protected BaseIdentity() {
078            // CDI proxying...
079        }
080        
081        public BaseIdentity(final ServerSession serverSession) {
082            super(serverSession);
083            
084            this.loggedIn.set(false);
085            this.loggedIn.addListener(new ChangeListener<Boolean>() {
086                            @Override
087                            public void changed(ObservableValue<? extends Boolean> property, Boolean oldValue, Boolean newValue) {
088                                    if (Boolean.TRUE.equals(newValue)) {
089                                            initSecurityCache();
090                                            serverSession.afterLogin();
091                                    }
092                                    else {
093                                            BaseIdentity.this.username.set(null);
094                                    clearSecurityCache();
095                                    }
096                            }
097            });
098        }
099        
100        /**
101         *  Triggers a remote call to check is user is currently logged in
102         *  Can be used at application startup to handle browser refresh cases
103         * 
104         *  @param resultHandler optional result handler
105         *  @param faultHandler optional fault handler 
106         */
107        public Future<String> checkLoggedIn(final TideResponder<String> tideResponder) {
108            return super.call("isLoggedIn", new SimpleTideResponder<String>() {
109                            @Override
110                            public void result(TideResultEvent<String> event) {
111                                    if (event.getResult() != null) {
112                                            BaseIdentity.this.username.set(event.getResult());
113                                            BaseIdentity.this.loggedIn.set(true);
114                                    }
115                                    else if (isLoggedIn()) {
116                                            BaseIdentity.this.loggedIn.set(false);
117                                            
118                                            // Session expired, directly mark the channel as logged out
119                                            getServerSession().sessionExpired();
120                                    }
121                                    
122                                    if (tideResponder != null)
123                                            tideResponder.result(event);
124                            }
125                            
126                            @Override
127                            public void fault(TideFaultEvent event) {
128                                    if (tideResponder != null)
129                                            tideResponder.fault(event);
130                            }
131            });
132        }
133        
134        
135        public void login(final String username, String password, final TideResponder<String> tideResponder) {
136            getServerSession().login(username, password);
137            
138            clearSecurityCache();
139            
140            checkLoggedIn(tideResponder);
141        }
142        
143        
144        public void logout(final TideResponder<Void> tideResponder) {
145            final Observer observer = new Observer() {
146                            @SuppressWarnings("unchecked")
147                            @Override
148                            public void update(Observable logout, Object event) {
149                            BaseIdentity.this.loggedIn.set(false);
150                            
151                            if (tideResponder != null) {
152                                            if (event instanceof TideResultEvent)
153                                            tideResponder.result((TideResultEvent<Void>)event);
154                                            else if (event instanceof TideFaultEvent)
155                                            tideResponder.fault((TideFaultEvent)event);
156                            }
157                            }
158            };
159            
160            getServerSession().logout(observer);
161        }
162        
163        
164        public abstract ObservableRole hasRole(String roleName);
165        
166        
167        protected abstract void initSecurityCache();
168        
169        /**
170         *  Clear the security cache
171         */
172        public abstract void clearSecurityCache();
173    
174        
175            @Override
176            public boolean accepts(FaultMessage emsg) {
177                    return emsg.getCode() == Code.NOT_LOGGED_IN;
178            }
179    
180            @Override
181            public void handle(Context context, FaultMessage emsg, TideFaultEvent faultEvent) {
182                    if (isLoggedIn()) {
183                            setLoggedIn(false);
184                            
185                            // Session expired, directly mark the channel as logged out
186                            getServerSession().sessionExpired();
187                    }
188            }
189    }
190