seb-server/src/main/java/ch/ethz/seb/sebserver/gui/RAPConfiguration.java

165 lines
7.2 KiB
Java
Raw Normal View History

2019-01-28 16:58:06 +01:00
/*
* Copyright (c) 2018 ETH Zürich, Educational Development and Technology (LET)
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
package ch.ethz.seb.sebserver.gui;
import java.util.HashMap;
import java.util.Map;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpSession;
import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.application.AbstractEntryPoint;
import org.eclipse.rap.rwt.application.Application;
import org.eclipse.rap.rwt.application.ApplicationConfiguration;
import org.eclipse.rap.rwt.application.EntryPoint;
import org.eclipse.rap.rwt.application.EntryPointFactory;
import org.eclipse.rap.rwt.client.WebClient;
2019-08-14 08:11:01 +02:00
import org.eclipse.rap.rwt.internal.theme.ThemeUtil;
import org.eclipse.rap.rwt.service.ServiceManager;
2019-01-28 16:58:06 +01:00
import org.eclipse.swt.widgets.Composite;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;
import ch.ethz.seb.sebserver.gui.service.remote.download.DownloadService;
2019-01-28 16:58:06 +01:00
import ch.ethz.seb.sebserver.gui.service.remote.webservice.auth.AuthorizationContextHolder;
import ch.ethz.seb.sebserver.gui.service.remote.webservice.auth.SEBServerAuthorizationContext;
public class RAPConfiguration implements ApplicationConfiguration {
2019-08-14 08:11:01 +02:00
private static final String DEFAULT_THEME_NAME = "sebserver";
2019-01-28 16:58:06 +01:00
private static final Logger log = LoggerFactory.getLogger(RAPConfiguration.class);
@Override
public void configure(final Application application) {
2019-02-27 12:44:03 +01:00
try {
2019-08-14 08:11:01 +02:00
// TODO get file path from properties
//application.addStyleSheet(RWT.DEFAULT_THEME_ID, "static/css/sebserver.css");
application.addStyleSheet(DEFAULT_THEME_NAME, "resource/theme/default.css");
application.addStyleSheet(DEFAULT_THEME_NAME, "static/css/sebserver.css");
application.addStyleSheet("sms", "resource/theme/default.css");
application.addStyleSheet("sms", "static/css/sms.css");
2019-02-27 12:44:03 +01:00
final Map<String, String> properties = new HashMap<>();
properties.put(WebClient.PAGE_TITLE, "SEB Server");
properties.put(WebClient.BODY_HTML, "<big>Loading Application<big>");
2019-08-14 08:11:01 +02:00
properties.put(WebClient.THEME_ID, DEFAULT_THEME_NAME);
2019-02-27 12:44:03 +01:00
// properties.put(WebClient.FAVICON, "icons/favicon.png");
2019-08-14 08:11:01 +02:00
application.addEntryPoint("/gui", new RAPSpringEntryPointFactory(), properties);
2019-02-27 12:44:03 +01:00
} catch (final RuntimeException re) {
throw re;
2019-01-28 16:58:06 +01:00
} catch (final Exception e) {
log.error("Error during CSS parsing. Please check the custom CSS files for errors.", e);
}
}
public static interface EntryPointService {
void loadLoginPage(final Composite parent);
void loadMainPage(final Composite parent);
}
2019-08-14 08:11:01 +02:00
public static final class RAPSpringEntryPointFactory implements EntryPointFactory {
2019-01-28 16:58:06 +01:00
2019-08-14 08:11:01 +02:00
private boolean initialized = false;
2019-01-28 16:58:06 +01:00
@Override
public EntryPoint create() {
2019-08-14 08:11:01 +02:00
2019-01-28 16:58:06 +01:00
return new AbstractEntryPoint() {
private static final long serialVersionUID = -1299125117752916270L;
@Override
protected void createContents(final Composite parent) {
final HttpSession httpSession = RWT
.getUISession(parent.getDisplay())
.getHttpSession();
log.debug("Create new GUI entrypoint. HttpSession: " + httpSession);
if (httpSession == null) {
log.error("HttpSession not available from RWT.getUISession().getHttpSession()");
throw new IllegalStateException(
"HttpSession not available from RWT.getUISession().getHttpSession()");
}
2019-08-14 08:11:01 +02:00
final Object themeId = httpSession.getAttribute("themeId");
if (themeId != null) {
ThemeUtil.setCurrentThemeId(RWT.getUISession(parent.getDisplay()), String.valueOf(themeId));
parent.redraw();
parent.layout(true);
parent.redraw();
}
2019-01-28 16:58:06 +01:00
final WebApplicationContext webApplicationContext = getWebApplicationContext(httpSession);
initSpringBasedRAPServices(webApplicationContext);
2019-01-28 16:58:06 +01:00
final EntryPointService entryPointService = webApplicationContext
.getBean(EntryPointService.class);
if (isAuthenticated(httpSession, webApplicationContext)) {
entryPointService.loadMainPage(parent);
} else {
entryPointService.loadLoginPage(parent);
}
}
};
}
private void initSpringBasedRAPServices(final WebApplicationContext webApplicationContext) {
2019-08-14 08:11:01 +02:00
if (!this.initialized) {
try {
final ServiceManager manager = RWT.getServiceManager();
final DownloadService downloadService = webApplicationContext.getBean(DownloadService.class);
manager.registerServiceHandler(DownloadService.DOWNLOAD_SERVICE_NAME, downloadService);
this.initialized = true;
} catch (final IllegalArgumentException iae) {
log.warn("Failed to register DownloadService on ServiceManager. Already registered: ", iae);
}
}
}
2019-01-28 16:58:06 +01:00
private boolean isAuthenticated(
final HttpSession httpSession,
final WebApplicationContext webApplicationContext) {
final AuthorizationContextHolder authorizationContextHolder = webApplicationContext
.getBean(AuthorizationContextHolder.class);
final SEBServerAuthorizationContext authorizationContext = authorizationContextHolder
.getAuthorizationContext(httpSession);
return authorizationContext.isValid() && authorizationContext.isLoggedIn();
}
private WebApplicationContext getWebApplicationContext(final HttpSession httpSession) {
try {
final ServletContext servletContext = httpSession.getServletContext();
log.debug("Initialize Spring-Context on Servlet-Context: " + servletContext);
2019-08-14 08:11:01 +02:00
return WebApplicationContextUtils
.getRequiredWebApplicationContext(servletContext);
2019-01-28 16:58:06 +01:00
2019-09-10 10:26:07 +02:00
} catch (final RuntimeException e) {
log.error("Failed to initialize Spring-Context on HttpSession: " + httpSession);
throw e;
2019-01-28 16:58:06 +01:00
} catch (final Exception e) {
log.error("Failed to initialize Spring-Context on HttpSession: " + httpSession);
throw new RuntimeException("Failed to initialize Spring-Context on HttpSession: " + httpSession);
}
}
};
}