Skip to content

Commit 4728cc3

Browse files
committed
Filled in some javadocs
1 parent 0f3a34f commit 4728cc3

File tree

12 files changed

+191
-20
lines changed

12 files changed

+191
-20
lines changed

client/src/main/java/org/threadly/litesockets/client/http/HTTPClient.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -658,10 +658,21 @@ public HTTPResponse getResponse() {
658658
return hr;
659659
}
660660

661+
/**
662+
* Get the response code sent with the response.
663+
*
664+
* @return {@link HTTPResponseCode} sent with the response.
665+
*/
661666
public HTTPResponseCode getResponseCode() {
662667
return hr.getResponseHeader().getResponseCode();
663668
}
664-
669+
670+
/**
671+
* Get the length associated with the response based off the header value if available, or the
672+
* size of the body if not.
673+
*
674+
* @return The size of the body in bytes
675+
*/
665676
public long getContentLength() {
666677
long result = hr.getHeaders().getContentLength();
667678
if (result >= 0) {
@@ -671,14 +682,31 @@ public long getContentLength() {
671682
}
672683
}
673684

685+
/**
686+
* Get a copy of the body as a {@link MergedByteBuffers}.
687+
*
688+
* @return A copy of the body buffers.
689+
*/
674690
public MergedByteBuffers getBody() {
675691
return body.duplicate();
676692
}
677693

694+
/**
695+
* Converts the entire body into a {@link String}.
696+
*
697+
* @return The body as a {@link String}
698+
*/
678699
public String getBodyAsString() {
679700
return body.duplicate().getAsString(body.remaining());
680701
}
681702

703+
/**
704+
* Returns the body as a {@link InputStream}. Consuming this stream wont impact the retention
705+
* of the body (still allowing it to be retrieved from {@link #getBody()} and
706+
* {@link #getBodyAsString()}).
707+
*
708+
* @return The response body as a stream
709+
*/
682710
public InputStream getBodyAsInputStream() {
683711
return body.duplicate().asInputStream();
684712
}

client/src/main/java/org/threadly/litesockets/client/http/HTTPStreamClient.java

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,10 @@ public ListenableFuture<?> write(ByteBuffer bb) {
183183
}
184184
}
185185

186+
/**
187+
* Get the {@link ListenableFuture} associated with the last write to the associated client.
188+
* This future will complete when the write has been written to the socket.
189+
*/
186190
public ListenableFuture<?> getLastWriteFuture() {
187191
return client.lastWriteFuture();
188192
}
@@ -229,31 +233,36 @@ public void addCloseListener(Runnable cl) {
229233
}
230234
}
231235

236+
/**
237+
* Check if the client is connected.
238+
*
239+
* @return {@code true} if the client has not been closed yet
240+
*/
232241
public boolean isConnected() {
233242
return isConnected;
234243
}
235244

245+
/**
246+
* Close the associated client.
247+
*/
236248
public void close() {
237249
isConnected = false;
238250
client.close();
239251
}
240252

241253
/**
242-
*
243-
* @author lwahlmeier
244-
*
254+
* Implementation of {@link Reader} which will provide the data to the {@code httpProcessor}.
245255
*/
246256
private class HTTPReader implements Reader {
247257
@Override
248258
public void onRead(Client client) {
249259
httpProcessor.processData(client.getRead());
250260
}
251261
}
252-
262+
253263
/**
254-
*
255-
* @author lwahlmeier
256-
*
264+
* Implementation of {@link ClientCloseListener} to communicate the closed status to this class
265+
* and attached listeners.
257266
*/
258267
private class HTTPCloser implements ClientCloseListener {
259268
@Override

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/ClientHTTPRequest.java

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,8 @@
44

55
import org.threadly.litesockets.protocols.http.shared.HTTPAddress;
66

7+
// TODO - do we want to move this into the `client`? I see the `HTTPRequestBuilder` references it,
8+
// but we could create an extending class `ClientHTTPRequestBuilder` which can build this
79
/**
810
* This contains a full HTTPRequest, including the HTTPRequest, the HTTPAddress the body and the timeout.
911
* This is immutable, though an HTTPRequestBuilder can be made from it.
@@ -25,18 +27,37 @@ public HTTPRequest getHTTPRequest() {
2527
return request;
2628
}
2729

30+
/**
31+
* Returns the {@link HTTPAddress} the request is associated with.
32+
*
33+
* @return The {@link HTTPAddress} the request will go to
34+
*/
2835
public HTTPAddress getHTTPAddress() {
2936
return ha;
3037
}
3138

39+
// TODO - does this make sense, it seems dangerous since the buffer could be consumed / read.
40+
// We could return a slice / duplicate copy to avoid this, or we can change this to `hasBodyContent`
41+
// (which seems how this is being used)
42+
/**
43+
* Returns the body data the request was constructed with.
44+
*
45+
* @return The ByteBuffer representing the body
46+
*/
3247
public ByteBuffer getBodyBuffer() {
3348
return bodyBytes;
3449
}
3550

51+
/**
52+
* Returns the timeout value that this request was constructed with.
53+
*
54+
* @return The request timeout in milliseconds
55+
*/
3656
public int getTimeoutMS() {
3757
return this.timeoutMS;
3858
}
3959

60+
// TODO - is this useful? I am not seeing any cases of it being used
4061
public HTTPRequestBuilder makeBuilder() {
4162
HTTPRequestBuilder hrb = request.makeBuilder();
4263
hrb.setHTTPAddress(ha, false);

protocol/src/main/java/org/threadly/litesockets/protocols/http/request/HTTPRequestHeader.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -141,6 +141,7 @@ public Map<String, List<String>> getRequestQuery() {
141141
* Gets the value to a given query parameter. This will throw an exception if there is multiple
142142
* values associated to the key.
143143
*
144+
* @param paramKey The key associated with the query parameter value
144145
* @return the request parameter value or {@code null} if none is associated
145146
*/
146147
public String getRequestQueryValue(String paramKey) {

protocol/src/main/java/org/threadly/litesockets/protocols/http/shared/HTTPHeaders.java

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,8 +53,13 @@ public HTTPHeaders(final Map<String, String> headerMap) {
5353
rawHeaders = formatHeaderMap(lheaders);
5454
this.headers = Collections.unmodifiableMap(lheaders);
5555
}
56-
5756

57+
/**
58+
* Converts a {@link Map} into the HTTP standard header format.
59+
*
60+
* @param headerMap The map to use for input
61+
* @return The string of HTTP encoded headers
62+
*/
5863
public static String formatHeaderMap(Map<String, String> headerMap) {
5964
StringBuilder sb = new StringBuilder();
6065
for(Entry<String, String> kv: headerMap.entrySet()) {
@@ -67,18 +72,41 @@ public static String formatHeaderMap(Map<String, String> headerMap) {
6772
return sb.toString();
6873
}
6974

75+
/**
76+
* Get the header key / values as a {@link Map}.
77+
*
78+
* @return The header key / values.
79+
*/
7080
public Map<String, String> getHeadersMap() {
7181
return headers;
7282
}
7383

84+
/**
85+
* Check if the headers are defining this as a chunked request / response. This is determined
86+
* by the presence of a header key {@link HTTPConstants#HTTP_KEY_TRANSFER_ENCODING}.
87+
*
88+
* @return {@code true} if the encoding is set as chunked
89+
*/
7490
public boolean isChunked() {
7591
return headers.containsKey(HTTPConstants.HTTP_KEY_TRANSFER_ENCODING);
7692
}
7793

94+
/**
95+
* Get the value associated with a key in the headers.
96+
*
97+
* @param header The key to use for the header value
98+
* @return The header value or {@code null} if no header key match is found
99+
*/
78100
public String getHeader(String header) {
79101
return headers.get(header);
80102
}
81103

104+
/**
105+
* Parse out the content length from the value of {@link HTTPConstants#HTTP_KEY_CONTENT_LENGTH}
106+
* header.
107+
*
108+
* @return The length sent in the header or {@code -1} if none is provided (or failed to parse)
109+
*/
82110
public long getContentLength() {
83111
String scl = headers.get(HTTPConstants.HTTP_KEY_CONTENT_LENGTH);
84112
long cl = -1;

protocol/src/main/java/org/threadly/litesockets/protocols/http/shared/HTTPResponseCode.java

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,10 @@ private HTTPResponseCode(int val, String text) {
7676
this.text = text;
7777
}
7878

79+
/**
80+
* Return the http standard numeric code / id associated to the response code.
81+
* @return The id value
82+
*/
7983
public int getId() {
8084
return val;
8185
}
@@ -85,6 +89,13 @@ public String toString() {
8589
return text;
8690
}
8791

92+
/**
93+
* Attempt to convert a response code from a numeric id to an enum value.
94+
*
95+
* @param val The value to check for match
96+
* @return A matching response code enum
97+
* @throws IllegalArgumentException thrown if no code is associated with the provided value
98+
*/
8899
public static HTTPResponseCode findResponseCode(int val) {
89100
for(HTTPResponseCode hrc: HTTPResponseCode.values()) {
90101
if(hrc.getId() == val) {

protocol/src/main/java/org/threadly/litesockets/protocols/http/shared/HTTPUtils.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -12,20 +12,30 @@
1212
import org.threadly.util.StringUtils;
1313

1414
/**
15-
*
16-
*
17-
* @author lwahlmeier
18-
*
15+
* Utility functions for working with the HTTP protocol.
1916
*/
2017
public class HTTPUtils {
18+
/**
19+
* Trim whitespace from the left side of the String only.
20+
*
21+
* @param value String to trim from
22+
* @return A left-trimmed string
23+
*/
2124
public static String leftTrim(String value) {
2225
int count = 0;
23-
while(Character.isWhitespace(value.charAt(count))) {
26+
while(value.length() > count && Character.isWhitespace(value.charAt(count))) {
2427
count++;
2528
}
2629
return value.substring(count);
2730
}
2831

32+
/**
33+
* Used for parsing a chunk encoded request / response. This will find the end of the chunk
34+
* and then parse out the size of the next chunk
35+
*
36+
* @param bb Source {@link ByteBuffer} to read from
37+
* @return The next chunk size or {@code -1} if could not be found or failed to parse
38+
*/
2939
public static int getNextChunkLength(final ByteBuffer bb) {
3040
MergedByteBuffers mbb = new ReuseableMergedByteBuffers();
3141
mbb.add(bb);
@@ -41,6 +51,12 @@ public static int getNextChunkLength(final ByteBuffer bb) {
4151
return -1;
4252
}
4353

54+
/**
55+
* Wraps the given data in a chunk to be used for chunked encoding.
56+
*
57+
* @param bb The data to wrap
58+
* @return A new buffer which wraps the data in a chunk encoded segment
59+
*/
4460
public static ByteBuffer wrapInChunk(ByteBuffer bb) {
4561
byte[] size = Integer.toHexString(bb.remaining()).getBytes();
4662
ByteBuffer newBB = ByteBuffer.allocate(bb.remaining()+
@@ -53,6 +69,12 @@ public static ByteBuffer wrapInChunk(ByteBuffer bb) {
5369
return newBB;
5470
}
5571

72+
/**
73+
* Converts query parameters stored in a map to a {@link String} that can be added to the URI.
74+
*
75+
* @param map Map to source the values from
76+
* @return HTTP standard query parameters, prefixed with {@code ?}
77+
*/
5678
public static String queryToString(Map<String, List<String>> map) {
5779
if(map.isEmpty()) {
5880
return "";
@@ -82,6 +104,12 @@ public static String queryToString(Map<String, List<String>> map) {
82104
return sb.toString();
83105
}
84106

107+
/**
108+
* Parses http standard query parameters from the URL into a {@link Map} representation.
109+
*
110+
* @param query The String to parse
111+
* @return The parsed our parameters into a {@link Map}
112+
*/
85113
public static Map<String, List<String>> queryToMap(String query) {
86114
if (StringUtils.isNullOrEmpty(query)) {
87115
return Collections.emptyMap();

protocol/src/main/java/org/threadly/litesockets/protocols/websocket/WSConstants.java

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,8 @@
11
package org.threadly.litesockets.protocols.websocket;
22

3+
/**
4+
* Constants used for websockets.
5+
*/
36
public class WSConstants {
47
public static final String MAGIC_UUID = "258EAFA5-E914-47DA-95CA-C5AB0DC85B11";
58
public static final int DEFAULT_SECRET_KEY_SIZE = 20;

protocol/src/main/java/org/threadly/litesockets/protocols/websocket/WSFrame.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,7 @@ public boolean hasMask() {
8888
}
8989

9090
/**
91-
* Returns the size of the websocket frames payload
91+
* Returns the size of the websocket frames payload.
9292
*
9393
* @return size of the payload.
9494
*/

protocol/src/main/java/org/threadly/litesockets/protocols/websocket/WSOPCode.java

Lines changed: 12 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,32 @@
22

33
/**
44
* Standard Websocket OpCodes.
5-
*
65
*/
76
public enum WSOPCode {
87
Continuation((byte)0), Text((byte)1), Binary((byte)2),
98
Close((byte)8), Ping((byte)9), Pong((byte)10);
109

1110
private final byte value;
1211

13-
WSOPCode(byte value) {
12+
private WSOPCode(byte value) {
1413
this.value = value;
1514
}
1615

16+
/**
17+
* Get the opcode value.
18+
*
19+
* @return The standard value associated with the opcode
20+
*/
1721
public byte getValue() {
1822
return value;
1923
}
2024

25+
/**
26+
* Convert a op code byte to an {@link WSOPCode} enum value.
27+
*
28+
* @param b Byte to check against
29+
* @return Matching opcode or {@code null} if none was found
30+
*/
2131
public static WSOPCode fromByte(byte b) {
2232
for(WSOPCode oc: WSOPCode.values()) {
2333
if(oc.getValue() == b) {

0 commit comments

Comments
 (0)