allow configuration of default logo image

This commit is contained in:
anhefti 2019-10-08 12:41:12 +02:00
parent d696c32763
commit e5560ea2f3
4 changed files with 52 additions and 5 deletions

View file

@ -17,6 +17,7 @@
<exclude name="BeanMembersShouldSerialize"/> <exclude name="BeanMembersShouldSerialize"/>
<exclude name="AvoidFieldNameMatchingMethodName"/> <exclude name="AvoidFieldNameMatchingMethodName"/>
<exclude name="NullAssignment"/> <exclude name="NullAssignment"/>
<exclude name="AvoidLiteralsInIfCondition"/>
</rule> </rule>
</ruleset> </ruleset>

View file

@ -27,6 +27,7 @@ public final class Constants {
public static final Character LIST_SEPARATOR_CHAR = ','; public static final Character LIST_SEPARATOR_CHAR = ',';
public static final String LIST_SEPARATOR = ","; public static final String LIST_SEPARATOR = ",";
public static final String EMBEDDED_LIST_SEPARATOR = "|"; public static final String EMBEDDED_LIST_SEPARATOR = "|";
public static final String NO_NAME = "NONE";
public static final String EMPTY_NOTE = "--"; public static final String EMPTY_NOTE = "--";
public static final String FORM_URL_ENCODED_SEPARATOR = "&"; public static final String FORM_URL_ENCODED_SEPARATOR = "&";
public static final String FORM_URL_ENCODED_NAME_VALUE_SEPARATOR = "="; public static final String FORM_URL_ENCODED_NAME_VALUE_SEPARATOR = "=";
@ -75,4 +76,8 @@ public final class Constants {
public static final String OAUTH2_GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials"; public static final String OAUTH2_GRANT_TYPE_CLIENT_CREDENTIALS = "client_credentials";
public static final String OAUTH2_SCOPE_READ = "read"; public static final String OAUTH2_SCOPE_READ = "read";
public static final String OAUTH2_SCOPE_WRITE = "write"; public static final String OAUTH2_SCOPE_WRITE = "write";
public static final int RWT_MOUSE_BUTTON_1 = 1;
public static final int RWT_MOUSE_BUTTON_2 = 2;
public static final int RWT_MOUSE_BUTTON_3 = 3;
} }

View file

@ -9,17 +9,23 @@
package ch.ethz.seb.sebserver.gui; package ch.ethz.seb.sebserver.gui;
import java.io.IOException; import java.io.IOException;
import java.io.InputStreamReader;
import java.io.Reader;
import javax.servlet.RequestDispatcher; import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException; import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse; import javax.servlet.http.HttpServletResponse;
import org.apache.commons.codec.Charsets;
import org.apache.commons.codec.binary.Base64InputStream;
import org.apache.commons.lang3.StringUtils; import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger; import org.slf4j.Logger;
import org.slf4j.LoggerFactory; import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value; import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Lazy; import org.springframework.context.annotation.Lazy;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod; import org.springframework.http.HttpMethod;
import org.springframework.http.HttpStatus; import org.springframework.http.HttpStatus;
@ -28,11 +34,14 @@ import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.security.core.AuthenticationException; import org.springframework.security.core.AuthenticationException;
import org.springframework.security.web.AuthenticationEntryPoint; import org.springframework.security.web.AuthenticationEntryPoint;
import org.springframework.stereotype.Component; import org.springframework.stereotype.Component;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.client.RestTemplate; import org.springframework.web.client.RestTemplate;
import ch.ethz.seb.sebserver.ClientHttpRequestFactoryService; import ch.ethz.seb.sebserver.ClientHttpRequestFactoryService;
import ch.ethz.seb.sebserver.gbl.Constants;
import ch.ethz.seb.sebserver.gbl.api.API; import ch.ethz.seb.sebserver.gbl.api.API;
import ch.ethz.seb.sebserver.gui.service.remote.webservice.auth.WebserviceURIService; import ch.ethz.seb.sebserver.gui.service.remote.webservice.auth.WebserviceURIService;
import ch.ethz.seb.sebserver.gui.widget.ImageUploadSelection;
@Lazy @Lazy
@Component @Component
@ -41,17 +50,50 @@ final class InstitutionalAuthenticationEntryPoint implements AuthenticationEntry
private static final Logger log = LoggerFactory.getLogger(InstitutionalAuthenticationEntryPoint.class); private static final Logger log = LoggerFactory.getLogger(InstitutionalAuthenticationEntryPoint.class);
private final String guiEntryPoint; private final String guiEntryPoint;
private final String defaultLogo;
private final WebserviceURIService webserviceURIService; private final WebserviceURIService webserviceURIService;
private final ClientHttpRequestFactoryService clientHttpRequestFactoryService; private final ClientHttpRequestFactoryService clientHttpRequestFactoryService;
protected InstitutionalAuthenticationEntryPoint( protected InstitutionalAuthenticationEntryPoint(
@Value("${sebserver.gui.entrypoint}") final String guiEntryPoint, @Value("${sebserver.gui.entrypoint}") final String guiEntryPoint,
@Value("${sebserver.gui.defaultLogo:" + Constants.NO_NAME + "}") final String defaultLogoFileName,
final WebserviceURIService webserviceURIService, final WebserviceURIService webserviceURIService,
final ClientHttpRequestFactoryService clientHttpRequestFactoryService) { final ClientHttpRequestFactoryService clientHttpRequestFactoryService,
final ResourceLoader resourceLoader) {
this.guiEntryPoint = guiEntryPoint; this.guiEntryPoint = guiEntryPoint;
this.webserviceURIService = webserviceURIService; this.webserviceURIService = webserviceURIService;
this.clientHttpRequestFactoryService = clientHttpRequestFactoryService; this.clientHttpRequestFactoryService = clientHttpRequestFactoryService;
String _defaultLogo = null;
if (!Constants.NO_NAME.equals(defaultLogoFileName)) {
try {
final String extension = ImageUploadSelection.SUPPORTED_IMAGE_FILES.stream()
.filter(ext -> defaultLogoFileName.endsWith(ext))
.findFirst()
.orElse(null);
if (extension == null) {
throw new IllegalArgumentException("Image of type: " + defaultLogoFileName + " not supported");
}
final Resource resource = resourceLoader.getResource("file:" + defaultLogoFileName);
final Reader reader = new InputStreamReader(
new Base64InputStream(resource.getInputStream(), true),
Charsets.UTF_8);
_defaultLogo = FileCopyUtils.copyToString(reader);
} catch (final Exception e) {
log.warn("Failed to load default logo image from filesystem: {}", defaultLogoFileName);
_defaultLogo = null;
}
this.defaultLogo = _defaultLogo;
} else {
this.defaultLogo = null;
}
} }
@Override @Override
@ -74,7 +116,6 @@ final class InstitutionalAuthenticationEntryPoint implements AuthenticationEntry
response.setStatus(HttpStatus.UNAUTHORIZED.value()); response.setStatus(HttpStatus.UNAUTHORIZED.value());
forwardToEntryPoint(request, response, this.guiEntryPoint); forwardToEntryPoint(request, response, this.guiEntryPoint);
} }
} }
private void forwardToEntryPoint( private void forwardToEntryPoint(
@ -104,7 +145,7 @@ final class InstitutionalAuthenticationEntryPoint implements AuthenticationEntry
private String requestLogoImage(final String institutionalEndpoint) { private String requestLogoImage(final String institutionalEndpoint) {
if (StringUtils.isBlank(institutionalEndpoint)) { if (StringUtils.isBlank(institutionalEndpoint)) {
return null; return this.defaultLogo;
} }
try { try {
@ -135,7 +176,7 @@ final class InstitutionalAuthenticationEntryPoint implements AuthenticationEntry
exchange); exchange);
} }
} catch (final Exception e) { } catch (final Exception e) {
log.error("Failed to verify insitution from requested entrypoint url: {}", log.warn("Failed to verify insitution from requested entrypoint url: {}",
institutionalEndpoint, institutionalEndpoint,
e); e);
} }

View file

@ -160,7 +160,7 @@ public class EntityTable<ROW extends Entity> {
} }
} }
this.table.addListener(SWT.MouseDown, event -> { this.table.addListener(SWT.MouseDown, event -> {
if (event.button == 1) { if (event.button == Constants.RWT_MOUSE_BUTTON_1) {
return; return;
} }
final Rectangle bounds = event.getBounds(); final Rectangle bounds = event.getBounds();