View Javadoc
1   /*
2    * Copyright 2014 James Pether Sörling
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *   http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   *	$Id$
17   *  $HeadURL$
18  */
19  package com.hack23.cia.web.impl.ui.application;
20  
21  import org.slf4j.Logger;
22  import org.slf4j.LoggerFactory;
23  import org.springframework.security.access.AccessDeniedException;
24  
25  import com.hack23.cia.web.impl.ui.application.views.common.viewnames.CommonsViews;
26  import com.vaadin.server.ErrorEvent;
27  import com.vaadin.server.ErrorHandler;
28  import com.vaadin.ui.Notification;
29  import com.vaadin.ui.UI;
30  
31  /**
32   * The Class UiInstanceErrorHandler.
33   */
34  public final class UiInstanceErrorHandler implements ErrorHandler {
35  
36  	/** The Constant serialVersionUID. */
37  	private static final long serialVersionUID = 1L;
38  
39  	/** The Constant LOG_WARN_VAADIN_ERROR. */
40  	private static final String LOG_WARN_VAADIN_ERROR = "Vaadin error";
41  
42  	/** The Constant LOGGER. */
43  	private static final Logger LOGGER = LoggerFactory.getLogger(UiInstanceErrorHandler.class);
44  
45  	/** The ui. */
46  	private final UI ui;
47  
48  	/**
49  	 * Instantiates a new ui instance error handler.
50  	 *
51  	 * @param ui
52  	 *            the ui
53  	 */
54  	public UiInstanceErrorHandler(final UI ui) {
55  		super();
56  		this.ui = ui;
57  	}
58  
59  	@Override
60  	public void error(final ErrorEvent event) {
61  		if (event.getThrowable() instanceof AccessDeniedException) {
62  			final AccessDeniedException accessDeniedException = (AccessDeniedException) event.getThrowable();
63  			Notification.show(accessDeniedException.getMessage(), Notification.Type.ERROR_MESSAGE);
64  			ui.getNavigator().navigateTo(CommonsViews.MAIN_VIEW_NAME);
65  			return;
66  		} else if (event.getThrowable().getCause() != null && event.getThrowable().getCause().getCause() != null
67  				&& event.getThrowable().getCause().getCause().getCause() instanceof AccessDeniedException) {
68  			final AccessDeniedException accessDeniedException = (AccessDeniedException) event.getThrowable().getCause()
69  					.getCause().getCause();
70  			Notification.show(accessDeniedException.getMessage(), Notification.Type.ERROR_MESSAGE);
71  			ui.getNavigator().navigateTo(CommonsViews.MAIN_VIEW_NAME);
72  			return;
73  		} else {
74  			LOGGER.warn(LOG_WARN_VAADIN_ERROR, event.getThrowable());
75  		}
76  	}
77  
78  }