From 72231250ed81e10d66bfe70701e64fa5fe50f712 Mon Sep 17 00:00:00 2001 From: Jon Bratseth Date: Wed, 15 Jun 2016 23:09:44 +0200 Subject: Publish --- .../java/com/yahoo/search/rendering/Renderer.java | 96 ++++++++++++++++++++++ 1 file changed, 96 insertions(+) create mode 100644 container-search/src/main/java/com/yahoo/search/rendering/Renderer.java (limited to 'container-search/src/main/java/com/yahoo/search/rendering/Renderer.java') diff --git a/container-search/src/main/java/com/yahoo/search/rendering/Renderer.java b/container-search/src/main/java/com/yahoo/search/rendering/Renderer.java new file mode 100644 index 00000000000..92e3bb15d06 --- /dev/null +++ b/container-search/src/main/java/com/yahoo/search/rendering/Renderer.java @@ -0,0 +1,96 @@ +// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root. +package com.yahoo.search.rendering; + +import com.yahoo.search.Query; +import com.yahoo.search.Result; +import com.google.common.util.concurrent.ListenableFuture; +import com.google.common.util.concurrent.SettableFuture; +import com.yahoo.io.ByteWriter; +import com.yahoo.processing.Request; +import com.yahoo.processing.execution.Execution; + +import java.io.IOException; +import java.io.OutputStream; +import java.io.Writer; +import java.nio.charset.Charset; +import java.nio.charset.CharsetEncoder; + +/** + * Renders a search result to a writer synchronously - the result is completely rendered when the render method returns.. + * The renderers are cloned just before rendering, + * and must therefore obey the following contract: + * + *
    + *
  1. At construction time, only final members shall be initialized, and these + * must refer to immutable data only.
  2. + *
  3. State mutated during rendering shall be initialized in the init method.
  4. + *
+ * + * @author tonytv + */ +abstract public class Renderer extends com.yahoo.processing.rendering.Renderer { + + /** + * Renders synchronously and returns when rendering is complete. + * + * @return a future which is always completed to true + */ + @Override + public final ListenableFuture render(OutputStream stream, Result response, Execution execution, Request request) { + Writer writer = null; + try { + writer = createWriter(stream,response); + render(writer, response); + } + catch (IOException e) { + throw new RuntimeException(e); + } + finally { + if (writer !=null) + try { writer.close(); } catch (IOException e2) {}; + } + SettableFuture completed=SettableFuture.create(); + completed.set(true); + return completed; + } + + /** + * Renders the result to the writer. + */ + protected abstract void render(Writer writer, Result result) throws IOException; + + private Writer createWriter(OutputStream stream,Result result) { + Charset cs = Charset.forName(getCharacterEncoding(result)); + CharsetEncoder encoder = cs.newEncoder(); + return new ByteWriter(stream, encoder); + } + + public String getCharacterEncoding(Result result) { + String encoding = result.getQuery().getModel().getEncoding(); + return (encoding != null) ? encoding : getEncoding(); + } + + /** + * @return The summary class to fill the hits with if no summary class was + * specified in the query presentation. + */ + public String getDefaultSummaryClass() { + return null; + } + + /** Returns the encoding of the query, or the encoding given by the template if none is set */ + public final String getRequestedEncoding(Query query) { + String encoding = query.getModel().getEncoding(); + if (encoding != null) return encoding; + return getEncoding(); + } + + /** + * Used to create a separate instance for each result to render. + */ + @Override + public Renderer clone() { + return (Renderer) super.clone(); + } + +} -- cgit v1.2.3