List of usage examples for org.apache.http.protocol HttpRequestHandler HttpRequestHandler
HttpRequestHandler
From source file:org.dataconservancy.access.connector.AbstractHttpConnectorTest.java
@BeforeClass public static void setUp() throws Exception { XMLUnit.setIgnoreComments(true);/* ww w .ja v a 2s .c o m*/ XMLUnit.setIgnoreWhitespace(true); final BasicHttpProcessor httpProc = new BasicHttpProcessor(); testServer = new LocalTestServer(httpProc, null); testServer.register("/entity/*", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { final String requestURI = request.getRequestLine().getUri(); final String requestedEntity = requestURI.substring(requestURI.lastIndexOf("/") + 1); log.trace("processing request for entity {}", requestedEntity); final File requestedEntityFile = new File(entitiesDir, requestedEntity + ".xml"); if (!requestedEntityFile.exists()) { response.setStatusCode(HttpStatus.SC_NOT_FOUND); } else { response.setStatusCode(HttpStatus.SC_OK); response.setHeader("content-type", "application/xml"); response.setEntity(new InputStreamEntity(new FileInputStream(requestedEntityFile), -1)); } } }); testServer.register("/query/*", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { final String requestURI = request.getRequestLine().getUri(); final String query = requestURI.substring(requestURI.lastIndexOf("/") + 1); int max = -1; int offset = 0; //For some reason request.getParams wasn't working so parse the string String[] params = query.split("&"); for (String param : params) { String name = param.split("=")[0]; if (name.equalsIgnoreCase("max")) { max = Integer.parseInt(param.split("=")[1]); } else if (name.equalsIgnoreCase("offset")) { offset = Integer.parseInt(param.split("=")[1]); } } if (max == -1) { max = allTestEntities.size(); } log.trace( "processing request for query {} (note that the query itself is ignored and the response is hardcoded)", query); // basically we iterate over all the entities in the file system and return a DCP of Files. The actual query is ignored final Dcp dcp = new Dcp(); int count = 0; for (DcsEntity e : allTestEntities) { if (e instanceof DcsFile) { if (count >= offset) { dcp.addFile((DcsFile) e); } count++; if (count == max) { break; } } } count = dcp.getFiles().size(); ByteArrayOutputStream baos = new ByteArrayOutputStream(); mb.buildSip(dcp, baos); response.setStatusCode(HttpStatus.SC_OK); response.setHeader("content-type", "application/xml"); response.setHeader("X-TOTAL-MATCHES", String.valueOf(count)); response.setEntity(new BufferedHttpEntity(new ByteArrayEntity(baos.toByteArray()))); } }); testServer.register("/deposit/file", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { log.trace("file upload"); response.setStatusCode(HttpStatus.SC_ACCEPTED); response.setHeader("content-type", "application/atom+xml;type=entry"); response.setHeader("Location", "http://dataconservancy.org/deposit/003210.atom"); response.setHeader("X-dcs-src", "http://dataconservancy.org/deposit/003210.file"); response.setEntity(new StringEntity("<?xml version='1.0'?>" + "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:dc='http://dataconservancy.org/ns/'>" + "</entry>", "UTF-8")); } }); testServer.register("/deposit/sip", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { log.trace("sip upload"); response.setStatusCode(HttpStatus.SC_ACCEPTED); response.setHeader("content-type", "application/atom+xml;type=entry"); response.setHeader("Location", "http://dcservice.moo.org:8080/dcs/content/sipDeposit/4331419"); response.setEntity(new StringEntity("<?xml version='1.0'?>" + "<entry xmlns='http://www.w3.org/2005/Atom' xmlns:sword='http://purl.org/net/sword/'><id>deposit:/sipDeposit/4331419</id><content type='application/xml' src='http://dcservice.moo.org:8080/dcs/content/sipDeposit/4331419' /><title type='text'>Deposit 4331419</title><updated>2011-10-28T18:12:50.171Z</updated><author><name>Depositor</name></author><summary type='text'>ingesting</summary><link href='http://dcservice.moo.org:8080/dcs/status/sipDeposit/4331419' type='application/atom+xml; type=feed' rel='alternate' title='Processing Status' /><sword:treatment>Deposit processing</sword:treatment></entry>")); } }); testServer.start(); log.info("Test HTTP server listening on {}:{}", testServer.getServiceHostName(), testServer.getServicePort()); }
From source file:org.apromore.plugin.deployment.yawl.YAWLEngineClientUnitTest.java
@Test public void testUploadYAWLSpecification() throws DeploymentException { server.register("/yawl/*", new HttpRequestHandler() { @Override// w w w . j a v a2 s . com public void handle(final HttpRequest request, final HttpResponse response, final HttpContext context) throws HttpException, IOException { String str = EntityUtils.toString(((HttpEntityEnclosingRequest) request).getEntity()); if (str.contains("action=upload")) { if (str.contains("sessionHandle=test") && str.contains("specXML=test")) { response.setEntity(new StringEntity("<response>test</response>", "UTF-8")); } else { response.setStatusCode(500); } } else { response.setStatusCode(500); } } }); assertNotNull(yawlEngineClient.uploadYAWLSpecification("test", "test")); }
From source file:org.apache.camel.component.http4.HttpProducerTwoParametersWithSameKeyTest.java
@Override protected void registerHandler(LocalTestServer server) { server.register("/myapp", new HttpRequestHandler() { @Override/*from ww w. j a v a 2 s.c o m*/ public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { String uri = request.getRequestLine().getUri(); assertEquals("/myapp?from=me&to=foo&to=bar", uri); response.setHeader("bar", "yes"); response.addHeader("foo", "123"); response.addHeader("foo", "456"); response.setEntity(new StringEntity("OK", "ASCII")); response.setStatusCode(HttpStatus.SC_OK); } }); }
From source file:org.apache.camel.component.http4.HttpProducerContentTypeTest.java
@Override protected void registerHandler(LocalTestServer server) { server.register("/content", new HttpRequestHandler() { @Override/*from w w w . j a v a 2 s . c o m*/ public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { String contentType = request.getFirstHeader(Exchange.CONTENT_TYPE).getValue(); assertEquals(CONTENT_TYPE, contentType); response.setEntity(new StringEntity(contentType, "ASCII")); response.setStatusCode(HttpStatus.SC_OK); } }); }
From source file:se.vgregion.pubsub.inttest.Subscriber.java
public Subscriber(String localServerName, final SubscriberResult result) throws Exception { this.localServerName = localServerName; server = new LocalTestServer(null, null); server.register("/*", new HttpRequestHandler() { @Override/*from ww w . j a va 2 s .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) { doVerify(); // subscription verification, confirm response.setEntity(new StringEntity(challenge)); } else if (request instanceof HttpEntityEnclosingRequest) { if (result != null && result.fail()) { response.setStatusCode(500); } HttpEntity entity = ((HttpEntityEnclosingRequest) request).getEntity(); doPublish(entity); } else { System.err.println("Unknown request"); } } }); server.start(); }
From source file:org.phenotips.pingback.internal.client.data.IPPingDataProviderTest.java
@Test public void testProvideData() throws Exception { server.register("/*", new HttpRequestHandler() { @Override/*from www. j ava 2s . c o m*/ public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws IOException { response.setEntity(new StringEntity("{\"ip\":\"192.168.1.1\"}")); } }); ConfigurationSource configuration = this.mocker.getInstance(ConfigurationSource.class); when(configuration.getProperty(IPPingDataProvider.IP_FETCH_URL_PROPERTY)) .thenReturn(serverURL + "/get/Stats/Id"); Map<String, Object> actual = this.mocker.getComponentUnderTest().provideData(); JSONAssert.assertEquals("{\"ip\":\"192.168.1.1\"}", new JSONObject(actual), false); }
From source file:org.apache.camel.component.http4.HttpProducerTwoHeadersWithSameKeyTest.java
@Override protected void registerHandler(LocalTestServer server) { server.register("/myapp", new HttpRequestHandler() { @Override//from w ww . j av a 2 s.c om public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { Header[] from = request.getHeaders("from"); assertEquals("me", from[0].getValue()); Header[] to = request.getHeaders("to"); assertEquals("[foo, bar]", to[0].getValue()); response.setHeader("bar", "yes"); response.addHeader("foo", "123"); response.addHeader("foo", "456"); response.setEntity(new StringEntity("OK", "ASCII")); response.setStatusCode(HttpStatus.SC_OK); } }); }
From source file:se.vgregion.pubsub.push.impl.DefaultFeedRetrieverTest.java
@Test public void simpleRetrieval() throws Exception { server.register("/*", new HttpRequestHandler() { @Override//from w ww . jav a 2 s . co m public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setEntity(testEntity); } }); URI url = buildTestUrl("/test"); retriever.retrieve(url); ArgumentCaptor<Feed> publishedFeed = ArgumentCaptor.forClass(Feed.class); Mockito.verify(pushSubscriberManager).publish(Mockito.eq(url), publishedFeed.capture()); Assert.assertEquals("f1", publishedFeed.getValue().getFeedId()); }
From source file:se.vgregion.pubsub.push.impl.DefaultPushSubscriberVerifyTest.java
@Test @Transactional/*from www.jav a 2 s. co m*/ @Rollback public void verify() throws Exception { final LinkedBlockingQueue<HttpRequest> requests = new LinkedBlockingQueue<HttpRequest>(); server.register("/*", new HttpRequestHandler() { @Override public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { requests.add(request); response.setEntity( new StringEntity(getQueryParamValue(request.getRequestLine().getUri(), "hub.challenge"))); } }); subscriber.verify(SubscriptionMode.SUBSCRIBE); Assert.assertEquals(1, requests.size()); HttpRequest actualRequest = requests.poll(); String requestUri = actualRequest.getRequestLine().getUri(); Assert.assertEquals("subscribe", getQueryParamValue(requestUri, "hub.mode")); Assert.assertEquals(subscriber.getTopic().toString(), URLDecoder.decode(getQueryParamValue(requestUri, "hub.topic"), "UTF-8")); Assert.assertNotNull(getQueryParamValue(requestUri, "hub.challenge")); Assert.assertEquals("123", getQueryParamValue(requestUri, "hub.lease_seconds")); Assert.assertEquals(subscriber.getVerifyToken(), getQueryParamValue(requestUri, "hub.verify_token")); }
From source file:io.joynr.messaging.MessageSchedulerTest.java
@Before public void setUp() throws Exception { String messagePath = CHANNELPATH + channelId + "/message/"; server = new LocalTestServer(null, null); server.register(messagePath, new HttpRequestHandler() { @Override// ww w. j a v a 2s. c om public void handle(HttpRequest request, HttpResponse response, HttpContext context) throws HttpException, IOException { response.setStatusCode(sendMessageResponseCode); response.setHeader("msgId", sendMessageId); serverResponded = true; } }); server.start(); serviceAddress = "http://" + server.getServiceAddress().getHostName() + ":" + server.getServiceAddress().getPort(); bounceProxyUrl = serviceAddress + BOUNCEPROXYPATH; Properties properties = new Properties(); properties.put(MessagingPropertyKeys.CHANNELID, channelId); properties.put(MessagingPropertyKeys.BOUNCE_PROXY_URL, bounceProxyUrl); AbstractModule mockModule = new AbstractModule() { @Override protected void configure() { bind(LocalChannelUrlDirectoryClient.class).toInstance(mockChannelUrlDir); } }; Injector injector = Guice.createInjector(new JoynrPropertiesModule(properties), Modules.override(new MessagingTestModule()).with(mockModule), new AbstractModule() { @Override protected void configure() { bind(RequestConfig.class).toProvider(HttpDefaultRequestConfigProvider.class) .in(Singleton.class); } }, new AtmosphereMessagingModule()); messageScheduler = injector.getInstance(MessageScheduler.class); }