Introduce fonts servlet.
authorMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Jan 2021 11:36:16 +0000 (12:36 +0100)
committerMathieu Baudier <mbaudier@argeo.org>
Wed, 13 Jan 2021 11:36:16 +0000 (12:36 +0100)
publishing/org.argeo.publishing.ui/.project
publishing/org.argeo.publishing.ui/OSGI-INF/fontsServlet.xml [new file with mode: 0644]
publishing/org.argeo.publishing.ui/bnd.bnd
publishing/org.argeo.publishing.ui/src/org/argeo/publishing/servlet/FontsServlet.java [new file with mode: 0644]

index 81f1cc18a62c6c62600c8c324a216f391a3f127a..5e9006695130c3ff37fe2154d69fc57ab1890d86 100644 (file)
                        <arguments>
                        </arguments>
                </buildCommand>
+               <buildCommand>
+                       <name>org.eclipse.pde.ds.core.builder</name>
+                       <arguments>
+                       </arguments>
+               </buildCommand>
        </buildSpec>
        <natures>
                <nature>org.eclipse.pde.PluginNature</nature>
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 (file)
index 0000000..6ca1c41
--- /dev/null
@@ -0,0 +1,10 @@
+<?xml version="1.0" encoding="UTF-8"?>
+<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0">
+   <implementation class="org.argeo.publishing.servlet.FontsServlet"/>
+   <service>
+      <provide interface="javax.servlet.Servlet"/>
+   </service>
+   <property name="osgi.http.whiteboard.servlet.pattern" type="String" value="/fonts/*"/>
+   <property name="osgi.http.whiteboard.context.select" type="String" value="(osgi.http.whiteboard.context.name=default)"/>
+   <reference bind="addTheme" cardinality="0..n" interface="org.argeo.cms.ui.CmsTheme" name="CmsTheme" policy="dynamic" unbind="removeTheme"/>
+</scr:component>
index a321ccb8f9884468be8e7f3aba4e4fc0169ec275..8d342507dd87b00f415b256348ebd1f3cf8275e9 100644 (file)
@@ -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 (file)
index 0000000..5bc6d0c
--- /dev/null
@@ -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<String, CmsTheme> 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<String, String> properties) {
+               themes.put(theme.getThemeId(), theme);
+       }
+
+       public void removeTheme(CmsTheme theme, Map<String, String> properties) {
+               themes.remove(theme.getThemeId());
+       }
+
+}