|
| 1 | +/* |
| 2 | + * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| 3 | + * |
| 4 | + * Copyright (c) 2016 Oracle and/or its affiliates. All rights reserved. |
| 5 | + * |
| 6 | + * The contents of this file are subject to the terms of either the GNU |
| 7 | + * General Public License Version 2 only ("GPL") or the Common Development |
| 8 | + * and Distribution License("CDDL") (collectively, the "License"). You |
| 9 | + * may not use this file except in compliance with the License. You can |
| 10 | + * obtain a copy of the License at |
| 11 | + * http://glassfish.java.net/public/CDDL+GPL_1_1.html |
| 12 | + * or packager/legal/LICENSE.txt. See the License for the specific |
| 13 | + * language governing permissions and limitations under the License. |
| 14 | + * |
| 15 | + * When distributing the software, include this License Header Notice in each |
| 16 | + * file and include the License file at packager/legal/LICENSE.txt. |
| 17 | + * |
| 18 | + * GPL Classpath Exception: |
| 19 | + * Oracle designates this particular file as subject to the "Classpath" |
| 20 | + * exception as provided by Oracle in the GPL Version 2 section of the License |
| 21 | + * file that accompanied this code. |
| 22 | + * |
| 23 | + * Modifications: |
| 24 | + * If applicable, add the following below the License Header, with the fields |
| 25 | + * enclosed by brackets [] replaced by your own identifying information: |
| 26 | + * "Portions Copyright [year] [name of copyright owner]" |
| 27 | + * |
| 28 | + * Contributor(s): |
| 29 | + * If you wish your version of this file to be governed by only the CDDL or |
| 30 | + * only the GPL Version 2, indicate your decision by adding "[Contributor] |
| 31 | + * elects to include this software in this distribution under the [CDDL or GPL |
| 32 | + * Version 2] license." If you don't indicate a single choice of license, a |
| 33 | + * recipient has the option to distribute your version of this file under |
| 34 | + * either the CDDL, the GPL Version 2 or to extend the choice of license to |
| 35 | + * its licensees as provided above. However, if you add GPL Version 2 code |
| 36 | + * and therefore, elected the GPL Version 2 license, then the option applies |
| 37 | + * only if the new code is made subject to such option by the copyright |
| 38 | + * holder. |
| 39 | + */ |
| 40 | + |
| 41 | +package org.glassfish.jersey.netty.connector; |
| 42 | + |
| 43 | +import java.io.ByteArrayInputStream; |
| 44 | +import java.io.IOException; |
| 45 | +import java.io.InputStream; |
| 46 | +import java.util.Map; |
| 47 | +import java.util.concurrent.LinkedBlockingDeque; |
| 48 | + |
| 49 | +import javax.ws.rs.core.Response; |
| 50 | + |
| 51 | +import io.netty.buffer.ByteBuf; |
| 52 | +import io.netty.channel.ChannelHandlerContext; |
| 53 | +import io.netty.channel.SimpleChannelInboundHandler; |
| 54 | +import io.netty.handler.codec.http.HttpContent; |
| 55 | +import io.netty.handler.codec.http.HttpHeaderNames; |
| 56 | +import io.netty.handler.codec.http.HttpObject; |
| 57 | +import io.netty.handler.codec.http.HttpResponse; |
| 58 | +import io.netty.handler.codec.http.HttpUtil; |
| 59 | +import io.netty.handler.codec.http.LastHttpContent; |
| 60 | +import io.netty.util.concurrent.Future; |
| 61 | +import io.netty.util.concurrent.GenericFutureListener; |
| 62 | +import jersey.repackaged.com.google.common.util.concurrent.SettableFuture; |
| 63 | +import org.glassfish.jersey.client.ClientRequest; |
| 64 | +import org.glassfish.jersey.client.ClientResponse; |
| 65 | +import org.glassfish.jersey.client.spi.AsyncConnectorCallback; |
| 66 | +import org.glassfish.jersey.netty.connector.internal.NettyInputStream; |
| 67 | + |
| 68 | +/** |
| 69 | + * Jersey implementation of Netty channel handler. |
| 70 | + * |
| 71 | + * @author Pavel Bucek (pavel.bucek at oracle.com) |
| 72 | + */ |
| 73 | +class JerseyClientHandler extends SimpleChannelInboundHandler<HttpObject> { |
| 74 | + |
| 75 | + private final NettyConnector connector; |
| 76 | + private final LinkedBlockingDeque<InputStream> isList = new LinkedBlockingDeque<>(); |
| 77 | + |
| 78 | + private final AsyncConnectorCallback asyncConnectorCallback; |
| 79 | + private final ClientRequest jerseyRequest; |
| 80 | + private final SettableFuture future; |
| 81 | + |
| 82 | + JerseyClientHandler(NettyConnector nettyConnector, ClientRequest request, |
| 83 | + AsyncConnectorCallback callback, SettableFuture future) { |
| 84 | + this.connector = nettyConnector; |
| 85 | + this.asyncConnectorCallback = callback; |
| 86 | + this.jerseyRequest = request; |
| 87 | + this.future = future; |
| 88 | + } |
| 89 | + |
| 90 | + @Override |
| 91 | + public void channelRead0(ChannelHandlerContext ctx, HttpObject msg) { |
| 92 | + if (msg instanceof HttpResponse) { |
| 93 | + final HttpResponse response = (HttpResponse) msg; |
| 94 | + |
| 95 | + final ClientResponse jerseyResponse = new ClientResponse(new Response.StatusType() { |
| 96 | + @Override |
| 97 | + public int getStatusCode() { |
| 98 | + return response.status().code(); |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public Response.Status.Family getFamily() { |
| 103 | + return Response.Status.Family.familyOf(response.status().code()); |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public String getReasonPhrase() { |
| 108 | + return response.status().reasonPhrase(); |
| 109 | + } |
| 110 | + }, jerseyRequest); |
| 111 | + |
| 112 | + for (Map.Entry<String, String> entry : response.headers().entries()) { |
| 113 | + jerseyResponse.getHeaders().add(entry.getKey(), entry.getValue()); |
| 114 | + } |
| 115 | + |
| 116 | + // request entity handling. |
| 117 | + if ((response.headers().contains(HttpHeaderNames.CONTENT_LENGTH) && HttpUtil.getContentLength(response) > 0) |
| 118 | + || HttpUtil.isTransferEncodingChunked(response)) { |
| 119 | + |
| 120 | + ctx.channel().closeFuture().addListener(new GenericFutureListener<Future<? super Void>>() { |
| 121 | + @Override |
| 122 | + public void operationComplete(Future<? super Void> future) throws Exception { |
| 123 | + isList.add(NettyInputStream.END_OF_INPUT_ERROR); |
| 124 | + } |
| 125 | + }); |
| 126 | + |
| 127 | + jerseyResponse.setEntityStream(new NettyInputStream(isList)); |
| 128 | + } else { |
| 129 | + jerseyResponse.setEntityStream(new InputStream() { |
| 130 | + @Override |
| 131 | + public int read() throws IOException { |
| 132 | + return -1; |
| 133 | + } |
| 134 | + }); |
| 135 | + } |
| 136 | + |
| 137 | + if (asyncConnectorCallback != null) { |
| 138 | + connector.executorService.execute(new Runnable() { |
| 139 | + @Override |
| 140 | + public void run() { |
| 141 | + asyncConnectorCallback.response(jerseyResponse); |
| 142 | + future.set(jerseyResponse); |
| 143 | + } |
| 144 | + }); |
| 145 | + } |
| 146 | + |
| 147 | + } |
| 148 | + if (msg instanceof HttpContent) { |
| 149 | + |
| 150 | + HttpContent httpContent = (HttpContent) msg; |
| 151 | + |
| 152 | + ByteBuf content = httpContent.content(); |
| 153 | + |
| 154 | + if (content.isReadable()) { |
| 155 | + // copy bytes - when netty reads last chunk, it automatically closes the channel, which invalidates all |
| 156 | + // relates ByteBuffs. |
| 157 | + byte[] bytes = new byte[content.readableBytes()]; |
| 158 | + content.getBytes(content.readerIndex(), bytes); |
| 159 | + isList.add(new ByteArrayInputStream(bytes)); |
| 160 | + } |
| 161 | + |
| 162 | + if (msg instanceof LastHttpContent) { |
| 163 | + isList.add(NettyInputStream.END_OF_INPUT); |
| 164 | + } |
| 165 | + } |
| 166 | + } |
| 167 | + |
| 168 | + @Override |
| 169 | + public void exceptionCaught(ChannelHandlerContext ctx, final Throwable cause) { |
| 170 | + if (asyncConnectorCallback != null) { |
| 171 | + |
| 172 | + if (asyncConnectorCallback != null) { |
| 173 | + connector.executorService.execute(new Runnable() { |
| 174 | + @Override |
| 175 | + public void run() { |
| 176 | + asyncConnectorCallback.failure(cause); |
| 177 | + } |
| 178 | + }); |
| 179 | + } |
| 180 | + } |
| 181 | + isList.add(NettyInputStream.END_OF_INPUT_ERROR); |
| 182 | + } |
| 183 | +} |
0 commit comments