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.IOException;
22  import java.net.MalformedURLException;
23  import java.net.URISyntaxException;
24  import java.net.URL;
25  import java.nio.file.Path;
26  import java.util.Optional;
27  
28  import javax.xml.transform.TransformerException;
29  
30  import org.apache.logging.log4j.LogManager;
31  import org.apache.logging.log4j.Logger;
32  import org.jdom2.JDOMException;
33  import org.mycore.common.MCRDeveloperTools;
34  import org.mycore.common.MCRException;
35  import org.mycore.common.content.MCRContent;
36  import org.mycore.common.content.MCRURLContent;
37  import org.mycore.frontend.MCRLayoutUtilities;
38  import org.xml.sax.SAXException;
39  
40  import jakarta.servlet.http.HttpServletRequest;
41  import jakarta.servlet.http.HttpServletResponse;
42  
43  /**
44   * This servlet displays static *.xml files stored in the web application by sending them to MCRLayoutService.
45   * 
46   * @author Frank Lützenkirchen
47   * @version $Revision$ $Date$
48   */
49  public class MCRStaticXMLFileServlet extends MCRServlet {
50  
51      private static final String READ_WEBPAGE_PERMISSION = MCRLayoutUtilities.getPermission2ReadWebpage();
52  
53      private static final long serialVersionUID = -9213353868244605750L;
54  
55      protected static final Logger LOGGER = LogManager.getLogger(MCRStaticXMLFileServlet.class);
56  
57      @Override
58      public void doGetPost(MCRServletJob job) throws java.io.IOException, MCRException, SAXException, JDOMException,
59          URISyntaxException, TransformerException {
60          String webpageID = getWebpageId(job.getRequest());
61          boolean hasAccess = MCRLayoutUtilities.webpageAccess(READ_WEBPAGE_PERMISSION, webpageID, true);
62          if (!hasAccess) {
63              job.getResponse().sendError(HttpServletResponse.SC_FORBIDDEN);
64              return;
65          }
66          URL resource = resolveResource(job);
67          if (resource != null) {
68              HttpServletRequest request = job.getRequest();
69              HttpServletResponse response = job.getResponse();
70              setXSLParameters(resource, request);
71              MCRContent content = getResourceContent(request, response, resource);
72              getLayoutService().doLayout(request, response, content);
73          }
74      }
75  
76      private String getWebpageId(HttpServletRequest request) {
77          String servletPath = request.getServletPath();
78          String queryString = request.getQueryString();
79          StringBuilder builder = new StringBuilder(servletPath);
80          if (queryString != null && !queryString.isEmpty()) {
81              builder.append('?').append(queryString);
82          }
83          return builder.toString();
84      }
85  
86      private void setXSLParameters(URL resource, HttpServletRequest request)
87          throws MalformedURLException, URISyntaxException {
88          String path = resource.getProtocol().equals("file") ? resource.getPath() : resource.toExternalForm();
89          int lastPathElement = path.lastIndexOf('/') + 1;
90          String fileName = path.substring(lastPathElement);
91          String parent = path.substring(0, lastPathElement);
92          request.setAttribute("XSL.StaticFilePath", request.getServletPath().substring(1));
93          request.setAttribute("XSL.DocumentBaseURL", parent);
94          request.setAttribute("XSL.FileName", fileName);
95          request.setAttribute("XSL.FilePath", path);
96      }
97  
98      private URL resolveResource(MCRServletJob job) throws IOException {
99          String requestedPath = job.getRequest().getServletPath();
100         LOGGER.info("MCRStaticXMLFileServlet {}", requestedPath);
101 
102         if (MCRDeveloperTools.overrideActive()) {
103             final Optional<Path> overriddenFilePath = MCRDeveloperTools
104                 .getOverriddenFilePath(requestedPath, true);
105             if (overriddenFilePath.isPresent()) {
106                 return overriddenFilePath.get().toUri().toURL();
107             }
108         }
109 
110         URL resource = getServletContext().getResource(requestedPath);
111         if (resource != null) {
112             if (LOGGER.isDebugEnabled()) {
113                 LOGGER.debug("Resolved to {}", resource);
114             }
115             return resource;
116         }
117 
118         String msg = "Could not find file " + requestedPath;
119         job.getResponse().sendError(HttpServletResponse.SC_NOT_FOUND, msg);
120         return null;
121     }
122 
123     protected MCRContent getResourceContent(HttpServletRequest request, HttpServletResponse response, URL resource)
124         throws IOException, JDOMException, SAXException {
125         return new MCRURLContent(resource);
126     }
127 }