Some bugfixes for themes

pull/11/head
Jens Pelzetter 2022-01-03 20:41:37 +01:00
parent d941a795c9
commit 0c0e1135c0
7 changed files with 97 additions and 49 deletions

View File

@ -62,10 +62,13 @@ public class FileSystemThemeProvider implements ThemeProvider {
); );
private static final String BASE_PATH = "/themes"; private static final String BASE_PATH = "/themes";
private static final String DRAFT_THEMES_PATH = BASE_PATH + "/draft"; private static final String DRAFT_THEMES_PATH = BASE_PATH + "/draft";
private static final String LIVE_THEMES_PATH = BASE_PATH + "/live"; private static final String LIVE_THEMES_PATH = BASE_PATH + "/live";
private static final String THEME_JSON = "%s/theme.json"; private static final String THEME_JSON = "%s/theme.json";
private static final String THEME_XML = "%s/theme.xml"; private static final String THEME_XML = "%s/theme.xml";
@Inject @Inject
@ -83,8 +86,12 @@ public class FileSystemThemeProvider implements ThemeProvider {
} }
@Override @Override
public List<ThemeInfo> getThemes() { public String getClassName() {
return FileSystemThemeProvider.class.getName();
}
@Override
public List<ThemeInfo> getThemes() {
try { try {
if (!ccmFiles.isDirectory(BASE_PATH) if (!ccmFiles.isDirectory(BASE_PATH)
|| !ccmFiles.isDirectory(DRAFT_THEMES_PATH)) { || !ccmFiles.isDirectory(DRAFT_THEMES_PATH)) {
@ -101,10 +108,10 @@ public class FileSystemThemeProvider implements ThemeProvider {
.collect(Collectors.toList()); .collect(Collectors.toList());
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} catch(CcmFilesNotConfiguredException ex) { } catch (CcmFilesNotConfiguredException ex) {
LOGGER.warn(ex); LOGGER.warn(ex);
return Collections.emptyList(); return Collections.emptyList();
} }
@ -128,11 +135,11 @@ public class FileSystemThemeProvider implements ThemeProvider {
.map(info -> info.get()) .map(info -> info.get())
.collect(Collectors.toList()); .collect(Collectors.toList());
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} catch(CcmFilesNotConfiguredException ex) { } catch (CcmFilesNotConfiguredException ex) {
LOGGER.warn(ex); LOGGER.warn(ex);
return Collections.emptyList(); return Collections.emptyList();
} }
@ -172,8 +179,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
ccmFiles.createDirectory(String.format(DRAFT_THEMES_PATH + "/%s", ccmFiles.createDirectory(String.format(DRAFT_THEMES_PATH + "/%s",
themeName)); themeName));
} catch (FileAccessException } catch (FileAccessException
| FileAlreadyExistsException | FileAlreadyExistsException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -187,7 +194,7 @@ public class FileSystemThemeProvider implements ThemeProvider {
+ ThemeConstants.THEME_MANIFEST_JSON, + ThemeConstants.THEME_MANIFEST_JSON,
themeName)); themeName));
} catch (FileAccessException } catch (FileAccessException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -231,9 +238,9 @@ public class FileSystemThemeProvider implements ThemeProvider {
themeName), themeName),
true); true);
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| DirectoryNotEmptyException | DirectoryNotEmptyException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
} }
@ -270,8 +277,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
} }
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -311,8 +318,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
return Optional.of(ccmFiles.createInputStream(filePath)); return Optional.of(ccmFiles.createInputStream(filePath));
} }
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -330,7 +337,7 @@ public class FileSystemThemeProvider implements ThemeProvider {
return ccmFiles.createOutputStream(filePath); return ccmFiles.createOutputStream(filePath);
} catch (FileAccessException } catch (FileAccessException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -345,9 +352,9 @@ public class FileSystemThemeProvider implements ThemeProvider {
try { try {
ccmFiles.deleteFile(filePath, true); ccmFiles.deleteFile(filePath, true);
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| DirectoryNotEmptyException | DirectoryNotEmptyException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -378,8 +385,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
ccmFiles.createDirectory(LIVE_THEMES_PATH); ccmFiles.createDirectory(LIVE_THEMES_PATH);
} }
} catch (FileAccessException } catch (FileAccessException
| InsufficientPermissionsException | InsufficientPermissionsException
| FileAlreadyExistsException ex) { | FileAlreadyExistsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
@ -395,10 +402,10 @@ public class FileSystemThemeProvider implements ThemeProvider {
ccmFiles.moveFile(liveThemePathTmp, liveThemePath); ccmFiles.moveFile(liveThemePathTmp, liveThemePath);
} catch (DirectoryNotEmptyException } catch (DirectoryNotEmptyException
| FileAccessException | FileAccessException
| FileAlreadyExistsException | FileAlreadyExistsException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
} }
@ -411,9 +418,9 @@ public class FileSystemThemeProvider implements ThemeProvider {
try { try {
ccmFiles.deleteFile(liveThemePath, true); ccmFiles.deleteFile(liveThemePath, true);
} catch (DirectoryNotEmptyException } catch (DirectoryNotEmptyException
| FileAccessException | FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
} }
@ -437,12 +444,15 @@ public class FileSystemThemeProvider implements ThemeProvider {
private Optional<ThemeInfo> readInfo(final String themeName) { private Optional<ThemeInfo> readInfo(final String themeName) {
final ThemeManifest manifest; final ThemeManifest manifest;
final ThemeVersion themeVersion;
try { try {
final String jsonPath = String.format( final String jsonPath = String.format(
DRAFT_THEMES_PATH + "/" + THEME_JSON, themeName); DRAFT_THEMES_PATH + "/" + THEME_JSON, themeName
);
final String xmlPath = String.format( final String xmlPath = String.format(
DRAFT_THEMES_PATH + "/" + THEME_XML, themeName); DRAFT_THEMES_PATH + "/" + THEME_XML, themeName
);
if (ccmFiles.existsFile(jsonPath)) { if (ccmFiles.existsFile(jsonPath)) {
final InputStream inputStream = ccmFiles final InputStream inputStream = ccmFiles
@ -455,15 +465,32 @@ public class FileSystemThemeProvider implements ThemeProvider {
} else { } else {
return Optional.empty(); return Optional.empty();
} }
final String liveJsonPath = String.format(
LIVE_THEMES_PATH + "/" + THEME_JSON, themeName
);
final String liveXmlPath = String.format(
LIVE_THEMES_PATH + "/" + THEME_XML, themeName
);
final boolean hasLiveVersion
= ccmFiles.existsFile(liveJsonPath)
|| ccmFiles.existsFile(liveXmlPath);
if (hasLiveVersion) {
themeVersion = ThemeVersion.LIVE;
} else {
themeVersion = ThemeVersion.DRAFT;
}
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }
final ThemeInfo themeInfo = new ThemeInfo(); final ThemeInfo themeInfo = new ThemeInfo();
themeInfo.setManifest(manifest); themeInfo.setManifest(manifest);
themeInfo.setProvider(FileSystemThemeProvider.class);
themeInfo.setVersion(themeVersion);
return Optional.of(themeInfo); return Optional.of(themeInfo);
} }
@ -473,10 +500,10 @@ public class FileSystemThemeProvider implements ThemeProvider {
final String path; final String path;
if (themePath.endsWith("/") if (themePath.endsWith("/")
|| filePath.startsWith("/")) { || filePath.startsWith("/")) {
path = String.join("", themePath, filePath); path = String.join("", themePath, filePath);
} else { } else {
path= String.join("/", themePath, filePath); path = String.join("/", themePath, filePath);
} }
final String name; final String name;
@ -496,8 +523,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
return fileInfo; return fileInfo;
} catch (FileAccessException } catch (FileAccessException
| FileDoesNotExistException | FileDoesNotExistException
| InsufficientPermissionsException ex) { | InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex); throw new UnexpectedErrorException(ex);
} }

View File

@ -92,6 +92,11 @@ public class StaticThemeProvider implements ThemeProvider {
return "StaticThemeProvider"; return "StaticThemeProvider";
} }
@Override
public String getClassName() {
return StaticThemeProvider.class.getName();
}
@Override @Override
public List<ThemeInfo> getThemes() { public List<ThemeInfo> getThemes() {

View File

@ -38,10 +38,20 @@ public interface ThemeProvider extends Serializable {
/** /**
* A human readable name for the {@code ThemeProvider} implementation. * A human readable name for the {@code ThemeProvider} implementation.
*
* @return * @return
*/ */
String getName(); String getName();
/**
* The class name of the theme provider. This method is necessary because
* using {@link Object#getClass() } may return the class name of the CDI
* proxy and not of the implementing class.
*
* @return The class name of the {@code ThemeProvider} implementation.
*/
String getClassName();
/** /**
* Provides a list of all themes provided by this theme provider. The list * Provides a list of all themes provided by this theme provider. The list
* should be ordered by the name of the theme. * should be ordered by the name of the theme.

View File

@ -99,9 +99,9 @@ public class Themes implements Serializable {
* @return An {@link Optional} with informations about theme {@code theme} * @return An {@link Optional} with informations about theme {@code theme}
* or an empty optional if there is no such theme. * or an empty optional if there is no such theme.
*/ */
public Optional<ThemeInfo> getTheme(final String name, public Optional<ThemeInfo> getTheme(
final ThemeVersion version) { final String name, final ThemeVersion version
) {
for (final ThemeProvider provider : providers) { for (final ThemeProvider provider : providers) {
if (provider.providesTheme(name, version)) { if (provider.providesTheme(name, version)) {
return provider.getThemeInfo(name, version); return provider.getThemeInfo(name, version);

View File

@ -76,6 +76,11 @@ public class DatabaseThemeProvider implements ThemeProvider {
return "DatabaseThemeProvider"; return "DatabaseThemeProvider";
} }
@Override
public String getClassName() {
return DatabaseThemeProvider.class.getName();
}
@Override @Override
@Transactional(Transactional.TxType.REQUIRED) @Transactional(Transactional.TxType.REQUIRED)
public List<ThemeInfo> getThemes() { public List<ThemeInfo> getThemes() {

View File

@ -206,7 +206,8 @@ public class ThemesMvc {
private ThemeInfo getTheme( private ThemeInfo getTheme(
final Site site, final Site site,
final String theme, final String theme,
final ThemeVersion themeVersion) { final ThemeVersion themeVersion
) {
if (DEFAULT_THEME_PARAM.equals(theme)) { if (DEFAULT_THEME_PARAM.equals(theme)) {
return themes return themes
.getTheme(site.getDefaultTheme(), themeVersion) .getTheme(site.getDefaultTheme(), themeVersion)

View File

@ -79,7 +79,7 @@ public class ThemesModel {
.filter(ThemeProvider::supportsDraftThemes) .filter(ThemeProvider::supportsDraftThemes)
.collect( .collect(
Collectors.toMap( Collectors.toMap(
provider -> provider.getClass().getName(), provider -> provider.getClassName(),
provider -> provider.getName() provider -> provider.getName()
) )
); );