From 2445e2ab93f79d90fb01481082cebef63d3e33bf Mon Sep 17 00:00:00 2001 From: Mathieu Baudier Date: Wed, 13 Jan 2021 12:36:16 +0100 Subject: [PATCH] Introduce fonts servlet. --- publishing/org.argeo.publishing.ui/.project | 5 ++ .../OSGI-INF/fontsServlet.xml | 10 ++++ publishing/org.argeo.publishing.ui/bnd.bnd | 3 ++ .../publishing/servlet/FontsServlet.java | 51 +++++++++++++++++++ 4 files changed, 69 insertions(+) create mode 100644 publishing/org.argeo.publishing.ui/OSGI-INF/fontsServlet.xml create mode 100644 publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/FontsServlet.java diff --git a/publishing/org.argeo.publishing.ui/.project b/publishing/org.argeo.publishing.ui/.project index 81f1cc1..5e90066 100644 --- a/publishing/org.argeo.publishing.ui/.project +++ b/publishing/org.argeo.publishing.ui/.project @@ -20,6 +20,11 @@ + + org.eclipse.pde.ds.core.builder + + + org.eclipse.pde.PluginNature diff --git a/publishing/org.argeo.publishing.ui/OSGI-INF/fontsServlet.xml b/publishing/org.argeo.publishing.ui/OSGI-INF/fontsServlet.xml new file mode 100644 index 0000000..6ca1c41 --- /dev/null +++ b/publishing/org.argeo.publishing.ui/OSGI-INF/fontsServlet.xml @@ -0,0 +1,10 @@ + + + + + + + + + + diff --git a/publishing/org.argeo.publishing.ui/bnd.bnd b/publishing/org.argeo.publishing.ui/bnd.bnd index a321ccb..8d34250 100644 --- a/publishing/org.argeo.publishing.ui/bnd.bnd +++ b/publishing/org.argeo.publishing.ui/bnd.bnd @@ -8,3 +8,6 @@ org.osgi.framework,\ Provide-Capability:\ cms.datamodel; name=docbook; cnd=/org/argeo/docbook/ui/docbook.cnd; abstract=true + +Service-Component:\ +OSGI-INF/fontsServlet.xml diff --git a/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/FontsServlet.java b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/FontsServlet.java new file mode 100644 index 0000000..5bc6d0c --- /dev/null +++ b/publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/FontsServlet.java @@ -0,0 +1,51 @@ +package org.argeo.publishing.servlet; + +import java.io.IOException; +import java.io.InputStream; +import java.util.Collections; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletException; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.io.IOUtils; +import org.argeo.cms.ui.CmsTheme; + +/** Serves fonts locally. */ +public class FontsServlet extends HttpServlet { + private static final long serialVersionUID = 6009572962850708537L; + private Map themes = Collections.synchronizedMap(new HashMap<>()); + + @Override + protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { + String font = req.getPathInfo(); + font = font.substring(1, font.length()); + for (CmsTheme theme : themes.values()) { + for (String fontPath : theme.getFontsPaths()) { + if (fontPath.endsWith(font)) { + if (font.endsWith(".woff")) + resp.setContentType("font/woff"); + else if (font.endsWith(".woff2")) + resp.setContentType("font/woff2"); + try (InputStream in = theme.loadPath(fontPath)) { + IOUtils.copy(in, resp.getOutputStream()); + return; + } + } + } + } + resp.setStatus(404); + } + + public void addTheme(CmsTheme theme, Map properties) { + themes.put(theme.getThemeId(), theme); + } + + public void removeTheme(CmsTheme theme, Map properties) { + themes.remove(theme.getThemeId()); + } + +} -- 2.30.2