use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project ratpack by ratpack.
the class WebSocketEngine method connect.
@SuppressWarnings("deprecation")
public static <T> void connect(final Context context, String path, int maxLength, final WebSocketHandler<T> handler) {
PublicAddress publicAddress = context.get(PublicAddress.class);
URI address = publicAddress.get(context);
URI httpPath = address.resolve(path);
URI wsPath;
try {
wsPath = new URI("ws", httpPath.getUserInfo(), httpPath.getHost(), httpPath.getPort(), httpPath.getPath(), httpPath.getQuery(), httpPath.getFragment());
} catch (URISyntaxException e) {
throw uncheck(e);
}
WebSocketServerHandshakerFactory factory = new WebSocketServerHandshakerFactory(wsPath.toString(), null, false, maxLength);
Request request = context.getRequest();
HttpMethod method = valueOf(request.getMethod().getName());
FullHttpRequest nettyRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
nettyRequest.headers().add(SEC_WEBSOCKET_VERSION, request.getHeaders().get(SEC_WEBSOCKET_VERSION));
nettyRequest.headers().add(SEC_WEBSOCKET_KEY, request.getHeaders().get(SEC_WEBSOCKET_KEY));
final WebSocketServerHandshaker handshaker = factory.newHandshaker(nettyRequest);
final DirectChannelAccess directChannelAccess = context.getDirectChannelAccess();
final Channel channel = directChannelAccess.getChannel();
if (!channel.config().isAutoRead()) {
channel.config().setAutoRead(true);
}
handshaker.handshake(channel, nettyRequest).addListener(new HandshakeFutureListener<>(context, handshaker, handler));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project ambry by linkedin.
the class NettyMessageProcessorTest method rawBytesPostTest.
/**
* Tests the case where raw bytes are POSTed as chunks.
* @throws InterruptedException
*/
@Test
public void rawBytesPostTest() throws InterruptedException {
Random random = new Random();
// request also contains content.
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(random.nextInt(128) + 128));
HttpRequest postRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/", Unpooled.wrappedBuffer(content));
postRequest.headers().set(RestUtils.Headers.SERVICE_ID, "rawBytesPostTest");
postRequest = ReferenceCountUtil.retain(postRequest);
ByteBuffer receivedContent = doPostTest(postRequest, null);
compareContent(receivedContent, Collections.singletonList(content));
// request and content separate.
final int NUM_CONTENTS = 5;
postRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
List<ByteBuffer> contents = new ArrayList<ByteBuffer>(NUM_CONTENTS);
int blobSize = 0;
for (int i = 0; i < NUM_CONTENTS; i++) {
ByteBuffer buffer = ByteBuffer.wrap(TestUtils.getRandomBytes(random.nextInt(128) + 128));
blobSize += buffer.remaining();
contents.add(i, buffer);
}
postRequest.headers().set(RestUtils.Headers.SERVICE_ID, "rawBytesPostTest");
receivedContent = doPostTest(postRequest, contents);
compareContent(receivedContent, contents);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project ambry by linkedin.
the class NettyMultipartRequestTest method createRequest.
// helpers
// general
/**
* Creates a {@link NettyMultipartRequest} with the given {@code headers} and {@code parts}.
* @param headers the {@link HttpHeaders} that need to be added to the request.
* @param parts the files that will form the parts of the request.
* @return a {@link NettyMultipartRequest} containing all the {@code headers} and {@code parts}.
* @throws Exception
*/
private NettyMultipartRequest createRequest(HttpHeaders headers, InMemoryFile[] parts) throws Exception {
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
if (headers != null) {
httpRequest.headers().set(headers);
}
HttpPostRequestEncoder encoder = createEncoder(httpRequest, parts);
NettyMultipartRequest request = new NettyMultipartRequest(encoder.finalizeRequest(), new MockChannel(), NETTY_METRICS, Collections.emptySet(), Long.MAX_VALUE);
assertTrue("Request channel is not open", request.isOpen());
while (!encoder.isEndOfInput()) {
// Sending null for ctx because the encoder is OK with that.
request.addContent(encoder.readChunk(PooledByteBufAllocator.DEFAULT));
}
return request;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project ambry by linkedin.
the class FrontendIntegrationTest method buildRequest.
// helpers
// general
/**
* Method to easily create a request.
* @param httpMethod the {@link HttpMethod} desired.
* @param uri string representation of the desired URI.
* @param headers any associated headers as a {@link HttpHeaders} object. Can be null.
* @param content the content that accompanies the request. Can be null.
* @return A {@link FullHttpRequest} object that defines the request required by the input.
*/
private FullHttpRequest buildRequest(HttpMethod httpMethod, String uri, HttpHeaders headers, ByteBuffer content) {
ByteBuf contentBuf;
if (content != null) {
contentBuf = Unpooled.wrappedBuffer(content);
} else {
contentBuf = Unpooled.buffer(0);
}
FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, uri, contentBuf);
if (headers != null) {
httpRequest.headers().set(headers);
}
if (HttpMethod.POST.equals(httpMethod) && !HttpUtil.isContentLengthSet(httpRequest)) {
HttpUtil.setTransferEncodingChunked(httpRequest, true);
}
return httpRequest;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.DefaultFullHttpRequest in project ambry by linkedin.
the class FrontendIntegrationTest method getReplicasTest.
/**
* Tests {@link RestUtils.SubResource#Replicas} requests
* <p/>
* For each {@link PartitionId} in the {@link ClusterMap}, a {@link BlobId} is created. The replica list returned from
* server is checked for equality against a locally obtained replica list.
* @throws Exception
*/
@Test
public void getReplicasTest() throws Exception {
List<? extends PartitionId> partitionIds = CLUSTER_MAP.getWritablePartitionIds();
for (PartitionId partitionId : partitionIds) {
String originalReplicaStr = partitionId.getReplicaIds().toString().replace(", ", ",");
BlobId blobId = new BlobId(CommonTestUtils.getCurrentBlobIdVersion(), BlobId.BlobIdType.NATIVE, ClusterMapUtils.UNKNOWN_DATACENTER_ID, Account.UNKNOWN_ACCOUNT_ID, Container.UNKNOWN_CONTAINER_ID, partitionId, false);
FullHttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, blobId.getID() + "/" + RestUtils.SubResource.Replicas, Unpooled.buffer(0));
ResponseParts responseParts = nettyClient.sendRequest(httpRequest, null, null).get();
HttpResponse response = getHttpResponse(responseParts);
assertEquals("Unexpected response status", HttpResponseStatus.OK, response.status());
ByteBuffer content = getContent(responseParts.queue, HttpUtil.getContentLength(response));
JSONObject responseJson = new JSONObject(new String(content.array()));
String returnedReplicasStr = responseJson.getString(GetReplicasHandler.REPLICAS_KEY).replace("\"", "");
assertEquals("Replica IDs returned for the BlobId do no match with the replicas IDs of partition", originalReplicaStr, returnedReplicasStr);
}
}
Aggregations