Skip to content
This repository was archived by the owner on Mar 3, 2026. It is now read-only.

Commit dd627ef

Browse files
committed
Minimal doc on spi API
1 parent 9d954aa commit dd627ef

File tree

5 files changed

+242
-4
lines changed

5 files changed

+242
-4
lines changed

jooby/src/main/java/org/jooby/spi/NativeRequest.java

Lines changed: 79 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,36 +34,113 @@
3434
*/
3535
public interface NativeRequest {
3636
/**
37-
* @return Http method.
37+
* @return The name of the HTTP method with which this request was made, for example, GET, POST,
38+
* or PUT.
3839
*/
3940
String method();
4041

42+
/**
43+
* @return The part of this request's URL from the protocol
44+
* name up to the query string in the first line of the HTTP request
45+
*/
4146
String path();
4247

48+
/**
49+
* @return List with all the parameter names from query string plus any other form/multipart param
50+
* names (excluding file uploads). This method should NOT returns null, absence of params
51+
* is represented by an empty list.
52+
* @throws Exception If param extraction fails.
53+
*/
4354
List<String> paramNames() throws Exception;
4455

56+
/**
57+
* Get all the params for the provided name or a empty list.
58+
*
59+
* @param name Parameter name.
60+
* @return Get all the params for the provided name or a empty list.
61+
* @throws Exception If param parsing fails.
62+
*/
4563
List<String> params(String name) throws Exception;
4664

65+
/**
66+
* Get all the headers for the provided name or a empty list.
67+
*
68+
* @param name Header name.
69+
* @return Get all the headers for the provided name or a empty list.
70+
*/
4771
List<String> headers(String name);
4872

49-
Optional<String> header(String name);
73+
/**
74+
* Get the first header for the provided name or a empty list.
75+
*
76+
* @param name Header name.
77+
* @return The first header for the provided name or a empty list.
78+
*/
79+
default Optional<String> header(final String name) {
80+
List<String> headers = headers(name);
81+
return headers.size() == 0 ? Optional.empty() : Optional.of(headers.get(0));
82+
}
5083

84+
/**
85+
* @return All the header names or an empty list.
86+
*/
5187
List<String> headerNames();
5288

89+
/**
90+
* @return All the cookies or an empty list.
91+
*/
5392
List<Cookie> cookies();
5493

94+
/**
95+
* Get all the files for the provided name or an empty list.
96+
*
97+
* @param name File name.
98+
* @return All the files or an empty list.
99+
* @throws IOException If file parsing fails.
100+
*/
55101
List<NativeUpload> files(String name) throws IOException;
56102

103+
/**
104+
* Input stream that represent the body.
105+
*
106+
* @return Body as an input stream.
107+
* @throws IOException If body read fails.
108+
*/
57109
InputStream in() throws IOException;
58110

111+
/**
112+
* @return The IP address of the client or last proxy that sent the request.
113+
*/
59114
String ip();
60115

116+
/**
117+
* The fully qualified name of the client or the last proxy that sent the request.
118+
* If the engine cannot or chooses not to resolve the hostname (to improve performance),
119+
* this method returns the dotted-string form of the IP address
120+
*
121+
* @return The fully qualified name of the client or the last proxy that sent the request.
122+
*/
61123
String hostname();
62124

125+
/**
126+
* @return The name and version of the protocol the request uses in the form
127+
* <i>protocol/majorVersion.minorVersion</i>, for example, HTTP/1.1
128+
*/
63129
String protocol();
64130

131+
/**
132+
* @return True if this request was made using a secure channel, such as HTTPS.
133+
*/
65134
boolean secure();
66135

136+
/**
137+
* Upgrade the request to something else...like a web socket.
138+
*
139+
* @param type Upgrade type.
140+
* @return A instance of the upgrade.
141+
* @throws Exception If the upgrade fails or it is un-supported.
142+
* @see NativeWebSocket
143+
*/
67144
<T> T upgrade(Class<T> type) throws Exception;
68145

69146
}

jooby/src/main/java/org/jooby/spi/NativeResponse.java

Lines changed: 65 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -25,33 +25,96 @@
2525

2626
import org.jooby.Cookie;
2727

28+
/**
29+
* Minimal/basic implementation of HTTP request. A server implementor must provide an implementation
30+
* of {@link NativeResponse}.
31+
*
32+
* @author edgar
33+
* @since 0.5.0
34+
*/
2835
public interface NativeResponse {
2936

30-
31-
37+
/**
38+
* Set a response cookie.
39+
*
40+
* @param cookie A cookie to add.
41+
*/
3242
void cookie(Cookie cookie);
3343

44+
/**
45+
* Clear a cookie and force a client to expire and delete it.
46+
*
47+
* @param name Cookie's name.
48+
*/
3449
void clearCookie(String name);
3550

51+
/**
52+
* Get a response header (previously set).
53+
*
54+
* @param name Header's name.
55+
* @return Header.
56+
*/
3657
default Optional<String> header(final String name) {
3758
List<String> headers = headers(name);
3859
return headers.size() == 0 ? Optional.empty() : Optional.of(headers.get(0));
3960
}
4061

62+
/**
63+
* Get all the response headers for the provided name.
64+
*
65+
* @param name A header's name
66+
* @return All the response headers.
67+
*/
4168
List<String> headers(String name);
4269

70+
/**
71+
* Set a response header.
72+
*
73+
* @param name Header's name.
74+
* @param value Header's value.
75+
*/
4376
void header(String name, String value);
4477

78+
/**
79+
* Get an output stream and prepare to send a response. You need to make sure status and headers
80+
* are set beforing calling this method. Attempt to set status and headers after calling output
81+
* stream if undefined and might result in error or just ignored.
82+
*
83+
* @param bufferSize The preferred buffer size for sending a response. Default buffer size is:
84+
* <code>16k</code>. Default buffer size by setting the:
85+
* <code>server.http.ResponseBufferSize</code> property in your <code>*.conf property file.
86+
* If the <code>Content-Length</code> header was set and it is less than buffer size, the
87+
* the <code>Content-Length</code> will be used it as buffer size.
88+
* @return
89+
* @throws IOException
90+
*/
4591
OutputStream out(int bufferSize) throws IOException;
4692

93+
/**
94+
* @return HTTP response status.
95+
*/
4796
int statusCode();
4897

98+
/**
99+
* Set the HTTP response status.
100+
*
101+
* @param code A HTTP response status.
102+
*/
49103
void statusCode(int code);
50104

105+
/**
106+
* @return True if response was committed to the client.
107+
*/
51108
boolean committed();
52109

110+
/**
111+
* End a response and clean up any resources used it.
112+
*/
53113
void end();
54114

115+
/**
116+
* Reset the HTTP status, headers and response buffer is need it.
117+
*/
55118
void reset();
56119

57120
}

jooby/src/main/java/org/jooby/spi/NativeUpload.java

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,14 +24,41 @@
2424
import java.util.List;
2525
import java.util.Optional;
2626

27+
/**
28+
* File upload from multipart/form-data post.
29+
*
30+
* @author edgar
31+
* @since 0.5.0
32+
*/
2733
public interface NativeUpload extends Closeable {
2834

35+
/**
36+
* @return File name.
37+
*/
2938
String name();
3039

40+
/**
41+
* File header, like <code>Content-Type</code>, <code>Content-Transfer-Encoding</code>, etc.
42+
*
43+
* @param name Header's name.
44+
* @return A header value or empty optional.
45+
*/
3146
Optional<String> header(String name);
3247

48+
/**
49+
* Get all the file headers for the given name.
50+
*
51+
* @param name A header's name.
52+
* @return All available values or and empty list.
53+
*/
3354
List<String> headers(String name);
3455

56+
/**
57+
* Get the actual file link/reference and do something with it.
58+
*
59+
* @return A file from local file system.
60+
* @throws IOException If file failed to read/write from local file system.
61+
*/
3562
File file() throws IOException;
3663

3764
}

jooby/src/main/java/org/jooby/spi/NativeWebSocket.java

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,30 +26,96 @@
2626

2727
import org.jooby.WebSocket;
2828

29+
/**
30+
* A web socket upgrade created from {@link NativeRequest#upgrade(Class)}.
31+
*
32+
* @author edgar
33+
* @since 0.5.0
34+
*/
2935
public interface NativeWebSocket {
3036

37+
/**
38+
* Close the web socket.
39+
*
40+
* @param status A web socket close status.
41+
* @param reason A close reason.
42+
*/
3143
void close(int status, String reason);
3244

45+
/**
46+
* Resume reads.
47+
*/
3348
void resume();
3449

50+
/**
51+
* Set the onconnect callback. It will be execute once per each client.
52+
*
53+
* @param callback A callback.
54+
*/
3555
void onConnect(Runnable callback);
3656

57+
/**
58+
* Set the ontext message callback. On message arrival the callback will be executed.
59+
*
60+
* @param callback A callback.
61+
*/
3762
void onTextMessage(Consumer<String> callback);
3863

64+
/**
65+
* Set the onbinary message callback. On message arrival the callback will be executed.
66+
*
67+
* @param callback A callback.
68+
*/
3969
void onBinaryMessage(Consumer<ByteBuffer> callback);
4070

71+
/**
72+
* Set the onclose message callback. It will be executed when clients close a connection and/or
73+
* connection idle timeout.
74+
*
75+
* @param callback A callback.
76+
*/
4177
void onCloseMessage(BiConsumer<Integer, Optional<String>> callback);
4278

79+
/**
80+
* Set the onerror message callback. It will be executed on errors.
81+
*
82+
* @param callback A callback.
83+
*/
4384
void onErrorMessage(Consumer<Throwable> callback);
4485

86+
/**
87+
* Pause reads.
88+
*/
4589
void pause();
4690

91+
/**
92+
* Terminate immediately a connection.
93+
*
94+
* @throws IOException If termination fails.
95+
*/
4796
void terminate() throws IOException;
4897

98+
/**
99+
* Send a binary message to the client.
100+
*
101+
* @param data Message to send.
102+
* @param success Success callback.
103+
* @param err Error callback.
104+
*/
49105
void send(ByteBuffer data, WebSocket.SuccessCallback success, WebSocket.ErrCallback err);
50106

107+
/**
108+
* Send a text message to the client.
109+
*
110+
* @param data Message to send.
111+
* @param success Success callback.
112+
* @param err Error callback.
113+
*/
51114
void send(String data, WebSocket.SuccessCallback success, WebSocket.ErrCallback err);
52115

116+
/**
117+
* @return True if the websocket connection is open.
118+
*/
53119
boolean isOpen();
54120

55121
}

jooby/src/main/java/org/jooby/spi/Server.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,11 @@ public interface Server {
4040
*/
4141
void stop() throws Exception;
4242

43+
/**
44+
* Waits for this thread to die.
45+
*
46+
* @throws InterruptedException If wait didn't success.
47+
*/
4348
void join() throws InterruptedException;
4449

4550
}

0 commit comments

Comments
 (0)