1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
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
43
44
45
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)) {
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);
126 } catch (final MCRException e) {
127
128 resp.sendError(HttpServletResponse.SC_BAD_REQUEST, getErrorI18N(I18N_ERROR_PREFIX, "invalidID", id));
129 return null;
130 }
131 }
132 return mcrid;
133 }
134 }