本文整理汇总了Java中io.undertow.server.handlers.form.FormData类的典型用法代码示例。如果您正苦于以下问题:Java FormData类的具体用法?Java FormData怎么用?Java FormData使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
FormData类属于io.undertow.server.handlers.form包,在下文中一共展示了FormData类的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Java代码示例。
示例1: parseForm
import io.undertow.server.handlers.form.FormData; //导入依赖的package包/类
private FormData parseForm(final HttpServerExchange exchange, final String charset) throws IOException {
String value = exchange.getRequestHeaders().getFirst("Content-Type");
if (value != null) {
if (value.startsWith(MediaType.APPLICATION_FORM_URLENCODED)) {
blocking.get();
return new FormEncodedDataDefinition()
.setDefaultEncoding(charset)
.create(exchange)
.parseBlocking();
} else if (value.startsWith(MediaType.MULTIPART_FORM_DATA)) {
blocking.get();
return new MultiPartParserDefinition()
.setTempFileLocation(TMP_DIR.toPath())
.setDefaultEncoding(charset)
.create(exchange)
.parseBlocking();
}
}
return new FormData(0);
}
示例2: handle
import io.undertow.server.handlers.form.FormData; //导入依赖的package包/类
/**
* 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(Object o) {
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;
}
示例3: getParameter
import io.undertow.server.handlers.form.FormData; //导入依赖的package包/类
@Override
public String getParameter(final String name) {
if(queryParameters == null) {
queryParameters = exchange.getQueryParameters();
}
Deque<String> params = queryParameters.get(name);
if (params == null) {
if (exchange.getRequestMethod().equals(Methods.POST)) {
final FormData parsedFormData = parseFormData();
if (parsedFormData != null) {
FormData.FormValue res = parsedFormData.getFirst(name);
if (res == null || res.isFile()) {
return null;
} else {
return res.getValue();
}
}
}
return null;
}
return params.getFirst();
}
示例4: getParameterNames
import io.undertow.server.handlers.form.FormData; //导入依赖的package包/类
@Override
public Enumeration<String> getParameterNames() {
if (queryParameters == null) {
queryParameters = exchange.getQueryParameters();
}
final Set<String> parameterNames = new HashSet<>(queryParameters.keySet());
if (exchange.getRequestMethod().equals(Methods.POST)) {
final FormData parsedFormData = parseFormData();
if (parsedFormData != null) {
Iterator<String> it = parsedFormData.iterator();
while (it.hasNext()) {
String name = it.next();
for(FormData.FormValue param : parsedFormData.get(name)) {
if(!param.isFile()) {
parameterNames.add(name);
break;
}
}
}
}
}
return new IteratorEnumeration<>(parameterNames.iterator());
}
示例5: parseFormData
import io.undertow.server.handlers.form.FormData; //导入依赖的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;
}
示例6: ban
import io.undertow.server.handlers.form.FormData; //导入依赖的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);
}
}
示例7: parseForm
import io.undertow.server.handlers.form.FormData; //导入依赖的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());
}
}
}
示例8: handleRequest
import io.undertow.server.handlers.form.FormData; //导入依赖的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);
}
示例9: extractMetadata
import io.undertow.server.handlers.form.FormData; //导入依赖的package包/类
/**
* Search the request for a field named 'metadata' (or 'properties') which
* must contain valid JSON
*
* @param formData
* @return the parsed BsonDocument from the form data or an empty
* BsonDocument
*/
protected static BsonDocument extractMetadata(
final FormData formData)
throws JSONParseException {
BsonDocument metadata = new BsonDocument();
final String metadataString;
metadataString = formData.getFirst(FILE_METADATA) != null
? formData.getFirst(FILE_METADATA).getValue()
: formData.getFirst(PROPERTIES) != null
? formData.getFirst(PROPERTIES).getValue()
: null;
if (metadataString != null) {
metadata = BsonDocument.parse(metadataString);
}
return metadata;
}
示例10: getFileFromForm
import io.undertow.server.handlers.form.FormData; //导入依赖的package包/类
/**
* Return the first file received in the request.
*
* @param formData The multipart request.
* @return A file if the request has one, null otherwise.
*/
private File getFileFromForm(FormData formData) {
for (String data: formData) {
for (FormData.FormValue formValue : formData.get(data)) {
if (formValue.isFile()) {
return formValue.getPath().toFile();
}
}
}
return null;
}
示例11: runFormAuth
import io.undertow.server.handlers.form.FormData; //导入依赖的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);
}
}
示例12: getParameterValues
import io.undertow.server.handlers.form.FormData; //导入依赖的package包/类
@Override
public String[] getParameterValues(final String name) {
if (queryParameters == null) {
queryParameters = exchange.getQueryParameters();
}
final List<String> ret = new ArrayList<>();
Deque<String> params = queryParameters.get(name);
if (params != null) {
for (String param : params) {
ret.add(param);
}
}
if (exchange.getRequestMethod().equals(Methods.POST)) {
final FormData parsedFormData = parseFormData();
if (parsedFormData != null) {
Deque<FormData.FormValue> res = parsedFormData.get(name);
if (res != null) {
for (FormData.FormValue value : res) {
if(!value.isFile()) {
ret.add(value.getValue());
}
}
}
}
}
if (ret.isEmpty()) {
return null;
}
return ret.toArray(new String[ret.size()]);
}
示例13: getBanReason
import io.undertow.server.handlers.form.FormData; //导入依赖的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;
}
示例14: getRollbackTime
import io.undertow.server.handlers.form.FormData; //导入依赖的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);
}
示例15: signUp
import io.undertow.server.handlers.form.FormData; //导入依赖的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));
}