Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
72 changes: 4 additions & 68 deletions src/main/java/de/focus_shift/launchpad/core/App.java
Original file line number Diff line number Diff line change
@@ -1,72 +1,8 @@
package de.focus_shift.launchpad.core;

import java.net.URL;
import java.util.Objects;
import java.util.Optional;

public final class App {

private final URL url;
private final AppName appName;
private final String icon;
private final String authority;

App(URL url, AppName appName, String icon) {
this(url, appName, icon, null);
}

App(URL url, AppName appName, String icon, String authority) {
this.url = url;
this.appName = appName;
this.icon = icon;
this.authority = authority;
}

public URL getUrl() {
return url;
}

public AppName getAppName() {
return appName;
}

public String getIcon() {
return icon;
}

public Optional<String> getAuthority() {
return Optional.ofNullable(authority);
}

@Override
public String toString() {
return "App{"
+ "url="
+ url
+ ", appName="
+ appName
+ ", icon='"
+ icon
+ '\''
+ ", authority='"
+ authority
+ '\''
+ '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
App app = (App) o;
return Objects.equals(url, app.url)
&& Objects.equals(appName, app.appName)
&& Objects.equals(icon, app.icon)
&& Objects.equals(authority, app.authority);
}

@Override
public int hashCode() {
return Objects.hash(url, appName, icon, authority);
}
}
/**
* @param authority required to see the app, or {@code null} if everyone may see it
*/
public record App(URL url, AppName appName, String icon, String authority) {}
57 changes: 1 addition & 56 deletions src/main/java/de/focus_shift/launchpad/core/AppDto.java
Original file line number Diff line number Diff line change
@@ -1,58 +1,3 @@
package de.focus_shift.launchpad.core;

import java.util.Objects;

public class AppDto {

private final String url;
private final String name;
private final String icon;

AppDto(String url, String name, String icon) {
this.url = url;
this.name = name;
this.icon = icon;
}

public String getUrl() {
return url;
}

public String getName() {
return name;
}

public String getIcon() {
return icon;
}

@Override
public String toString() {
return "AppDto{"
+ "url='"
+ url
+ '\''
+ ", name='"
+ name
+ '\''
+ ", icon='"
+ icon
+ '\''
+ '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AppDto appDto = (AppDto) o;
return Objects.equals(url, appDto.url)
&& Objects.equals(name, appDto.name)
&& Objects.equals(icon, appDto.icon);
}

@Override
public int hashCode() {
return Objects.hash(url, name, icon);
}
}
public record AppDto(String url, String name, String icon) {}
29 changes: 1 addition & 28 deletions src/main/java/de/focus_shift/launchpad/core/AppName.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,39 +2,12 @@

import java.util.Locale;
import java.util.Map;
import java.util.Objects;

public final class AppName {

private final String defaultName;
private final Map<Locale, String> names;

AppName(String defaultName, Map<Locale, String> names) {
this.defaultName = defaultName;
this.names = names;
}
public record AppName(String defaultName, Map<Locale, String> names) {

public String get(Locale locale) {
// browsers send e.g. en-US while names are configured as en
return names.getOrDefault(
locale, names.getOrDefault(Locale.forLanguageTag(locale.getLanguage()), defaultName));
}

@Override
public String toString() {
return "AppName{" + "defaultName='" + defaultName + '\'' + ", names=" + names + '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
AppName appName = (AppName) o;
return Objects.equals(defaultName, appName.defaultName) && Objects.equals(names, appName.names);
}

@Override
public int hashCode() {
return Objects.hash(defaultName, names);
}
}
32 changes: 1 addition & 31 deletions src/main/java/de/focus_shift/launchpad/core/Launchpad.java
Original file line number Diff line number Diff line change
@@ -1,35 +1,5 @@
package de.focus_shift.launchpad.core;

import java.util.List;
import java.util.Objects;

public final class Launchpad {

private final List<App> apps;

Launchpad(List<App> apps) {
this.apps = apps;
}

public List<App> getApps() {
return apps;
}

@Override
public String toString() {
return "Launchpad{" + "apps=" + apps + '}';
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
if (o == null || getClass() != o.getClass()) return false;
Launchpad launchpad = (Launchpad) o;
return Objects.equals(apps, launchpad.apps);
}

@Override
public int hashCode() {
return Objects.hash(apps);
}
}
public record Launchpad(List<App> apps) {}
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ LaunchpadControllerAdvice launchpadControllerAdvice(
LaunchpadService launchpadService(
LaunchpadConfigProperties launchpadConfigProperties,
LaunchpadAppUrlCustomizer appUrlCustomizer) {
return new LaunchpadServiceImpl(launchpadConfigProperties, appUrlCustomizer);
return new LaunchpadService(launchpadConfigProperties, appUrlCustomizer);
}

@Bean
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,8 @@ public void addModelAttribute(Model model, Locale locale, Authentication authent
final Launchpad launchpad = launchpadService.getLaunchpad(authentication);

final List<AppDto> appDtos =
launchpad.getApps().stream()
.map(app ->
new AppDto(app.getUrl().toString(), app.getAppName().get(locale), app.getIcon()))
launchpad.apps().stream()
.map(app -> new AppDto(app.url().toString(), app.appName().get(locale), app.icon()))
.toList();

if (!appDtos.isEmpty()) {
Expand Down
67 changes: 65 additions & 2 deletions src/main/java/de/focus_shift/launchpad/core/LaunchpadService.java
Original file line number Diff line number Diff line change
@@ -1,8 +1,71 @@
package de.focus_shift.launchpad.core;

import static java.lang.invoke.MethodHandles.lookup;

import de.focus_shift.launchpad.api.LaunchpadAppUrlCustomizer;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.Optional;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.GrantedAuthority;

class LaunchpadService {

private static final Logger LOG = LoggerFactory.getLogger(lookup().lookupClass());

private final LaunchpadConfigProperties appsProperties;
private final LaunchpadAppUrlCustomizer appUrlCustomizer;

LaunchpadService(
LaunchpadConfigProperties appsProperties, LaunchpadAppUrlCustomizer appUrlCustomizer) {
this.appsProperties = appsProperties;
this.appUrlCustomizer = appUrlCustomizer;
}

Launchpad getLaunchpad(Authentication authentication) {
return new Launchpad(getApplications(authentication));
}

private List<App> getApplications(Authentication authentication) {
return appsProperties.getApps().stream()
.map(this::toApp)
.flatMap(Optional::stream)
.filter(app -> isAllowed(app, authentication))
.toList();
}

private boolean isAllowed(App app, Authentication authentication) {
if (app.authority() == null) {
return true;
}
// null when nobody is logged in
return authentication != null
&& authentication.getAuthorities().stream()
.map(GrantedAuthority::getAuthority)
.anyMatch(app.authority()::equals);
}

interface LaunchpadService {
private Optional<App> toApp(LaunchpadConfigProperties.App app) {
return getAppUrl(app)
.map(url -> {
final String defaultName = app.getName().get(appsProperties.getNameDefaultLocale());
return new App(
url, new AppName(defaultName, app.getName()), app.getIcon(), app.getAuthority());
});
}

Launchpad getLaunchpad(Authentication authentication);
private Optional<URL> getAppUrl(LaunchpadConfigProperties.App app) {
try {
return Optional.of(appUrlCustomizer.customize(app.getUrl()));
} catch (MalformedURLException | IllegalArgumentException e) {
LOG.warn(
"ignoring launchpad app: could not build URL from url={}: {}",
app.getUrl(),
e.getMessage());
return Optional.empty();
}
}
}

This file was deleted.

Loading