net.sf.aguacate.http.impl.HttpBodyParserImpl Maven / Gradle / Ivy
package net.sf.aguacate.http.impl;
import java.io.IOException;
import java.io.Reader;
import java.util.Map;
import javax.servlet.http.HttpServletRequest;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import net.sf.aguacate.http.HttpBodyParser;
import net.sf.aguacate.util.codec.bridge.CodecCoupling;
public class HttpBodyParserImpl implements HttpBodyParser {
private static final Logger LOGGER = LogManager.getLogger(HttpBodyParserImpl.class);
private static final String MIME_TYPE = "application/json";
@Override
public Map parse(HttpServletRequest request) throws IOException {
String contentType = request.getContentType().toLowerCase();
if (contentType.startsWith(MIME_TYPE)) {
Reader reader = request.getReader();
try {
return CodecCoupling.jsonCodecBridge().decodeMap(reader);
} finally {
try {
reader.close();
} catch (IOException e) {
LOGGER.error("On closing reader", e);
}
}
} else {
throw new IllegalArgumentException(contentType);
}
}
}