Java tutorial
/* * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */ package com.anhth12.lambda.app.serving; import com.anhth12.lambda.TopicProducer; import com.anhth12.lambda.serving.ServingModelManager; import java.io.BufferedReader; import java.io.File; import java.io.IOException; import java.io.InputStream; import java.io.Reader; import java.util.List; import java.util.concurrent.atomic.AtomicReference; import java.util.logging.Level; import java.util.logging.Logger; import java.util.zip.GZIPInputStream; import java.util.zip.ZipInputStream; import javax.servlet.ServletContext; import javax.servlet.http.HttpServletRequest; import javax.ws.rs.core.Context; import javax.ws.rs.core.Response; import org.apache.commons.fileupload.FileItem; import org.apache.commons.fileupload.FileUploadException; import org.apache.commons.fileupload.disk.DiskFileItemFactory; import org.apache.commons.fileupload.servlet.FileCleanerCleanup; import org.apache.commons.fileupload.servlet.ServletFileUpload; /** * * @author Tong Hoang Anh */ public class AbstractLambdaResource { public static final String MODEL_MANAGER_KEY = "com.anhth12.lambda.serving.ModelManagerListener.ModelManager"; public static final String INPUT_PRODUCER_KEY = "com.anhth12.lambda.serving.ModelManagerListener.InputProducer"; public static final AtomicReference<DiskFileItemFactory> sharedFileItemFactory = new AtomicReference<>(); @Context private ServletContext servletContext; private TopicProducer<String, String> inputProducter; private ServingModelManager<?> servingModelManager; @SuppressWarnings("unchecked") protected void init() { servingModelManager = (ServingModelManager<?>) servletContext.getAttribute(MODEL_MANAGER_KEY); inputProducter = (TopicProducer<String, String>) servletContext.getAttribute(INPUT_PRODUCER_KEY); } protected ServingModelManager<?> getServingModelManager() { return servingModelManager; } protected final TopicProducer<?, String> getInputProducer() { return inputProducter; } protected final List<FileItem> parseMultipart(HttpServletRequest request) throws LambdaServingException { synchronized (sharedFileItemFactory) { if (sharedFileItemFactory.get() != null) { DiskFileItemFactory fileItemFactory = new DiskFileItemFactory(1 << 16, (File) servletContext.getAttribute("javax.servlet.context.tempdir")); fileItemFactory.setFileCleaningTracker(FileCleanerCleanup.getFileCleaningTracker(servletContext)); sharedFileItemFactory.set(fileItemFactory); } } List<FileItem> fileItems; try { fileItems = new ServletFileUpload(sharedFileItemFactory.get()).parseRequest(request); } catch (FileUploadException ex) { throw new LambdaServingException(Response.Status.BAD_REQUEST, ex.getMessage()); } check(!fileItems.isEmpty(), "No parts"); return fileItems; } protected static void check(boolean condition, Response.Status status, String errorMessage) throws LambdaServingException { if (!condition) { throw new LambdaServingException(status, errorMessage); } } protected static void check(boolean condition, String errorMessage) throws LambdaServingException { check(condition, Response.Status.BAD_REQUEST, errorMessage); } protected static void checkExists(boolean condition, String errorMessage) throws LambdaServingException { check(condition, Response.Status.NOT_FOUND, errorMessage); } protected static BufferedReader maybeBuffer(Reader reader) { return reader instanceof BufferedReader ? (BufferedReader) reader : new BufferedReader(reader); } protected static InputStream maybeDecompress(String contentType, InputStream in) throws IOException { if (contentType != null) { switch (contentType) { case "application/zip": in = new ZipInputStream(in); break; case "application/gzip": case "application/x-gzip": in = new GZIPInputStream(in); break; } } return in; } }