test/jdk/java/net/httpclient/security/Security.java
branchhttp-client-branch
changeset 55763 634d8e14c172
parent 47216 71c04702a3d5
child 55764 34d7cc00f87a
equal deleted inserted replaced
55762:e947a3a50a95 55763:634d8e14c172
    80 import java.util.List;
    80 import java.util.List;
    81 import java.util.concurrent.CompletableFuture;
    81 import java.util.concurrent.CompletableFuture;
    82 import java.util.concurrent.CompletionException;
    82 import java.util.concurrent.CompletionException;
    83 import java.util.concurrent.CompletionStage;
    83 import java.util.concurrent.CompletionStage;
    84 import java.util.concurrent.ExecutionException;
    84 import java.util.concurrent.ExecutionException;
    85 import java.util.concurrent.Executor;
       
    86 import java.util.concurrent.Executors;
    85 import java.util.concurrent.Executors;
    87 import java.util.concurrent.ExecutorService;
    86 import java.util.concurrent.ExecutorService;
    88 import java.util.concurrent.Flow;
    87 import java.util.concurrent.Flow;
    89 import java.util.logging.ConsoleHandler;
    88 import java.util.logging.ConsoleHandler;
    90 import java.util.logging.Level;
    89 import java.util.logging.Level;
   295                 HttpResponse.BodyHandler<String> sth = asString();
   294                 HttpResponse.BodyHandler<String> sth = asString();
   296 
   295 
   297                 CompletableFuture<HttpResponse<String>> cf =
   296                 CompletableFuture<HttpResponse<String>> cf =
   298                     client.sendAsync(request, new HttpResponse.BodyHandler<String>() {
   297                     client.sendAsync(request, new HttpResponse.BodyHandler<String>() {
   299                         @Override
   298                         @Override
   300                         public HttpResponse.BodyProcessor<String> apply(int status, HttpHeaders responseHeaders)  {
   299                         public HttpResponse.BodySubscriber<String> apply(int status, HttpHeaders responseHeaders)  {
   301                             final HttpResponse.BodyProcessor<String> stproc = sth.apply(status, responseHeaders);
   300                             final HttpResponse.BodySubscriber<String> stproc = sth.apply(status, responseHeaders);
   302                             return new HttpResponse.BodyProcessor<String>() {
   301                             return new HttpResponse.BodySubscriber<String>() {
   303                                 @Override
   302                                 @Override
   304                                 public CompletionStage<String> getBody() {
   303                                 public CompletionStage<String> getBody() {
   305                                     return stproc.getBody();
   304                                     return stproc.getBody();
   306                                 }
   305                                 }
   307                                 @Override
   306                                 @Override
   308                                 public void onNext(ByteBuffer item) {
   307                                 public void onNext(List<ByteBuffer> item) {
   309                                     SecurityManager sm = System.getSecurityManager();
   308                                     SecurityManager sm = System.getSecurityManager();
   310                                     // should succeed.
   309                                     // should succeed.
   311                                     sm.checkPermission(new RuntimePermission("foobar"));
   310                                     sm.checkPermission(new RuntimePermission("foobar"));
   312                                     // do some mischief here
   311                                     // do some mischief here
   313                                     System.setSecurityManager(null);
   312                                     System.setSecurityManager(null);
   335                     cf.join();
   334                     cf.join();
   336                 } catch (CompletionException e) {
   335                 } catch (CompletionException e) {
   337                     Throwable t = e.getCause();
   336                     Throwable t = e.getCause();
   338                     if (t instanceof SecurityException)
   337                     if (t instanceof SecurityException)
   339                         throw (SecurityException)t;
   338                         throw (SecurityException)t;
       
   339                     else if ((t instanceof IOException)
       
   340                               && (t.getCause() instanceof SecurityException))
       
   341                         throw ((SecurityException)t.getCause());
   340                     else
   342                     else
   341                         throw new RuntimeException(t);
   343                         throw new RuntimeException(t);
   342                 }
   344                 }
   343             })
   345             })
   344         };
   346         };
   417             TestAndResult tr = tests[testnum];
   419             TestAndResult tr = tests[testnum];
   418             runtest(tr.test, policy, tr.result);
   420             runtest(tr.test, policy, tr.result);
   419         } finally {
   421         } finally {
   420             s1.stop(0);
   422             s1.stop(0);
   421             executor.shutdownNow();
   423             executor.shutdownNow();
   422             for (HttpClient client : clients) {
       
   423                 Executor e = client.executor();
       
   424                 if (e instanceof ExecutorService) {
       
   425                     ((ExecutorService)e).shutdownNow();
       
   426                 }
       
   427             }
       
   428         }
   424         }
   429     }
   425     }
   430 
   426 
   431     public static void initServer() throws Exception {
   427     public static void initServer() throws Exception {
   432         String portstring = System.getProperty("port.number");
   428         String portstring = System.getProperty("port.number");