< prev index next >

test/jdk/java/net/HttpURLConnection/SetAuthenticator/HTTPTestServer.java

Print this page


   1 /*
   2  * Copyright (c) 2016, 2017, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


 191         protected abstract B createBindable() throws IOException;
 192 
 193         protected abstract SocketAddress getAddress(B bindable);
 194 
 195         protected abstract void close(B bindable) throws IOException;
 196     }
 197 
 198     /*
 199      * Used to create ServerSocket for a proxy.
 200      */
 201     private static final class ServerSocketFactory
 202             extends SocketBindableFactory<ServerSocket> {
 203         private static final ServerSocketFactory instance = new ServerSocketFactory();
 204 
 205         static ServerSocket create() throws IOException {
 206             return instance.createInternal();
 207         }
 208 
 209         @Override
 210         protected ServerSocket createBindable() throws IOException {
 211             return new ServerSocket(0, 0, InetAddress.getByName("127.0.0.1"));

 212         }
 213 
 214         @Override
 215         protected SocketAddress getAddress(ServerSocket socket) {
 216             return socket.getLocalSocketAddress();
 217         }
 218 
 219         @Override
 220         protected void close(ServerSocket socket) throws IOException {
 221             socket.close();
 222         }
 223     }
 224 
 225     /*
 226      * Used to create HttpServer for a NTLMTestServer.
 227      */
 228     private static abstract class WebServerFactory<S extends HttpServer>
 229             extends SocketBindableFactory<S> {
 230         @Override
 231         protected S createBindable() throws IOException {
 232             S server = newHttpServer();
 233             server.bind(new InetSocketAddress("127.0.0.1", 0), 0);

 234             return server;
 235         }
 236 
 237         @Override
 238         protected SocketAddress getAddress(S server) {
 239             return server.getAddress();
 240         }
 241 
 242         @Override
 243         protected void close(S server) throws IOException {
 244             server.stop(1);
 245         }
 246 
 247         /*
 248          * Returns a HttpServer or a HttpsServer in different subclasses.
 249          */
 250         protected abstract S newHttpServer() throws IOException;
 251     }
 252 
 253     private static final class HttpServerFactory extends WebServerFactory<HttpServer> {


   1 /*
   2  * Copyright (c) 2016, 2019, Oracle and/or its affiliates. All rights reserved.
   3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
   4  *
   5  * This code is free software; you can redistribute it and/or modify it
   6  * under the terms of the GNU General Public License version 2 only, as
   7  * published by the Free Software Foundation.
   8  *
   9  * This code is distributed in the hope that it will be useful, but WITHOUT
  10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
  11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
  12  * version 2 for more details (a copy is included in the LICENSE file that
  13  * accompanied this code).
  14  *
  15  * You should have received a copy of the GNU General Public License version
  16  * 2 along with this work; if not, write to the Free Software Foundation,
  17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18  *
  19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
  20  * or visit www.oracle.com if you need additional information or have any
  21  * questions.
  22  */


 191         protected abstract B createBindable() throws IOException;
 192 
 193         protected abstract SocketAddress getAddress(B bindable);
 194 
 195         protected abstract void close(B bindable) throws IOException;
 196     }
 197 
 198     /*
 199      * Used to create ServerSocket for a proxy.
 200      */
 201     private static final class ServerSocketFactory
 202             extends SocketBindableFactory<ServerSocket> {
 203         private static final ServerSocketFactory instance = new ServerSocketFactory();
 204 
 205         static ServerSocket create() throws IOException {
 206             return instance.createInternal();
 207         }
 208 
 209         @Override
 210         protected ServerSocket createBindable() throws IOException {
 211             InetAddress address = InetAddress.getLoopbackAddress();
 212             return new ServerSocket(0, 0, address);
 213         }
 214 
 215         @Override
 216         protected SocketAddress getAddress(ServerSocket socket) {
 217             return socket.getLocalSocketAddress();
 218         }
 219 
 220         @Override
 221         protected void close(ServerSocket socket) throws IOException {
 222             socket.close();
 223         }
 224     }
 225 
 226     /*
 227      * Used to create HttpServer for a NTLMTestServer.
 228      */
 229     private static abstract class WebServerFactory<S extends HttpServer>
 230             extends SocketBindableFactory<S> {
 231         @Override
 232         protected S createBindable() throws IOException {
 233             S server = newHttpServer();
 234             InetAddress address = InetAddress.getLoopbackAddress();
 235             server.bind(new InetSocketAddress(address, 0), 0);
 236             return server;
 237         }
 238 
 239         @Override
 240         protected SocketAddress getAddress(S server) {
 241             return server.getAddress();
 242         }
 243 
 244         @Override
 245         protected void close(S server) throws IOException {
 246             server.stop(1);
 247         }
 248 
 249         /*
 250          * Returns a HttpServer or a HttpsServer in different subclasses.
 251          */
 252         protected abstract S newHttpServer() throws IOException;
 253     }
 254 
 255     private static final class HttpServerFactory extends WebServerFactory<HttpServer> {


< prev index next >