/*******************************************************************************
 * Copyright (c) 2011, 2016 Eurotech and/or its affiliates and others
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     Eurotech - initial API and implementation
 *
 *******************************************************************************/
package org.eclipse.kapua.app.console.servlet;

import java.io.File;

import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;

import org.apache.commons.fileupload.FileUploadException;
import org.apache.commons.fileupload.disk.DiskFileItemFactory;
import org.apache.commons.fileupload.servlet.FileCleanerCleanup;
import org.apache.commons.io.FileCleaningTracker;
import org.eclipse.kapua.app.console.ConsoleJAXBContextProvider;
import org.eclipse.kapua.app.console.setting.ConsoleSetting;
import org.eclipse.kapua.app.console.setting.ConsoleSettingKeys;
import org.eclipse.kapua.app.console.shared.model.KapuaFormFields;
import org.eclipse.kapua.commons.util.xml.JAXBContextProvider;
import org.eclipse.kapua.commons.util.xml.XmlUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class KapuaHttpServlet extends HttpServlet
{
    private static final long     serialVersionUID = 8120495078076069807L;
    private static Logger         s_logger         = LoggerFactory.getLogger(KapuaHttpServlet.class);

    protected DiskFileItemFactory m_diskFileItemFactory;
    protected FileCleaningTracker m_fileCleaningTracker;

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

        s_logger.info("Servlet {} initialized", getServletName());

        ServletContext ctx = getServletContext();
        m_fileCleaningTracker = FileCleanerCleanup.getFileCleaningTracker(ctx);

        int sizeThreshold = ConsoleSetting.getInstance().getInt(ConsoleSettingKeys.FILE_UPLOAD_INMEMORY_SIZE_THRESHOLD);
        File repository = new File(System.getProperty("java.io.tmpdir"));

        s_logger.info("DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD: {}", DiskFileItemFactory.DEFAULT_SIZE_THRESHOLD);
        s_logger.info("DiskFileItemFactory: using size threshold of: {}", sizeThreshold);

        m_diskFileItemFactory = new DiskFileItemFactory(sizeThreshold, repository);
        m_diskFileItemFactory.setFileCleaningTracker(m_fileCleaningTracker);

        JAXBContextProvider consoleProvider = new ConsoleJAXBContextProvider();
        XmlUtil.setContextProvider(consoleProvider);
    }

    @Override
    public void destroy()
    {
        super.destroy();
        s_logger.info("Servlet {} destroyed", getServletName());

        if (m_fileCleaningTracker != null) {
            s_logger.info("Number of temporary files tracked: " + m_fileCleaningTracker.getTrackCount());
        }
    }

    public KapuaFormFields getFormFields(HttpServletRequest req)
        throws ServletException
    {
        UploadRequest upload = new UploadRequest(m_diskFileItemFactory);

        try {
            upload.parse(req);
        }
        catch (FileUploadException e) {
            s_logger.error("Error parsing the provision request", e);
            throw new ServletException("Error parsing the provision request", e);
        }

        return new KapuaFormFields(upload.getFormFields(), upload.getFileItems());
    }
}

@@ -1498,10 +1498,11 @@
 vlet
+ %7B
 %0A
-%7B
 %0A   
@@ -1528,20 +1528,16 @@
 al long 
-    
 serialVe
@@ -1592,39 +1592,27 @@
 tic 
+final 
 Logger 
-        s_logger        
+logger
  = L
@@ -1694,18 +1694,16 @@
 Factory 
-m_
 diskFile
@@ -1749,18 +1749,16 @@
 Tracker 
-m_
 fileClea
@@ -1807,24 +1807,16 @@
 d init()
-%0A       
  throws 
@@ -1823,36 +1823,32 @@
 ServletException
-%0A   
  %7B%0A        super
@@ -1857,34 +1857,32 @@
 nit();%0A%0A        
-s_
 logger.info(%22Ser
@@ -1981,18 +1981,16 @@
         
-m_
 fileClea
@@ -2248,34 +2248,32 @@
 ir%22));%0A%0A        
-s_
 logger.info(%22Dis
@@ -2363,34 +2363,32 @@
 SHOLD);%0A        
-s_
 logger.info(%22Dis
@@ -2452,34 +2452,32 @@
 hold);%0A%0A        
-m_
 diskFileItemFact
@@ -2542,18 +2542,16 @@
         
-m_
 diskFile
@@ -2585,18 +2585,16 @@
 Tracker(
-m_
 fileClea
@@ -2787,20 +2787,16 @@
 estroy()
-%0A   
  %7B%0A     
@@ -2815,34 +2815,32 @@
 troy();%0A        
-s_
 logger.info(%22Ser
@@ -2891,18 +2891,16 @@
     if (
-m_
 fileClea
@@ -2930,26 +2930,24 @@
             
-s_
 logger.info(
@@ -2986,18 +2986,16 @@
 ed: %22 + 
-m_
 fileClea
@@ -3110,24 +3110,28 @@
 eq)%0A        
+   
+ 
 throws Servl
@@ -3141,20 +3141,16 @@
 xception
-%0A   
  %7B%0A     
@@ -3197,10 +3197,8 @@
 est(
-m_
 disk
@@ -3270,24 +3270,16 @@
        %7D
-%0A       
  catch (
@@ -3319,10 +3319,8 @@
     
-s_
 logg

Apply code formatter and cleanup profile, clean up