本文整理汇总了Java中io.undertow.server.handlers.form.FormDataParser类的典型用法代码示例。如果您正苦于以下问题:Java FormDataParser类的具体用法?Java FormDataParser怎么用?Java FormDataParser使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FormDataParser类属于io.undertow.server.handlers.form包,在下文中一共展示了FormDataParser类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: setCharacterEncoding
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
@Override
public void setCharacterEncoding(final String env) throws UnsupportedEncodingException {
if (readStarted) {
return;
}
try {
characterEncoding = Charset.forName(env);
final ManagedServlet originalServlet = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getOriginalServletPathMatch().getServletChain().getManagedServlet();
final FormDataParser parser = originalServlet.getFormParserFactory().createParser(exchange);
if (parser != null) {
parser.setCharacterEncoding(env);
}
} catch (UnsupportedCharsetException e) {
throw new UnsupportedEncodingException();
}
}
示例2: parseFormData
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
private FormData parseFormData() {
if (parsedFormData == null) {
if (readStarted) {
return null;
}
final ManagedServlet originalServlet = exchange.getAttachment(ServletRequestContext.ATTACHMENT_KEY).getCurrentServlet().getManagedServlet();
final FormDataParser parser = originalServlet.getFormParserFactory().createParser(exchange);
if (parser == null) {
return null;
}
readStarted = true;
try {
return parsedFormData = parser.parseBlocking();
} catch (IOException e) {
throw new RuntimeException(e);
}
}
return parsedFormData;
}
示例3: ban
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
public void ban(HttpServerExchange exchange) {
User user = parseUserFromForm(exchange);
User user_perform = exchange.getAttachment(AuthReader.USER);
if (user != null && user.getRole().lessThan(user_perform.getRole())) {
String time = "86400";
FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
FormData.FormValue time_form = data.getFirst("time");
if (time_form != null) {
time = time_form.getValue();
}
if (doLog(exchange)) {
App.getDatabase().adminLog("ban " + user.getName(), user_perform.getId());
}
user.ban(Integer.parseInt(time), getBanReason(exchange), getRollbackTime(exchange));
exchange.getResponseHeaders().put(Headers.CONTENT_TYPE, "application/text");
exchange.setStatusCode(200);
} else {
exchange.setStatusCode(400);
}
}
示例4: handleRequest
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
if (hasBody(exchange)) { // parse body early, not process until body is read (e.g. for chunked), to save one blocking thread during read
FormDataParser parser = formParserFactory.createParser(exchange);
if (parser != null) {
parser.parse(handler);
return;
}
RequestBodyReader reader = new RequestBodyReader(exchange, handler);
StreamSourceChannel channel = exchange.getRequestChannel();
reader.read(channel); // channel will be null if getRequestChannel() is already called, but here should not be that case
if (!reader.complete()) {
channel.getReadSetter().set(reader);
channel.resumeReads();
return;
}
}
exchange.dispatch(handler);
}
示例5: parseForm
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
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, value.getValue());
request.formParams.put(name, value.getValue());
}
}
}
示例6: OvertownRequest
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
public OvertownRequest(HttpServerExchange exchange) {
this.exchange = exchange;
this.queryParameters = exchange.getQueryParameters();
this.parameterConverters = new HashMap<>();
this.viewAttributes = new ViewAttributes();
this.sessionManager = OvertownSessionManager.getInstance();
if (isPostRequest()) {
FormDataParser create = new FormEncodedDataDefinition().create(exchange);
try {
formData = create != null ? create.parseBlocking() : null;
} catch (IOException ioe) {
LOGGER.error(ioe.getMessage());
}
}
if( exchange.getAttachment( EXCEPTION_ATTACHMENT_KEY ) != null){
Exception e = exchange.getAttachment( EXCEPTION_ATTACHMENT_KEY );
addAttribute(ErrorHandler.ERROR_500, e);
exchange.removeAttachment( EXCEPTION_ATTACHMENT_KEY );
}
}
示例7: handleRequest
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
@Override
public void handleRequest(HttpServerExchange exchange) throws Exception {
if (Methods.POST.equals(exchange.getRequestMethod())) {
String newMethod = exchange.getRequestHeaders().getFirst(HEADER);
Optional<HttpString> opt = HttpMethod.find(newMethod);
if (opt.isPresent()) {
exchange.setRequestMethod(opt.get());
} else {
FormData data = exchange
.getAttachment(FormDataParser.FORM_DATA);
if (data != null) {
FormData.FormValue fv = data.getFirst(FORM);
if (fv != null && fv.isFile() == false) {
HttpMethod.find(fv.getValue()).map(
exchange::setRequestMethod);
}
}
}
}
this.next.handleRequest(exchange);
}
示例8: runFormAuth
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
public AuthenticationMechanismOutcome runFormAuth(final HttpServerExchange exchange, final SecurityContext securityContext) {
final FormDataParser parser = formParserFactory.createParser(exchange);
if (parser == null) {
UndertowLogger.REQUEST_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;
}
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.REQUEST_LOGGER.debug("Could not authenticate as username or password was not present in the posted result");
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 = securityContext.getIdentityManager();
Account account = identityManager.verify(userName, credential);
if (account != null) {
securityContext.authenticationComplete(account, name, true);
outcome = AuthenticationMechanismOutcome.AUTHENTICATED;
} else {
securityContext.authenticationFailed(MESSAGES.authenticationFailed(userName), name);
}
} finally {
if (outcome == AuthenticationMechanismOutcome.AUTHENTICATED) {
handleRedirectBack(exchange);
exchange.endExchange();
}
return outcome != null ? outcome : AuthenticationMechanismOutcome.NOT_AUTHENTICATED;
}
} catch (IOException e) {
throw new RuntimeException(e);
}
}
示例9: getBanReason
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
private String getBanReason(HttpServerExchange exchange) {
FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
FormData.FormValue reason = data.getFirst("reason");
String reason_str = "";
if (reason != null) {
reason_str = reason.getValue();
}
return reason_str;
}
示例10: getRollbackTime
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
private int getRollbackTime(HttpServerExchange exchange) {
FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
FormData.FormValue rollback = data.getFirst("rollback_time");
String rollback_int = "0";
if (rollback != null) {
rollback_int = rollback.getValue();
}
return Integer.parseInt(rollback_int);
}
示例11: signUp
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
public void signUp(HttpServerExchange exchange) {
FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
FormData.FormValue nameVal = data.getFirst("username");
FormData.FormValue tokenVal = data.getFirst("token");
if (nameVal == null || tokenVal == null) {
respond(exchange, StatusCodes.BAD_REQUEST, new Error("bad_params", "Missing parameters"));
return;
}
String name = nameVal.getValue();
String token = tokenVal.getValue();
if (token.isEmpty()) {
respond(exchange, StatusCodes.BAD_REQUEST, new Error("bad_token", "Missing signup token"));
return;
} else if (name.isEmpty()) {
respond(exchange, StatusCodes.BAD_REQUEST, new Error("bad_username", "Username may not be empty"));
return;
} else if (!name.matches("[a-zA-Z0-9_\\-]+")) {
respond(exchange, StatusCodes.BAD_REQUEST, new Error("bad_username", "Username contains invalid characters"));
return;
} else if (!App.getUserManager().isValidSignupToken(token)) {
respond(exchange, StatusCodes.BAD_REQUEST, new Error("bad_token", "Invalid signup token"));
return;
}
String ip = exchange.getAttachment(IPReader.IP);
User user = App.getUserManager().signUp(name, token, ip);
if (user == null) {
respond(exchange, StatusCodes.BAD_REQUEST, new Error("bad_username", "Username taken, try another?"));
return;
}
String loginToken = App.getUserManager().logIn(user, ip);
setAuthCookie(exchange, loginToken, 24);
respond(exchange, StatusCodes.OK, new SignUpResponse(loginToken));
}
示例12: report
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
public void report(HttpServerExchange exchange) {
User user = exchange.getAttachment(AuthReader.USER);
if (user == null) {
exchange.setStatusCode(StatusCodes.BAD_REQUEST);
exchange.endExchange();
return;
}
FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
FormData.FormValue xq = data.getFirst("x");
FormData.FormValue yq = data.getFirst("y");
FormData.FormValue idq = data.getFirst("id");
FormData.FormValue msgq = data.getFirst("message");
if (xq == null || yq == null || idq == null || msgq == null) {
exchange.setStatusCode(StatusCodes.BAD_REQUEST);
exchange.endExchange();
return;
}
int x = Integer.parseInt(xq.getValue());
int y = Integer.parseInt(yq.getValue());
int id = Integer.parseInt(idq.getValue());
if (x < 0 || x >= App.getWidth() || y < 0 || y >= App.getHeight()) {
exchange.setStatusCode(StatusCodes.BAD_REQUEST);
exchange.endExchange();
return;
}
DBPixelPlacement pxl = App.getDatabase().getPixelByID(id);
if (pxl.x != x || pxl.y != y) {
exchange.setStatusCode(StatusCodes.BAD_REQUEST);
exchange.endExchange();
return;
}
App.getDatabase().addReport(user.getId(), id, x, y, msgq.getValue());
exchange.setStatusCode(200);
}
示例13: parseUserFromForm
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
private User parseUserFromForm(HttpServerExchange exchange) {
FormData data = exchange.getAttachment(FormDataParser.FORM_DATA);
if (data != null) {
FormData.FormValue username = data.getFirst("username");
if (username != null) {
return App.getUserManager().getByName(username.getValue());
}
}
return null;
}
示例14: getForm
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
/**
* Retrieves the form parameter from a request
*
* @param exchange The Undertow HttpServerExchange
*
* @throws IOException
*/
@SuppressWarnings("all")
protected Form getForm(HttpServerExchange exchange) throws IOException {
final Form form = Application.getInstance(Form.class);
if (this.requestHelper.isPostPutPatch(exchange)) {
final Builder builder = FormParserFactory.builder();
builder.setDefaultCharset(Charsets.UTF_8.name());
final FormDataParser formDataParser = builder.build().createParser(exchange);
if (formDataParser != null) {
exchange.startBlocking();
final FormData formData = formDataParser.parseBlocking();
formData.forEach(data -> {
Deque<FormValue> deque = formData.get(data);
if (deque != null) {
FormValue formValue = deque.element();
if (formValue != null) {
if (formValue.isFile() && formValue.getPath() != null) {
form.addFile(formValue.getPath().toFile());
} else {
if (data.contains("[]")) {
String key = StringUtils.replace(data, "[]", "");
for (Iterator iterator = deque.iterator(); iterator.hasNext();) {
form.addValueList(new HttpString(key).toString(), ((FormValue) iterator.next()).getValue());
}
} else {
form.addValue(new HttpString(data).toString(), formValue.getValue());
}
}
}
}
});
form.setSubmitted(true);
}
}
return form;
}
示例15: translateForms
import io.undertow.server.handlers.form.FormDataParser; //导入依赖的package包/类
<T> Map<String, List<T>> translateForms(Predicate<FormData.FormValue> pred,
Function<FormData.FormValue, T> translator) {
FormData fd = this.exchange.getAttachment(FormDataParser.FORM_DATA);
if (fd == null) {
return Collections.emptyMap();
}
Map<String, List<T>> newone = new HashMap<>();
fd.forEach(k -> {
List<T> list = fd.get(k).stream().filter(pred).map(translator)
.collect(Collectors.toList());
newone.put(k, list);
});
return Collections.unmodifiableMap(newone);
}