// Path: jadler-core/src/main/java/net/jadler/RequestManager.java
// public interface RequestManager {
// 
//     /**
//      * Returns a stub response for the given request. The request is recorded for further mocking (verifying).
//      *
//      * @param req http request to return a stub response for
//      * @return definition of a stub response to be returned by the stub http server (never returns {@code null})
//      */
//     StubResponse provideStubResponseFor(Request req);
// 
// 
//     /**
//      * Verifies whether the number of received http requests fitting the given predicates is as expected. Basically
//      * at first this operation computes the exact number of http requests received so far fitting the given predicates
//      * and then verifies whether the number is as expected. If not a {@link net.jadler.mocking.VerificationException}
//      * is thrown and the exact reason is logged on the {@code INFO} level.
//      *
//      * @param requestPredicates   predicates about the http requests received so far (cannot be {@code null}, can be
//      *                            empty however)
//      * @param nrRequestsPredicate a predicate about the number of http requests received so far which fit the given
//      *                            request predicates (cannot be {@code null})
//      * @throws net.jadler.mocking.VerificationException if the verification fails
//      */
//     void evaluateVerification(Collection<Matcher<? super Request>> requestPredicates,
//                               Matcher<Integer> nrRequestsPredicate);
// 
// 
//     /**
//      * @param predicates predicates to be applied on all incoming http requests
//      * @return number of requests recorded by {@link #provideStubResponseFor(net.jadler.Request)} matching the
//      * given matchers
//      * @deprecated this (rather internal) method has been deprecated. Please use
//      * {@link #evaluateVerification(java.util.Collection, org.hamcrest.Matcher)} instead
//      */
//     @Deprecated
//     int numberOfRequestsMatching(Collection<Matcher<? super Request>> predicates);
// }
// 
// Path: jadler-core/src/main/java/net/jadler/exception/JadlerException.java
// public class JadlerException extends RuntimeException {
// 
//     /**
//      * @param cause the cause of this exception
//      * @see RuntimeException#RuntimeException(java.lang.Throwable)
//      */
//     public JadlerException(final Throwable cause) {
//         super(cause);
//     }
// 
//     /**
//      * @param message the detail message
//      * @param cause   the cause of this exception
//      * @see RuntimeException#RuntimeException(java.lang.String, java.lang.Throwable)
//      */
//     public JadlerException(final String message, final Throwable cause) {
//         super(message, cause);
//     }
// 
//     /**
//      * @param message the detail message
//      * @see RuntimeException#RuntimeException(java.lang.String)
//      */
//     public JadlerException(final String message) {
//         super(message);
//     }
// 
//     /**
//      * @see RuntimeException#RuntimeException()
//      */
//     public JadlerException() {
//     }
// }
// 
// Path: jadler-core/src/main/java/net/jadler/stubbing/server/StubHttpServer.java
// public interface StubHttpServer {
// 
//     /**
//      * Registers a response provider. This component provides a response prescription (in form
//      * of a {@link net.jadler.stubbing.StubResponse} instance) for a given http request.
//      *
//      * @param requestManager response provider to use to retrieve response prescriptions.
//      */
//     void registerRequestManager(RequestManager requestManager);
// 
// 
//     /**
//      * Starts the underlying http server. From now, the server must be able to respond
//      * according to prescriptions returned from the registered {@link StubHttpServerManager} instance.
//      *
//      * @throws Exception when ugh... something went wrong
//      */
//     void start() throws Exception;
// 
// 
//     /**
//      * Stops the underlying http server.
//      *
//      * @throws Exception when an error occurred while stopping the server
//      */
//     void stop() throws Exception;
// 
// 
//     /**
//      * @return HTTP server port
//      */
//     int getPort();
// }


import com.sun.net.httpserver.HttpServer;
import net.jadler.RequestManager;
import net.jadler.exception.JadlerException;
import net.jadler.stubbing.server.StubHttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import static org.apache.commons.lang.Validate.isTrue;
import static org.apache.commons.lang.Validate.notNull;
/*
 * Copyright (c) 2012 - 2016 Jadler contributors
 * This program is made available under the terms of the MIT License.
 */
package net.jadler.stubbing.server.jdk;





/**
 * Stub server implementation based on {@link HttpServer} which is part of JDK.
 */
public class JdkStubHttpServer implements StubHttpServer {

    private final HttpServer server;

    public JdkStubHttpServer(final int port) {
        isTrue(port >= 0, "port cannot be a negative number");

        try {
            server = HttpServer.create(new InetSocketAddress(port), 0);
        } catch (final IOException e) {
            throw new JadlerException("Cannot create JDK server", e);
        }
    }

    public JdkStubHttpServer() {
        this(0);
    }

    @Override
    public void registerRequestManager(final RequestManager ruleProvider) {
        notNull(ruleProvider, "ruleProvider cannot be null");
        server.createContext("/", new JdkHandler(ruleProvider));
    }

    @Override
// Path: jadler-core/src/main/java/net/jadler/RequestManager.java
// public interface RequestManager {
// 
//     /**
//      * Returns a stub response for the given request. The request is recorded for further mocking (verifying).
//      *
//      * @param req http request to return a stub response for
//      * @return definition of a stub response to be returned by the stub http server (never returns {@code null})
//      */
//     StubResponse provideStubResponseFor(Request req);
// 
// 
//     /**
//      * Verifies whether the number of received http requests fitting the given predicates is as expected. Basically
//      * at first this operation computes the exact number of http requests received so far fitting the given predicates
//      * and then verifies whether the number is as expected. If not a {@link net.jadler.mocking.VerificationException}
//      * is thrown and the exact reason is logged on the {@code INFO} level.
//      *
//      * @param requestPredicates   predicates about the http requests received so far (cannot be {@code null}, can be
//      *                            empty however)
//      * @param nrRequestsPredicate a predicate about the number of http requests received so far which fit the given
//      *                            request predicates (cannot be {@code null})
//      * @throws net.jadler.mocking.VerificationException if the verification fails
//      */
//     void evaluateVerification(Collection<Matcher<? super Request>> requestPredicates,
//                               Matcher<Integer> nrRequestsPredicate);
// 
// 
//     /**
//      * @param predicates predicates to be applied on all incoming http requests
//      * @return number of requests recorded by {@link #provideStubResponseFor(net.jadler.Request)} matching the
//      * given matchers
//      * @deprecated this (rather internal) method has been deprecated. Please use
//      * {@link #evaluateVerification(java.util.Collection, org.hamcrest.Matcher)} instead
//      */
//     @Deprecated
//     int numberOfRequestsMatching(Collection<Matcher<? super Request>> predicates);
// }
// 
// Path: jadler-core/src/main/java/net/jadler/exception/JadlerException.java
// public class JadlerException extends RuntimeException {
// 
//     /**
//      * @param cause the cause of this exception
//      * @see RuntimeException#RuntimeException(java.lang.Throwable)
//      */
//     public JadlerException(final Throwable cause) {
//         super(cause);
//     }
// 
//     /**
//      * @param message the detail message
//      * @param cause   the cause of this exception
//      * @see RuntimeException#RuntimeException(java.lang.String, java.lang.Throwable)
//      */
//     public JadlerException(final String message, final Throwable cause) {
//         super(message, cause);
//     }
// 
//     /**
//      * @param message the detail message
//      * @see RuntimeException#RuntimeException(java.lang.String)
//      */
//     public JadlerException(final String message) {
//         super(message);
//     }
// 
//     /**
//      * @see RuntimeException#RuntimeException()
//      */
//     public JadlerException() {
//     }
// }
// 
// Path: jadler-core/src/main/java/net/jadler/stubbing/server/StubHttpServer.java
// public interface StubHttpServer {
// 
//     /**
//      * Registers a response provider. This component provides a response prescription (in form
//      * of a {@link net.jadler.stubbing.StubResponse} instance) for a given http request.
//      *
//      * @param requestManager response provider to use to retrieve response prescriptions.
//      */
//     void registerRequestManager(RequestManager requestManager);
// 
// 
//     /**
//      * Starts the underlying http server. From now, the server must be able to respond
//      * according to prescriptions returned from the registered {@link StubHttpServerManager} instance.
//      *
//      * @throws Exception when ugh... something went wrong
//      */
//     void start() throws Exception;
// 
// 
//     /**
//      * Stops the underlying http server.
//      *
//      * @throws Exception when an error occurred while stopping the server
//      */
//     void stop() throws Exception;
// 
// 
//     /**
//      * @return HTTP server port
//      */
//     int getPort();
// }


// Path: jadler-jdk/src/main/java/net/jadler/stubbing/server/jdk/JdkStubHttpServer.java
import com.sun.net.httpserver.HttpServer;
import net.jadler.RequestManager;
import net.jadler.exception.JadlerException;
import net.jadler.stubbing.server.StubHttpServer;
import java.io.IOException;
import java.net.InetSocketAddress;
import static org.apache.commons.lang.Validate.isTrue;
import static org.apache.commons.lang.Validate.notNull;

/*
 * Copyright (c) 2012 - 2016 Jadler contributors
 * This program is made available under the terms of the MIT License.
 */
package net.jadler.stubbing.server.jdk;





/**
 * Stub server implementation based on {@link HttpServer} which is part of JDK.
 */
public class JdkStubHttpServer implements StubHttpServer {

    private final HttpServer server;

    public JdkStubHttpServer(final int port) {
        isTrue(port >= 0, "port cannot be a negative number");

        try {
            server = HttpServer.create(new InetSocketAddress(port), 0);
        } catch (final IOException e) {
            throw new JadlerException("Cannot create JDK server", e);
        }
    }

    public JdkStubHttpServer() {
        this(0);
    }

    @Override
    public void registerRequestManager(final RequestManager ruleProvider) {
        notNull(ruleProvider, "ruleProvider cannot be null");
        server.createContext("/", new JdkHandler(ruleProvider));
    }

    @Override
    public void start() throws Exception {