play.data.parsing.TextParser Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of framework Show documentation
Show all versions of framework Show documentation
RePlay is a fork of the Play1 framework, created by Codeborne.
package play.data.parsing;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import play.exceptions.UnexpectedException;
import play.mvc.Http;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import static org.apache.commons.io.IOUtils.toByteArray;
public class TextParser extends DataParser {
private static final Logger log = LoggerFactory.getLogger(TextParser.class);
@Override
public Map parse(Http.Request request) {
Map params = new HashMap<>();
try {
byte[] data = toByteArray(request.body);
params.put("body", new String[] {new String(data, request.encoding)});
} catch (IOException e) {
throw new UnexpectedException(e);
}
resetBodyInputStreamIfPossible(request);
return params;
}
private void resetBodyInputStreamIfPossible(Http.Request request) {
try {
request.body.reset();
}
catch (IOException resetNotSupported) {
log.warn("Failed to reset request.body of type {}: {}",
resetNotSupported.getClass().getName(), resetNotSupported.toString());
}
}
}