11/*
2- * Copyright (c) 2023, 2024, Oracle and/or its affiliates. All rights reserved.
2+ * Copyright (c) 2023, Oracle and/or its affiliates. All rights reserved.
33 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44 *
55 * This code is free software; you can redistribute it and/or modify it
2323
2424/*
2525 * @test
26- * @bug 8288109 8235786
26+ * @bug 8288109
2727 * @summary Tests for HttpExchange set/getAttribute
2828 * @library /test/lib
2929 * @run junit/othervm ExchangeAttributeTest
3030 */
3131
32- import com .sun .net .httpserver .Filter ;
33- import com .sun .net .httpserver .HttpContext ;
3432import com .sun .net .httpserver .HttpExchange ;
3533import com .sun .net .httpserver .HttpHandler ;
3634import com .sun .net .httpserver .HttpServer ;
3735import jdk .test .lib .net .URIBuilder ;
38- import org .junit .jupiter .api .AfterAll ;
3936import org .junit .jupiter .api .BeforeAll ;
4037import org .junit .jupiter .api .Test ;
4138
5653
5754public class ExchangeAttributeTest {
5855
59- private static final InetAddress LOOPBACK_ADDR = InetAddress .getLoopbackAddress ();
60- private static final boolean ENABLE_LOGGING = true ;
61- private static final Logger logger = Logger .getLogger ("com.sun.net.httpserver" );
62-
63- private static HttpServer server ;
56+ static final InetAddress LOOPBACK_ADDR = InetAddress .getLoopbackAddress ();
57+ static final boolean ENABLE_LOGGING = true ;
58+ static final Logger logger = Logger .getLogger ("com.sun.net.httpserver" );
6459
6560 @ BeforeAll
66- public static void setup () throws Exception {
61+ public static void setup () {
6762 if (ENABLE_LOGGING ) {
6863 ConsoleHandler ch = new ConsoleHandler ();
6964 logger .setLevel (Level .ALL );
7065 ch .setLevel (Level .ALL );
7166 logger .addHandler (ch );
7267 }
73- server = HttpServer .create (new InetSocketAddress (LOOPBACK_ADDR , 0 ), 10 );
74- server .createContext ("/normal" , new AttribHandler ());
75- final HttpContext filteredCtx = server .createContext ("/filtered" , new AttribHandler ());
76- filteredCtx .getFilters ().add (new AttributeAddingFilter ());
77- server .start ();
78- System .out .println ("Server started at " + server .getAddress ());
79- }
80-
81- @ AfterAll
82- public static void afterAll () {
83- if (server != null ) {
84- System .out .println ("Stopping server " + server .getAddress ());
85- server .stop (0 );
86- }
8768 }
8869
89- /*
90- * Verifies that HttpExchange.setAttribute() allows for null value.
91- */
9270 @ Test
93- public void testNullAttributeValue () throws Exception {
94- try (var client = HttpClient .newBuilder ().proxy (NO_PROXY ).build ()) {
95- var request = HttpRequest .newBuilder (uri (server , "/normal" , null )).build ();
71+ public void testExchangeAttributes () throws Exception {
72+ var handler = new AttribHandler ();
73+ var server = HttpServer .create (new InetSocketAddress (LOOPBACK_ADDR ,0 ), 10 );
74+ server .createContext ("/" , handler );
75+ server .start ();
76+ try {
77+ var client = HttpClient .newBuilder ().proxy (NO_PROXY ).build ();
78+ var request = HttpRequest .newBuilder (uri (server , "" )).build ();
9679 var response = client .send (request , HttpResponse .BodyHandlers .ofString ());
9780 assertEquals (200 , response .statusCode ());
98- }
99- }
100-
101- /*
102- * Verifies that an attribute set on one exchange is accessible to another exchange that
103- * belongs to the same HttpContext.
104- */
105- @ Test
106- public void testSharedAttribute () throws Exception {
107- try (var client = HttpClient .newBuilder ().proxy (NO_PROXY ).build ()) {
108- final var firstReq = HttpRequest .newBuilder (uri (server , "/filtered" , "firstreq" ))
109- .build ();
110- System .out .println ("issuing request " + firstReq );
111- final var firstResp = client .send (firstReq , HttpResponse .BodyHandlers .ofString ());
112- assertEquals (200 , firstResp .statusCode ());
113-
114- // issue the second request
115- final var secondReq = HttpRequest .newBuilder (uri (server , "/filtered" , "secondreq" ))
116- .build ();
117- System .out .println ("issuing request " + secondReq );
118- final var secondResp = client .send (secondReq , HttpResponse .BodyHandlers .ofString ());
119- assertEquals (200 , secondResp .statusCode ());
120-
121- // verify that the filter was invoked for both the requests. the filter internally
122- // does the setAttribute() and getAttribute() and asserts that the attribute value
123- // set by the first exchange was available through the second exchange.
124- assertTrue (AttributeAddingFilter .filteredFirstReq , "Filter wasn't invoked for "
125- + firstReq .uri ());
126- assertTrue (AttributeAddingFilter .filteredSecondReq , "Filter wasn't invoked for "
127- + secondReq .uri ());
81+ } finally {
82+ server .stop (0 );
12883 }
12984 }
13085
13186 // --- infra ---
13287
133- static URI uri (HttpServer server , String path , String query ) throws URISyntaxException {
88+ static URI uri (HttpServer server , String path ) throws URISyntaxException {
13489 return URIBuilder .newBuilder ()
13590 .scheme ("http" )
13691 .loopback ()
13792 .port (server .getAddress ().getPort ())
13893 .path (path )
139- .query (query )
14094 .build ();
14195 }
14296
@@ -158,54 +112,4 @@ public void handle(HttpExchange exchange) throws IOException {
158112 }
159113 }
160114 }
161-
162- private static final class AttributeAddingFilter extends Filter {
163-
164- private static final String ATTR_NAME ="foo-bar" ;
165- private static final String ATTR_VAL ="hello-world" ;
166- private static volatile boolean filteredFirstReq ;
167- private static volatile boolean filteredSecondReq ;
168-
169- @ Override
170- public void doFilter (final HttpExchange exchange , final Chain chain ) throws IOException {
171- if (exchange .getRequestURI ().getQuery ().contains ("firstreq" )) {
172- filteredFirstReq = true ;
173- // add a request attribute through the exchange, for this first request
174- // and at the same time verify that the attribute doesn't already exist
175- final Object attrVal = exchange .getAttribute (ATTR_NAME );
176- if (attrVal != null ) {
177- throw new IOException ("attribute " + ATTR_NAME + " with value: " + attrVal
178- + " unexpectedly present in exchange: " + exchange .getRequestURI ());
179- }
180- // set the value
181- exchange .setAttribute (ATTR_NAME , ATTR_VAL );
182- System .out .println (exchange .getRequestURI () + " set attribute "
183- + ATTR_NAME + "=" + ATTR_VAL );
184- } else if (exchange .getRequestURI ().getQuery ().contains ("secondreq" )) {
185- filteredSecondReq = true ;
186- // verify the attribute is already set and the value is the expected one.
187- final Object attrVal = exchange .getAttribute (ATTR_NAME );
188- if (attrVal == null ) {
189- throw new IOException ("attribute " + ATTR_NAME + " is missing in exchange: "
190- + exchange .getRequestURI ());
191- }
192- if (!ATTR_VAL .equals (attrVal )) {
193- throw new IOException ("unexpected value: " + attrVal + " for attribute "
194- + ATTR_NAME + " in exchange: " + exchange .getRequestURI ());
195- }
196- System .out .println (exchange .getRequestURI () + " found attribute "
197- + ATTR_NAME + "=" + attrVal );
198- } else {
199- // unexpected request
200- throw new IOException ("unexpected request " + exchange .getRequestURI ());
201- }
202- // let the request proceed
203- chain .doFilter (exchange );
204- }
205-
206- @ Override
207- public String description () {
208- return "AttributeAddingFilter" ;
209- }
210- }
211115}
0 commit comments