Java 类org.apache.http.protocol.HttpProcessorBuilder 实例源码

项目:gondola    文件:LocalTestServer.java   
public LocalTestServer(HttpRequestHandler handler) {
    try {
        setUp();
        HttpProcessor httpproc = HttpProcessorBuilder.create()
            .add(new ResponseDate())
            .add(new ResponseServer(LocalServerTestBase.ORIGIN))
            .add(new ResponseContent())
            .add(new ResponseConnControl())
            .add(new RequestBasicAuth())
            .add(new ResponseBasicUnauthorized()).build();
        this.serverBootstrap.setHttpProcessor(httpproc);
        this.serverBootstrap.registerHandler("*", handler);
        host = start();
    } catch (Exception e) {
        throw new RuntimeException(e);
    }
}
项目:Cherry    文件:WebEngine.java   
private HttpProcessor getHttpProcessor() {
  if (null == _httpProcessor) {
    final HttpProcessorBuilder httpProcessorBuilder = HttpProcessorBuilder.create();

    httpProcessorBuilder.add(new ResponseDate());
    httpProcessorBuilder.add(new ResponseServer(getOriginServer()));
    httpProcessorBuilder.add(new ResponseContent());
    httpProcessorBuilder.add(new ResponseConnControl());
    httpProcessorBuilder.add(getRequestInterceptorService());
    httpProcessorBuilder.add(getResponseInterceptorService());

    _httpProcessor = httpProcessorBuilder.build();
  }

  return _httpProcessor;
}
项目:nio-benchmark    文件:AsyncServer.java   
public void start() throws Exception {
    // Create HTTP protocol basic processing chain
    HttpProcessor httpProcessor = HttpProcessorBuilder.create()
            .add(new ResponseDate()).add(new ResponseContent())
            .add(new ResponseConnControl()).build();
    // Create server
    HttpAsyncService protocolHandler = new HttpAsyncService(httpProcessor,
            uriMapper);
    NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory = new DefaultNHttpServerConnectionFactory();
    IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(
            protocolHandler, connFactory);
    IOReactorConfig config = IOReactorConfig.custom()
            .setIoThreadCount(threads).setSoReuseAddress(true).build();
    ListeningIOReactor ioReactor = new DefaultListeningIOReactor(config);
    // Start server
    ioReactor.listen(new InetSocketAddress(port));
    ioReactor.execute(ioEventDispatch);
}
项目:HttpToolbox    文件:HttpClientExample.java   
/**
 * 完全控制由httpclient发出去的HTTP请求协议的规格
 */
private void turnOffDefaultInterceptor() throws Exception {
    // 注意看这个HTTP请求的协议, 完全由我们决定控制, 只会有一个User-Agent头
    // POST / HTTP/1.1
    // User-Agent: Android
    HttpUriRequest request = RequestBuilder.post()
                                           .setUri("http://baidu.com")
                                           .addHeader("User-Agent", "Android")
                                           .build();

    // 删除所有的HttpRequestInterceptor/HttpResponseInterceptor
    // 这样所有HTTP请求头都可以由我们自己来控制, 因此HTTP header中就不会出现你没有设置过的header了(有一些都是Interceptor自动加进去的)
    // 也不会自动解压gzip的response了
    CloseableHttpClient hc = HttpClients.custom()
                                        .setHttpProcessor(HttpProcessorBuilder.create().build())
                                        .build();

    CloseableHttpResponse response = hc.execute(request);
    System.out.println(EntityUtils.toString(response.getEntity()));
}
项目:haogrgr-test    文件:HttpSrvMain.java   
public static void main(String[] args) throws Exception {
    int port = 8080;
    String docRoot = "D:\\svn_file\\TEST2\\150503\\web-customer";

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseDate())
            .add(new ResponseServer("Haogrgr/1.1")).add(new ResponseContent())
            .add(new ResponseConnControl()).build();

    // Set up request handlers
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", new HttpFileHandler(docRoot));

    // Set up the HTTP service
    HttpService httpService = new HttpService(httpproc, reqistry);

    Thread t = new RequestListenerThread(port, httpService, null);
    t.setDaemon(false);
    t.start();
}
项目:outcomes    文件:TestHttpCore.java   
@Test
public void test() throws ExecutionException, InterruptedException {


    HttpHost target = new HttpHost("localhost");
    BasicConnPool connpool = new BasicConnPool();
    connpool.setMaxTotal(200);
    connpool.setDefaultMaxPerRoute(10);
    connpool.setMaxPerRoute(target, 20);
    Future<BasicPoolEntry> future = connpool.lease(target, null);
    BasicPoolEntry poolEntry = future.get();
    HttpClientConnection conn = poolEntry.getConnection();

    HttpProcessor httpproc = HttpProcessorBuilder.create()
            .add(new ResponseDate())
            .add(new ResponseServer("MyServer-HTTP/1.1"))
            .add(new ResponseContent())
            .add(new ResponseConnControl())
            .build();

    HttpRequestHandler myRequestHandler = new HttpRequestHandler() {

        public void handle(
                HttpRequest request,
                HttpResponse response,
                HttpContext context) throws HttpException, IOException {
            response.setStatusCode(HttpStatus.SC_OK);
            response.setEntity(
                    new StringEntity("some important message",
                            ContentType.TEXT_PLAIN));
        }

    };

    UriHttpRequestHandlerMapper handlerMapper = new UriHttpRequestHandlerMapper();
    handlerMapper.register("/service/*", myRequestHandler);
    HttpService httpService = new HttpService(httpproc, handlerMapper);
}
项目:java-binrepo-proxy    文件:HttpCoreTransportClientImpl.java   
@Override
public TransportFetch httpGetOtherFile(String uri) {
    HttpProcessor httpproc = HttpProcessorBuilder.create()
    .add(new RequestContent())
    .add(new RequestTargetHost())
    .add(new RequestConnControl())
    .add(new RequestUserAgent("Test/1.1"))
    .add(new RequestExpectContinue(true)).build();

    try {
        HttpRequest ascRequest = new BasicHttpRequest("GET", uri);
        LOG.info("Will fetch {}", ascRequest.getRequestLine());
        httpexecutor.preProcess(ascRequest, httpproc, context);
        final HttpResponse otherResponse = httpexecutor.execute(ascRequest, conn, context);
        httpexecutor.postProcess(response, httpproc, context);

        final byte[] otherBody = EntityUtils.toByteArray(otherResponse.getEntity());
        LOG.info("Read body of {} bytes", otherBody.length, " bytes");
        EntityUtils.consume(otherResponse.getEntity());

        boolean keepalive = connStrategy.keepAlive(response, context);
        context.setAttribute(HTTP_CONN_KEEPALIVE, new Boolean(keepalive));

        return new TransportFetch(otherResponse.getStatusLine().getStatusCode(), otherResponse.getStatusLine().getReasonPhrase(), otherBody);
    } catch (HttpException | IOException ex) {
        return new TransportFetch(500, ex.getLocalizedMessage(), null);
    }
}
项目:purecloud-iot    文件:TestClientAuthentication.java   
@Before @Override
public void setUp() throws Exception {
    super.setUp();
    final HttpProcessor httpproc = HttpProcessorBuilder.create()
        .add(new ResponseDate())
        .add(new ResponseServer(LocalServerTestBase.ORIGIN))
        .add(new ResponseContent())
        .add(new ResponseConnControl())
        .add(new RequestBasicAuth())
        .add(new ResponseBasicUnauthorized()).build();
    this.serverBootstrap.setHttpProcessor(httpproc);
}
项目:purecloud-iot    文件:TestClientAuthentication.java   
@Test
public void testBasicAuthenticationSuccessOnNonRepeatablePutExpectContinue() throws Exception {
    final HttpProcessor httpproc = HttpProcessorBuilder.create()
        .add(new ResponseDate())
        .add(new ResponseServer(LocalServerTestBase.ORIGIN))
        .add(new ResponseContent())
        .add(new ResponseConnControl())
        .add(new RequestBasicAuth())
        .add(new ResponseBasicUnauthorized()).build();
    this.serverBootstrap.setHttpProcessor(httpproc)
        .setExpectationVerifier(new AuthExpectationVerifier())
        .registerHandler("*", new AuthHandler());

    final HttpHost target = start();

    final RequestConfig config = RequestConfig.custom()
            .setExpectContinueEnabled(true)
            .build();
    final HttpPut httpput = new HttpPut("/");
    httpput.setConfig(config);
    httpput.setEntity(new InputStreamEntity(
            new ByteArrayInputStream(
                    new byte[] { 1, 2, 3, 4, 5, 6, 7, 8, 9 } ),
                    -1));
    final HttpClientContext context = HttpClientContext.create();
    final TestCredentialsProvider credsProvider = new TestCredentialsProvider(
            new UsernamePasswordCredentials("test", "test"));
    context.setCredentialsProvider(credsProvider);

    final HttpResponse response = this.httpclient.execute(target, httpput, context);
    final HttpEntity entity = response.getEntity();
    Assert.assertEquals(HttpStatus.SC_OK, response.getStatusLine().getStatusCode());
    Assert.assertNotNull(entity);
}
项目:purecloud-iot    文件:TestClientReauthentication.java   
@Before @Override
public void setUp() throws Exception {
    super.setUp();
    final HttpProcessor httpproc = HttpProcessorBuilder.create()
        .add(new ResponseDate())
        .add(new ResponseServer(LocalServerTestBase.ORIGIN))
        .add(new ResponseContent())
        .add(new ResponseConnControl())
        .add(new RequestBasicAuth())
        .add(new ResponseBasicUnauthorized()).build();
    this.serverBootstrap.setHttpProcessor(httpproc);
}
项目:purecloud-iot    文件:TestClientAuthenticationFallBack.java   
@Before @Override
public void setUp() throws Exception {
    super.setUp();
    final HttpProcessor httpproc = HttpProcessorBuilder.create()
        .add(new ResponseDate())
        .add(new ResponseServer(LocalServerTestBase.ORIGIN))
        .add(new ResponseContent())
        .add(new ResponseConnControl())
        .add(new RequestBasicAuth())
        .add(new ResponseBasicUnauthorized()).build();
    this.serverBootstrap.setHttpProcessor(httpproc);
}
项目:oap    文件:NioServer.java   
@SneakyThrows
public NioServer( int port, int workers ) {
    this.port = port;
    this.mapper.register( "/static/*", new NioClasspathResourceHandler( "/static", "/WEB-INF" ) );

    val ioReactorConfig = IOReactorConfig.custom().setIoThreadCount( workers ).build();
    val httpProcessor = HttpProcessorBuilder.create()
        .add( new ResponseDate() )
        .add( new ResponseServer( "OAP Server/1.0" ) )
        .add( new ResponseContent() )
        .add( new ResponseConnControl() )
        .build();

    SSLContext sslContext = getSslContext( port );


    server = ServerBootstrap.bootstrap()
        .setListenerPort( port )
        .setServerInfo( "OAP Server/1.0" )
        .setConnectionReuseStrategy( DefaultClientConnectionReuseStrategy.INSTANCE )
        .setHttpProcessor( httpProcessor )
        .setIOReactorConfig( ioReactorConfig )
        .setSslContext( sslContext )
        .setExceptionLogger( ex -> log.debug( ex.getMessage(), ex ) )
        .setHandlerMapper( mapper )
        .create();
}
项目:OpsDev    文件:ElementalHttpServer.java   
public static void startServer(int port) {

        // 设置端口号  后期需要对端口号进行判断是否已经被占用,并把端口号信息写进模板中
        isRun = true;


        // Set up the HTTP protocol processor
        HttpProcessor httpproc = HttpProcessorBuilder.create()
                .add(new ResponseDate())
                .add(new ResponseServer("Test/1.1"))
                .add(new ResponseContent())
                .add(new ResponseConnControl()).build();

        // Set up request handlers
        UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
        reqistry.register("*", new HttpFileHandler(null));

        // Set up the HTTP service
        httpService = new HttpService(httpproc, reqistry);

        SSLServerSocketFactory sf = null;

        try {
            t = new RequestListenerThread(port, httpService, sf);
        } catch (IOException e) {
            e.printStackTrace();
        }
        t.setDaemon(true);
        t.start();

    }
项目:jntlm    文件:ProxyProxy.java   
ProxyProxy(int listenPort, InetAddress listenAddress, HttpHost proxy) throws IOException {
    this.serversocket = new ServerSocket(listenPort, 0, listenAddress);

    HttpProcessor httpproc = HttpProcessorBuilder.create().add(new ResponseConnControl()).build();
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", new HttpForwardingHandler(proxy));
    this.httpService = new HttpService(httpproc, new UpgradedConnectionAwareReusingStrategy(), null, reqistry);

}
项目:purecloud-iot    文件:TestConnectionReuse.java   
@Test
public void testKeepAliveHeaderRespected() throws Exception {
    final HttpProcessor httpproc = HttpProcessorBuilder.create()
        .add(new ResponseDate())
            .add(new ResponseServer(LocalServerTestBase.ORIGIN))
        .add(new ResponseContent())
        .add(new ResponseConnControl())
        .add(new ResponseKeepAlive()).build();

    this.serverBootstrap.setHttpProcessor(httpproc)
            .registerHandler("/random/*", new RandomHandler());

    this.connManager.setMaxTotal(1);
    this.connManager.setDefaultMaxPerRoute(1);

    final HttpHost target = start();

    HttpResponse response = this.httpclient.execute(target, new HttpGet("/random/2000"));
    EntityUtils.consume(response.getEntity());

    Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());

    response = this.httpclient.execute(target, new HttpGet("/random/2000"));
    EntityUtils.consume(response.getEntity());

    Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());

    // Now sleep for 1.1 seconds and let the timeout do its work
    Thread.sleep(1100);
    response = this.httpclient.execute(target, new HttpGet("/random/2000"));
    EntityUtils.consume(response.getEntity());

    Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());

    // Do another request just under the 1 second limit & make
    // sure we reuse that connection.
    Thread.sleep(500);
    response = this.httpclient.execute(target, new HttpGet("/random/2000"));
    EntityUtils.consume(response.getEntity());

    Assert.assertEquals(1, this.connManager.getTotalStats().getAvailable());
}
项目:manami    文件:JavaUrlConnection.java   
/**
 * Downloads the site and returns it.
 *
 * @since 2.12.9
 * @param infoLink
 *            URL
 * @return Plain xml text of the website.
 */
public String pageAsString(final InfoLink infoLink) {
    MDC.put("infoLink", infoLink.getUrl());
    if (!infoLink.isValid()) {
        log.warn("Seems not be a valid URL");
        return null;
    }

    final CloseableHttpClient hc = HttpClients.custom().setHttpProcessor(HttpProcessorBuilder.create().build()).build();
    final HttpGet request = new HttpGet(infoLink.getUrl());

    newArrayList(request.getAllHeaders()).forEach(header -> {
        request.removeHeader(header);
    });

    request.setProtocolVersion(HttpVersion.HTTP_1_1);
    request.setHeader("Host", "myanimelist.net");
    request.setHeader("User-Agent", "curl/7.53.0");
    request.setHeader("Accept", "*/*");

    String ret = null;

    try {
        final CloseableHttpResponse execute = hc.execute(request);

        if (execute.getStatusLine().getStatusCode() == HTTP_TOO_MANY_CONNECTIONS) {
            log.warn("Too many connections");
            final long waitingTime = ThreadLocalRandom.current().nextLong(MIN_WAITING_TIME, MAX_WAITING_TIME);
            log.warn("Waiting [{}]ms then retry.", waitingTime);
            Thread.sleep(waitingTime);
            return pageAsString(infoLink);
        }

        if (execute.getStatusLine().getStatusCode() != HttpStatus.SC_OK) {
            log.error("Other error, status code [{}]", execute.getStatusLine().getStatusCode());
        }

        ret = EntityUtils.toString(execute.getEntity());
    } catch (final IOException | InterruptedException e) {
        log.error("An error occured during download of infosite: ", e);
    }

    return ret;
}
项目:SparrowWorker    文件:HttpWorker.java   
/**
 * @param args the command line arguments
 * @throws java.lang.Exception
 */
public static void main(String[] args) throws Exception {
    int fixedExecutorSize = 4;

    if (args.length != 2) {
        System.err.println("Invalid command line parameters for worker");
        System.exit(-1);
    }

    // Set worker mode
    String mode = args[1];
    // Creating fixed size executor
    ThreadPoolExecutor taskExecutor = new ThreadPoolExecutor(fixedExecutorSize, fixedExecutorSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());

    int port = Integer.parseInt(args[0]);

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create()
            .add(new ResponseDate())
            .add(new ResponseServer("Test/1.1"))
            .add(new ResponseContent())
            .add(new ResponseConnControl()).build();

    // Set up request handler (either generic or late binding)
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    if (mode.equals("--late")) 
        reqistry.register("*", new LateBindingRequestHandler(taskExecutor));
    else
        reqistry.register("*", new GenericRequestHandler(taskExecutor));


    // Set up the HTTP service
    HttpService httpService = new HttpService(httpproc, reqistry);

    SSLServerSocketFactory sf = null;
    // SSL code removed as it is not needed

    // create a thread to listen for possible scheduler available connections
    Thread t = new RequestListenerThread(port, httpService, sf);
    System.out.println("Request Listener Thread created");
    t.setDaemon(false);
    t.start();

    // main thread should wait for the listener to exit before shutdown the
    // task executor pool
    t.join();

    // shutdown task executor pool and wait for any taskExecutor thread
    // still running
    taskExecutor.shutdown();
    while (!taskExecutor.isTerminated()) {}

    System.out.println("Finished all task executor threads");
}
项目:SparrowScheduler    文件:HttpScheduler.java   
/**
 * @param args the command line arguments
 * @throws java.lang.Exception
 */

public static void main(String[] args) throws Exception{

     if (args.length != 2) {
        System.err.println("Invalid command line parameters for worker");
        System.exit(-1);
    }

    int fixedExecutorSize = 4;

    //Creating fixed size executor
    ThreadPoolExecutor taskCommExecutor = new ThreadPoolExecutor(fixedExecutorSize, fixedExecutorSize, 0L, TimeUnit.MILLISECONDS, new LinkedBlockingQueue<Runnable>());
    // Used for late binding
    JobMap jobMap = new JobMap();

    // Set port number
    int port = Integer.parseInt(args[0]);

     // Set worker mode
    String mode = args[1].substring(2);

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create()
            .add(new ResponseDate())
            .add(new ResponseServer("Test/1.1"))
            .add(new ResponseContent())
            .add(new ResponseConnControl()).build();

    // Set up request handlers
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    // Different handlers for late binding and generic cases
    if (mode.equals("late"))
            reqistry.register("*", new LateBindingRequestHandler(taskCommExecutor, jobMap));
    else
            reqistry.register("*", new GenericRequestHandler(taskCommExecutor, mode));

    // Set up the HTTP service
    HttpService httpService = new HttpService(httpproc, reqistry);

    SSLServerSocketFactory sf = null;

    // create a thread to listen for possible client available connections
    Thread t;
    if (mode.equals("late"))
        t = new LateBindingRequestListenerThread(port, httpService, sf);
    else 
        t = new GenericRequestListenerThread(port, httpService, sf);
    System.out.println("Request Listener Thread created");
    t.setDaemon(false);
    t.start();

    // main thread should wait for the listener to exit before shutdown the
    // task executor pool
    t.join();

    // shutdown task executor pool and wait for any taskCommExecutor thread
    // still running
    taskCommExecutor.shutdown();
    while (!taskCommExecutor.isTerminated()) {}

    System.out.println("Finished all task communication executor threads");
    System.out.println("Finished all tasks");

}
项目:dcl    文件:SimpleHttpServer.java   
public SimpleHttpServer(int port, ConnectedAddressesProvider connectedAddressesProvider){
    this.connFactory = DefaultBHttpServerConnectionFactory.INSTANCE;

    // Set up the HTTP protocol processor
    HttpProcessor httpproc = HttpProcessorBuilder.create()
            .add(new ResponseDate())
            .add(new ResponseServer("Test/1.1"))
            .add(new ResponseContent())
            .add(new ResponseConnControl()).build();

    // Set up request handlers
    UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
    reqistry.register("*", new HttpRequestHandlerNew(connectedAddressesProvider));

    // Set up the HTTP service
    httpService = new HttpService(httpproc, reqistry);

    try {
        serversocket = new ServerSocket(port, 1, InetAddress.getByName("127.0.0.1"));

    System.out.println("Listening on port " + serversocket.getLocalPort());
    while (!Thread.interrupted()) {
        try {
            // Set up HTTP connection
            Socket socket = serversocket.accept();
            System.out.println("Incoming connection from " + socket.getInetAddress());
            HttpServerConnection conn = connFactory.createConnection(socket);

            // Start worker thread
            Thread t = new WorkerThread(httpService, conn);
            t.setDaemon(true);
            t.start();
        } catch (InterruptedIOException ex) {
            break;
        } catch (IOException e) {
            System.err.println("I/O error initialising connection thread: "
                    + e.getMessage());
            break;
        }
    }
    } catch (IOException e1) {
        // TODO Auto-generated catch block
        e1.printStackTrace();
    }
}
项目:Megapode    文件:HttpServer.java   
public static void start(String[] args) throws Exception {

        int port = 8082;
        if (args.length >= 1) {
            port = Integer.parseInt(args[0]);
        }

        ActorSystem system = ActorSystem.create("Application-System");
        Properties config = new Properties();
        config.load(HttpServer.class.getResourceAsStream("/megapode.conf"));
        ActorRef master = system.actorOf(
                Props.create(CoordinatingActor.class, config), "Coordinator");

        // Set up the HTTP protocol processor
        HttpProcessor httpproc = HttpProcessorBuilder.create()
                .add(new ResponseDate()).add(new ResponseServer("Test/1.1"))
                .add(new ResponseContent()).add(new ResponseConnControl())
                .build();

        // Set up request handlers
        UriHttpRequestHandlerMapper reqistry = new UriHttpRequestHandlerMapper();
        reqistry.register("*", new HttpHandler(system, master));

        // Set up the HTTP service
        HttpService httpService = new HttpService(httpproc, reqistry);

        SSLServerSocketFactory sf = null;
        if (port == 8443) {
            // Initialize SSL context
            ClassLoader cl = HttpServer.class.getClassLoader();
            URL url = cl.getResource("my.keystore");
            if (url == null) {
                System.out.println("Keystore not found");
                System.exit(1);
            }
            KeyStore keystore = KeyStore.getInstance("jks");
            keystore.load(url.openStream(), "secret".toCharArray());
            KeyManagerFactory kmfactory = KeyManagerFactory
                    .getInstance(KeyManagerFactory.getDefaultAlgorithm());
            kmfactory.init(keystore, "secret".toCharArray());
            KeyManager[] keymanagers = kmfactory.getKeyManagers();
            SSLContext sslcontext = SSLContext.getInstance("TLS");
            sslcontext.init(keymanagers, null, null);
            sf = sslcontext.getServerSocketFactory();
        }

        RequestListenerThread t = new RequestListenerThread(port, httpService,
                sf);
        t.setDaemon(false);
        t.start();

        t.join();
    }