View Javadoc
1   /*
2    * This file is part of ***  M y C o R e  ***
3    * See http://www.mycore.de/ for details.
4    *
5    * MyCoRe is free software: you can redistribute it and/or modify
6    * it under the terms of the GNU General Public License as published by
7    * the Free Software Foundation, either version 3 of the License, or
8    * (at your option) any later version.
9    *
10   * MyCoRe is distributed in the hope that it will be useful,
11   * but WITHOUT ANY WARRANTY; without even the implied warranty of
12   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13   * GNU General Public License for more details.
14   *
15   * You should have received a copy of the GNU General Public License
16   * along with MyCoRe.  If not, see <http://www.gnu.org/licenses/>.
17   */
18  
19  package org.mycore.services.packaging;
20  
21  import java.io.IOException;
22  import java.util.Map;
23  import java.util.stream.Collectors;
24  
25  import org.apache.logging.log4j.LogManager;
26  import org.apache.logging.log4j.Logger;
27  import org.mycore.access.MCRAccessException;
28  import org.mycore.common.MCRUsageException;
29  import org.mycore.frontend.servlets.MCRServlet;
30  import org.mycore.frontend.servlets.MCRServletJob;
31  import org.mycore.services.queuedjob.MCRJob;
32  
33  import jakarta.servlet.http.HttpServletResponse;
34  
35  /**
36   * <p>Servlet for {@link MCRPackerManager}.</p>
37   * <p>
38   * <p>You can pass a <code>redirect</code> parameter to the servlet!</p>
39   * <p>The rights you need to start a Packer depends on the implementation!</p>
40   * @author Sebastian Hofmann (mcrshofm)
41   */
42  public class MCRPackerServlet extends MCRServlet {
43  
44      private static final long serialVersionUID = 1L;
45  
46      private static final Logger LOGGER = LogManager.getLogger();
47  
48      @Override
49      protected void doGetPost(MCRServletJob job) throws IOException {
50          String packer = job.getRequest().getParameter("packer");
51          if (packer == null || packer.isEmpty()) {
52              try {
53                  job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "No or invalid 'packer' parameter!");
54              } catch (IOException e) {
55                  LOGGER.error("Error while sending request error to client!", e);
56                  return;
57              }
58          }
59  
60          Map<String, String> jobParameters = resolveJobParameters(job);
61  
62          try {
63              MCRJob mcrJob = MCRPackerManager.startPacking(jobParameters);
64              if (mcrJob == null) {
65                  job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "No packer parameter!");
66              }
67          } catch (MCRAccessException e) {
68              job.getResponse().sendError(HttpServletResponse.SC_UNAUTHORIZED, e.getMessage());
69          } catch (MCRUsageException e) {
70              job.getResponse().sendError(HttpServletResponse.SC_BAD_REQUEST, "Invalid Parameters: " + e.getMessage());
71          }
72  
73          if (jobParameters.containsKey("redirect")) {
74              String redirect = jobParameters.get("redirect");
75              job.getResponse().sendRedirect(job.getResponse().encodeRedirectURL(redirect));
76          }
77      }
78  
79      private Map<String, String> resolveJobParameters(MCRServletJob job) {
80          return job.getRequest().getParameterMap()
81              .entrySet()
82              .stream()
83              .collect(Collectors.toMap(Map.Entry::getKey, e -> (e.getValue().length >= 1) ? e.getValue()[0] : ""));
84      }
85  }