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

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

Introduction

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

Prototype

HttpRequestHandler

Source Link

Usage

From source file:se.vgregion.pubsub.inttest.SubscriberRunner.java

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

    LocalTestServer server = new LocalTestServer(null, null);

    server.register("/*", new HttpRequestHandler() {
        @Override// www . j  a  va 2s  . c o  m
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            String challenge = getQueryParamValue(request.getRequestLine().getUri(), "hub.challenge");

            if (challenge != null) {
                // subscription verification, confirm
                System.out.println("Respond to challenge");
                response.setEntity(new StringEntity(challenge));
            } else if (request instanceof HttpEntityEnclosingRequest) {
                HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity();
                //                    System.out.println(HttpUtil.readContent(entity));
            } else {
                System.err.println("Unknown request");
            }
        }
    });
    server.start();

    HttpPost post = new HttpPost("http://localhost:8080/");

    List<NameValuePair> parameters = new ArrayList<NameValuePair>();
    parameters.add(new BasicNameValuePair("hub.callback", buildTestUrl(server, "/").toString()));
    parameters.add(new BasicNameValuePair("hub.mode", "subscribe"));
    parameters.add(new BasicNameValuePair("hub.topic", "http://feeds.feedburner.com/protocol7/main"));
    parameters.add(new BasicNameValuePair("hub.verify", "sync"));

    post.setEntity(new UrlEncodedFormEntity(parameters));

    DefaultHttpClient client = new DefaultHttpClient();
    client.execute(post);
}

From source file:org.apache.camel.itest.http.Http4EndpointTest.java

@BeforeClass
public static void setUp() throws Exception {
    localServer = new HttpTestServer(null, null);
    localServer.register("/", new HttpRequestHandler() {
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setStatusCode(HttpStatus.SC_OK);
            response.setEntity(new StringEntity("OK", Consts.ISO_8859_1));
        }//from   www .j  a  va2 s . com
    });
    localServer.start();
}

From source file:org.ojbc.util.TestHttpUtils.java

@Test
public void test() throws Exception {
    final String responseString = "yeppers";
    LocalTestServer server = new LocalTestServer(null, null);
    server.register("/test", new HttpRequestHandler() {
        @Override//  w ww . ja  v  a2s  . c o  m
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setEntity(new StringEntity(responseString));
        }
    });
    server.start();
    String host = server.getServiceAddress().getHostName();
    int port = server.getServiceAddress().getPort();
    String response = HttpUtils.post("ok?", "http://" + host + ":" + port + "/test");
    assertEquals(responseString, response);
}

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

@Override
protected void registerHandler(LocalTestServer server) {
    server.register("/", new HttpRequestHandler() {
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                // ignore
            }/*w w  w . j a  v a2 s. c o  m*/
            response.setStatusCode(HttpStatus.SC_OK);
            response.setEntity(new StringEntity("" + counter.incrementAndGet()));
        }
    });
}

From source file:com.android.unit_tests.GoogleHttpClientTest.java

protected void setUp() throws Exception {
    // Run a test server that echoes the URI back to the caller.
    mServer = new TestHttpServer();
    mServer.registerHandler("*", new HttpRequestHandler() {
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            String uri = request.getRequestLine().getUri();
            response.setEntity(new StringEntity(uri));
        }/*w w  w.j a v a 2 s. com*/
    });

    mServer.start();
    mServerUrl = "http://localhost:" + mServer.getPort() + "/";
}

From source file:nl.surfnet.sab.HttpClientTransportTest.java

@Before
public void setUp() throws Exception {
    server = new LocalTestServer(null, null);

    server.register("/test", new HttpRequestHandler() {
        @Override//from   w  w w  .  j a  v  a  2s  .  co  m
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setEntity(new StringEntity("This is the response"));
            response.setStatusCode(200);
        }
    });

    server.start();
    endPoint = URI.create("http:/" + server.getServiceAddress().toString() + "/test");
    credentials = new UsernamePasswordCredentials("theuser", "thepass");
    transport = new HttpClientTransport(credentials, credentials, endPoint, endPoint);
}

From source file:com.flipkart.foxtrot.client.TestSelector.java

@Before
public void setup() throws Exception {
    localTestServer.register("/foxtrot/v1/cluster/members", new HttpRequestHandler() {
        @Override//  w  w  w.  jav a 2 s. c  o m
        public void handle(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response,
                HttpContext context) throws HttpException, IOException {
            response.setEntity(new StringEntity(mapper.writeValueAsString(
                    new FoxtrotClusterStatus(Lists.newArrayList(new FoxtrotClusterMember("host1", 18000),
                            new FoxtrotClusterMember("host2", 18000))))));
            response.setStatusCode(200);
        }
    });
    localTestServer.start();
}

From source file:com.flipkart.foxtrot.client.TestSelectorNoMember.java

@Before
public void setup() throws Exception {
    localTestServer.register("/foxtrot/v1/cluster/members", new HttpRequestHandler() {
        @Override//from  ww  w.ja  va2  s. c o m
        public void handle(org.apache.http.HttpRequest request, org.apache.http.HttpResponse response,
                HttpContext context) throws HttpException, IOException {
            response.setEntity(new StringEntity(mapper
                    .writeValueAsString(new FoxtrotClusterStatus(Lists.<FoxtrotClusterMember>newArrayList()))));
            response.setStatusCode(200);
        }
    });
    localTestServer.start();
}

From source file:se.vgregion.pubsub.inttest.Publisher.java

public Publisher(String host) throws Exception {
    this.localServerName = host;
    server = new LocalTestServer(null, null);
    server.register("/*", new HttpRequestHandler() {
        @Override/*from   www.  j av  a2  s.c  o m*/
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setHeader("Content-type", ContentType.ATOM.toString());
            response.setEntity(new StringEntity(AbstractSerializer.printFeed(ContentType.ATOM, feed).toXML()));
        }
    });
    server.start();
}

From source file:org.surfnet.cruncher.integration.CruncherClientTestIntegration.java

@BeforeClass
public static void beforeClass() throws Exception {
    oauth2AuthServer = new LocalTestServer(null, null);
    oauth2AuthServer.start();/*from ww  w .j  av  a  2  s  . c  om*/
    oauth2AuthServer.register("/oauth2/token", new HttpRequestHandler() {
        @Override
        public void handle(HttpRequest request, HttpResponse response, HttpContext context)
                throws HttpException, IOException {
            response.setEntity(new StringEntity(answer, ContentType.APPLICATION_JSON));
            response.setStatusCode(200);
        }

    });
    String apisOAuth2AuthorizationUrl = String.format("http://%s:%d/oauth2/token",
            oauth2AuthServer.getServiceAddress().getHostName(), oauth2AuthServer.getServiceAddress().getPort());
    cruncherClient = new CruncherClient(cruncherBaseLocation);
    ClientCredentialsClient oauthClient = new ClientCredentialsClient();
    oauthClient.setClientKey("key");
    oauthClient.setClientSecret("secret");
    oauthClient.setOauthAuthorizationUrl(apisOAuth2AuthorizationUrl);
    cruncherClient.setOauthClient(oauthClient);
}