mirror of
https://github.com/eclipse-sailing-analytics/sailing-analytics.git
synced 2026-09-19 12:15:28 +00:00
Merge branch 'master' into bug5153
# Conflicts: # java/com.sap.sailing.gwt.ui/WEB-INF/web.xml # java/com.sap.sse.debranding/src/com/sap/sse/debranding/ClientConfigurationServlet.java
This commit is contained in:
+133
@@ -0,0 +1,133 @@
|
||||
package com.sap.sse.debranding;
|
||||
|
||||
import java.io.FileNotFoundException;
|
||||
import java.io.IOException;
|
||||
import java.io.InputStream;
|
||||
import java.util.HashMap;
|
||||
import java.util.Map;
|
||||
import java.util.concurrent.ConcurrentHashMap;
|
||||
import java.util.logging.Level;
|
||||
import java.util.logging.Logger;
|
||||
|
||||
import javax.servlet.ServletException;
|
||||
import javax.servlet.http.HttpServlet;
|
||||
import javax.servlet.http.HttpServletRequest;
|
||||
import javax.servlet.http.HttpServletResponse;
|
||||
import javax.ws.rs.core.MediaType;
|
||||
|
||||
import org.apache.commons.io.IOUtils;
|
||||
import org.apache.http.HttpStatus;
|
||||
|
||||
/**
|
||||
* Use ${[variable name]} to get strings replaced within static pages. No escape syntax is currently available. All occurrences of the variables listed below
|
||||
* that are found in the document will be replaced. The following variables are available at the moment:
|
||||
* <table border="1">
|
||||
* <tr>
|
||||
* <th>Variablename</th>
|
||||
* <th>branded value</th>
|
||||
* <th>debranded/whitelabeled</th>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>"SAP"</td>
|
||||
* <td>"SAP "</td>
|
||||
* <td>""</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>"debrandingActive"</td>
|
||||
* <td>"false"</td>
|
||||
* <td>"true"</td>
|
||||
* </tr>
|
||||
* <tr>
|
||||
* <td>"whitelabeled"</td>
|
||||
* <td>""</td>
|
||||
* <td>"-whitelabeled"</td>
|
||||
* </tr>
|
||||
* </table>
|
||||
* <p>
|
||||
*
|
||||
* Register as a servlet for all the URLs that produce such static pages that you'd like to run replacements on. Example
|
||||
* registration in a {@code web.xml} configuration file:
|
||||
*
|
||||
* <pre>
|
||||
* <servlet>
|
||||
* <display-name>ClientConfigurationServlet</display-name>
|
||||
* <servlet-name>ClientConfigurationServlet</servlet-name>
|
||||
* <servlet-class>com.sap.sse.debranding.ClientConfigurationServlet</servlet-class>
|
||||
* </servlet>
|
||||
* <servlet-mapping>
|
||||
* <servlet-name>ClientConfigurationServlet</servlet-name>
|
||||
* <url-pattern>*.html</url-pattern>
|
||||
* </servlet-mapping>
|
||||
* </pre>
|
||||
* <p>
|
||||
*
|
||||
* The servlet caches the results, both, for the branded as well as the unbranded/replaced contents.
|
||||
*
|
||||
* @see com.sap.sailing.server.gateway.test.support.WhitelabelSwitchServlet
|
||||
* @author Georg Herdt
|
||||
*
|
||||
*/
|
||||
public class ClientConfigurationServlet extends HttpServlet {
|
||||
|
||||
private static final Logger logger = Logger.getLogger(ClientConfigurationServlet.class.getName());
|
||||
|
||||
/** serial version uid */
|
||||
private static final long serialVersionUID = -2228462977010198686L;
|
||||
|
||||
public static final String DEBRANDING_PROPERTY_NAME = "com.sap.sse.debranding";
|
||||
|
||||
private final ConcurrentHashMap<String, byte[]> cache = new ConcurrentHashMap<>();
|
||||
|
||||
@Override
|
||||
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
|
||||
final boolean deBrandingActive = Boolean.valueOf(System.getProperty(DEBRANDING_PROPERTY_NAME, "false"));
|
||||
final String servletPath = req.getServletPath();
|
||||
final byte[] cachedPage;
|
||||
final String pageKey = generateKey(servletPath, deBrandingActive);
|
||||
resp.setContentType(MediaType.TEXT_HTML);
|
||||
if ((cachedPage = cache.get(pageKey)) != null) {
|
||||
IOUtils.write(cachedPage, resp.getOutputStream());
|
||||
} else {
|
||||
try (InputStream in = this.getServletContext().getResourceAsStream(servletPath)) {
|
||||
if (in == null) {
|
||||
throw new FileNotFoundException(servletPath);
|
||||
}
|
||||
byte[] buffer = IOUtils.toByteArray(in);
|
||||
String content = new String(buffer);
|
||||
for (Map.Entry<String, String> item : createReplacementMap(deBrandingActive).entrySet()) {
|
||||
content = content.replace("${" + item.getKey() + "}", item.getValue());
|
||||
}
|
||||
byte[] bytes = content.getBytes();
|
||||
IOUtils.write(bytes, resp.getOutputStream());
|
||||
cache.computeIfAbsent(pageKey, key -> bytes);
|
||||
} catch (RuntimeException e) {
|
||||
logger.log(Level.WARNING, "could not process or read resource " + servletPath, e);
|
||||
resp.sendError(HttpStatus.SC_INTERNAL_SERVER_ERROR);
|
||||
} catch (FileNotFoundException f) {
|
||||
logger.log(Level.WARNING, "could not find resource " + servletPath);
|
||||
resp.sendError(HttpStatus.SC_NOT_FOUND);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private String generateKey(String servletPath, boolean active) {
|
||||
return servletPath + "_" + active;
|
||||
}
|
||||
|
||||
private Map<String,String> createReplacementMap(boolean deBrandingActive) {
|
||||
final Map<String,String> map = new HashMap<>();
|
||||
final String title;
|
||||
final String whitelabeled;
|
||||
if (deBrandingActive) {
|
||||
title = "";
|
||||
whitelabeled = "-whitelabeled";
|
||||
} else {
|
||||
title = "SAP ";
|
||||
whitelabeled = "";
|
||||
}
|
||||
map.put("SAP", title);
|
||||
map.put("debrandingActive", Boolean.toString(deBrandingActive));
|
||||
map.put("whitelabeled", whitelabeled);
|
||||
return map;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user