- Deliver theme files using JAX-RS
- ACSObjectCategoryForm now works again (#2800)


git-svn-id: https://svn.libreccm.org/ccm/ccm_ng@5218 8810af33-2d31-482b-a856-94f89814c4df
ccm-docs
jensp 2018-01-25 14:28:24 +00:00
parent 57fbaacc54
commit 488291d174
6 changed files with 220 additions and 27 deletions

View File

@ -27,6 +27,7 @@ import org.libreccm.core.CcmObjectRepository;
import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;
import javax.enterprise.context.RequestScoped;
@ -42,10 +43,10 @@ class ACSObjectCategoryController {
@Inject
private CategoryRepository categoryRepo;
@Inject
private CategoryManager categoryManager;
@Inject
private CcmObjectRepository ccmObjectRepo;
@ -81,19 +82,19 @@ class ACSObjectCategoryController {
object.getObjectId())));
final Category cat = categoryRepo
.findById(category.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.findById(category.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No Category with ID %d in the database.",
category.getObjectId())));
categoryManager.addObjectToCategory(ccmObject, cat);
}
@Transactional(Transactional.TxType.REQUIRED)
protected void removeObjectFromCategory(final CcmObject object,
final Category category)
final Category category)
throws ObjectNotAssignedToCategoryException {
Objects.requireNonNull(object);
Objects.requireNonNull(category);
@ -104,12 +105,24 @@ class ACSObjectCategoryController {
object.getObjectId())));
final Category cat = categoryRepo
.findById(category.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.findById(category.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No Category with ID %d in the database.",
category.getObjectId())));
categoryManager.removeObjectFromCategory(ccmObject, cat);
}
@Transactional(Transactional.TxType.REQUIRED)
protected long getParentCategoryId(final Category forCategory) {
final Category category = categoryRepo
.findById(forCategory.getObjectId())
.orElseThrow(() -> new IllegalArgumentException(String
.format("No Category with ID %d in the database.",
forCategory.getObjectId())));
return category.getParentCategory().getObjectId();
}
}

View File

@ -194,7 +194,11 @@ public abstract class ACSObjectCategoryForm extends Form {
final Category category) {
if (category.getParentCategory() != null) {
ancestorCats.add(category.getParentCategory().getObjectId());
final CdiUtil cdiUtil = CdiUtil.createCdiUtil();
final ACSObjectCategoryController controller = cdiUtil
.findBean(ACSObjectCategoryController.class);
ancestorCats
.add(controller.getParentCategoryId(category));
}
}

View File

@ -0,0 +1,95 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.theming;
import java.io.BufferedReader;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import javax.enterprise.context.RequestScoped;
import javax.inject.Inject;
import javax.ws.rs.GET;
import javax.ws.rs.NotFoundException;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@RequestScoped
@Path("/{theme}")
public class ThemeFiles {
@Inject
private Themes themes;
@GET
@Path("/{path}")
public Response getFile(@PathParam("theme") final String theme,
@PathParam("path") final String path) {
final ThemeInfo info = themes
.getTheme(theme, ThemeVersion.LIVE)
.orElseThrow(() -> new NotFoundException(String
.format("Theme \"%s\" does not exist.", theme)));
final InputStream inputStream = themes
.getFileFromTheme(info, path)
.orElseThrow(() -> new NotFoundException(String
.format("The file \"%s\" does exist in the theme \"%s\".",
path,
theme)));
final MediaType mediaType = getMediaTypeFromPath(path);
final BufferedReader reader = new BufferedReader(
new InputStreamReader(inputStream));
final ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
try {
int value = reader.read();
while (value != -1) {
outputStream.write(value);
value = reader.read();
}
} catch (IOException ex) {
throw new WebApplicationException(ex);
}
final byte[] data = outputStream.toByteArray();
return Response.ok(data, mediaType).build();
}
private MediaType getMediaTypeFromPath(final String path) {
if (path.endsWith(".css")) {
return new MediaType("text", "css");
} else {
return MediaType.WILDCARD_TYPE;
}
}
}

View File

@ -23,6 +23,7 @@ import org.apache.logging.log4j.Logger;
import org.libreccm.core.UnexpectedErrorException;
import org.libreccm.pagemodel.PageModel;
import java.io.InputStream;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
@ -144,4 +145,36 @@ public class Themes implements Serializable {
return processor.process(page, theme, provider);
}
/**
* Retrieve a file from a theme using the {@link ThemeProvider} for the
* theme.
*
* @param theme The theme from which the file is retrieved.
* @param path The path of the file relative to the root directory of the
* theme.
*
* @return An {@link Optional} containing an {@link InputStream} for reading
* the contents of the file or an empty {@link Optional} if the file
* was not found.
*/
public Optional<InputStream> getFileFromTheme(final ThemeInfo theme,
final String path) {
final Instance<? extends ThemeProvider> forTheme = providers.select(
theme.getProvider());
if (forTheme.isUnsatisfied()) {
LOGGER.error("ThemeProvider \"{}\" not found.",
theme.getProvider().getName());
throw new UnexpectedErrorException(String.format(
"ThemeProvider \"%s\" not found.",
theme.getProvider().getName()));
}
final ThemeProvider provider = forTheme.get();
return provider.getThemeFileAsStream(theme.getName(),
theme.getVersion(),
path);
}
}

View File

@ -0,0 +1,43 @@
/*
* Copyright (C) 2018 LibreCCM Foundation.
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public
* License as published by the Free Software Foundation; either
* version 2.1 of the License, or (at your option) any later version.
*
* This library is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston,
* MA 02110-1301 USA
*/
package org.libreccm.theming;
import java.util.HashSet;
import java.util.Set;
import javax.ws.rs.ApplicationPath;
import javax.ws.rs.core.Application;
/**
*
* @author <a href="mailto:jens.pelzetter@googlemail.com">Jens Pelzetter</a>
*/
@ApplicationPath("/theming")
public class ThemesService extends Application {
@Override
public Set<Class<?>> getClasses() {
final Set<Class<?>> classes = new HashSet<>();
classes.add(ThemeFiles.class);
return classes;
}
}

View File

@ -4,31 +4,36 @@
xmlns:ccm="http://xmlns.libreccm.org"
exclude-result-prefixes="ccm xsl">
<xsl:output method="html"
doctype-system="about:legacy-compat"
indent="yes"
encoding="utf-8"/>
<xsl:output method="html"
doctype-system="about:legacy-compat"
indent="yes"
encoding="utf-8"/>
<xsl:template match="page">
<html>
<head>
<title>Category page</title>
<link rel="stylesheet" href="/themes/ccm/style.css" />
<link rel="stylesheet" href="{ccm:getContextPath()}/theming/ccm/style.css" />
</head>
<body>
<main>
<ul class="news">
<xsl:for-each select="/page/newsList/items">
<span>
<pre>
<xsl:value-of select="count(./attachments[name='.images']/attachments[1])" />
</pre>
<img src="/content-sections/info/images/{./attachments[name='.images']/attachments[0]/asset/uuid}" width="354" height="286" alt="" />
</span>
<span>
<xsl:value-of select="./title" />
</span>
<li>
<span>
<!--<pre>
<xsl:value-of select="count(./attachments[name='.images']/attachments[1])" />
</pre>-->
<img src="{ccm:getContextPath()}/content-sections/info/images/uuid-{./attachments[name='.images']/attachments[1]/asset/uuid}"
width="354"
height="286"
alt="" />
</span>
<span>
<xsl:value-of select="./title" />
</span>
</li>
</xsl:for-each>
</ul>
<div class="boxes">
@ -38,7 +43,7 @@
<xsl:value-of select="./title" />
</h1>
<p>
<img src="/content-sections/info/images/{./attachments[name='.images']/attachments[0]/asset/uuid}" alt="" />
<img src="{ccm:getContextPath()}/content-sections/info/images/uuid-{./attachments[name='.images']/attachments[1]/asset/uuid}" alt="" />
<xsl:value-of select="./description" />
</p>
</div>