Example usage for org.apache.http.protocol ResponseContent ResponseContent

List of usage examples for org.apache.http.protocol ResponseContent ResponseContent

Introduction

In this page you can find the example usage for org.apache.http.protocol ResponseContent ResponseContent.

Prototype

public ResponseContent() 

Source Link

Usage

From source file:se.ginkou.interfaceio.InterfaceServer.java

public static void startServer() throws Exception {
    // HTTP parameters for the server
    HttpParams params = new SyncBasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, 5000)
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
            .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "HttpComponents/1.1");

    // Create HTTP protocol processing chain
    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] {
            // Use standard server-side protocol interceptors
            new ResponseDate(), new ResponseServer(), new ResponseContent(), new ResponseConnControl() });

    // Set up request handlers
    HttpAsyncRequestHandlerRegistry reqistry = new HttpAsyncRequestHandlerRegistry();
    reqistry.register("*/datatables", new DataTablesHandler());
    reqistry.register("*/loginmodules", new RuleFileHandler());
    reqistry.register("*/login", new LoginHandler());
    reqistry.register("*", new HttpFileHandler(new File("website")));
    reqistry.register("*/ping", new TestHandler());

    // Create server-side HTTP protocol handler
    HttpAsyncService protocolHandler = new HttpAsyncService(httpproc, new DefaultConnectionReuseStrategy(),
            reqistry, params) {/*from   w w w.ja  v  a 2  s  . c om*/

        @Override
        public void connected(final NHttpServerConnection conn) {
            System.out.println(conn + ": connection open");
            super.connected(conn);
        }

        @Override
        public void closed(final NHttpServerConnection conn) {
            System.out.println(conn + ": connection closed");
            super.closed(conn);
        }
    };
    // Create HTTP connection factory
    NHttpConnectionFactory<DefaultNHttpServerConnection> connFactory;

    //        // Initialize SSL context
    //        ClassLoader cl = NHttpServer.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);
    //        connFactory = new SSLNHttpServerConnectionFactory(sslcontext, null, params);
    connFactory = new DefaultNHttpServerConnectionFactory(params);

    // Create server-side I/O event dispatch
    IOEventDispatch ioEventDispatch = new DefaultHttpServerIODispatch(protocolHandler, connFactory);
    // Create server-side I/O reactor
    ListeningIOReactor ioReactor = new DefaultListeningIOReactor();
    try {
        // Listen of the given port
        ioReactor.listen(new InetSocketAddress(PORT));
        // Ready to go!
        ioReactor.execute(ioEventDispatch);
    } catch (InterruptedIOException ex) {
        System.err.println("Interrupted");
    } catch (IOException e) {
        System.err.println("I/O error: " + e.getMessage());
    }
    System.out.println("Shutdown");
}

From source file:com.limegroup.gnutella.http.HttpTestServer.java

public void execute(EventListener listener) throws IOException {
    BasicHttpProcessor processor = new BasicHttpProcessor();
    processor.addInterceptor(new ResponseDate());
    processor.addInterceptor(new ResponseServer());
    processor.addInterceptor(new ResponseContent());
    processor.addInterceptor(new ResponseConnControl());

    AsyncNHttpServiceHandler serviceHandler = new AsyncNHttpServiceHandler(processor,
            new DefaultHttpResponseFactory(), new DefaultConnectionReuseStrategy(), params);

    serviceHandler.setEventListener(listener);

    serviceHandler.setHandlerResolver(this.registry);

    reactor = new DefaultDispatchedIOReactor(params, NIODispatcher.instance().getScheduledExecutorService());
    IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(serviceHandler, params);
    reactor.execute(ioEventDispatch);//from w  w w  .jav a2 s.  co m
}

From source file:com.asakusafw.yaess.jobqueue.client.HttpJobClientTest.java

/**
 * Initializes the test.//from ww w.j  a  va  2  s .co  m
 * @throws Exception if some errors were occurred
 */
@Before
public void setUp() throws Exception {
    BasicHttpProcessor proc = new BasicHttpProcessor();
    proc.addInterceptor(new ResponseDate());
    proc.addInterceptor(new ResponseServer());
    proc.addInterceptor(new ResponseContent());
    proc.addInterceptor(new ResponseConnControl());
    proc.addInterceptor(new RequestBasicAuth());
    proc.addInterceptor(new ResponseBasicUnauthorized());
    server = new LocalTestServer(proc, null);
    server.start();
    InetSocketAddress address = server.getServiceAddress();
    baseUrl = new URL("http", address.getHostName(), address.getPort(), "/").toExternalForm();
}

From source file:com.yanzhenjie.andserver.DefaultAndServer.java

@Override
public void run() {
    try {/*w w w.j  a  va2s.c  o m*/
        mServerSocket = new ServerSocket();
        mServerSocket.setReuseAddress(true);
        mServerSocket.bind(new InetSocketAddress(mPort));

        // HTTP??
        BasicHttpProcessor httpProcessor = new BasicHttpProcessor();
        httpProcessor.addInterceptor(new ResponseDate());
        httpProcessor.addInterceptor(new ResponseServer());
        httpProcessor.addInterceptor(new ResponseContent());
        httpProcessor.addInterceptor(new ResponseConnControl());

        // HTTP Attribute.
        HttpParams httpParams = new BasicHttpParams();
        httpParams.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, timeout)
                .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
                .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
                .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true)
                .setParameter(CoreProtocolPNames.ORIGIN_SERVER, "WebServer/1.1");

        // Http?
        HttpRequestHandlerRegistry handlerRegistry = new HttpRequestHandlerRegistry();
        for (Map.Entry<String, AndServerRequestHandler> handlerEntry : mRequestHandlerMap.entrySet()) {
            handlerRegistry.register("/" + handlerEntry.getKey(),
                    new DefaultHttpRequestHandler(handlerEntry.getValue()));
        }

        // HTTP?
        HttpService httpService = new HttpService(httpProcessor, new DefaultConnectionReuseStrategy(),
                new DefaultHttpResponseFactory());
        httpService.setParams(httpParams);
        httpService.setHandlerResolver(handlerRegistry);

        /**
         * ?
         */
        while (isLoop) {
            // 
            if (!mServerSocket.isClosed()) {
                Socket socket = mServerSocket.accept();
                DefaultHttpServerConnection serverConnection = new DefaultHttpServerConnection();
                serverConnection.bind(socket, httpParams);

                // Dispatch request handler.
                RequestHandleTask requestTask = new RequestHandleTask(this, httpService, serverConnection);
                requestTask.setDaemon(true);
                AndWebUtil.executeRunnable(requestTask);
            }
        }
    } catch (Exception e) {
    } finally {
        close();
    }
}

From source file:org.siddhiesb.transport.passthru.config.SourceConfiguration.java

public SourceConfiguration(Scheme scheme, WorkerPool workerPool) {
    super(workerPool);
    this.scheme = scheme;
    httpProcessor = new ImmutableHttpProcessor(new HttpResponseInterceptor[] { new ResponseDate(),
            new ResponseServer(), new ResponseContent(), new ResponseConnControl() });

    responseFactory = new DefaultHttpResponseFactory();

    sourceConnections = new SourceConnections();
}

From source file:com.gravspace.core.HttpServer.java

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

    int port = 8082;
    if (args.length >= 1) {
        port = Integer.parseInt(args[0]);
    }//from  ww  w .  j  ava  2s. c  o  m

    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();
}

From source file:io.github.kitarek.elasthttpd.builder.HttpConnectionListenerBuilder.java

private HttpProcessor createHttpProcessor() {
    final HttpProcessorBuilder builder = HttpProcessorBuilder.create();
    builder.addAll(new ResponseDate(), new ResponseServer(serverInfo), new ResponseContent(),
            new ResponseConnControl());
    return builder.build();
}

From source file:niproxy.NiProxy.java

/**
 * Setups a monitoring http proxy and starts a non blocking listening
 * service based on a {@link NiProxyConfig}. This is based on the Apache 
 * Software Foundation's example code "Basic non-blocking HTTP server" that 
 * can be found from http://hc.apache.org/httpcomponents-core-ga/examples.html. 
 *//*from w w  w.  j  ava 2s . c  o m*/
public NiProxy() {
    HttpParams params = new SyncBasicHttpParams();
    params.setIntParameter(CoreConnectionPNames.SO_TIMEOUT, NiProxyConfig.getHttpConnectionTimeout())
            .setIntParameter(CoreConnectionPNames.SOCKET_BUFFER_SIZE, 8 * 1024)
            .setBooleanParameter(CoreConnectionPNames.STALE_CONNECTION_CHECK, false)
            .setBooleanParameter(CoreConnectionPNames.TCP_NODELAY, true);

    HttpProcessor httpproc = new ImmutableHttpProcessor(new HttpResponseInterceptor[] { new ResponseDate(),
            new ResponseServer(), new ResponseContent(), new ResponseConnControl() });

    AsyncNHttpServiceHandler handler = new AsyncNHttpServiceHandler(httpproc, new DefaultHttpResponseFactory(),
            new DefaultConnectionReuseStrategy(), params);

    // Set up request handlers
    NHttpRequestHandlerRegistry registry = new NHttpRequestHandlerRegistry();
    registry.register("*", NiProxyMonitor.get());

    handler.setHandlerResolver(registry);

    // Provide an event logger
    handler.setEventListener(new EventLogger());

    IOEventDispatch ioEventDispatch = new DefaultServerIOEventDispatch(handler, params);
    try {
        logger.info("Setting up " + NiProxyConfig.getIOReactorWorkersCount() + " IOReactor workers");
        ListeningIOReactor ioReactor = new DefaultListeningIOReactor(NiProxyConfig.getIOReactorWorkersCount(),
                params);
        String host = NiProxyConfig.getNiProxyHost();
        int port = NiProxyConfig.getNiProxyPort();
        logger.info("Listening for connections at " + host + ":" + port);
        ioReactor.listen(new InetSocketAddress(host, port));
        logger.info("NiProxy set up done. Launching event dispatch...");
        ioReactor.execute(ioEventDispatch);
    } catch (Exception e) {
        logger.error("I/O reactor was interrupted. Exception: " + e);
        e.printStackTrace();
    }
    logger.info("NiProxy server shutdown.");
}

From source file:org.apache.camel.component.http4.HttpAuthenticationTest.java

@Override
protected BasicHttpProcessor getBasicHttpProcessor() {
    BasicHttpProcessor httpproc = new BasicHttpProcessor();
    httpproc.addInterceptor(new RequestBasicAuth());

    httpproc.addInterceptor(new ResponseContent());
    httpproc.addInterceptor(new ResponseBasicUnauthorized());

    return httpproc;
}