use of io.undertow.server.handlers.form.FormData in project camel by apache.
the class DefaultUndertowHttpBinding method toCamelMessage.
@Override
public Message toCamelMessage(HttpServerExchange httpExchange, Exchange exchange) throws Exception {
Message result = new DefaultMessage();
populateCamelHeaders(httpExchange, result.getHeaders(), exchange);
// Map form data which is parsed by undertow form parsers
FormData formData = httpExchange.getAttachment(FormDataParser.FORM_DATA);
if (formData != null) {
Map<String, Object> body = new HashMap<>();
formData.forEach(key -> {
formData.get(key).forEach(value -> {
if (value.isFile()) {
DefaultAttachment attachment = new DefaultAttachment(new FilePartDataSource(value));
result.addAttachmentObject(key, attachment);
body.put(key, attachment.getDataHandler());
} else if (headerFilterStrategy != null && !headerFilterStrategy.applyFilterToExternalHeaders(key, value.getValue(), exchange)) {
UndertowHelper.appendHeader(result.getHeaders(), key, value.getValue());
UndertowHelper.appendHeader(body, key, value.getValue());
}
});
});
result.setBody(body);
} else {
//body is extracted as byte[] then auto TypeConverter kicks in
if (Methods.POST.equals(httpExchange.getRequestMethod()) || Methods.PUT.equals(httpExchange.getRequestMethod())) {
result.setBody(readFromChannel(httpExchange.getRequestChannel()));
} else {
result.setBody(null);
}
}
return result;
}
use of io.undertow.server.handlers.form.FormData in project core-ng-project by neowu.
the class RequestParser method parseForm.
private void parseForm(RequestImpl request, HttpServerExchange exchange) {
FormData formData = exchange.getAttachment(FormDataParser.FORM_DATA);
if (formData == null)
return;
for (String name : formData) {
FormData.FormValue value = formData.getFirst(name);
if (value.isFile()) {
if (!Strings.isEmpty(value.getFileName())) {
// browser passes empty file name if not choose file in form
logger.debug("[request:file] {}={}, size={}", name, value.getFileName(), Files.size(value.getPath()));
request.files.put(name, new MultipartFile(value.getPath(), value.getFileName(), value.getHeaders().getFirst(Headers.CONTENT_TYPE)));
}
} else {
logger.debug("[request:form] {}={}", name, new FieldParam(name, value.getValue()));
request.formParams.put(name, value.getValue());
}
}
}
use of io.undertow.server.handlers.form.FormData in project light-codegen by networknt.
the class ValidateUploadFileHandler method handle.
/**
* Retrieve the file from the request and validate the json contents against a schema.
* Will return the contents of the file as well.
*
* @param o The multipart as parsed from com.networknt.rpc.router.MultipartHandler
*
* @return The contents of the file if valid, null otherwise. TODO to provide error messages.
*/
@Override
public ByteBuffer handle(HttpServerExchange exchange, Object o) {
exchange.getResponseHeaders().add(new HttpString("Content-Type"), "application/json");
logger.entry(o);
if (o instanceof FormData) {
File file = this.getFileFromForm((FormData) o);
try {
// TODO validate against schema... where do I find the schema?
String fileContents = new String(Files.readAllBytes(file.toPath()), StandardCharsets.UTF_8);
return NioUtils.toByteBuffer(fileContents);
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
use of io.undertow.server.handlers.form.FormData in project undertow by undertow-io.
the class FormAuthenticationMechanism method runFormAuth.
public AuthenticationMechanismOutcome runFormAuth(final HttpServerExchange exchange, final SecurityContext securityContext) {
final FormDataParser parser = formParserFactory.createParser(exchange);
if (parser == null) {
UndertowLogger.SECURITY_LOGGER.debug("Could not authenticate as no form parser is present");
// TODO - May need a better error signaling mechanism here to prevent repeated attempts.
return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
}
Throwable original = null;
AuthenticationMechanismOutcome retValue = null;
try {
final FormData data = parser.parseBlocking();
final FormData.FormValue jUsername = data.getFirst("j_username");
final FormData.FormValue jPassword = data.getFirst("j_password");
if (jUsername == null || jPassword == null) {
UndertowLogger.SECURITY_LOGGER.debugf("Could not authenticate as username or password was not present in the posted result for %s", exchange);
return AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
}
final String userName = jUsername.getValue();
final String password = jPassword.getValue();
AuthenticationMechanismOutcome outcome = null;
PasswordCredential credential = new PasswordCredential(password.toCharArray());
try {
IdentityManager identityManager = getIdentityManager(securityContext);
Account account = identityManager.verify(userName, credential);
if (account != null) {
securityContext.authenticationComplete(account, name, true);
UndertowLogger.SECURITY_LOGGER.debugf("Authenticated user %s using for auth for %s", account.getPrincipal().getName(), exchange);
outcome = AuthenticationMechanismOutcome.AUTHENTICATED;
} else {
securityContext.authenticationFailed(MESSAGES.authenticationFailed(userName), name);
}
} catch (Throwable t) {
original = t;
} finally {
try {
if (outcome == AuthenticationMechanismOutcome.AUTHENTICATED) {
handleRedirectBack(exchange);
exchange.endExchange();
}
retValue = outcome != null ? outcome : AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
} catch (Throwable t) {
if (original != null) {
original.addSuppressed(t);
} else {
original = t;
}
}
}
} catch (IOException e) {
original = new UncheckedIOException(e);
}
if (original != null) {
if (original instanceof RuntimeException) {
throw (RuntimeException) original;
}
if (original instanceof Error) {
throw (Error) original;
}
}
return retValue;
}
use of io.undertow.server.handlers.form.FormData in project undertow by undertow-io.
the class RequestDumpingHandler method dumpRequestBody.
private void dumpRequestBody(HttpServerExchange exchange, StringBuilder sb) {
try {
FormData formData = exchange.getAttachment(FormDataParser.FORM_DATA);
if (formData != null) {
sb.append("body=\n");
for (String formField : formData) {
Deque<FormData.FormValue> formValues = formData.get(formField);
sb.append(formField).append("=");
for (FormData.FormValue formValue : formValues) {
sb.append(formValue.isFileItem() ? "[file-content]" : formValue.getValue());
sb.append("\n");
if (formValue.getHeaders() != null) {
sb.append("headers=\n");
for (HeaderValues header : formValue.getHeaders()) {
sb.append("\t").append(header.getHeaderName()).append("=").append(header.getFirst()).append("\n");
}
}
}
}
}
} catch (Exception e) {
throw new RuntimeException(e);
}
}
Aggregations