/*
 * Copyright (C) 2000 - 2020 Silverpeas
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 * License, or (at your option) any later version.
 *
 * As a special exception to the terms and conditions of version 3.0 of
 * the GPL, you may redistribute this Program in connection with Free/Libre
 * Open Source Software ("FLOSS") applications as described in Silverpeas's
 * FLOSS exception.  You should have received a copy of the text describing
 * the FLOSS exception, and it is also available here:
 * "https://www.silverpeas.org/legal/floss_exception.html"
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
package org.silverpeas.web.templatedesigner.servlets;

import org.apache.commons.io.FileUtils;
import org.silverpeas.core.SilverpeasRuntimeException;
import org.silverpeas.core.admin.user.model.User;
import org.silverpeas.core.contribution.template.publication.PublicationTemplateManager;
import org.silverpeas.core.util.ResourceLocator;
import org.silverpeas.core.util.URLUtil;
import org.silverpeas.core.util.file.FileUtil;
import org.silverpeas.core.web.mvc.webcomponent.SilverpeasAuthenticatedHttpServlet;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.IOException;

public class FormLayerServlet extends SilverpeasAuthenticatedHttpServlet {

  private static final long serialVersionUID = 1013565640540446129L;

  @Override
  public void doPost(HttpServletRequest request, HttpServletResponse response) throws IOException {
    final String pathInfo = request.getPathInfo();
    final String form = pathInfo.substring(1);
    if (User.getCurrentRequester().isAccessAdmin()) {
      final String filename = checkPath(request.getParameter("Layer"));
      String dir = PublicationTemplateManager.getInstance().makePath(form);
      File file = new File(dir, filename);
      response.setContentType(FileUtil.getMimeType(filename));
      response.setHeader("Content-Disposition", "inline; filename=\"" + filename + '"');
      response.setHeader("Content-Length", String.valueOf(file.length()));
      FileUtils.copyFile(file, response.getOutputStream());
      response.getOutputStream().flush();
    } else {
      response.sendRedirect(URLUtil.getApplicationURL() +
          ResourceLocator.getGeneralSettingBundle().getString("sessionTimeout"));
    }
  }

  @Override
  public void doGet(HttpServletRequest request, HttpServletResponse response)
      throws ServletException, IOException {
    doPost(request, response);
  }

  private String checkPath(String path) {
    if (path.contains("..")) {
      throw new SilverpeasRuntimeException("'..' is not allowed into path definition");
    }
    return path;
  }
}

@@ -1549,24 +1549,78 @@
 e.FileUtil;%0A
+import org.silverpeas.core.util.logging.SilverLogger;%0A
 import org.s
@@ -1696,47 +1696,8 @@
 t;%0A%0A
-import javax.servlet.ServletException;%0A
 impo
@@ -2076,33 +2076,26 @@
 se) 
-throws IOException
+%7B%0A    try
  %7B%0A    
+ 
+ 
 fina
@@ -2137,24 +2137,26 @@
 Info();%0A    
+  
 final String
@@ -2186,24 +2186,26 @@
 ing(1);%0A    
+ 
+ 
 if (User.get
@@ -2238,24 +2238,26 @@
 sAdmin()) %7B%0A
+  
       final 
@@ -2322,16 +2322,18 @@
 ;%0A      
+ 
+ 
 String d
@@ -2396,24 +2396,26 @@
 orm);%0A      
+  
 File file = 
@@ -2437,32 +2437,34 @@
 ilename);%0A      
+ 
+ 
 response.setCont
@@ -2500,24 +2500,26 @@
 filename));%0A
+  
       respon
@@ -2593,32 +2593,34 @@
 e + '%22');%0A      
+ 
+ 
 response.setHead
@@ -2674,24 +2674,26 @@
 ()));%0A      
+  
 FileUtils.co
@@ -2736,24 +2736,26 @@
 m());%0A      
+ 
+ 
 response.get
@@ -2782,16 +2782,18 @@
 ();%0A    
+  
 %7D else %7B
@@ -2795,24 +2795,26 @@
 lse %7B%0A      
+ 
+ 
 response.sen
@@ -2853,16 +2853,18 @@
 URL() +%0A
+  
         
@@ -2933,24 +2933,179 @@
 Timeout%22));%0A
+      %7D%0A    %7D catch (IOException e) %7B%0A      SilverLogger.getLogger(this).error(e);%0A      response.setStatus(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);%0A
     %7D%0A  %7D%0A%0A 
@@ -3196,51 +3196,8 @@
 nse)
-%0A      throws ServletException, IOException
  %7B%0A 

Fix quality issues in FormLayerServlet