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.frontend.servlets;
20  
21  import java.io.FileNotFoundException;
22  import java.io.IOException;
23  import java.nio.file.NoSuchFileException;
24  import java.util.Locale;
25  import java.util.Objects;
26  
27  import org.apache.logging.log4j.LogManager;
28  import org.apache.logging.log4j.Logger;
29  import org.apache.logging.log4j.util.Supplier;
30  import org.mycore.common.content.MCRContent;
31  import org.mycore.common.content.util.MCRServletContentHelper;
32  
33  import jakarta.servlet.ServletException;
34  import jakarta.servlet.http.HttpServletRequest;
35  import jakarta.servlet.http.HttpServletResponse;
36  
37  /**
38   * @author Thomas Scheffler (yagee)
39   *
40   */
41  public abstract class MCRContentServlet extends MCRServlet {
42      private static final long serialVersionUID = 1L;
43  
44      private static Logger LOGGER = LogManager.getLogger(MCRContentServlet.class);
45  
46      private MCRServletContentHelper.Config config;
47  
48      /**
49       * Returns MCRContent matching current request.
50       */
51      public abstract MCRContent getContent(HttpServletRequest req, HttpServletResponse resp) throws IOException;
52  
53      @Override
54      public void init() throws ServletException {
55          super.init();
56          this.config = MCRServletContentHelper.buildConfig(getServletConfig());
57      }
58  
59      /**
60       * Handles a HEAD request for the specified content.
61       */
62      @Override
63      protected void doHead(final HttpServletRequest request, final HttpServletResponse response) throws IOException,
64          ServletException {
65  
66          request.setAttribute(MCRServletContentHelper.ATT_SERVE_CONTENT, Boolean.FALSE);
67          super.doGet(request, response);
68      }
69  
70      @Override
71      protected void doOptions(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException,
72          IOException {
73          resp.setHeader("Allow", "GET, HEAD, POST, OPTIONS");
74      }
75  
76      @Override
77      protected void render(final MCRServletJob job, final Exception ex) throws Exception {
78          if (ex != null) {
79              throw ex;
80          }
81          final HttpServletRequest request = job.getRequest();
82          final HttpServletResponse response = job.getResponse();
83          final MCRContent content = getContent(request, response);
84          boolean serveContent = MCRServletContentHelper.isServeContent(request);
85          try {
86              MCRServletContentHelper.serveContent(content, request, response, getServletContext(), getConfig(),
87                  serveContent);
88          } catch (NoSuchFileException | FileNotFoundException e) {
89              LOGGER.info("Catched {}:", e.getClass().getSimpleName(), e);
90              response.sendError(HttpServletResponse.SC_NOT_FOUND, e.getMessage());
91              return;
92          }
93  
94          final Supplier<Object> getResourceName = () -> {
95              String id = "";
96              if (Objects.nonNull(content.getSystemId())) {
97                  id = content.getSystemId();
98              } else if (Objects.nonNull(content.getName())) {
99                  id = content.getName();
100             }
101             return String.format(Locale.ROOT, "Finished serving resource:%s", id);
102         };
103         LOGGER.debug(getResourceName);
104     }
105 
106     public MCRServletContentHelper.Config getConfig() {
107         return config;
108     }
109 
110 }