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.beans.factory.annotation.Autowired;
24  import org.springframework.web.context.request.RequestContextHolder;
25  
26  import com.hack23.cia.model.internal.application.system.impl.ApplicationSessionType;
27  import com.hack23.cia.service.api.ApplicationManager;
28  import com.hack23.cia.service.api.ConfigurationManager;
29  import com.hack23.cia.service.api.UserConfiguration;
30  import com.hack23.cia.service.api.action.application.CreateApplicationSessionRequest;
31  import com.hack23.cia.service.api.action.common.ServiceResponse;
32  import com.hack23.cia.web.impl.ui.application.util.WebBrowserUtil;
33  import com.hack23.cia.web.impl.ui.application.views.common.MainView;
34  import com.vaadin.annotations.Push;
35  import com.vaadin.annotations.StyleSheet;
36  import com.vaadin.annotations.Theme;
37  import com.vaadin.server.Page;
38  import com.vaadin.server.VaadinRequest;
39  import com.vaadin.server.VaadinSession;
40  import com.vaadin.server.WebBrowser;
41  import com.vaadin.shared.ui.ui.Transport;
42  import com.vaadin.spring.annotation.SpringUI;
43  import com.vaadin.spring.annotation.SpringViewDisplay;
44  import com.vaadin.spring.navigator.SpringNavigator;
45  import com.vaadin.ui.UI;
46  
47  
48  /**
49   * The Class CitizenIntelligenceAgencyUI.
50   */
51  @SpringUI
52  @SpringViewDisplay
53  @Theme("cia")
54  @Push(transport = Transport.WEBSOCKET_XHR)
55  @StyleSheet({"https://fonts.googleapis.com/css?family=Inconsolata"})
56  public final class CitizenIntelligenceAgencyUI extends UI {
57  
58  	/** The Constant serialVersionUID. */
59  	private static final long serialVersionUID = 1L;
60  
61  	/** The Constant LOG_INFO_BROWSER_ADDRESS_APPLICATION_SESSION_ID_RESULT. */
62  	private static final String LOG_INFO_BROWSER_ADDRESS_APPLICATION_SESSION_ID_RESULT = "Browser url: {} , lang: {} , address: {} , application:{}, sessionId:{}, result:{}";
63  
64  	/** The Constant LOGGER. */
65  	private static final Logger LOGGER = LoggerFactory
66  			.getLogger(CitizenIntelligenceAgencyUI.class);
67  
68  	/** The main view. */
69  	@Autowired
70  	private MainView mainView;
71  
72  	/** The application manager. */
73  	@Autowired
74  	private transient ApplicationManager applicationManager;
75  
76  	@Autowired
77  	private transient ConfigurationManager configurationManager;
78  
79  	@Autowired
80  	private SpringNavigator springNavigator;
81  
82  	/**
83  	 * Instantiates a new citizen intelligence agency ui.
84  	 */
85  	public CitizenIntelligenceAgencyUI() {
86  		super();
87  	}
88  
89  
90  	@Override
91  	protected void init(final VaadinRequest request) {
92  		VaadinSession.getCurrent().setErrorHandler(new UiInstanceErrorHandler(this));
93  		setSizeFull();
94  		//final Navigator navigator = new SpringNavigator(this, this);
95  		springNavigator.addView("", mainView);
96  		setNavigator(springNavigator);
97  
98  
99  		final Page currentPage = Page.getCurrent();
100 		final String requestUrl = currentPage.getLocation().toString();
101 		final String language = request.getLocale().getLanguage();
102 		final UserConfiguration userConfiguration = configurationManager.getUserConfiguration(requestUrl, language);
103 
104 		currentPage.setTitle(userConfiguration.getAgency().getAgencyName() + ":" + userConfiguration.getPortal().getPortalName() + ":" + userConfiguration.getLanguage().getLanguageName());
105 
106 		if (getSession().getUIs().isEmpty()) {
107 			final WebBrowser webBrowser = currentPage.getWebBrowser();
108 
109 			final CreateApplicationSessionRequest serviceRequest = new CreateApplicationSessionRequest();
110 			serviceRequest.setSessionId(RequestContextHolder.currentRequestAttributes().getSessionId());
111 
112 			final String ipInformation = WebBrowserUtil.getIpInformation(webBrowser);
113 			serviceRequest.setIpInformation(ipInformation);
114 			serviceRequest.setUserAgentInformation(webBrowser.getBrowserApplication());
115 			serviceRequest.setLocale(webBrowser.getLocale().toString());
116 			serviceRequest.setOperatingSystem(WebBrowserUtil.getOperatingSystem(webBrowser));
117 			serviceRequest.setSessionType(ApplicationSessionType.ANONYMOUS);
118 
119 			final ServiceResponse serviceResponse = applicationManager.service(serviceRequest);
120 			LOGGER.info(LOG_INFO_BROWSER_ADDRESS_APPLICATION_SESSION_ID_RESULT,requestUrl,language,ipInformation,webBrowser.getBrowserApplication(),serviceRequest.getSessionId(),serviceResponse.getResult().toString());
121 		}
122 	}
123 
124 
125 }