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 static org.mycore.access.MCRAccessManager.PERMISSION_READ;
22  
23  import java.io.IOException;
24  
25  import javax.xml.transform.TransformerException;
26  
27  import org.mycore.access.MCRAccessManager;
28  import org.mycore.common.MCRException;
29  import org.mycore.common.MCRSession;
30  import org.mycore.common.MCRSessionMgr;
31  import org.mycore.common.content.MCRContent;
32  import org.mycore.datamodel.common.MCRXMLMetadataManager;
33  import org.mycore.datamodel.metadata.MCRMetadataManager;
34  import org.mycore.datamodel.metadata.MCRObjectID;
35  import org.xml.sax.SAXException;
36  
37  import jakarta.servlet.ServletException;
38  import jakarta.servlet.http.HttpServletRequest;
39  import jakarta.servlet.http.HttpServletResponse;
40  
41  /**
42   * Serves a given MCROBject.
43   * <em>.../receive/{MCRObjectID}</em>
44   * 
45   * @author Thomas Scheffler (yagee)
46   */
47  public class MCRObjectServlet extends MCRContentServlet {
48      private static final long serialVersionUID = 1L;
49  
50      private static final int REV_CURRENT = 0;
51  
52      private static final String I18N_ERROR_PREFIX = "component.base.error";
53  
54      private MCRXMLMetadataManager metadataManager = null;
55  
56      @Override
57      public void init() throws ServletException {
58          super.init();
59          metadataManager = MCRXMLMetadataManager.instance();
60      }
61  
62      @Override
63      public MCRContent getContent(final HttpServletRequest req, final HttpServletResponse resp) throws IOException {
64          final MCRObjectID mcrid = getMCRObjectID(req, resp);
65          if (mcrid == null) {
66              return null;
67          }
68          if (!MCRAccessManager.checkPermission(mcrid, PERMISSION_READ)) { // check read permission for ID
69              final MCRSession currentSession = MCRSessionMgr.getCurrentSession();
70              resp.sendError(
71                  HttpServletResponse.SC_UNAUTHORIZED,
72                  getErrorI18N(I18N_ERROR_PREFIX, "accessDenied", mcrid.toString(), currentSession.getUserInformation()
73                      .getUserID(), currentSession.getCurrentIP()));
74              return null;
75          }
76          String rev = null;
77          final String revision = getProperty(req, "r");
78          if (revision != null) {
79              rev = revision;
80          }
81          MCRContent localObject = (rev == null) ? requestLocalObject(mcrid, resp) : requestVersionedObject(mcrid,
82              resp, rev);
83          if (localObject == null) {
84              return null;
85          }
86          try {
87              return getLayoutService().getTransformedContent(req, resp, localObject);
88          } catch (TransformerException | SAXException e) {
89              throw new IOException(e);
90          }
91      }
92  
93      private MCRContent requestLocalObject(MCRObjectID mcrid, final HttpServletResponse resp) throws IOException {
94          if (MCRMetadataManager.exists(mcrid)) {
95              return metadataManager.retrieveContent(mcrid);
96          }
97          resp.sendError(HttpServletResponse.SC_NOT_FOUND, getErrorI18N(I18N_ERROR_PREFIX, "notFound", mcrid));
98          return null;
99      }
100 
101     private MCRContent requestVersionedObject(final MCRObjectID mcrid, final HttpServletResponse resp, final String rev)
102         throws IOException {
103         MCRXMLMetadataManager xmlMetadataManager = MCRXMLMetadataManager.instance();
104         if (xmlMetadataManager.listRevisions(mcrid) != null) {
105             MCRContent content = xmlMetadataManager.retrieveContent(mcrid, rev);
106             if (content != null) {
107                 return content;
108             }
109             resp.sendError(HttpServletResponse.SC_NOT_FOUND,
110                 getErrorI18N(I18N_ERROR_PREFIX, "revisionNotFound", rev, mcrid));
111             return null;
112         }
113         resp.sendError(HttpServletResponse.SC_BAD_REQUEST, getErrorI18N(I18N_ERROR_PREFIX, "noVersions", mcrid));
114         return null;
115     }
116 
117     private MCRObjectID getMCRObjectID(final HttpServletRequest req, final HttpServletResponse resp)
118         throws IOException {
119         final String pathInfo = req.getPathInfo();
120         final String id = pathInfo == null ? null : pathInfo.substring(1);
121 
122         MCRObjectID mcrid = null;
123         if (id != null) {
124             try {
125                 mcrid = MCRObjectID.getInstance(id); // create Object with given ID, only ID syntax check performed
126             } catch (final MCRException e) {
127                 // handle exception: invalid ID syntax, set HTTP error 400 "Invalid request"
128                 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, getErrorI18N(I18N_ERROR_PREFIX, "invalidID", id));
129                 return null; // sorry, no object to return
130             }
131         }
132         return mcrid;
133     }
134 }