Search in sources :

Example 1 with Benchmark

use of org.apache.stanbol.enhancer.benchmark.Benchmark in project stanbol by apache.

the class BenchmarkServlet method doPost.

@Override
protected void doPost(final HttpServletRequest request, final HttpServletResponse response) throws ServletException, IOException {
    final String content = request.getParameter(PARAM_CONTENT);
    if (content == null) {
        throw new ServletException("Missing " + PARAM_CONTENT + " parameter");
    }
    String chainName = request.getParameter(PARAM_CHAIN);
    final Template t = AccessController.doPrivileged(new PrivilegedAction<Template>() {

        @Override
        public Template run() {
            return getTemplate("/velocity/benchmark-results.html");
        }
    });
    final VelocityContext ctx = getVelocityContext(request, "Benchmark Results");
    ctx.put("contentItemFactory", ciFactory);
    ctx.put("jobManager", jobManager);
    List<? extends Benchmark> benchmarks = parser.parse(new StringReader(content));
    if (chainName != null && !chainName.isEmpty()) {
        Chain chain = chainManager.getChain(chainName);
        if (chain == null) {
            response.setStatus(404);
            PrintWriter w = response.getWriter();
            w.println("Unable to perform benchmark on EnhancementChain '" + StringEscapeUtils.escapeHtml(chainName) + "' because no chain with that name is active!");
            IOUtils.closeQuietly(w);
            return;
        }
        for (Benchmark benchmark : benchmarks) {
            benchmark.setChain(chain);
        }
    }
    ctx.put("benchmarks", benchmarks);
    ctx.put("graphFormatter", new GraphFormatter(graphSerializer));
    response.setContentType("text/html");
    response.setCharacterEncoding("UTF-8");
    try {
        AccessController.doPrivileged(new PrivilegedExceptionAction<Object>() {

            @Override
            public Object run() throws IOException {
                t.merge(ctx, response.getWriter());
                return null;
            }
        });
    } catch (PrivilegedActionException pae) {
        Exception e = pae.getException();
        if (e instanceof IOException) {
            throw (IOException) e;
        } else {
            throw RuntimeException.class.cast(e);
        }
    }
}
Also used : Chain(org.apache.stanbol.enhancer.servicesapi.Chain) PrivilegedActionException(java.security.PrivilegedActionException) VelocityContext(org.apache.velocity.VelocityContext) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) NamespaceException(org.osgi.service.http.NamespaceException) PrivilegedActionException(java.security.PrivilegedActionException) IOException(java.io.IOException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) Template(org.apache.velocity.Template) ServletException(javax.servlet.ServletException) StringReader(java.io.StringReader) Benchmark(org.apache.stanbol.enhancer.benchmark.Benchmark) PrintWriter(java.io.PrintWriter)

Aggregations

IOException (java.io.IOException)1 PrintWriter (java.io.PrintWriter)1 StringReader (java.io.StringReader)1 UnsupportedEncodingException (java.io.UnsupportedEncodingException)1 PrivilegedActionException (java.security.PrivilegedActionException)1 ServletException (javax.servlet.ServletException)1 Benchmark (org.apache.stanbol.enhancer.benchmark.Benchmark)1 Chain (org.apache.stanbol.enhancer.servicesapi.Chain)1 Template (org.apache.velocity.Template)1 VelocityContext (org.apache.velocity.VelocityContext)1 NamespaceException (org.osgi.service.http.NamespaceException)1