/*
* Copyright 2014-2016 CyberVision, Inc.
*
* 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.
*/
package org.kaaproject.kaa.server.admin.servlet;
import org.apache.commons.fileupload.FileItemIterator;
import org.apache.commons.fileupload.FileItemStream;
import org.apache.commons.fileupload.servlet.ServletFileUpload;
import org.apache.commons.fileupload.util.Streams;
import org.kaaproject.kaa.server.admin.services.cache.CacheService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.context.support.SpringBeanAutowiringSupport;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
public class FileUpload extends HttpServlet {
private static final long serialVersionUID = 2959115024959843564L;
private static final Logger LOG = LoggerFactory.getLogger(FileUpload.class);
@Autowired
private CacheService cacheService;
@Override
public void init(ServletConfig config) throws ServletException {
super.init(config);
SpringBeanAutowiringSupport.processInjectionBasedOnServletContext(this,
config.getServletContext());
}
//CHECKSTYLE:OFF
public void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException {
//CHECKSTYLE:ON
ServletFileUpload upload = new ServletFileUpload();
try {
FileItemIterator iter = upload.getItemIterator(request);
if (iter.hasNext()) {
FileItemStream item = iter.next();
String name = item.getFieldName();
LOG.debug("Uploading file '{}' with item name '{}'", item.getName(), name);
InputStream stream = item.openStream();
// Process the input stream
ByteArrayOutputStream out = new ByteArrayOutputStream();
Streams.copy(stream, out, true);
byte[] data = out.toByteArray();
cacheService.uploadedFile(name, data);
} else {
LOG.error("No file found in post request!");
throw new RuntimeException("No file found in post request!");
}
} catch (Exception ex) {
LOG.error("Unexpected error in FileUpload.doPost: ", ex);
throw new RuntimeException(ex);
}
}
}