*/
public final class IngestServlet extends AbstractMyrrixServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException, ServletException {
MyrrixRecommender recommender = getRecommender();
boolean fromBrowserUpload = request.getContentType().startsWith("multipart/form-data");
Reader reader;
if (fromBrowserUpload) {
Collection<Part> parts = request.getParts();
if (parts == null || parts.isEmpty()) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "No form data");
return;
}
Part part = parts.iterator().next();
String partContentType = part.getContentType();
InputStream in = part.getInputStream();
if ("application/zip".equals(partContentType)) {
in = new ZipInputStream(in);
} else if ("application/gzip".equals(partContentType)) {
in = new GZIPInputStream(in);
} else if ("application/x-gzip".equals(partContentType)) {
in = new GZIPInputStream(in);
} else if ("application/bzip2".equals(partContentType)) {
in = new BZip2CompressorInputStream(in);
} else if ("application/x-bzip2".equals(partContentType)) {
in = new BZip2CompressorInputStream(in);
}
reader = new InputStreamReader(in, Charsets.UTF_8);
} else {
String charEncodingName = request.getCharacterEncoding();
Charset charEncoding = charEncodingName == null ? Charsets.UTF_8 : Charset.forName(charEncodingName);
String contentEncoding = request.getHeader(HttpHeaders.CONTENT_ENCODING);
if (contentEncoding == null) {
reader = request.getReader();
} else if ("gzip".equals(contentEncoding)) {
reader = new InputStreamReader(new GZIPInputStream(request.getInputStream()), charEncoding);
} else if ("zip".equals(contentEncoding)) {
reader = new InputStreamReader(new ZipInputStream(request.getInputStream()), charEncoding);
} else if ("bzip2".equals(contentEncoding)) {
reader = new InputStreamReader(new BZip2CompressorInputStream(request.getInputStream()), charEncoding);
} else {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, "Unsupported Content-Encoding");
return;
}
}
try {
recommender.ingest(reader);
} catch (IllegalArgumentException iae) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, iae.toString());
return;
} catch (NoSuchElementException nsee) {
response.sendError(HttpServletResponse.SC_BAD_REQUEST, nsee.toString());