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.io.ByteArrayInputStream; import com.google.inject.Inject; import io.netty.buffer.ByteBuf; import io.netty.handler.codec.http.HttpMethod; import io.reactivex.netty.protocol.http.client.HttpClientRequest; import net.nikore.gozer.FilterType; import net.nikore.gozer.GozerFilter; import net.nikore.gozer.context.Debug; import net.nikore.gozer.context.RequestContext; import net.nikore.gozer.util.HTTPRequestUtils; public class RibbonRouter extends GozerFilter { private final RibbonUtils utils; @Inject public RibbonRouter(RibbonUtils utils) { super(); this.utils = utils; } @Override public boolean shouldFilter(RequestContext ctx) { return ctx.getOriginHost() != null && ctx.sendZuulResponse() && !ctx.isMarathonRequest(); } @Override public FilterType filterType() { return FilterType.ROUTE; } @Override public int filterOrder() { return 100; } @Override public RequestContext apply(RequestContext ctx) { HttpMethod verb = utils.getVerb(ctx.getRequest()); String requestURL; if (ctx.hasOriginRequest()) { requestURL = ctx.getOriginHost() + ctx.getOringRequest(); } else { requestURL = ctx.getOriginHost() + ctx.getRequest().getRequestURI(); } HttpClientRequest<ByteBuf> request = HttpClientRequest.create(verb, requestURL); HTTPRequestUtils.buildRequestHeaders(ctx.getRequest()).forEach(request::withHeader); SimpleRibbonResponse response = utils.simpleRequest(request); ctx.setResponseStatusCode(response.getStatusCode()); ctx.setResponseBody(new String(response.getContent())); ctx.setResponseDataStream(new ByteArrayInputStream(response.getContent())); ctx.setResponseGZipped(response.isGzipped()); if (ctx.debugRequest()) { Debug.addRequestDebug("GOZER:: host=" + ctx.getOriginHost(), ctx); HTTPRequestUtils.buildRequestHeaders(ctx.getRequest()) .forEach((s, s2) -> Debug.addRequestDebug("GOZER Headers::> " + s + " " + s2, ctx)); Debug.addRequestDebug("GOZER:: Request Info >" + verb.name() + " " + requestURL, ctx); Debug.addRequestDebug("GOZER:: response entity >" + new String(response.getContent()), ctx); } return ctx; } }