1 // -*- Mode: Java; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
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
;
16 import java
.util
.TreeMap
;
19 * Builder for HTTP responses.
23 public final class SimpleHTTPResponseBuilder
26 private final Map
<String
, String
> _headers
=
27 new TreeMap
<>(String
.CASE_INSENSITIVE_ORDER
);
30 public SimpleHTTPStatus status
;
36 * Sets the header key and value.
38 * @param __key The key.
39 * @param __value The value.
42 public void addHeader(String __key
, String __value
)
44 this._headers
.put(__key
, __value
);
48 * Sets the response body.
50 * @param __bytes The bytes to use.
51 * @throws NullPointerException On null arguments.
54 public void body(byte... __bytes
)
55 throws NullPointerException
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
));
70 * @return The request.
73 public SimpleHTTPResponse
build()
75 return new SimpleHTTPResponse(this.status
, this._headers
, this._body
);
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}.
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())
100 byte[] buf
= new byte[4096];
101 if (__header
!= null)
102 try (InputStream in
= __basis
.getResourceAsStream(__header
))
106 int rc
= in
.read(buf
);
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
))
125 int rc
= in
.read(buf
);
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
);
148 * @param __status The status to use.
149 * @return {@code this}.
152 public SimpleHTTPResponseBuilder
status(SimpleHTTPStatus __status
)
154 this.status
= __status
;