|
1 | 1 | package io.github.kimmking.gateway.outbound.okhttp; |
2 | 2 |
|
| 3 | +import io.netty.buffer.Unpooled; |
| 4 | +import io.netty.channel.ChannelFutureListener; |
| 5 | +import io.netty.channel.ChannelHandlerContext; |
| 6 | +import io.netty.handler.codec.http.DefaultFullHttpResponse; |
| 7 | +import io.netty.handler.codec.http.FullHttpRequest; |
| 8 | +import io.netty.handler.codec.http.FullHttpResponse; |
| 9 | +import io.netty.handler.codec.http.HttpUtil; |
| 10 | +import okhttp3.OkHttpClient; |
| 11 | +import okhttp3.Request; |
| 12 | +import okhttp3.Response; |
| 13 | + |
| 14 | +import java.io.IOException; |
| 15 | + |
| 16 | +import static io.netty.handler.codec.http.HttpResponseStatus.NO_CONTENT; |
| 17 | +import static io.netty.handler.codec.http.HttpResponseStatus.OK; |
| 18 | +import static io.netty.handler.codec.http.HttpVersion.HTTP_1_1; |
| 19 | + |
| 20 | +/** |
| 21 | + * 基于OkHttp实现的网关handler |
| 22 | + * @author romanlu |
| 23 | + */ |
3 | 24 | public class OkhttpOutboundHandler { |
| 25 | + private String backendUrl; |
| 26 | + |
| 27 | + public OkhttpOutboundHandler(String backendUrl) { |
| 28 | + this.backendUrl = backendUrl.endsWith("/") ? backendUrl.substring(0, backendUrl.length() - 1) : backendUrl; |
| 29 | + } |
| 30 | + |
| 31 | + public void handle(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx) { |
| 32 | + final String url = this.backendUrl + fullRequest.uri(); |
| 33 | + OkHttpClient client = new OkHttpClient(); |
| 34 | + try { |
| 35 | + Request request = new Request.Builder().url(url).build(); |
| 36 | + Response response = client.newCall(request).execute(); |
| 37 | + //打印结果 |
| 38 | + handleResponse(fullRequest, ctx, response); |
| 39 | + } catch (IOException e) { |
| 40 | + e.printStackTrace(); |
| 41 | + } catch (Exception e) { |
| 42 | + e.printStackTrace(); |
| 43 | + } finally { |
| 44 | + client.clone(); |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + private void handleResponse(final FullHttpRequest fullRequest, final ChannelHandlerContext ctx, final Response endpointResponse) throws Exception { |
| 49 | + FullHttpResponse response = null; |
| 50 | + try { |
| 51 | + |
| 52 | + byte[] body = endpointResponse.body().bytes(); |
| 53 | + response = new DefaultFullHttpResponse(HTTP_1_1, OK, Unpooled.wrappedBuffer(body)); |
| 54 | + response.headers().set("Content-Type", "application/json"); |
| 55 | + response.headers().setInt("Content-Length", body.length); |
| 56 | + |
| 57 | + |
| 58 | + } catch (Exception e) { |
| 59 | + e.printStackTrace(); |
| 60 | + response = new DefaultFullHttpResponse(HTTP_1_1, NO_CONTENT); |
| 61 | + exceptionCaught(ctx, e); |
| 62 | + } finally { |
| 63 | + if (fullRequest != null) { |
| 64 | + if (!HttpUtil.isKeepAlive(fullRequest)) { |
| 65 | + ctx.write(response).addListener(ChannelFutureListener.CLOSE); |
| 66 | + } else { |
| 67 | + ctx.write(response); |
| 68 | + } |
| 69 | + } |
| 70 | + ctx.flush(); |
| 71 | + } |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + public void exceptionCaught(ChannelHandlerContext ctx, Throwable cause) { |
| 76 | + cause.printStackTrace(); |
| 77 | + ctx.close(); |
| 78 | + } |
4 | 79 | } |
0 commit comments