| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
package net.admin4j.ui.servlets; |
| 15 | |
|
| 16 | |
import java.io.ByteArrayInputStream; |
| 17 | |
import java.io.ByteArrayOutputStream; |
| 18 | |
import java.io.IOException; |
| 19 | |
import java.io.OutputStreamWriter; |
| 20 | |
import java.util.Enumeration; |
| 21 | |
import java.util.Map; |
| 22 | |
|
| 23 | |
import javax.servlet.http.HttpServletRequest; |
| 24 | |
import javax.servlet.http.HttpServletResponse; |
| 25 | |
|
| 26 | |
import net.admin4j.deps.commons.io.IOUtils; |
| 27 | |
import net.admin4j.util.Admin4jRuntimeException; |
| 28 | |
import net.admin4j.util.FreemarkerUtils; |
| 29 | |
import freemarker.template.Template; |
| 30 | |
|
| 31 | |
|
| 32 | |
|
| 33 | |
|
| 34 | |
|
| 35 | |
|
| 36 | 48 | public abstract class AdminDisplayServlet extends Admin4JServlet { |
| 37 | |
|
| 38 | |
public static final String ADMIN4J_SESSION_VARIABLE_PREFIX = "Admin4j"; |
| 39 | |
private static final long serialVersionUID = -7324380011384838424L; |
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
protected void displayFreeMarkerPage(HttpServletRequest request, |
| 49 | |
HttpServletResponse response, String templateName, |
| 50 | |
Map<String, Object> variableMap) throws IOException { |
| 51 | 27 | response.setContentType("text/html"); |
| 52 | |
|
| 53 | 27 | displayFreeMarkerResponse(request, response, templateName, variableMap); |
| 54 | 27 | } |
| 55 | |
|
| 56 | |
@SuppressWarnings("unchecked") |
| 57 | |
protected void displayFreeMarkerResponse(HttpServletRequest request, |
| 58 | |
HttpServletResponse response, |
| 59 | |
String templateName, Map<String, Object> variableMap) |
| 60 | |
throws IOException { |
| 61 | 27 | Enumeration<String> attrNameEnum = request.getSession().getAttributeNames(); |
| 62 | |
String attrName; |
| 63 | 30 | while (attrNameEnum.hasMoreElements()) { |
| 64 | 3 | attrName = attrNameEnum.nextElement(); |
| 65 | 3 | if (attrName != null && attrName.startsWith(ADMIN4J_SESSION_VARIABLE_PREFIX)) { |
| 66 | 3 | variableMap.put("Session" +attrName, request.getSession().getAttribute(attrName)); |
| 67 | |
} |
| 68 | |
} |
| 69 | 27 | variableMap.put("RequestAdmin4jCurrentUri", request.getRequestURI()); |
| 70 | |
|
| 71 | 27 | Template temp = FreemarkerUtils.createConfiguredTemplate(this.getClass(), templateName); |
| 72 | 27 | ByteArrayOutputStream outStream = new ByteArrayOutputStream(); |
| 73 | |
|
| 74 | |
try { |
| 75 | 27 | temp.process(variableMap, new OutputStreamWriter(outStream)); |
| 76 | 27 | response.setContentLength(outStream.size()); |
| 77 | |
IOUtils.copy(new ByteArrayInputStream(outStream.toByteArray()), response.getOutputStream()); |
| 78 | |
|
| 79 | 27 | response.getOutputStream().flush(); |
| 80 | 27 | response.getOutputStream().close(); |
| 81 | |
} |
| 82 | 0 | catch (Exception e) { |
| 83 | 0 | throw new Admin4jRuntimeException(e); |
| 84 | 27 | } |
| 85 | 27 | } |
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
@Override |
| 91 | |
public boolean hasDisplay() { |
| 92 | 0 | return true; |
| 93 | |
} |
| 94 | |
|
| 95 | |
public abstract String getServletLabel(); |
| 96 | |
|
| 97 | |
} |