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

@ -49,23 +49,26 @@ import javax.inject.Inject;
/**
* {@link ThemeProvider} implementation that loads themes from the file system
* using {@link CcmFiles}.
*
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
public class FileSystemThemeProvider implements ThemeProvider {
private static final long serialVersionUID = 1L;
private static final Logger LOGGER = LogManager.getLogger(
FileSystemThemeProvider.class
);
private static final String BASE_PATH = "/themes";
private static final String DRAFT_THEMES_PATH = BASE_PATH + "/draft";
private static final String LIVE_THEMES_PATH = BASE_PATH + "/live";
private static final String THEME_JSON = "%s/theme.json";
private static final String THEME_XML = "%s/theme.xml";
@Inject
@ -81,10 +84,14 @@ public class FileSystemThemeProvider implements ThemeProvider {
public String getName() {
return "FileSystemThemeProvider";
}
@Override
public String getClassName() {
return FileSystemThemeProvider.class.getName();
}
@Override
public List<ThemeInfo> getThemes() {
try {
if (!ccmFiles.isDirectory(BASE_PATH)
|| !ccmFiles.isDirectory(DRAFT_THEMES_PATH)) {
@ -101,10 +108,10 @@ public class FileSystemThemeProvider implements ThemeProvider {
.collect(Collectors.toList());
} catch (FileAccessException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
} catch(CcmFilesNotConfiguredException ex) {
} catch (CcmFilesNotConfiguredException ex) {
LOGGER.warn(ex);
return Collections.emptyList();
}
@ -128,11 +135,11 @@ public class FileSystemThemeProvider implements ThemeProvider {
.map(info -> info.get())
.collect(Collectors.toList());
} catch (FileAccessException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
} catch(CcmFilesNotConfiguredException ex) {
} catch (CcmFilesNotConfiguredException ex) {
LOGGER.warn(ex);
return Collections.emptyList();
}
@ -172,8 +179,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
ccmFiles.createDirectory(String.format(DRAFT_THEMES_PATH + "/%s",
themeName));
} catch (FileAccessException
| FileAlreadyExistsException
| InsufficientPermissionsException ex) {
| FileAlreadyExistsException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
@ -187,7 +194,7 @@ public class FileSystemThemeProvider implements ThemeProvider {
+ ThemeConstants.THEME_MANIFEST_JSON,
themeName));
} catch (FileAccessException
| InsufficientPermissionsException ex) {
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
@ -231,9 +238,9 @@ public class FileSystemThemeProvider implements ThemeProvider {
themeName),
true);
} catch (FileAccessException
| FileDoesNotExistException
| DirectoryNotEmptyException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| DirectoryNotEmptyException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
}
@ -270,8 +277,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
}
} catch (FileAccessException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
@ -311,8 +318,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
return Optional.of(ccmFiles.createInputStream(filePath));
}
} catch (FileAccessException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
@ -330,7 +337,7 @@ public class FileSystemThemeProvider implements ThemeProvider {
return ccmFiles.createOutputStream(filePath);
} catch (FileAccessException
| InsufficientPermissionsException ex) {
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
@ -345,9 +352,9 @@ public class FileSystemThemeProvider implements ThemeProvider {
try {
ccmFiles.deleteFile(filePath, true);
} catch (FileAccessException
| FileDoesNotExistException
| DirectoryNotEmptyException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| DirectoryNotEmptyException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
@ -378,8 +385,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
ccmFiles.createDirectory(LIVE_THEMES_PATH);
}
} catch (FileAccessException
| InsufficientPermissionsException
| FileAlreadyExistsException ex) {
| InsufficientPermissionsException
| FileAlreadyExistsException ex) {
throw new UnexpectedErrorException(ex);
}
@ -395,10 +402,10 @@ public class FileSystemThemeProvider implements ThemeProvider {
ccmFiles.moveFile(liveThemePathTmp, liveThemePath);
} catch (DirectoryNotEmptyException
| FileAccessException
| FileAlreadyExistsException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileAccessException
| FileAlreadyExistsException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
}
@ -411,9 +418,9 @@ public class FileSystemThemeProvider implements ThemeProvider {
try {
ccmFiles.deleteFile(liveThemePath, true);
} catch (DirectoryNotEmptyException
| FileAccessException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileAccessException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
}
@ -437,12 +444,15 @@ public class FileSystemThemeProvider implements ThemeProvider {
private Optional<ThemeInfo> readInfo(final String themeName) {
final ThemeManifest manifest;
final ThemeVersion themeVersion;
try {
final String jsonPath = String.format(
DRAFT_THEMES_PATH + "/" + THEME_JSON, themeName);
DRAFT_THEMES_PATH + "/" + THEME_JSON, themeName
);
final String xmlPath = String.format(
DRAFT_THEMES_PATH + "/" + THEME_XML, themeName);
DRAFT_THEMES_PATH + "/" + THEME_XML, themeName
);
if (ccmFiles.existsFile(jsonPath)) {
final InputStream inputStream = ccmFiles
@ -455,15 +465,32 @@ public class FileSystemThemeProvider implements ThemeProvider {
} else {
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
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}
final ThemeInfo themeInfo = new ThemeInfo();
themeInfo.setManifest(manifest);
themeInfo.setProvider(FileSystemThemeProvider.class);
themeInfo.setVersion(themeVersion);
return Optional.of(themeInfo);
}
@ -473,12 +500,12 @@ public class FileSystemThemeProvider implements ThemeProvider {
final String path;
if (themePath.endsWith("/")
|| filePath.startsWith("/")) {
|| filePath.startsWith("/")) {
path = String.join("", themePath, filePath);
} else {
path= String.join("/", themePath, filePath);
path = String.join("/", themePath, filePath);
}
final String name;
if (path.startsWith(("/"))) {
name = path;
@ -496,8 +523,8 @@ public class FileSystemThemeProvider implements ThemeProvider {
return fileInfo;
} catch (FileAccessException
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
| FileDoesNotExistException
| InsufficientPermissionsException ex) {
throw new UnexpectedErrorException(ex);
}

View File

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

View File

@ -38,10 +38,20 @@ public interface ThemeProvider extends Serializable {
/**
* A human readable name for the {@code ThemeProvider} implementation.
* @return
*
* @return
*/
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
* 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}
* or an empty optional if there is no such theme.
*/
public Optional<ThemeInfo> getTheme(final String name,
final ThemeVersion version) {
public Optional<ThemeInfo> getTheme(
final String name, final ThemeVersion version
) {
for (final ThemeProvider provider : providers) {
if (provider.providesTheme(name, version)) {
return provider.getThemeInfo(name, version);

View File

@ -75,6 +75,11 @@ public class DatabaseThemeProvider implements ThemeProvider {
public String getName() {
return "DatabaseThemeProvider";
}
@Override
public String getClassName() {
return DatabaseThemeProvider.class.getName();
}
@Override
@Transactional(Transactional.TxType.REQUIRED)

View File

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

View File

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