use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute in project asterixdb by apache.
the class PostRequest method create.
public static IServletRequest create(FullHttpRequest request) throws IOException {
List<String> names = new ArrayList<>();
List<String> values = new ArrayList<>();
HttpPostRequestDecoder decoder = null;
try {
decoder = new HttpPostRequestDecoder(request);
} catch (Exception e) {
//ignore. this means that the body of the POST request does not have key value pairs
LOGGER.log(Level.WARNING, "Failed to decode a post message. Fix the API not to have queries as POST body", e);
}
if (decoder != null) {
try {
List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
for (InterfaceHttpData data : bodyHttpDatas) {
if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.Attribute)) {
Attribute attr = (MixedAttribute) data;
names.add(data.getName());
values.add(attr.getValue());
}
}
} finally {
decoder.destroy();
}
}
return new PostRequest(request, new QueryStringDecoder(request.uri()).parameters(), names, values);
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute in project dubbo by alibaba.
the class HttpCommandDecoder method decode.
public static CommandContext decode(HttpRequest request) {
CommandContext commandContext = null;
if (request != null) {
QueryStringDecoder queryStringDecoder = new QueryStringDecoder(request.getUri());
String path = queryStringDecoder.path();
String[] array = path.split("/");
if (array.length == 2) {
String name = array[1];
// process GET request and POST request separately. Check url for GET, and check body for POST
if (request.getMethod() == HttpMethod.GET) {
if (queryStringDecoder.parameters().isEmpty()) {
commandContext = CommandContextFactory.newInstance(name);
commandContext.setHttp(true);
} else {
List<String> valueList = new ArrayList<String>();
for (List<String> values : queryStringDecoder.parameters().values()) {
valueList.addAll(values);
}
commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
}
} else if (request.getMethod() == HttpMethod.POST) {
HttpPostRequestDecoder httpPostRequestDecoder = new HttpPostRequestDecoder(request);
List<String> valueList = new ArrayList<String>();
for (InterfaceHttpData interfaceHttpData : httpPostRequestDecoder.getBodyHttpDatas()) {
if (interfaceHttpData.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
Attribute attribute = (Attribute) interfaceHttpData;
try {
valueList.add(attribute.getValue());
} catch (IOException ex) {
throw new RuntimeException(ex);
}
}
}
if (valueList.isEmpty()) {
commandContext = CommandContextFactory.newInstance(name);
commandContext.setHttp(true);
} else {
commandContext = CommandContextFactory.newInstance(name, valueList.toArray(new String[] {}), true);
}
}
}
}
return commandContext;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute in project duangframework by tcrct.
the class PostDecoder method decoder.
@Override
public Map<String, String[]> decoder() throws Exception {
HttpPostRequestDecoder requestDecoder = new HttpPostRequestDecoder(HTTP_DATA_FACTORY, request);
List<InterfaceHttpData> paramsList = requestDecoder.getBodyHttpDatas();
if (null != paramsList && !paramsList.isEmpty()) {
Map<String, List<String>> params = new HashMap<>();
for (InterfaceHttpData httpData : paramsList) {
Attribute attribute = (Attribute) httpData;
String key = attribute.getName();
String value = attribute.getValue();
parseValue2List(params, key, value);
paramsMap.put(key, params.get(key).toArray(EMPTY_ARRAYS));
}
}
return paramsMap;
}
use of org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute 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 org.apache.flink.shaded.netty4.io.netty.handler.codec.http.multipart.Attribute in project flink by apache.
the class AbstractHandlerTest method testFileCleanup.
@Test
public void testFileCleanup() throws Exception {
final Path dir = temporaryFolder.newFolder().toPath();
final Path file = dir.resolve("file");
Files.createFile(file);
RestfulGateway mockRestfulGateway = new TestingRestfulGateway.Builder().build();
final GatewayRetriever<RestfulGateway> mockGatewayRetriever = () -> CompletableFuture.completedFuture(mockRestfulGateway);
CompletableFuture<Void> requestProcessingCompleteFuture = new CompletableFuture<>();
TestHandler handler = new TestHandler(requestProcessingCompleteFuture, mockGatewayRetriever);
RouteResult<?> routeResult = new RouteResult<>("", "", Collections.emptyMap(), Collections.emptyMap(), "");
HttpRequest request = new DefaultFullHttpRequest(HttpVersion.HTTP_1_1, HttpMethod.GET, TestHandler.TestHeaders.INSTANCE.getTargetRestEndpointURL(), Unpooled.wrappedBuffer(new byte[0]));
RoutedRequest<?> routerRequest = new RoutedRequest<>(routeResult, request);
Attribute<FileUploads> attribute = new SimpleAttribute();
attribute.set(new FileUploads(dir));
Channel channel = mock(Channel.class);
when(channel.attr(any(AttributeKey.class))).thenReturn(attribute);
ChannelHandlerContext context = mock(ChannelHandlerContext.class);
when(context.channel()).thenReturn(channel);
handler.respondAsLeader(context, routerRequest, mockRestfulGateway);
// the (asynchronous) request processing is not yet complete so the files should still exist
Assert.assertTrue(Files.exists(file));
requestProcessingCompleteFuture.complete(null);
Assert.assertFalse(Files.exists(file));
}
Aggregations