use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project alien4cloud by alien4cloud.
the class WebSocketClientHandler method channelActive.
@Override
public void channelActive(ChannelHandlerContext ctx) throws Exception {
if (this.authenticationUrl != null) {
HttpRequest loginRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, this.authenticationUrl);
loginRequest.headers().set(new AsciiString("host"), this.host);
HttpPostRequestEncoder bodyRequestEncoder = new HttpPostRequestEncoder(loginRequest, false);
bodyRequestEncoder.addBodyAttribute("j_username", user);
bodyRequestEncoder.addBodyAttribute("j_password", password);
bodyRequestEncoder.addBodyAttribute("submit", "Login");
loginRequest = bodyRequestEncoder.finalizeRequest();
if (log.isDebugEnabled()) {
log.debug("Authentication request for user {} to {}", this.user, this.authenticationUrl);
}
ctx.writeAndFlush(loginRequest);
} else {
handShaker.handshake(ctx.channel());
}
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project flink by apache.
the class RestClient method createRequest.
private static Request createRequest(String targetAddress, String targetUrl, HttpMethod httpMethod, ByteBuf jsonPayload, Collection<FileUpload> fileUploads) throws IOException {
if (fileUploads.isEmpty()) {
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, targetUrl, jsonPayload);
httpRequest.headers().set(HttpHeaders.Names.HOST, targetAddress).set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE).add(HttpHeaders.Names.CONTENT_LENGTH, jsonPayload.capacity()).add(HttpHeaders.Names.CONTENT_TYPE, RestConstants.REST_CONTENT_TYPE);
return new SimpleRequest(httpRequest);
} else {
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, httpMethod, targetUrl);
httpRequest.headers().set(HttpHeaders.Names.HOST, targetAddress).set(HttpHeaders.Names.CONNECTION, HttpHeaders.Values.CLOSE);
// takes care of splitting the request into multiple parts
HttpPostRequestEncoder bodyRequestEncoder;
try {
// we could use mixed attributes here but we have to ensure that the minimum size is
// greater than
// any file as the upload otherwise fails
DefaultHttpDataFactory httpDataFactory = new DefaultHttpDataFactory(true);
// the FileUploadHandler explicitly checks for multipart headers
bodyRequestEncoder = new HttpPostRequestEncoder(httpDataFactory, httpRequest, true);
Attribute requestAttribute = new MemoryAttribute(FileUploadHandler.HTTP_ATTRIBUTE_REQUEST);
requestAttribute.setContent(jsonPayload);
bodyRequestEncoder.addBodyHttpData(requestAttribute);
int fileIndex = 0;
for (FileUpload fileUpload : fileUploads) {
Path path = fileUpload.getFile();
if (Files.isDirectory(path)) {
throw new IllegalArgumentException("Upload of directories is not supported. Dir=" + path);
}
File file = path.toFile();
LOG.trace("Adding file {} to request.", file);
bodyRequestEncoder.addBodyFileUpload("file_" + fileIndex, file, fileUpload.getContentType(), false);
fileIndex++;
}
} catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
throw new IOException("Could not encode request.", e);
}
try {
httpRequest = bodyRequestEncoder.finalizeRequest();
} catch (HttpPostRequestEncoder.ErrorDataEncoderException e) {
throw new IOException("Could not finalize request.", e);
}
return new MultipartRequest(httpRequest, bodyRequestEncoder);
}
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project ambry by linkedin.
the class NettyMessageProcessorTest method createEncoder.
// multipartPostTest() helpers.
/**
* Creates a {@link HttpPostRequestEncoder} that encodes the given {@code request} and {@code blobContent}.
* @param request the {@link HttpRequest} containing headers and other metadata about the request.
* @param blobContent the {@link ByteBuffer} that represents the content of the blob.
* @return a {@link HttpPostRequestEncoder} that can encode the {@code request} and {@code blobContent}.
* @throws HttpPostRequestEncoder.ErrorDataEncoderException
* @throws IOException
*/
private HttpPostRequestEncoder createEncoder(HttpRequest request, ByteBuffer blobContent) throws HttpPostRequestEncoder.ErrorDataEncoderException, IOException {
HttpDataFactory httpDataFactory = new DefaultHttpDataFactory(false);
HttpPostRequestEncoder encoder = new HttpPostRequestEncoder(httpDataFactory, request, true);
FileUpload fileUpload = new MemoryFileUpload(RestUtils.MultipartPost.BLOB_PART, RestUtils.MultipartPost.BLOB_PART, "application/octet-stream", "", Charset.forName("UTF-8"), blobContent.remaining());
fileUpload.setContent(Unpooled.wrappedBuffer(blobContent));
encoder.addBodyHttpData(fileUpload);
return encoder;
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project ambry by linkedin.
the class NettyMessageProcessorTest method multipartPostTest.
/**
* Tests the case where multipart upload is used.
* @throws Exception
*/
@Test
public void multipartPostTest() throws Exception {
Random random = new Random();
ByteBuffer content = ByteBuffer.wrap(TestUtils.getRandomBytes(random.nextInt(128) + 128));
HttpRequest httpRequest = RestTestUtils.createRequest(HttpMethod.POST, "/", null);
httpRequest.headers().set(RestUtils.Headers.SERVICE_ID, "rawBytesPostTest");
HttpPostRequestEncoder encoder = createEncoder(httpRequest, content);
HttpRequest postRequest = encoder.finalizeRequest();
List<ByteBuffer> contents = new ArrayList<ByteBuffer>();
while (!encoder.isEndOfInput()) {
// Sending null for ctx because the encoder is OK with that.
contents.add(encoder.readChunk(PooledByteBufAllocator.DEFAULT).content().nioBuffer());
}
ByteBuffer receivedContent = doPostTest(postRequest, contents);
compareContent(receivedContent, Collections.singletonList(content));
}
use of io.netty.handler.codec.http.multipart.HttpPostRequestEncoder in project ambry by linkedin.
the class NettyMultipartRequestTest method sizeLimitationTest.
/**
* Tests to make sure the max allowed size for multipart requests is enforced.
* @throws Exception
*/
// Disabling test because the encoded size is different at different times and it is hard to predict. Need a better
// test
// @Test
public void sizeLimitationTest() throws Exception {
int blobPartSize = 1024;
byte[] bytes = TestUtils.getRandomBytes(blobPartSize);
int encodedSize = getEncodedSize(bytes);
long[] maxSizesAllowed = { encodedSize + 1, encodedSize, encodedSize - 1, 0 };
for (long maxSizeAllowed : maxSizesAllowed) {
InMemoryFile[] files = { new InMemoryFile(RestUtils.MultipartPost.BLOB_PART, ByteBuffer.wrap(bytes)) };
HttpRequest httpRequest = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
HttpPostRequestEncoder encoder = createEncoder(httpRequest, files);
NettyMultipartRequest request = new NettyMultipartRequest(encoder.finalizeRequest(), new MockChannel(), NETTY_METRICS, Collections.emptySet(), maxSizeAllowed);
assertTrue("Request channel is not open", request.isOpen());
long currentSizeAdded = 0;
boolean failedToAdd = false;
while (!encoder.isEndOfInput()) {
HttpContent httpContent = encoder.readChunk(PooledByteBufAllocator.DEFAULT);
int readableBytes = httpContent.content().readableBytes();
if (currentSizeAdded + readableBytes <= maxSizeAllowed) {
request.addContent(httpContent);
} else {
assertTrue("Max size [" + maxSizeAllowed + "] must be lesser than content size: " + encodedSize, maxSizeAllowed < encodedSize);
try {
request.addContent(httpContent);
fail("Should have failed to add content of size [" + encodedSize + "] because it is over the max size allowed: " + maxSizeAllowed);
} catch (RestServiceException e) {
failedToAdd = true;
assertEquals("Unexpected RestServiceErrorCode", RestServiceErrorCode.RequestTooLarge, e.getErrorCode());
break;
}
}
currentSizeAdded += readableBytes;
}
assertEquals("Success state not as expected. maxSizeAllowed=[" + maxSizeAllowed + "], encodedSize expected=[" + encodedSize + "], actual size added=[" + currentSizeAdded + "]", maxSizeAllowed < encodedSize, failedToAdd);
}
}
Aggregations