diff options
author | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-12-01 12:23:47 +0100 |
---|---|---|
committer | Andreas Fitzek <andreas.fitzek@iaik.tugraz.at> | 2014-12-02 10:09:35 +0100 |
commit | e929b5f4c6d9351b29150e6c1843f06806ee7b00 (patch) | |
tree | c6ecc4e5f6c3da6ef381a3d852c0a2211ca060e2 /pdf-as-web-status/src/main/java/at/gv/egiz/status/content | |
parent | 6398c85d80213f316dd0f9e4be10e34b54b1f5f9 (diff) | |
download | pdf-as-4-e929b5f4c6d9351b29150e6c1843f06806ee7b00.tar.gz pdf-as-4-e929b5f4c6d9351b29150e6c1843f06806ee7b00.tar.bz2 pdf-as-4-e929b5f4c6d9351b29150e6c1843f06806ee7b00.zip |
added status servlet to pdf-as-web
Diffstat (limited to 'pdf-as-web-status/src/main/java/at/gv/egiz/status/content')
5 files changed, 344 insertions, 0 deletions
diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/ContentGenerator.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/ContentGenerator.java new file mode 100644 index 00000000..6ba85284 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/ContentGenerator.java @@ -0,0 +1,25 @@ +package at.gv.egiz.status.content; + +import java.io.IOException; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import at.gv.egiz.status.TestResult; + +/** + * The Interface ContentGenerator. + */ +public interface ContentGenerator { + + /** + * Generate. + * + * @param request the request + * @param response the response + * @param details the details + * @throws IOException Signals that an I/O exception has occurred. + */ + public void generate(HttpServletRequest request, HttpServletResponse response, Map<String, TestResult> results, boolean details) throws IOException; +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/HtmlGenerator.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/HtmlGenerator.java new file mode 100644 index 00000000..c94cde22 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/HtmlGenerator.java @@ -0,0 +1,92 @@ +package at.gv.egiz.status.content; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringEscapeUtils; + +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.TestStatus; +import at.gv.egiz.status.impl.TestStatusString; + +public class HtmlGenerator implements ContentGenerator { + + @Override + public void generate(HttpServletRequest request, + HttpServletResponse response, Map<String, TestResult> results, boolean details) throws IOException { + + boolean allOk = true; + + Iterator<TestResult> testIterator = results.values().iterator(); + while(testIterator.hasNext()) { + TestResult result = testIterator.next(); + if(!result.getStatus().equals(TestStatus.OK)){ + allOk = false; + break; + } + } + + if(!allOk) { + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } else { + response.setStatus(HttpServletResponse.SC_OK); + } + + response.setContentType("text/html"); + + StringBuilder sb = new StringBuilder(); + + sb.append("<html><head></head><body>"); + sb.append("<table border='1'><thead><tr><th>Name</th><th>Status</th>"); + + if(details) { + sb.append("<th>Details</th>"); + } + + sb.append("</tr></thead><tbody>"); + + Iterator<Entry<String,TestResult>> testResultIterator = results.entrySet().iterator(); + while(testResultIterator.hasNext()) { + Entry<String,TestResult> entry = testResultIterator.next(); + TestResult result = entry.getValue(); + String testName = entry.getKey(); + + sb.append("<tr><td>"); + sb.append(StringEscapeUtils.escapeHtml4(testName)); + sb.append("</td><td>"); + sb.append(StringEscapeUtils.escapeHtml4(TestStatusString.getString(result.getStatus()))); + + + if(details) { + sb.append("</td><td>"); + + StringBuilder detail = new StringBuilder(); + + Iterator<String> detailStringIt = result.getDetails().iterator(); + + while(detailStringIt.hasNext()) { + String detailString = detailStringIt.next(); + detail.append(StringEscapeUtils.escapeHtml4(detailString)); + detail.append("</br>"); + } + + sb.append(detail.toString()); + } + + sb.append("</td></tr>"); + } + + sb.append("</tbody></table>"); + + sb.append("</body></html>"); + + response.getOutputStream().write(sb.toString().getBytes("UTF-8")); + response.getOutputStream().close(); + } + +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/JsonGenerator.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/JsonGenerator.java new file mode 100644 index 00000000..f26c0885 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/JsonGenerator.java @@ -0,0 +1,87 @@ +package at.gv.egiz.status.content; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringEscapeUtils; + +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.TestStatus; +import at.gv.egiz.status.impl.TestStatusString; + +public class JsonGenerator implements ContentGenerator { + + @Override + public void generate(HttpServletRequest request, + HttpServletResponse response, Map<String, TestResult> results, + boolean details) throws IOException { + boolean allOk = true; + + Iterator<TestResult> testIterator = results.values().iterator(); + while(testIterator.hasNext()) { + TestResult result = testIterator.next(); + if(!result.getStatus().equals(TestStatus.OK)){ + allOk = false; + break; + } + } + + if(!allOk) { + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } else { + response.setStatus(HttpServletResponse.SC_OK); + } + + response.setContentType("application/json"); + + StringBuilder sb = new StringBuilder(); + + sb.append("{"); + + Iterator<Entry<String,TestResult>> testResultIterator = results.entrySet().iterator(); + while(testResultIterator.hasNext()) { + Entry<String,TestResult> entry = testResultIterator.next(); + TestResult result = entry.getValue(); + String testName = entry.getKey(); + + sb.append("\""); + sb.append(StringEscapeUtils.escapeJson(testName)); + sb.append("\": {\"Status\": \""); + sb.append(StringEscapeUtils.escapeJson(TestStatusString.getString(result.getStatus()))); + sb.append("\""); + + if(details) { + sb.append(", \"Detail\": \""); + + StringBuilder detail = new StringBuilder(); + + Iterator<String> detailStringIt = result.getDetails().iterator(); + + while(detailStringIt.hasNext()) { + String detailString = detailStringIt.next(); + detail.append(StringEscapeUtils.escapeJson(detailString)); + detail.append(" "); + } + + sb.append(detail.toString()); + sb.append("\""); + } + + sb.append("}"); + if(testResultIterator.hasNext()) { + sb.append(", "); + } + } + + sb.append("}"); + + response.getOutputStream().write(sb.toString().getBytes("UTF-8")); + response.getOutputStream().close(); + } + +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/ResponseBuilder.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/ResponseBuilder.java new file mode 100644 index 00000000..1e248808 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/ResponseBuilder.java @@ -0,0 +1,54 @@ +package at.gv.egiz.status.content; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import at.gv.egiz.status.TestResult; + +public class ResponseBuilder { + + public enum ContentType { + HTML("HTML"), JSON("JSON"), XML("XML"); + + private final String name; + + private ContentType(String s) { + name = s; + } + + public boolean equalsName(String otherName){ + return (otherName == null)? false:name.equals(otherName); + } + + public String toString(){ + return name; + } + } + + private ContentGenerator defaultGenerator; + + private Map<String, ContentGenerator> contentBuilder = new HashMap<String, ContentGenerator>(); + + public ResponseBuilder() { + defaultGenerator = new HtmlGenerator(); + contentBuilder.put(ContentType.HTML.toString(), defaultGenerator); + contentBuilder.put(ContentType.JSON.toString(), new JsonGenerator()); + contentBuilder.put(ContentType.XML.toString(), new XMLGenerator()); + } + + public void generate(HttpServletRequest request, + HttpServletResponse response, Map<String, TestResult> results, + boolean details, String content) throws IOException { + + if(contentBuilder.containsKey(content)) { + contentBuilder.get(content).generate(request, response, results, details); + } else { + defaultGenerator.generate(request, response, results, details); + } + + } +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/XMLGenerator.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/XMLGenerator.java new file mode 100644 index 00000000..00b116bb --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/content/XMLGenerator.java @@ -0,0 +1,86 @@ +package at.gv.egiz.status.content; + +import java.io.IOException; +import java.util.Iterator; +import java.util.Map; +import java.util.Map.Entry; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.apache.commons.lang3.StringEscapeUtils; + +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.TestStatus; +import at.gv.egiz.status.impl.TestStatusString; + +public class XMLGenerator implements ContentGenerator { + + @Override + public void generate(HttpServletRequest request, + HttpServletResponse response, Map<String, TestResult> results, + boolean details) throws IOException { + boolean allOk = true; + + Iterator<TestResult> testIterator = results.values().iterator(); + while(testIterator.hasNext()) { + TestResult result = testIterator.next(); + if(!result.getStatus().equals(TestStatus.OK)){ + allOk = false; + break; + } + } + + if(!allOk) { + response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR); + } else { + response.setStatus(HttpServletResponse.SC_OK); + } + response.setCharacterEncoding("UTF-8"); + response.setContentType("application/xml"); + + StringBuilder sb = new StringBuilder(); + + sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>"); + + sb.append("<tests>"); + + Iterator<Entry<String,TestResult>> testResultIterator = results.entrySet().iterator(); + while(testResultIterator.hasNext()) { + Entry<String,TestResult> entry = testResultIterator.next(); + TestResult result = entry.getValue(); + String testName = entry.getKey(); + + sb.append("<test><name>"); + sb.append(StringEscapeUtils.escapeXml10(testName)); + sb.append("</name><status>"); + sb.append(StringEscapeUtils.escapeXml10(TestStatusString.getString(result.getStatus()))); + sb.append("</status>"); + + if(details) { + sb.append("<detail>"); + + StringBuilder detail = new StringBuilder(); + + Iterator<String> detailStringIt = result.getDetails().iterator(); + + while(detailStringIt.hasNext()) { + String detailString = detailStringIt.next(); + detail.append(StringEscapeUtils.escapeXml10(detailString)); + detail.append(" "); + } + + sb.append(detail.toString()); + sb.append("</detail>"); + } + + sb.append("</test>"); + } + + sb.append("</tests>"); + + response.getOutputStream().write(sb.toString().getBytes("UTF-8")); + response.getOutputStream().close(); + } + +} |