@Override public void doGet(final HttpServletRequest req, final HttpServletResponse res) { String id = getInstanceId(); if (id == null) { res.setStatus(500); } else { try { HttpClientRequest<ByteBuf> eddaReq = HttpClientRequest.createGet(String.format(EDDA_PATH, id)); HttpClientResponse<ByteBuf> eddaRes = eddaClient.submit(eddaReq).toBlocking().single(); res.setStatus(eddaRes.getStatus().code()); eddaRes .getContent() .toBlocking() .forEach( (ByteBuf content) -> { try { content.readBytes(res.getOutputStream(), content.capacity()); } catch (IOException e) { LOGGER.error("failed to write edda output", e); } }); } catch (Exception e) { LOGGER.error("failed to get data from edda", e); res.setStatus(500); } } }
public RxMeetupService() { this.subject = PublishSubject.<String>create(); this.meetupClient = withTimeout( CompletableFuture.supplyAsync( () -> RxNetty.<ByteBuf, ByteBuf>newHttpClientBuilder("stream.meetup.com", 443) .pipelineConfigurator(new HttpClientPipelineConfigurator<>()) .withSslEngineFactory(DefaultFactories.trustAll()) .build()), Duration.ofSeconds(2)) .thenApply( client -> { log.info("<RxMeetupService> set up meetup subject"); final Observable<String> jsons = StringObservable.split( client .submit(HttpClientRequest.createGet("/2/open_events")) .flatMap(AbstractHttpContentHolder::getContent) .map(bb -> bb.toString(StandardCharsets.UTF_8)), "\n"); jsons.subscribe(this.subject); return client; }) .exceptionally( ex -> { // TODO: timed re-connect log.error("<RxMeetupService> ", ex); this.subject.onError(ex); return null; }); }
@Test public void testVarReplacement() { HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("test").build(); HttpRequestTemplate<ByteBuf> template = group .newTemplateBuilder("testVarReplacement", ByteBuf.class) .withMethod("GET") .withUriTemplate("/foo/{id}?name={name}") .build(); HttpClientRequest<ByteBuf> request = template .requestBuilder() .withRequestProperty("id", "3") .withRequestProperty("name", "netflix") .createClientRequest(); assertEquals("/foo/3?name=netflix", request.getUri()); }
@Test public void testHttpHeaders() { HttpResourceGroup group = Ribbon.createHttpResourceGroupBuilder("test").withHeader("header1", "group").build(); HttpRequestTemplate<String> template = group .newTemplateBuilder("testHttpHeaders", String.class) .withUriTemplate("/foo/bar") .withMethod("GET") .withHeader("header2", "template") .withHeader("header1", "template") .build(); HttpClientRequest<ByteBuf> request = template.requestBuilder().createClientRequest(); HttpRequestHeaders headers = request.getHeaders(); List<String> header1 = headers.getAll("header1"); assertEquals(2, header1.size()); assertEquals("group", header1.get(0)); assertEquals("template", header1.get(1)); List<String> header2 = headers.getAll("header2"); assertEquals(1, header2.size()); assertEquals("template", header2.get(0)); }