Project

General

Profile

« Previous | Next » 

Revision daf5d5b4

Added by Andreas Kohlbecker almost 5 years ago

ref #7046 removing request start and end listener classes

View differences:

src/main/java/eu/etaxonomy/cdm/addon/config/CdmVaadinConfiguration.java
27 27
import org.springframework.context.annotation.Lazy;
28 28
import org.springframework.security.authentication.AuthenticationProvider;
29 29

  
30
import com.vaadin.server.DeploymentConfiguration;
31
import com.vaadin.server.ServiceException;
32
import com.vaadin.server.VaadinServletService;
33 30
import com.vaadin.spring.annotation.EnableVaadin;
34 31
import com.vaadin.spring.annotation.SpringUI;
35 32
import com.vaadin.spring.annotation.UIScope;
......
43 40
import eu.etaxonomy.cdm.dataInserter.RegistrationRequiredDataInserter;
44 41
import eu.etaxonomy.cdm.opt.config.DataSourceConfigurer;
45 42
import eu.etaxonomy.cdm.vaadin.security.annotation.EnableAnnotationBasedAccessControl;
46
import eu.etaxonomy.cdm.vaadin.server.CdmSpringVaadinServletService;
47 43
import eu.etaxonomy.cdm.vaadin.ui.ConceptRelationshipUI;
48 44
import eu.etaxonomy.cdm.vaadin.ui.DistributionStatusUI;
49 45
import eu.etaxonomy.cdm.vaadin.ui.RegistrationUI;
......
96 92

  
97 93
        private static final long serialVersionUID = -2615042297393028775L;
98 94

  
99
        @Override
100
        protected VaadinServletService createServletService(
101
                DeploymentConfiguration deploymentConfiguration)
102
                throws ServiceException {
103

  
104
            //  - The SpringVaadinServletService is needed when using a custom service URL
105
            //  - The CdmSpringVaadinServletService allows to attach listeners to the requestEnd and
106
            //    requestStart method this is important for proper unbinding of Conversations from
107
            //    the request threads.
108
            //    see ViewScopeConversationHolder
109
            CdmSpringVaadinServletService service = new CdmSpringVaadinServletService(
110
                    this, deploymentConfiguration, getServiceUrlPath());
111
            service.init();
112
            return service;
113
        }
114

  
115 95
        /**
116 96
         *
117 97
        @SuppressWarnings("serial")
src/main/java/eu/etaxonomy/cdm/vaadin/server/CdmSpringVaadinServletService.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.server;
10

  
11
import java.util.ArrayList;
12
import java.util.List;
13

  
14
import com.vaadin.server.DeploymentConfiguration;
15
import com.vaadin.server.ServiceException;
16
import com.vaadin.server.VaadinRequest;
17
import com.vaadin.server.VaadinResponse;
18
import com.vaadin.server.VaadinServlet;
19
import com.vaadin.server.VaadinSession;
20
import com.vaadin.spring.server.SpringVaadinServletService;
21

  
22
/**
23
 * @author a.kohlbecker
24
 * @since Jul 4, 2017
25
 *
26
 */
27
public class CdmSpringVaadinServletService extends SpringVaadinServletService {
28

  
29
    private static final long serialVersionUID = 5956798985059823698L;
30

  
31
    List<RequestEndListener> requestEndListeners = new ArrayList<>();
32

  
33
    List<RequestStartListener> requestStartListeners = new ArrayList<>();
34

  
35
    /**
36
     * @param servlet
37
     * @param deploymentConfiguration
38
     * @param serviceUrl
39
     * @throws ServiceException
40
     */
41
    public CdmSpringVaadinServletService(VaadinServlet servlet, DeploymentConfiguration deploymentConfiguration,
42
            String serviceUrl) throws ServiceException {
43
        super(servlet, deploymentConfiguration, serviceUrl);
44
    }
45

  
46
    @Override
47
    public void requestStart(VaadinRequest request, VaadinResponse response) {
48
        super.requestStart(request, response);
49
        requestStartListeners.forEach(l -> l.onRequestStart(request));
50
    }
51

  
52
    @Override
53
    public void requestEnd(VaadinRequest request, VaadinResponse response,
54
            VaadinSession session) {
55
        super.requestEnd(request, response, session);
56
        requestEndListeners.forEach(l -> l.onRequestEnd(request, session));
57
    }
58

  
59
    public void addRequestStartListener(RequestStartListener requestStartListener){
60
        requestStartListeners.add(requestStartListener);
61
    }
62

  
63
    public void removeRequestStartListener(RequestStartListener requestStartListener){
64
        requestStartListeners.remove(requestStartListener);
65
    }
66

  
67
    public void addRequestEndListener(RequestEndListener requestEndListener){
68
        requestEndListeners.add(requestEndListener);
69
    }
70

  
71
    public void removeRequestEndListener(RequestEndListener requestEndListener){
72
        requestEndListeners.remove(requestEndListener);
73
    }
74

  
75

  
76

  
77

  
78

  
79

  
80
}
src/main/java/eu/etaxonomy/cdm/vaadin/server/RequestEndListener.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.server;
10

  
11
import com.vaadin.server.VaadinRequest;
12
import com.vaadin.server.VaadinSession;
13

  
14
/**
15
 * @author a.kohlbecker
16
 * @since Jul 4, 2017
17
 *
18
 */
19
public interface RequestEndListener {
20

  
21
    public void onRequestEnd(VaadinRequest request, VaadinSession session);
22

  
23
}
src/main/java/eu/etaxonomy/cdm/vaadin/server/RequestStartListener.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.server;
10

  
11
import com.vaadin.server.VaadinRequest;
12

  
13
/**
14
 * @author a.kohlbecker
15
 * @since Jul 4, 2017
16
 *
17
 */
18
public interface RequestStartListener {
19

  
20
    public void onRequestStart(VaadinRequest request);
21

  
22
}
src/main/java/eu/etaxonomy/cdm/vaadin/session/IntraViewConversationDirector.java
1
/**
2
* Copyright (C) 2017 EDIT
3
* European Distributed Institute of Taxonomy
4
* http://www.e-taxonomy.eu
5
*
6
* The contents of this file are subject to the Mozilla Public License Version 1.1
7
* See LICENSE.TXT at the top of this package for the full license terms.
8
*/
9
package eu.etaxonomy.cdm.vaadin.session;
10

  
11
import eu.etaxonomy.cdm.vaadin.server.RequestEndListener;
12

  
13
/**
14
 * @author a.kohlbecker
15
 * @since Jul 6, 2017
16
 *
17
 */
18
public interface IntraViewConversationDirector extends ConversationDirector, RequestEndListener {
19

  
20
}
src/main/java/eu/etaxonomy/vaadin/mvp/AbstractEditorPresenter.java
13 13
import org.springframework.context.ApplicationEventPublisher;
14 14
import org.springframework.context.event.EventListener;
15 15

  
16
import com.vaadin.server.ServletPortletHelper;
17
import com.vaadin.server.VaadinRequest;
18
import com.vaadin.server.VaadinService;
19
import com.vaadin.server.VaadinSession;
20
import com.vaadin.ui.UI;
21

  
22 16
import eu.etaxonomy.cdm.vaadin.event.AbstractEditorAction;
23
import eu.etaxonomy.cdm.vaadin.server.CdmSpringVaadinServletService;
24
import eu.etaxonomy.cdm.vaadin.server.RequestEndListener;
25
import eu.etaxonomy.cdm.vaadin.server.RequestStartListener;
26 17
import eu.etaxonomy.vaadin.mvp.event.EditorDeleteEvent;
27 18
import eu.etaxonomy.vaadin.mvp.event.EditorPreSaveEvent;
28 19
import eu.etaxonomy.vaadin.mvp.event.EditorSaveEvent;
......
34 25
 * @since Apr 5, 2017
35 26
 *
36 27
 */
37
public abstract class AbstractEditorPresenter<DTO extends Object, V extends ApplicationView<?>> extends AbstractPresenter<V>
38
implements RequestEndListener, RequestStartListener {
28
public abstract class AbstractEditorPresenter<DTO extends Object, V extends ApplicationView<?>> extends AbstractPresenter<V> {
39 29

  
40 30

  
41 31
    private static final long serialVersionUID = -6677074110764145236L;
......
140 130
        return action.getSourceView() != null && getView().equals(action.getSourceView());
141 131
    }
142 132

  
143
    @Override
144
    protected void init(V view) {
145
        super.init(view);
146
        registerListeners();
147
    }
148

  
149
    @Override
150
    public void onViewExit() {
151
        super.onViewExit();
152
        unregisterListeners();
153
    }
154

  
155

  
156
    // -------------------------------------------------------------------------
157

  
158
    protected void registerListeners() {
159
     // register as request start and end listener
160
        VaadinService service = UI.getCurrent().getSession().getService();
161
        if(service instanceof CdmSpringVaadinServletService){
162
            logger.trace(String.format("~~~~~ %s register as request listener", _toString()));
163
            ((CdmSpringVaadinServletService)service).addRequestEndListener(this);
164
            if(logger.isTraceEnabled()){
165
                ((CdmSpringVaadinServletService)service).addRequestStartListener(this);
166
            }
167
        } else {
168
            throw new RuntimeException("Using the CdmSpringVaadinServletService is required for proper per view conversation handling");
169
        }
170
    }
171

  
172
    /**
173
    *
174
    */
175
   protected void unregisterListeners() {
176
       VaadinService service = UI.getCurrent().getSession().getService();
177
       if(service instanceof CdmSpringVaadinServletService){
178
           logger.trace(String.format("~~~~~ %s un-register as request listener", _toString()));
179
           ((CdmSpringVaadinServletService)service).removeRequestEndListener(this);
180
           if(logger.isTraceEnabled()){
181
               ((CdmSpringVaadinServletService)service).removeRequestStartListener(this);
182
           }
183
       } else {
184
           throw new RuntimeException("Using the CdmSpringVaadinServletService is required for proper per view conversation handling");
185
       }
186
   }
187

  
188
    /**
189
     * <b>ONLY USED FOR LOGGING</b> when Level==TRACE
190
     * {@inheritDoc}
191
     */
192
    @Override
193
    public void onRequestStart(VaadinRequest request){
194

  
195
        if(requestNeedsSession(request) ){
196

  
197
            if(getView() instanceof AbstractPopupEditor){
198
                Object bean = ((AbstractPopupEditor)getView()).getBean();
199
                getSession().merge(bean);
200
            }
201

  
202
        } else {
203
            // ignore hartbeat, fileupload, push etc
204
            logger.trace("ignoring request:" + request.getPathInfo());
205
        }
206
    }
207

  
208
    /**
209
     * Returns <code>true</code> for:
210
     * <ul>
211
     *   <li>..</li>
212
     * <ul>
213
     *
214
     * Return <code>false</code> for:
215
     *
216
     * <ul>
217
     *   <li>UILD request in a existing view, like clicking on a button</li>
218
     * <ul>
219
     *
220
     * @return
221
    protected boolean isActiveView(){
222
        return UI.getCurrent() != null && getView() != null && getView() == navigationManager.getCurrentView();
223
    }
224
     */
225

  
226
    @Override
227
    public void onRequestEnd(VaadinRequest request, VaadinSession session){
228

  
229
        if(requestNeedsSession(request) ){
230
            logger.trace("onRequestEnd() " + request.getPathInfo() + " " + _toString());
231

  
232
        } else {
233
            // ignore hartbeat, fileupload, push etc
234
            logger.trace("ignoring request:" + request.getPathInfo());
235
        }
236

  
237
    }
238

  
239
    /**
240
     * @param request
241
     * @return
242
     */
243
    protected boolean requestNeedsSession(VaadinRequest request) {
244
        return !(
245
                ServletPortletHelper.isAppRequest(request) // includes published file request
246
             || ServletPortletHelper.isFileUploadRequest(request)
247
             || ServletPortletHelper.isHeartbeatRequest(request)
248
             || ServletPortletHelper.isPushRequest(request)
249
             );
250
    }
251 133

  
252 134
    protected abstract void saveBean(DTO bean);
253 135

  

Also available in: Unified diff