Skip to content

Commit 31320c3

Browse files
author
Julia Boes
committedMay 20, 2021
8267262: com/sun/net/httpserver/Filter improve API documentation of static methods
Reviewed-by: dfuchs, chegar
1 parent 7dcb9fd commit 31320c3

File tree

2 files changed

+49
-5
lines changed

2 files changed

+49
-5
lines changed
 

‎src/jdk.httpserver/share/classes/com/sun/net/httpserver/Filter.java

+6-3
Original file line numberDiff line numberDiff line change
@@ -149,7 +149,8 @@ public abstract void doFilter (HttpExchange exchange, Chain chain)
149149
* <p>The {@link Consumer operation} is the effective implementation of the
150150
* filter. It is executed for each {@code HttpExchange} before invoking
151151
* either the next filter in the chain or the exchange handler (if this is
152-
* the final filter in the chain).
152+
* the final filter in the chain). Exceptions thrown by the
153+
* {@code operation} are not handled by the filter.
153154
*
154155
* @apiNote
155156
* A beforeHandler filter is typically used to examine or modify the
@@ -197,7 +198,8 @@ public String description() {
197198
* <p>The {@link Consumer operation} is the effective implementation of the
198199
* filter. It is executed for each {@code HttpExchange} after invoking
199200
* either the next filter in the chain or the exchange handler (if this
200-
* filter is the final filter in the chain).
201+
* filter is the final filter in the chain). Exceptions thrown by the
202+
* {@code operation} are not handled by the filter.
201203
*
202204
* @apiNote
203205
* An afterHandler filter is typically used to examine the exchange state
@@ -207,7 +209,8 @@ public String description() {
207209
* executed. The filter {@code operation} is not expected to handle the
208210
* exchange or {@linkplain HttpExchange#sendResponseHeaders(int, long) send the response headers}.
209211
* Doing so is likely to fail, since the exchange has commonly been handled
210-
* before the operation is invoked.
212+
* before the {@code operation} is invoked. More specifically, the response
213+
* may be sent before the filter {@code operation} is executed.
211214
*
212215
* <p> Example of adding a filter that logs the response code of all exchanges:
213216
* <pre>{@code

‎test/jdk/com/sun/net/httpserver/FilterTest.java

+43-2
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323

2424
/*
2525
* @test
26+
* @bug 8267262
2627
* @summary Tests for Filter static factory methods
2728
* @run testng/othervm FilterTest
2829
*/
@@ -47,13 +48,16 @@
4748
import com.sun.net.httpserver.HttpHandler;
4849
import com.sun.net.httpserver.HttpServer;
4950
import static java.net.http.HttpClient.Builder.NO_PROXY;
51+
import org.testng.annotations.DataProvider;
5052
import org.testng.annotations.Test;
5153
import org.testng.annotations.BeforeTest;
5254
import static org.testng.Assert.*;
5355

5456
public class FilterTest {
5557

5658
static final Class<NullPointerException> NPE = NullPointerException.class;
59+
static final Class<IOException> IOE = IOException.class;
60+
5761
static final InetAddress LOOPBACK_ADDR = InetAddress.getLoopbackAddress();
5862
static final boolean ENABLE_LOGGING = true;
5963
static final Logger logger = Logger.getLogger("com.sun.net.httpserver");
@@ -72,19 +76,56 @@ public void setup() {
7276
public void testNull() {
7377
expectThrows(NPE, () -> Filter.beforeHandler(null, e -> e.getResponseHeaders().set("X-Foo", "Bar")));
7478
expectThrows(NPE, () -> Filter.beforeHandler("Some description", null));
75-
expectThrows(NPE, () -> Filter.afterHandler(null, HttpExchange::getResponseCode));
79+
7680
expectThrows(NPE, () -> Filter.afterHandler("Some description", null));
81+
expectThrows(NPE, () -> Filter.afterHandler(null, HttpExchange::getResponseCode));
7782
}
7883

7984
@Test
8085
public void testDescription() {
8186
var desc = "Some description";
87+
8288
var beforeFilter = Filter.beforeHandler(desc, HttpExchange::getRequestBody);
83-
var afterFilter = Filter.afterHandler(desc, HttpExchange::getResponseCode);
8489
assertEquals(desc, beforeFilter.description());
90+
91+
var afterFilter = Filter.afterHandler(desc, HttpExchange::getResponseCode);
8592
assertEquals(desc, afterFilter.description());
8693
}
8794

95+
@DataProvider
96+
public static Object[][] throwingFilters() {
97+
return new Object[][] {
98+
{Filter.beforeHandler("before RE", e -> { throw new RuntimeException(); }), IOE},
99+
{Filter.beforeHandler("before AE", e -> { throw new AssertionError(); }), IOE},
100+
101+
{Filter.afterHandler( "after RE", e -> { throw new RuntimeException(); }), null},
102+
{Filter.afterHandler( "after AE", e -> { throw new AssertionError(); }), null},
103+
};
104+
}
105+
106+
@Test(dataProvider = "throwingFilters")
107+
public void testException(Filter filter, Class<Exception> exception)
108+
throws Exception
109+
{
110+
var handler = new EchoHandler();
111+
var server = HttpServer.create(new InetSocketAddress(LOOPBACK_ADDR,0), 10);
112+
server.createContext("/", handler).getFilters().add(filter);
113+
server.start();
114+
try {
115+
var client = HttpClient.newBuilder().proxy(NO_PROXY).build();
116+
var request = HttpRequest.newBuilder(uri(server, "")).build();
117+
if (exception != null) {
118+
expectThrows(exception, () -> client.send(request, HttpResponse.BodyHandlers.ofString()));
119+
} else {
120+
var response = client.send(request, HttpResponse.BodyHandlers.ofString());
121+
assertEquals(response.statusCode(), 200);
122+
assertEquals(response.body(), "hello world");
123+
}
124+
} finally {
125+
server.stop(0);
126+
}
127+
}
128+
88129
@Test
89130
public void testBeforeHandler() throws Exception {
90131
var handler = new EchoHandler();

0 commit comments

Comments
 (0)