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

org.teamapps.ux.servlet.UploadServlet Maven / Gradle / Ivy

There is a newer version: 0.9.194
Show newest version
/*-
 * ========================LICENSE_START=================================
 * TeamApps
 * ---
 * Copyright (C) 2014 - 2024 TeamApps.org
 * ---
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 * 
 *      http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 * =========================LICENSE_END==================================
 */
package org.teamapps.ux.servlet;

import jakarta.servlet.ServletException;
import jakarta.servlet.http.HttpServlet;
import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.servlet.http.Part;
import org.apache.commons.io.IOUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.*;
import java.util.ArrayList;
import java.util.Collection;
import java.util.UUID;
import java.util.function.BiConsumer;
import java.util.stream.Collectors;

public class UploadServlet extends HttpServlet {

	private static final Logger LOGGER  = LoggerFactory.getLogger(UploadServlet.class);

	private final File uploadDirectory;

	private final BiConsumer uploadListener;

	public UploadServlet(File uploadDirectory, BiConsumer uploadListener) {
		this.uploadDirectory = uploadDirectory;
		this.uploadListener = uploadListener;
	}

	@Override
	public void init() throws ServletException {
		super.init();
	}

	@Override
	protected void doOptions(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
	}

	@Override
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		Collection parts = request.getParts();
		ArrayList uuids = new ArrayList<>();

		try {
			for (Part part : parts) {
				String uuidString = UUID.randomUUID().toString();
				File tempFile = new File(uploadDirectory, uuidString);

				try (InputStream in = part.getInputStream();
				     OutputStream out = new FileOutputStream(tempFile)) {
					IOUtils.copy(in, out);
				}

				uuids.add(uuidString);

				uploadListener.accept(tempFile, uuidString);
			}
		} catch (Exception e) {
			LOGGER.warn("Error while uploading files" + e);
			response.setStatus(500);
			return;
		}

		response.setStatus(200);
		response.setHeader("Access-Control-Allow-Origin", request.getHeader("Origin"));
		response.setContentType("application/json");
		response.getWriter().println("[" + uuids.stream()
				.map(uuid -> "\"" + uuid + "\"")
				.collect(Collectors.joining(",")) + "]");
	}
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy