use of io.netty.handler.codec.http.multipart.InterfaceHttpData 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 io.netty.handler.codec.http.multipart.InterfaceHttpData 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 io.netty.handler.codec.http.multipart.InterfaceHttpData in project cosmic by MissionCriticalCloud.
the class HttpUploadServerHandler method readFileUploadData.
private HttpResponseStatus readFileUploadData() throws IOException {
while (decoder.hasNext()) {
final InterfaceHttpData data = decoder.next();
if (data != null) {
try {
logger.info("BODY FileUpload: " + data.getHttpDataType().name() + ": " + data);
if (data.getHttpDataType() == HttpDataType.FileUpload) {
final FileUpload fileUpload = (FileUpload) data;
if (fileUpload.isCompleted()) {
requestProcessed = true;
final String format = ImageStoreUtil.checkTemplateFormat(fileUpload.getFile().getAbsolutePath(), fileUpload.getFilename());
if (StringUtils.isNotBlank(format)) {
final String errorString = "File type mismatch between the sent file and the actual content. Received: " + format;
logger.error(errorString);
responseContent.append(errorString);
storageResource.updateStateMapWithError(uuid, errorString);
return HttpResponseStatus.BAD_REQUEST;
}
final String status = storageResource.postUpload(uuid, fileUpload.getFile().getName());
if (status != null) {
responseContent.append(status);
storageResource.updateStateMapWithError(uuid, status);
return HttpResponseStatus.INTERNAL_SERVER_ERROR;
} else {
responseContent.append("upload successful.");
return HttpResponseStatus.OK;
}
}
}
} finally {
data.release();
}
}
}
responseContent.append("received entity is not a file");
return HttpResponseStatus.UNPROCESSABLE_ENTITY;
}
use of io.netty.handler.codec.http.multipart.InterfaceHttpData in project duangframework by tcrct.
the class MultiPartPostDecoder method decoder.
@Override
public Map<String, String[]> decoder() throws Exception {
HttpPostMultipartRequestDecoder requestDecoder = new HttpPostMultipartRequestDecoder(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) {
MemoryAttribute attribute = (MemoryAttribute) 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 io.netty.handler.codec.http.multipart.InterfaceHttpData 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;
}
Aggregations