aboutsummaryrefslogtreecommitdiffstats
path: root/container-search/src/main/java/com/yahoo/search/rendering/Renderer.java
diff options
context:
space:
mode:
authorJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
committerJon Bratseth <bratseth@yahoo-inc.com>2016-06-15 23:09:44 +0200
commit72231250ed81e10d66bfe70701e64fa5fe50f712 (patch)
tree2728bba1131a6f6e5bdf95afec7d7ff9358dac50 /container-search/src/main/java/com/yahoo/search/rendering/Renderer.java
Publish
Diffstat (limited to 'container-search/src/main/java/com/yahoo/search/rendering/Renderer.java')
-rw-r--r--container-search/src/main/java/com/yahoo/search/rendering/Renderer.java96
1 files changed, 96 insertions, 0 deletions
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:
+ *
+ * <ol>
+ * <li>At construction time, only final members shall be initialized, and these
+ * must refer to immutable data only.</li>
+ * <li>State mutated during rendering shall be initialized in the init method.</li>
+ * </ol>
+ *
+ * @author tonytv
+ */
+abstract public class Renderer extends com.yahoo.processing.rendering.Renderer<Result> {
+
+ /**
+ * Renders synchronously and returns when rendering is complete.
+ *
+ * @return a future which is always completed to true
+ */
+ @Override
+ public final ListenableFuture<Boolean> 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<Boolean> 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();
+ }
+
+}