use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project riposte by Nike-Inc.
the class RequestInfoImplTest method getMultipartParts_works_as_expected_with_known_valid_data.
@Test
public void getMultipartParts_works_as_expected_with_known_valid_data() throws IOException {
// given
RequestInfoImpl<?> requestInfo = RequestInfoImpl.dummyInstanceForUnknownRequests();
Whitebox.setInternalState(requestInfo, "isMultipart", true);
Whitebox.setInternalState(requestInfo, "contentCharset", CharsetUtil.UTF_8);
Whitebox.setInternalState(requestInfo, "protocolVersion", HttpVersion.HTTP_1_1);
Whitebox.setInternalState(requestInfo, "method", HttpMethod.POST);
requestInfo.isCompleteRequestWithAllChunks = true;
requestInfo.rawContentBytes = KNOWN_MULTIPART_DATA_BODY.getBytes(CharsetUtil.UTF_8);
requestInfo.getHeaders().set("Content-Type", KNOWN_MULTIPART_DATA_CONTENT_TYPE_HEADER);
// when
List<InterfaceHttpData> result = requestInfo.getMultipartParts();
// then
assertThat(result, notNullValue());
assertThat(result.size(), is(1));
InterfaceHttpData data = result.get(0);
assertThat(data, instanceOf(FileUpload.class));
FileUpload fileUploadData = (FileUpload) data;
assertThat(fileUploadData.getName(), is(KNOWN_MULTIPART_DATA_NAME));
assertThat(fileUploadData.getFilename(), is(KNOWN_MULTIPART_DATA_FILENAME));
assertThat(fileUploadData.getString(CharsetUtil.UTF_8), is(KNOWN_MULTIPART_DATA_ATTR_UUID));
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project jocean-http by isdom.
the class HttpPostRequestEncoderTestCase method testGetBodyListAttributes.
@Test
public final void testGetBodyListAttributes() throws Exception {
final HttpDataFactory factory = new DefaultHttpDataFactory(false);
final HttpRequest request = new DefaultHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.POST, "/");
// Use the PostBody encoder
final HttpPostRequestEncoder postRequestEncoder = // true => multipart
new HttpPostRequestEncoder(factory, request, true);
final MemoryFileUpload f1 = new MemoryFileUpload("m1", "m1", "application/json", null, null, 100);
final DiskFileUpload f2 = new DiskFileUpload("d1", "d1", "application/json", null, null, 100);
final DiskFileUpload f3 = new DiskFileUpload("d2", "d2", "application/json", null, null, 100);
postRequestEncoder.addBodyHttpData(f1);
postRequestEncoder.addBodyHttpData(f2);
postRequestEncoder.addBodyHttpData(f3);
final List<InterfaceHttpData> attrs = postRequestEncoder.getBodyListAttributes();
final InterfaceHttpData[] datas = new InterfaceHttpData[] { f1, f2, f3 };
for (int idx = 0; idx < datas.length; idx++) {
assertEquals(datas[idx], attrs.toArray(new InterfaceHttpData[0])[idx]);
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project jocean-http by isdom.
the class DefaultSignalClientTestCase method testSignalClientWithAttachmentSuccess.
@Test
public void testSignalClientWithAttachmentSuccess() throws Exception {
final TestResponse respToSendback = new TestResponse("0", "OK");
final AtomicReference<HttpMethod> reqMethodReceivedRef = new AtomicReference<>();
final AtomicReference<String> reqpathReceivedRef = new AtomicReference<>();
final AtomicReference<TestRequestByPost> reqbeanReceivedRef = new AtomicReference<>();
final List<FileUpload> uploads = new ArrayList<>();
final Action2<FullHttpRequest, HttpTrade> requestAndTradeAwareWhenCompleted = new Action2<FullHttpRequest, HttpTrade>() {
@Override
public void call(final FullHttpRequest req, final HttpTrade trade) {
reqMethodReceivedRef.set(req.method());
reqpathReceivedRef.set(req.uri());
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(HTTP_DATA_FACTORY, req);
// first is signal
boolean isfirst = true;
while (decoder.hasNext()) {
final InterfaceHttpData data = decoder.next();
if (!isfirst) {
if (data instanceof FileUpload) {
uploads.add((FileUpload) data);
}
} else {
isfirst = false;
try {
reqbeanReceivedRef.set((TestRequestByPost) JSON.parseObject(Nettys.dumpByteBufAsBytes(((FileUpload) data).content()), TestRequestByPost.class));
} catch (Exception e) {
LOG.warn("exception when JSON.parseObject, detail: {}", ExceptionUtils.exception2detail(e));
}
}
}
trade.outbound(buildResponse(respToSendback, trade.onTerminate()));
}
};
// launch test server for attachment send
final String testAddr = UUID.randomUUID().toString();
final Subscription server = TestHttpUtil.createTestServerWith(testAddr, requestAndTradeAwareWhenCompleted, Feature.ENABLE_LOGGING, Feature.ENABLE_COMPRESSOR);
try {
final TestChannelCreator creator = new TestChannelCreator();
final TestChannelPool pool = new TestChannelPool(1);
final DefaultHttpClient httpclient = new DefaultHttpClient(creator, pool);
final DefaultSignalClient signalClient = new DefaultSignalClient(new URI("http://test"), httpclient, new AttachmentBuilder4InMemory());
signalClient.registerRequestType(TestRequestByPost.class, TestResponse.class, null, buildUri2Addr(testAddr), Feature.ENABLE_LOGGING);
final AttachmentInMemory[] attachsToSend = new AttachmentInMemory[] { new AttachmentInMemory("1", "text/plain", "11111111111111".getBytes(Charsets.UTF_8)), new AttachmentInMemory("2", "text/plain", "22222222222222222".getBytes(Charsets.UTF_8)), new AttachmentInMemory("3", "text/plain", "333333333333333".getBytes(Charsets.UTF_8)) };
final TestRequestByPost reqToSend = new TestRequestByPost("1", null);
final TestResponse respReceived = ((SignalClient) signalClient).interaction().request(reqToSend).feature(attachsToSend).<TestResponse>build().timeout(1, TimeUnit.SECONDS).toBlocking().single();
assertEquals(HttpMethod.POST, reqMethodReceivedRef.get());
assertEquals("/test/simpleRequest", reqpathReceivedRef.get());
assertEquals(reqToSend, reqbeanReceivedRef.get());
assertEquals(respToSendback, respReceived);
final FileUpload[] attachsReceived = uploads.toArray(new FileUpload[0]);
assertEquals(attachsToSend.length, attachsReceived.length);
for (int idx = 0; idx < attachsToSend.length; idx++) {
final AttachmentInMemory inmemory = attachsToSend[idx];
final FileUpload upload = attachsReceived[idx];
assertEquals(inmemory.filename, upload.getName());
assertEquals(inmemory.contentType, upload.getContentType());
assertTrue(Arrays.equals(inmemory.content(), upload.get()));
}
pool.awaitRecycleChannels();
} finally {
server.unsubscribe();
}
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project jocean-http by isdom.
the class AsBlob method onNext4Multipart.
private Observable<? extends Blob> onNext4Multipart(final HttpContent content) {
try {
_postDecoder.offer(content);
} catch (ErrorDataDecoderException e) {
LOG.warn("exception when postDecoder.offer, detail: {}", ExceptionUtils.exception2detail(e));
}
final List<Blob> blobs = new ArrayList<>();
try {
while (_postDecoder.hasNext()) {
final InterfaceHttpData data = _postDecoder.next();
if (data != null) {
try {
final Blob blob = this._holder.hold(processHttpData(data));
if (null != blob) {
blobs.add(blob);
LOG.info("onNext4Multipart: add Blob {}", blob);
}
} finally {
data.release();
}
}
}
} catch (EndOfDataDecoderException e) {
LOG.warn("exception when postDecoder.hasNext, detail: {}", ExceptionUtils.exception2detail(e));
}
return blobs.isEmpty() ? Observable.<Blob>empty() : Observable.from(blobs);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.InterfaceHttpData in project ratpack by ratpack.
the class FormDecoder method parseForm.
@SuppressWarnings("deprecation")
public static Form parseForm(Context context, TypedData body, MultiValueMap<String, String> base) throws RuntimeException {
Request request = context.getRequest();
HttpMethod method = io.netty.handler.codec.http.HttpMethod.valueOf(request.getMethod().getName());
HttpRequest nettyRequest = new DefaultHttpRequest(HttpVersion.HTTP_1_1, method, request.getUri());
nettyRequest.headers().add(HttpHeaderNames.CONTENT_TYPE, body.getContentType().toString());
HttpPostRequestDecoder decoder = new HttpPostRequestDecoder(nettyRequest);
HttpContent content = new DefaultHttpContent(body.getBuffer());
decoder.offer(content);
decoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
Map<String, List<String>> attributes = new LinkedHashMap<>(base.getAll());
Map<String, List<UploadedFile>> files = new LinkedHashMap<>();
try {
InterfaceHttpData data = decoder.next();
while (data != null) {
if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
List<String> values = attributes.get(data.getName());
if (values == null) {
values = new ArrayList<>(1);
attributes.put(data.getName(), values);
}
try {
values.add(((Attribute) data).getValue());
} catch (IOException e) {
throw uncheck(e);
}
} else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) {
List<UploadedFile> values = files.computeIfAbsent(data.getName(), k -> new ArrayList<>(1));
try {
FileUpload nettyFileUpload = (FileUpload) data;
final ByteBuf byteBuf = nettyFileUpload.getByteBuf();
byteBuf.retain();
context.onClose(ro -> byteBuf.release());
MediaType contentType;
String rawContentType = nettyFileUpload.getContentType();
if (rawContentType == null) {
contentType = null;
} else {
Charset charset = nettyFileUpload.getCharset();
if (charset == null) {
contentType = DefaultMediaType.get(rawContentType);
} else {
contentType = DefaultMediaType.get(rawContentType + ";charset=" + charset);
}
}
UploadedFile fileUpload = new DefaultUploadedFile(new ByteBufBackedTypedData(byteBuf, contentType), nettyFileUpload.getFilename());
values.add(fileUpload);
} catch (IOException e) {
throw uncheck(e);
}
}
data = decoder.next();
}
} catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) {
// ignore
} finally {
decoder.destroy();
}
return new DefaultForm(new ImmutableDelegatingMultiValueMap<>(attributes), new ImmutableDelegatingMultiValueMap<>(files));
}
Aggregations