]> git.argeo.org Git - gpl/argeo-suite.git/blob - org.argeo.app.servlet.publish/src/org/argeo/app/servlet/publish/FontsServlet.java
Improve build and deployment
[gpl/argeo-suite.git] / org.argeo.app.servlet.publish / src / org / argeo / app / servlet / publish / FontsServlet.java
1 package org.argeo.app.servlet.publish;
2
3 import java.io.IOException;
4 import java.io.InputStream;
5 import java.util.Collections;
6 import java.util.HashMap;
7 import java.util.Map;
8
9 import javax.servlet.ServletException;
10 import javax.servlet.http.HttpServlet;
11 import javax.servlet.http.HttpServletRequest;
12 import javax.servlet.http.HttpServletResponse;
13
14 import org.apache.commons.io.IOUtils;
15 import org.argeo.api.cms.ux.CmsTheme;
16
17 /** Serves fonts locally. */
18 public class FontsServlet extends HttpServlet {
19 private static final long serialVersionUID = 6009572962850708537L;
20 private Map<String, CmsTheme> themes = Collections.synchronizedMap(new HashMap<>());
21
22 @Override
23 protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
24 String font = req.getPathInfo();
25 font = font.substring(1, font.length());
26 for (CmsTheme theme : themes.values()) {
27 for (String fontPath : theme.getFontsPaths()) {
28 if (fontPath.endsWith(font)) {
29 if (font.endsWith(".woff"))
30 resp.setContentType("font/woff");
31 else if (font.endsWith(".woff2"))
32 resp.setContentType("font/woff2");
33 try (InputStream in = theme.loadPath(fontPath)) {
34 IOUtils.copy(in, resp.getOutputStream());
35 return;
36 }
37 }
38 }
39 }
40 resp.setStatus(404);
41 }
42
43 public void addTheme(CmsTheme theme, Map<String, String> properties) {
44 themes.put(theme.getThemeId(), theme);
45 }
46
47 public void removeTheme(CmsTheme theme, Map<String, String> properties) {
48 themes.remove(theme.getThemeId());
49 }
50
51 }