use of io.netty.handler.codec.http.multipart.Attribute in project netty by netty.
the class HttpUploadServerHandler method writeHttpData.
private void writeHttpData(InterfaceHttpData data) {
if (data.getHttpDataType() == HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
String value;
try {
value = attribute.getValue();
} catch (IOException e1) {
// Error while reading data from File, only print name and error
e1.printStackTrace();
responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": " + attribute.getName() + " Error while reading value: " + e1.getMessage() + "\r\n");
return;
}
if (value.length() > 100) {
responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": " + attribute.getName() + " data too long\r\n");
} else {
responseContent.append("\r\nBODY Attribute: " + attribute.getHttpDataType().name() + ": " + attribute + "\r\n");
}
} else {
responseContent.append("\r\nBODY FileUpload: " + data.getHttpDataType().name() + ": " + data + "\r\n");
if (data.getHttpDataType() == HttpDataType.FileUpload) {
FileUpload fileUpload = (FileUpload) data;
if (fileUpload.isCompleted()) {
if (fileUpload.length() < 10000) {
responseContent.append("\tContent of file\r\n");
try {
responseContent.append(fileUpload.getString(fileUpload.getCharset()));
} catch (IOException e1) {
// do nothing for the example
e1.printStackTrace();
}
responseContent.append("\r\n");
} else {
responseContent.append("\tFile too long to be printed out:" + fileUpload.length() + "\r\n");
}
// fileUpload.isInMemory();// tells if the file is in Memory
// or on File
// fileUpload.renameTo(dest); // enable to move into another
// File dest
// decoder.removeFileUploadFromClean(fileUpload); //remove
// the File of to delete file
} else {
responseContent.append("\tFile to be continued but should not!\r\n");
}
}
}
}
use of 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 io.netty.handler.codec.http.multipart.Attribute in project moco by dreamhead.
the class FormsRequestExtractor method doExtractForms.
private ImmutableMap<String, String> doExtractForms(final HttpPostRequestDecoder decoder) throws IOException {
List<InterfaceHttpData> bodyHttpDatas = decoder.getBodyHttpDatas();
Map<String, String> forms = newHashMap();
for (InterfaceHttpData data : bodyHttpDatas) {
if (data.getHttpDataType() == InterfaceHttpData.HttpDataType.Attribute) {
Attribute attribute = (Attribute) data;
forms.put(attribute.getName(), attribute.getValue());
}
}
return copyOf(forms);
}
use of io.netty.handler.codec.http.multipart.Attribute in project vert.x by eclipse.
the class Http2ServerRequestImpl method handleEnd.
void handleEnd(MultiMap trailers) {
ended = true;
conn.reportBytesRead(bytesRead);
if (postRequestDecoder != null) {
try {
postRequestDecoder.offer(LastHttpContent.EMPTY_LAST_CONTENT);
while (postRequestDecoder.hasNext()) {
InterfaceHttpData data = postRequestDecoder.next();
if (data instanceof Attribute) {
Attribute attr = (Attribute) data;
try {
formAttributes().add(attr.getName(), attr.getValue());
} catch (Exception e) {
// Will never happen, anyway handle it somehow just in case
handleException(e);
}
}
}
} catch (HttpPostRequestDecoder.EndOfDataDecoderException e) {
// ignore this as it is expected
} catch (Exception e) {
handleException(e);
} finally {
postRequestDecoder.destroy();
}
}
if (endHandler != null) {
endHandler.handle(null);
}
}
use of io.netty.handler.codec.http.multipart.Attribute 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);
} finally {
data.release();
}
} else if (data.getHttpDataType().equals(InterfaceHttpData.HttpDataType.FileUpload)) {
List<UploadedFile> values = files.get(data.getName());
if (values == null) {
values = new ArrayList<>(1);
files.put(data.getName(), values);
}
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);
} finally {
data.release();
}
}
data = decoder.next();
}
} catch (HttpPostRequestDecoder.EndOfDataDecoderException ignore) {
// ignore
} finally {
decoder.destroy();
}
return new DefaultForm(new ImmutableDelegatingMultiValueMap<>(attributes), new ImmutableDelegatingMultiValueMap<>(files));
}
Aggregations