Commonize into PathUtils; On Linux/BSD try to use xdg-open/x-www-browser if Java...
[SquirrelJME.git] / buildSrc / src / main / java / cc / squirreljme / plugin / util / SimpleHTTPResponseBuilder.java
blobef295118c0511ebe5234fe2454474260b56df7b2
1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
3 // SquirrelJME
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the GNU General Public License v3+, or later.
7 // See license.mkd for licensing and copyright information.
8 // ---------------------------------------------------------------------------
10 package cc.squirreljme.plugin.util;
12 import java.io.ByteArrayOutputStream;
13 import java.io.IOException;
14 import java.io.InputStream;
15 import java.util.Map;
16 import java.util.TreeMap;
18 /**
19 * Builder for HTTP responses.
21 * @since 2020/06/26
23 public final class SimpleHTTPResponseBuilder
25 /** Headers. */
26 private final Map<String, String> _headers =
27 new TreeMap<>(String.CASE_INSENSITIVE_ORDER);
29 /** The status. */
30 public SimpleHTTPStatus status;
32 /** The body. */
33 private byte[] _body;
35 /**
36 * Sets the header key and value.
38 * @param __key The key.
39 * @param __value The value.
40 * @since 2020/06/26
42 public void addHeader(String __key, String __value)
44 this._headers.put(__key, __value);
47 /**
48 * Sets the response body.
50 * @param __bytes The bytes to use.
51 * @throws NullPointerException On null arguments.
52 * @since 2020/06/26
54 public void body(byte... __bytes)
55 throws NullPointerException
57 if (__bytes == null)
58 throw new NullPointerException("NARG");
60 this._body = __bytes.clone();
62 // Ensure that the content length is updated
63 this.addHeader("Content-Length",
64 Integer.toString(__bytes.length));
67 /**
68 * Builds the request.
70 * @return The request.
71 * @since 2020/06/26
73 public SimpleHTTPResponse build()
75 return new SimpleHTTPResponse(this.status, this._headers, this._body);
78 /**
79 * Splices resources into the body.
81 * @param __basis The base class, may be {@code null} if not used.
82 * @param __header The header.
83 * @param __footer The footer.
84 * @param __center The center body data.
85 * @throws NullPointerException If the header/footer were specified and the
86 * basis is {@code null}.
87 * @since 2020/06/27
89 public void spliceResources(Class<?> __basis, String __header,
90 String __footer, byte... __center)
91 throws NullPointerException
93 if (__basis == null && (__header != null || __footer != null))
94 throw new NullPointerException("NARG");
96 // Write the response body
97 try (ByteArrayOutputStream baos = new ByteArrayOutputStream())
99 // Implant the header
100 byte[] buf = new byte[4096];
101 if (__header != null)
102 try (InputStream in = __basis.getResourceAsStream(__header))
104 for (;;)
106 int rc = in.read(buf);
108 if (rc < 0)
109 break;
111 baos.write(buf, 0, rc);
115 // Write the center content here
116 if (__center != null)
117 baos.write(__center);
119 // Implant the footer
120 if (__footer != null)
121 try (InputStream in = __basis.getResourceAsStream(__footer))
123 for (;;)
125 int rc = in.read(buf);
127 if (rc < 0)
128 break;
130 baos.write(buf, 0, rc);
134 // Send the client the body
135 this.body(baos.toByteArray());
138 // Failed to write the body
139 catch (IOException|NullPointerException e)
141 throw new RuntimeException("Could not write response body.", e);
146 * Sets the status.
148 * @param __status The status to use.
149 * @return {@code this}.
150 * @since 2020/06/26
152 public SimpleHTTPResponseBuilder status(SimpleHTTPStatus __status)
154 this.status = __status;
156 return this;