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 | |
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
26 files changed, 1087 insertions, 2 deletions
diff --git a/pdf-as-web-db/build.gradle b/pdf-as-web-db/build.gradle index 51897928..6d87f82b 100644 --- a/pdf-as-web-db/build.gradle +++ b/pdf-as-web-db/build.gradle @@ -15,6 +15,7 @@ repositories { dependencies { compile project (':pdf-as-web') + compile project (':pdf-as-web-status') compile "org.hibernate:hibernate-core:4.3.6.Final" compile "org.hibernate:hibernate-entitymanager:4.3.6.Final" compile group: 'log4j', name: 'log4j', version: '1.2.17' diff --git a/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/db/status/DBTest.java b/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/db/status/DBTest.java new file mode 100644 index 00000000..e1cc1a8d --- /dev/null +++ b/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/db/status/DBTest.java @@ -0,0 +1,45 @@ +package at.gv.egiz.pdfas.web.db.status; + +import java.util.ArrayList; +import java.util.List; + +import at.gv.egiz.pdfas.web.store.DBRequestStore; +import at.gv.egiz.status.Test; +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.TestStatus; +import at.gv.egiz.status.impl.BaseTestResult; + +public class DBTest implements Test { + + private DBRequestStore requestStore; + + public DBTest() { + requestStore = new DBRequestStore(); + } + + @Override + public String getName() { + return "DB"; + } + + @Override + public long getCacheDelay() { + return 300000; + } + + @Override + public TestResult runTest() { + BaseTestResult result = new BaseTestResult(); + try { + this.requestStore.cleanOldRequestException(); + result.setStatus(TestStatus.OK); + } catch(Throwable e) { + result.setStatus(TestStatus.FAILED); + List<String> details = new ArrayList<String>(); + details.add(e.getMessage()); + result.setDetails(details); + } + return result; + } + +} diff --git a/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/db/status/DBTestFactory.java b/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/db/status/DBTestFactory.java new file mode 100644 index 00000000..bc0112a2 --- /dev/null +++ b/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/db/status/DBTestFactory.java @@ -0,0 +1,18 @@ +package at.gv.egiz.pdfas.web.db.status; + +import java.util.ArrayList; +import java.util.List; + +import at.gv.egiz.status.Test; +import at.gv.egiz.status.TestFactory; + +public class DBTestFactory implements TestFactory { + + @Override + public List<Test> createTests() { + List<Test> testList = new ArrayList<Test>(); + testList.add(new DBTest()); + return testList; + } + +} diff --git a/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/store/DBRequestStore.java b/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/store/DBRequestStore.java index a1b88270..df006f02 100644 --- a/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/store/DBRequestStore.java +++ b/pdf-as-web-db/src/main/java/at/gv/egiz/pdfas/web/store/DBRequestStore.java @@ -64,6 +64,29 @@ public class DBRequestStore implements IRequestStore { } } + public void cleanOldRequestException() { + int seconds = WebConfiguration.getDBTimeout(); + Calendar calendar = Calendar.getInstance(); + calendar.add(Calendar.SECOND, (-1)* seconds); + Date date = calendar.getTime(); + SimpleDateFormat dt = new SimpleDateFormat("yyyy-mm-dd hh:mm:ss"); + logger.info("Clearing Entries before: " + dt.format(date)); + Session session = null; + Transaction tx = null; + try { + session = sessions.openSession(); + tx = session.beginTransaction(); + Query query = session.createQuery("delete from Request as req" + + " where req.created < :date"); + query.setCalendar("date", calendar); + query.executeUpdate(); + } finally { + if(session != null) { + session.close(); + } + } + } + public String createNewStoreEntry(PDFASSignRequest request) { // Clean Old Requests this.cleanOldRequests(); diff --git a/pdf-as-web-db/src/main/resources/META-INF/services/at.gv.egiz.status.TestFactory b/pdf-as-web-db/src/main/resources/META-INF/services/at.gv.egiz.status.TestFactory new file mode 100644 index 00000000..40ad6651 --- /dev/null +++ b/pdf-as-web-db/src/main/resources/META-INF/services/at.gv.egiz.status.TestFactory @@ -0,0 +1 @@ +at.gv.egiz.pdfas.web.db.status.DBTestFactory
\ No newline at end of file diff --git a/pdf-as-web-status/.gitignore b/pdf-as-web-status/.gitignore new file mode 100644 index 00000000..ae3c1726 --- /dev/null +++ b/pdf-as-web-status/.gitignore @@ -0,0 +1 @@ +/bin/ diff --git a/pdf-as-web-status/build.gradle b/pdf-as-web-status/build.gradle new file mode 100644 index 00000000..710f04e4 --- /dev/null +++ b/pdf-as-web-status/build.gradle @@ -0,0 +1,39 @@ +apply plugin: 'java' +apply plugin: 'war' +apply plugin: 'eclipse' +apply plugin: 'java-library-distribution' + +jar { + manifest { + attributes 'Implementation-Title': 'PDF-AS-4 Web Status Library', 'JARMANIFEST': 'PDF-AS-LIB' + } +} + +repositories { + mavenLocal() + mavenCentral() +} + +configurations { providedCompile } + +sourceSets.main.compileClasspath += configurations.providedCompile +sourceSets.test.compileClasspath += configurations.providedCompile +sourceSets.test.runtimeClasspath += configurations.providedCompile + +dependencies { + compile group: 'org.slf4j', name: 'slf4j-api', version: slf4jVersion + compile 'org.apache.commons:commons-lang3:3.3.2' + providedCompile 'javax.servlet:javax.servlet-api:3.0.1' + testCompile group: 'junit', name: 'junit', version: '4.+' +} + +task releases(type: Copy) { + from jar.outputs + from distZip.outputs + from distTar.outputs + into rootDir.toString() + "/releases/" + version +} + +releases.dependsOn jar +releases.dependsOn distZip +releases.dependsOn distTar diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/Test.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/Test.java new file mode 100644 index 00000000..97f5f4d7 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/Test.java @@ -0,0 +1,28 @@ +package at.gv.egiz.status; + +/** + * The Interface Test. + */ +public interface Test { + + /** + * Gets the name. + * + * @return the name + */ + public String getName(); + + /** + * Gets the cache delay. + * + * @return the cache delay + */ + public long getCacheDelay(); + + /** + * Run test. + * + * @return the test result + */ + public TestResult runTest(); +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestFactory.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestFactory.java new file mode 100644 index 00000000..682f809b --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestFactory.java @@ -0,0 +1,16 @@ +package at.gv.egiz.status; + +import java.util.List; + +/** + * A factory for creating Test objects. + */ +public interface TestFactory { + + /** + * Creates a new Test object. + * + * @return the list< test> + */ + public List<Test> createTests(); +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestResult.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestResult.java new file mode 100644 index 00000000..9c57efdf --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestResult.java @@ -0,0 +1,23 @@ +package at.gv.egiz.status; + +import java.util.List; + +/** + * The Interface TestResult. + */ +public interface TestResult { + + /** + * Gets the status. + * + * @return the status + */ + public TestStatus getStatus(); + + /** + * Gets the details. + * + * @return the details + */ + public List<String> getDetails(); +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestStatus.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestStatus.java new file mode 100644 index 00000000..9513a5d5 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/TestStatus.java @@ -0,0 +1,16 @@ +package at.gv.egiz.status; + +/** + * The Enum TestStatus. + */ +public enum TestStatus { + + /** The ok. */ + OK, + + /** The failed. */ + FAILED, + + /** The indeterminate. */ + INDETERMINATE +} 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(); + } + +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/BaseTestResult.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/BaseTestResult.java new file mode 100644 index 00000000..da77f424 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/BaseTestResult.java @@ -0,0 +1,91 @@ +package at.gv.egiz.status.impl; + +import java.util.ArrayList; +import java.util.List; + +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.TestStatus; + +/** + * The Class BaseTestResult. + */ +public class BaseTestResult implements TestResult { + + /** The status. */ + private TestStatus status; + + /** The details. */ + private List<String> details; + + /** + * Instantiates a new base test result. + */ + public BaseTestResult() { + this.init(TestStatus.INDETERMINATE, new ArrayList<String>()); + } + + /** + * Instantiates a new base test result. + * + * @param status the status + */ + public BaseTestResult(TestStatus status) { + this.init(status, new ArrayList<String>()); + } + + /** + * Instantiates a new base test result. + * + * @param status the status + * @param details the details + */ + public BaseTestResult(TestStatus status, List<String> details) { + this.init(status, details); + } + + /** + * Inits the. + * + * @param status the status + * @param details the details + */ + private void init(TestStatus status, List<String> details) { + this.status = status; + this.details = details; + } + + /** + * Sets the status. + * + * @param status the new status + */ + public void setStatus(TestStatus status) { + this.status = status; + } + + /** + * Sets the details. + * + * @param details the new details + */ + public void setDetails(List<String> details) { + this.details = details; + } + + /* (non-Javadoc) + * @see at.gv.egiz.status.TestResult#getStatus() + */ + @Override + public TestStatus getStatus() { + return status; + } + + /* (non-Javadoc) + * @see at.gv.egiz.status.TestResult#getDetails() + */ + @Override + public List<String> getDetails() { + return details; + } + +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestManager.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestManager.java new file mode 100644 index 00000000..db40f96c --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestManager.java @@ -0,0 +1,149 @@ +package at.gv.egiz.status.impl; + +import java.util.Date; +import java.util.HashMap; +import java.util.Iterator; +import java.util.List; +import java.util.Map; +import java.util.ServiceLoader; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.status.Test; +import at.gv.egiz.status.TestFactory; +import at.gv.egiz.status.TestResult; + +/** + * The Class TestManager. + */ +public class TestManager { + + /** The log. */ + private final Logger log = LoggerFactory.getLogger(TestManager.class); + + /** The tests. */ + private Map<String, Test> tests = new HashMap<String, Test>(); + + /** The test result cache. */ + private Map<String, TestRunResult> testResultCache = new HashMap<String, TestRunResult>(); + + /** The loader. */ + private static ServiceLoader<TestFactory> loader = ServiceLoader + .load(TestFactory.class); + + /** + * Instantiates a new test manager. + */ + public TestManager() { + Iterator<TestFactory> factoryIterator = loader.iterator(); + + while (factoryIterator.hasNext()) { + TestFactory factory = factoryIterator.next(); + + log.debug("Running Factory: " + factory.getClass().getName()); + + List<Test> testList = factory.createTests(); + if (testList != null && !testList.isEmpty()) { + log.debug("Factory: " + factory.getClass().getName() + + " produced " + testList.size() + " tests!"); + Iterator<Test> testIterator = testList.iterator(); + while (testIterator.hasNext()) { + Test test = testIterator.next(); + log.debug("adding Test: " + test.getName() + " [" + + test.getClass().getName() + "]"); + tests.put(test.getName(), test); + } + } else { + log.debug("Factory: " + factory.getClass().getName() + + " produced no tests!"); + } + } + } + + /** + * Store test result. + * + * @param testName the test name + * @param testResult the test result + * @return the test result + */ + private TestResult storeTestResult(String testName, TestResult testResult) { + if(testResult != null) { + TestRunResult runResult = new TestRunResult(); + runResult.setExecutionTimestamp(new Date().getTime()); + runResult.setTestResult(testResult); + testResultCache.put(testName, runResult); + } + return testResult; + } + + /** + * Run test. + * + * @param testName the test name + * @return the test result + */ + public TestResult runTest(String testName) { + if(tests.containsKey(testName)) { + return storeTestResult(testName, tests.get(testName).runTest()); + } + log.debug("Not test \"" + testName + "\" available"); + return null; + } + + /** + * Run test. + * + * @param testName the test name + * @param forceExecution the force execution + * @return the test result + */ + public TestResult runTest(String testName, boolean forceExecution) { + if(!tests.containsKey(testName)) { + log.debug("Not test \"" + testName + "\" available"); + return null; + } + + if (forceExecution) { + return runTest(testName); + } + + Test test = tests.get(testName); + + if (testResultCache.containsKey(testName)) { + // + Date now = new Date(); + + TestRunResult result = testResultCache.get(testName); + long lastTest = result.getExecutionTimestamp(); + long delay = test.getCacheDelay(); + + if (lastTest < now.getTime() - delay) { + // Too old + return runTest(testName); + } else { + // Cache is fine! + return result.getTestResult(); + } + } + return runTest(testName); + } + + /** + * Run all tests. + * + * @param forceExecution the force execution + * @return the map + */ + public Map<String, TestResult> runAllTests(boolean forceExecution) { + Map<String, TestResult> results = new HashMap<String, TestResult>(); + Iterator<Test> testIterator = tests.values().iterator(); + while(testIterator.hasNext()) { + Test test = testIterator.next(); + String testName = test.getName(); + results.put(testName, runTest(testName, forceExecution)); + } + return results; + } +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestRunResult.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestRunResult.java new file mode 100644 index 00000000..9385392c --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestRunResult.java @@ -0,0 +1,88 @@ +package at.gv.egiz.status.impl; + +import at.gv.egiz.status.TestResult; + +/** + * The Class TestRunResult. + */ +public class TestRunResult { + + /** The test result. */ + private TestResult testResult; + + /** The execution timestamp. */ + private long executionTimestamp; + + /** + * Instantiates a new test run result. + */ + public TestRunResult() { + this.init(null, 0); + } + + /** + * Instantiates a new test run result. + * + * @param testResult the test result + */ + public TestRunResult(TestResult testResult) { + this.init(testResult, 0); + } + + /** + * Instantiates a new test run result. + * + * @param testResult the test result + * @param executionTimestamp the execution timestamp + */ + public TestRunResult(TestResult testResult, long executionTimestamp) { + this.init(testResult, executionTimestamp); + } + + /** + * Inits the. + * + * @param testResult the test result + * @param executionTimestamp the execution timestamp + */ + private void init(TestResult testResult, long executionTimestamp) { + this.testResult = testResult; + this.executionTimestamp = executionTimestamp; + } + + /** + * Gets the test result. + * + * @return the test result + */ + public TestResult getTestResult() { + return testResult; + } + + /** + * Sets the test result. + * + * @param testResult the new test result + */ + public void setTestResult(TestResult testResult) { + this.testResult = testResult; + } + + /** + * Gets the execution timestamp. + * + * @return the execution timestamp + */ + public long getExecutionTimestamp() { + return executionTimestamp; + } + + /** + * Sets the execution timestamp. + * + * @param executionTimestamp the new execution timestamp + */ + public void setExecutionTimestamp(long executionTimestamp) { + this.executionTimestamp = executionTimestamp; + } +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestStatusString.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestStatusString.java new file mode 100644 index 00000000..50605cf5 --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/impl/TestStatusString.java @@ -0,0 +1,29 @@ +package at.gv.egiz.status.impl; + +import at.gv.egiz.status.TestStatus; + +// TODO: Auto-generated Javadoc +/** + * The Class TestStatusString. + */ +public class TestStatusString { + + /** + * Gets the string. + * + * @param status the status + * @return the string + */ + public static String getString(TestStatus status) { + switch (status) { + case OK: + return "OK"; + case FAILED: + return "FAILED"; + case INDETERMINATE: + return "INDETERMINATE"; + default: + return "UNKNOWN"; + } + } +} diff --git a/pdf-as-web-status/src/main/java/at/gv/egiz/status/servlet/StatusServlet.java b/pdf-as-web-status/src/main/java/at/gv/egiz/status/servlet/StatusServlet.java new file mode 100644 index 00000000..6790fccc --- /dev/null +++ b/pdf-as-web-status/src/main/java/at/gv/egiz/status/servlet/StatusServlet.java @@ -0,0 +1,120 @@ +package at.gv.egiz.status.servlet; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import javax.servlet.ServletConfig; +import javax.servlet.ServletException; +import javax.servlet.annotation.WebServlet; +import javax.servlet.http.HttpServlet; +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.content.ResponseBuilder; +import at.gv.egiz.status.content.ResponseBuilder.ContentType; +import at.gv.egiz.status.impl.TestManager; + +/** + * Servlet implementation class StatusServlet + */ +@WebServlet(name="statusServlet", urlPatterns={"/status"}) +public class StatusServlet extends HttpServlet { + + private static final long serialVersionUID = 1201254769913428186L; + + public static final String PARAM_CONTENT_TYPE = "content"; + public static final String PARAM_FORCE = "force"; + public static final String PARAM_DETAILS = "details"; + public static final String PARAM_DETAILS_TRUE = "true"; + public static final String PARAM_TEST = "test"; + + private final Logger log = LoggerFactory.getLogger(StatusServlet.class); + + private TestManager manager; + + private ResponseBuilder builder; + + private boolean showDetails = false; + + /** + * @see HttpServlet#HttpServlet() + */ + public StatusServlet() { + super(); + + manager = new TestManager(); + builder = new ResponseBuilder(); + } + + /** + * @see javax.servlet.GenericServlet#init(javax.servlet.ServletConfig) + */ + @Override + public void init(ServletConfig config) throws ServletException { + super.init(config); + + // TODO: Environment Parameter to show details -> showDetails + } + + /** + * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse response) + */ + protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doProcess(request, response); + } + + /** + * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response) + */ + protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + this.doProcess(request, response); + } + + protected void doProcess(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + String content = request.getParameter(PARAM_CONTENT_TYPE); + + if(content == null) { + content = ContentType.HTML.toString(); + } + + log.debug("Producing Content: " + content); + + // Parameter to force execution + boolean force = true; + + String forceExec = request.getParameter(PARAM_FORCE); + + if(forceExec != null) { + if(forceExec.equalsIgnoreCase("false")) { + force = false; + } + } + boolean showingDetails = showDetails; + String detail = request.getParameter(PARAM_DETAILS); + if(detail != null) { + showingDetails = detail.equalsIgnoreCase(PARAM_DETAILS_TRUE); + } + + // Parameter for specific test + String test = request.getParameter(PARAM_TEST); + + Map<String, TestResult> results = null; + + if(test != null) { + results = new HashMap<String, TestResult>(); + TestResult result = this.manager.runTest(test, force); + if(result != null) { + results.put(test, result); + } + } else { + results = this.manager.runAllTests(force); + } + + this.builder.generate(request, response, results, showingDetails, content); + } +} diff --git a/pdf-as-web/build.gradle b/pdf-as-web/build.gradle index 57eb54f9..2dbd7188 100644 --- a/pdf-as-web/build.gradle +++ b/pdf-as-web/build.gradle @@ -15,19 +15,26 @@ repositories { mavenCentral() } +configurations { providedCompile } + +sourceSets.main.compileClasspath += configurations.providedCompile +sourceSets.test.compileClasspath += configurations.providedCompile +sourceSets.test.runtimeClasspath += configurations.providedCompile + dependencies { compile project (':pdf-as-lib') compile project (':pdf-as-moa') compile project (':signature-standards:sigs-pkcs7detached') compile project (':signature-standards:sigs-pades') compile project (':pdf-as-pdfbox') + compile project (':pdf-as-web-status') compile group: 'commons-fileupload', name: 'commons-fileupload', version: '1.3.1' compile group: 'opensymphony', name: 'sitemesh', version: '2.4.2' compile "commons-codec:commons-codec:1.9" compile 'org.apache.commons:commons-lang3:3.3.2' compile 'org.apache.cxf:cxf-rt-transports-http:3.0.1' compile 'org.apache.cxf:cxf-rt-frontend-jaxws:3.0.1' - providedCompile "javax.servlet:servlet-api:2.5" + providedCompile 'javax.servlet:javax.servlet-api:3.0.1' testCompile group: 'junit', name: 'junit', version: '4.+' } diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/status/PdfAsWebTestFactory.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/status/PdfAsWebTestFactory.java new file mode 100644 index 00000000..a6fa8a05 --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/status/PdfAsWebTestFactory.java @@ -0,0 +1,18 @@ +package at.gv.egiz.pdfas.web.status; + +import java.util.ArrayList; +import java.util.List; + +import at.gv.egiz.status.Test; +import at.gv.egiz.status.TestFactory; + +public class PdfAsWebTestFactory implements TestFactory { + + @Override + public List<Test> createTests() { + List<Test> testList = new ArrayList<Test>(); + testList.add(new SimpleAliveTest()); + return testList; + } + +} diff --git a/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/status/SimpleAliveTest.java b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/status/SimpleAliveTest.java new file mode 100644 index 00000000..1f78f90c --- /dev/null +++ b/pdf-as-web/src/main/java/at/gv/egiz/pdfas/web/status/SimpleAliveTest.java @@ -0,0 +1,27 @@ +package at.gv.egiz.pdfas.web.status; + +import at.gv.egiz.status.Test; +import at.gv.egiz.status.TestResult; +import at.gv.egiz.status.TestStatus; +import at.gv.egiz.status.impl.BaseTestResult; + +public class SimpleAliveTest implements Test { + + @Override + public String getName() { + return "ALIVE"; + } + + @Override + public long getCacheDelay() { + return 0; + } + + @Override + public TestResult runTest() { + BaseTestResult result = new BaseTestResult(); + result.setStatus(TestStatus.OK); + return result; + } + +} diff --git a/pdf-as-web/src/main/resources/META-INF/services/at.gv.egiz.status.TestFactory b/pdf-as-web/src/main/resources/META-INF/services/at.gv.egiz.status.TestFactory new file mode 100644 index 00000000..d0787dae --- /dev/null +++ b/pdf-as-web/src/main/resources/META-INF/services/at.gv.egiz.status.TestFactory @@ -0,0 +1 @@ +at.gv.egiz.pdfas.web.status.PdfAsWebTestFactory
\ No newline at end of file diff --git a/settings.gradle b/settings.gradle index 13193fd8..dffd05ec 100644 --- a/settings.gradle +++ b/settings.gradle @@ -1,2 +1,2 @@ -include "pdf-as-common", "signature-standards:sigs-pkcs7detached", "signature-standards:sigs-pades", "pdf-as-lib", "pdf-as-pdfbox", "pdf-as-moa", "pdf-as-tests", "pdf-as-cli", "pdf-as-legacy", "pdf-as-web", "pdf-as-web-db", "pdf-as-web-client" +include "pdf-as-common", "signature-standards:sigs-pkcs7detached", "signature-standards:sigs-pades", "pdf-as-lib", "pdf-as-pdfbox", "pdf-as-moa", "pdf-as-tests", "pdf-as-cli", "pdf-as-legacy", "pdf-as-web-status", "pdf-as-web", "pdf-as-web-db", "pdf-as-web-client" |