Java tutorial
/** * Copyright (C) 2015 Matt Christiansen (matt@nikore.net) * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ package net.nikore.gozer.router; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; import javax.servlet.http.HttpServletRequest; import com.google.inject.Inject; import com.netflix.ribbon.transport.netty.http.LoadBalancingHttpClient; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.HttpMethod; import io.reactivex.netty.protocol.http.client.HttpClientRequest; import net.nikore.gozer.constants.GozerHeaders; public class RibbonUtils { private final LoadBalancingHttpClient<ByteBuf, ByteBuf> client; @Inject public RibbonUtils(LoadBalancingHttpClient<ByteBuf, ByteBuf> client) { this.client = client; } public HttpMethod getVerb(HttpServletRequest request) { String method = request.getMethod(); if (method == null) { return HttpMethod.GET; } else { return HttpMethod.valueOf(method); } } public SimpleRibbonResponse simpleRequest(HttpClientRequest<ByteBuf> request) { CountDownLatch latch = new CountDownLatch(1); final SimpleRibbonResponse[] rtn = new SimpleRibbonResponse[1]; client.submit(request).subscribe(response -> { response.getContent().subscribe(content -> { boolean gzip = (response.getHeaders().get(GozerHeaders.CONTENT_ENCODING.getName()) != null); byte[] bytes = new byte[content.readableBytes()]; content.readBytes(bytes); rtn[0] = new SimpleRibbonResponse(response.getStatus().code(), bytes, gzip); latch.countDown(); }); }); try { latch.await(5, TimeUnit.DAYS); } catch (InterruptedException e) { e.printStackTrace(); } return rtn[0]; } }