aboutsummaryrefslogtreecommitdiffstats
path: root/application/src/main/java/com/yahoo/application/container/handler/Response.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 /application/src/main/java/com/yahoo/application/container/handler/Response.java
Publish
Diffstat (limited to 'application/src/main/java/com/yahoo/application/container/handler/Response.java')
-rw-r--r--application/src/main/java/com/yahoo/application/container/handler/Response.java125
1 files changed, 125 insertions, 0 deletions
diff --git a/application/src/main/java/com/yahoo/application/container/handler/Response.java b/application/src/main/java/com/yahoo/application/container/handler/Response.java
new file mode 100644
index 00000000000..37450e9d33f
--- /dev/null
+++ b/application/src/main/java/com/yahoo/application/container/handler/Response.java
@@ -0,0 +1,125 @@
+// Copyright 2016 Yahoo Inc. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
+package com.yahoo.application.container.handler;
+
+import com.google.common.annotations.Beta;
+import com.yahoo.jdisc.http.HttpHeaders;
+import com.yahoo.text.Utf8;
+import net.jcip.annotations.Immutable;
+
+import java.nio.ByteBuffer;
+import java.nio.charset.CharacterCodingException;
+import java.nio.charset.Charset;
+import java.nio.charset.CharsetDecoder;
+import java.nio.charset.UnsupportedCharsetException;
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+/**
+ * A response for use with {@link com.yahoo.application.container.JDisc#handleRequest(Request)}.
+ *
+ * @author <a href="mailto:einarmr@yahoo-inc.com">Einar M R Rosenvinge</a>
+ * @since 5.1.15
+ * @see Request
+ */
+@Immutable
+@Beta
+public class Response {
+ private final static Pattern charsetPattern = Pattern.compile("charset=([^\\s\\;]+)", Pattern.CASE_INSENSITIVE);
+ private final int status;
+ private final Headers headers = new Headers();
+ private final byte[] body;
+
+ /**
+ * Creates a Response with an empty body, and 200 (OK) response code.
+ */
+ public Response() {
+ this(new byte[0]);
+ }
+
+ /**
+ * Creates a Response with a message body, and 200 (OK) response code.
+ *
+ * @param body the body of the response
+ */
+ public Response(byte[] body) {
+ this(com.yahoo.jdisc.Response.Status.OK, body);
+ }
+
+ /**
+ * Creates a Response with a message body, and the given response code.
+ *
+ * @param status the status code of the response
+ * @param body the body of the response
+ * @since 5.1.28
+ */
+ public Response(int status, byte[] body) {
+ this.status = status;
+ this.body = body;
+ }
+
+ /**
+ * <p>Returns the status code of this response. This is an integer result code of the attempt to understand and
+ * satisfy the corresponding {@link com.yahoo.application.container.handler.Request}.
+ *
+ * @return The status code.
+ * @since 5.1.28
+ */
+ public int getStatus() {
+ return status;
+ }
+
+ /**
+ * Returns the body of this Response.
+ *
+ * @return the body of this Response
+ */
+ public byte[] getBody() {
+ return body;
+ }
+
+ /**
+ * Attempts to decode the buffer returned by {@link #getBody()} as a String in a best-effort manner. This is done
+ * using the Content-Type header - and defaults to UTF-8 encoding if the header is unparseable or not found.
+ * Note that this may very well throw a {@link CharacterCodingException}.
+ *
+ * @return a String with the decoded contents of the body buffer
+ * @throws CharacterCodingException if the body buffer was not well-formed
+ */
+ public String getBodyAsString() throws CharacterCodingException {
+ CharsetDecoder decoder = charset().newDecoder();
+ return decoder.decode(ByteBuffer.wrap(body)).toString();
+ }
+
+ /**
+ * Returns a mutable multi-map of headers for this Response.
+ *
+ * @return a mutable multi-map of headers for this Response
+ */
+ public Headers getHeaders() {
+ return headers;
+ }
+
+ @Override
+ public String toString() {
+ String bodyStr = (body == null || body.length == 0) ? "[empty]" : "[omitted]";
+ return "Response, headers: " + headers + ", body: " + bodyStr;
+ }
+
+ private Charset charset() {
+ return charset(headers.getFirst(HttpHeaders.Names.CONTENT_TYPE));
+ }
+
+ static Charset charset(String contentType) {
+ if (contentType != null) {
+ Matcher matcher = charsetPattern.matcher(contentType);
+ if (matcher.find()) {
+ try {
+ return Charset.forName(matcher.group(1));
+ } catch (UnsupportedCharsetException uce) {
+ return Utf8.getCharset();
+ }
+ }
+ }
+ return Utf8.getCharset();
+ }
+}