All Downloads are FREE. Search and download functionalities are using the official Maven repository.

play.data.parsing.TextParser Maven / Gradle / Ivy

There is a newer version: 2.6.3
Show newest version
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());
        }
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy