blob: 1d357e45b39e369f933e642bb12de3e17a56bd4f [file] [log] [blame]
Michael Hanle25dea22015-09-24 19:37:56 +02001package de.ids_mannheim.korap.security.ac;
2
Michael Hanlc0ed00f2016-06-23 14:33:10 +02003import de.ids_mannheim.korap.config.KustvaktCacheable;
Michael Hanle25dea22015-09-24 19:37:56 +02004import de.ids_mannheim.korap.exceptions.EmptyResultException;
5import de.ids_mannheim.korap.exceptions.KustvaktException;
6import de.ids_mannheim.korap.exceptions.NotAuthorizedException;
7import de.ids_mannheim.korap.exceptions.StatusCodes;
8import de.ids_mannheim.korap.resources.KustvaktResource;
9import de.ids_mannheim.korap.resources.Permissions;
10import de.ids_mannheim.korap.resources.ResourceFactory;
11import de.ids_mannheim.korap.user.User;
Michael Hanle25dea22015-09-24 19:37:56 +020012import net.sf.ehcache.CacheManager;
13import net.sf.ehcache.Element;
14import org.slf4j.Logger;
Michael Hanlac113e52016-01-19 15:49:20 +010015import org.slf4j.LoggerFactory;
Michael Hanle25dea22015-09-24 19:37:56 +020016
17import java.util.Collection;
18
19/**
20 * @author hanl
21 * @date 23/03/2014
22 */
23
Michael Hanldaf86602016-05-12 14:31:52 +020024//todo: use interface (maybe a cachable interface?) and bean instanceing
25// todo: if cachable, data integrity needs to be checked! either remove caching or check integrity!
Michael Hanle25dea22015-09-24 19:37:56 +020026@SuppressWarnings("all")
Michael Hanlc0ed00f2016-06-23 14:33:10 +020027public class ResourceHandler extends KustvaktCacheable {
Michael Hanle25dea22015-09-24 19:37:56 +020028
Michael Hanlac113e52016-01-19 15:49:20 +010029 private static Logger jlog = LoggerFactory.getLogger(ResourceHandler.class);
Michael Hanle25dea22015-09-24 19:37:56 +020030
Michael Hanle25dea22015-09-24 19:37:56 +020031
Michael Hanlc0ed00f2016-06-23 14:33:10 +020032 public ResourceHandler () {
33 super("resources", "key:resources");
34 }
Michael Hanl8abaf9e2016-05-23 16:46:35 +020035
36
Michael Hanlc0ed00f2016-06-23 14:33:10 +020037 @Deprecated
38 public <T extends KustvaktResource> T getCache (Object id, Class<T> cz) {
Michael Hanl8abaf9e2016-05-23 16:46:35 +020039 Element e = CacheManager.getInstance().getCache("resources").get(id);
Michael Hanle25dea22015-09-24 19:37:56 +020040 if (e != null)
41 return (T) e.getObjectValue();
42 else
43 return null;
44 }
45
Michael Hanl8abaf9e2016-05-23 16:46:35 +020046
Michael Hanlc0ed00f2016-06-23 14:33:10 +020047 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +020048 public <R extends KustvaktResource> void cache (R resource) {
Michael Hanle25dea22015-09-24 19:37:56 +020049 CacheManager.getInstance().getCache("resources")
50 .put(new Element(resource.getPersistentID(), resource));
51 }
52
Michael Hanl8abaf9e2016-05-23 16:46:35 +020053
Michael Hanle25dea22015-09-24 19:37:56 +020054 /**
55 * @param id
56 * @param user
57 * @return
Michael Hanl8abaf9e2016-05-23 16:46:35 +020058 * @throws KustvaktException
59 * if there is no handler registered, resource might
60 * still be valid,
61 * only Notauthorized exception will cause a parsing
62 * error here
Michael Hanle25dea22015-09-24 19:37:56 +020063 * @throws NotAuthorizedException
64 */
Michael Hanl8abaf9e2016-05-23 16:46:35 +020065 public <T extends KustvaktResource> T findbyIntId (Integer id, User user)
Michael Hanle25dea22015-09-24 19:37:56 +020066 throws KustvaktException, NotAuthorizedException {
67 SecurityManager<T> p;
68 try {
69 p = SecurityManager.findbyId(id, user);
Michael Hanl8abaf9e2016-05-23 16:46:35 +020070 }
71 catch (EmptyResultException e) {
Michael Hanl99cb9632016-06-29 16:24:40 +020072 throw new NotAuthorizedException(StatusCodes.NO_VALUE_FOUND,
Michael Hanl8abaf9e2016-05-23 16:46:35 +020073 String.valueOf(id));
Michael Hanle25dea22015-09-24 19:37:56 +020074 }
Michael Hanle25dea22015-09-24 19:37:56 +020075 return p.getResource();
76 }
77
Michael Hanl8abaf9e2016-05-23 16:46:35 +020078
79 public <T extends KustvaktResource> T findbyStrId (String persistent_id,
80 User user, String type) throws KustvaktException,
81 NotAuthorizedException {
Michael Hanldaf86602016-05-12 14:31:52 +020082 return (T) findbyStrId(persistent_id, user,
83 ResourceFactory.getResourceClass(type));
Michael Hanle25dea22015-09-24 19:37:56 +020084 }
85
Michael Hanl8abaf9e2016-05-23 16:46:35 +020086
87 public <T extends KustvaktResource> T findbyStrId (String persistent_id,
88 User user, Class<T> type) throws KustvaktException,
89 NotAuthorizedException {
Michael Hanl8abaf9e2016-05-23 16:46:35 +020090 SecurityManager<T> p;
91 try {
92 p = SecurityManager.findbyId(persistent_id, user, type);
93 }
94 catch (EmptyResultException e) {
Michael Hanl99cb9632016-06-29 16:24:40 +020095 throw new NotAuthorizedException(StatusCodes.NO_VALUE_FOUND,
Michael Hanl8abaf9e2016-05-23 16:46:35 +020096 persistent_id);
97 }
98 return p.getResource();
Michael Hanle25dea22015-09-24 19:37:56 +020099 }
100
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200101
102 public <T extends KustvaktResource> Collection<T> findbyPath (String path,
103 Class type, User user) throws KustvaktException,
104 NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200105 return ResourceFinder.search(path, false, user, type);
106 }
107
108
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200109 public <T extends KustvaktResource> void updateResources (User user,
110 T ... resources) throws KustvaktException, NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200111 // fixme: what if update fails? then i have a root policy lingering for a resource that is not available?!
112 // fixme: transaction management
113
114 for (T resource : resources) {
115 SecurityManager policies;
116 try {
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200117 policies = SecurityManager.init(resource.getPersistentID(),
118 user, Permissions.Permission.WRITE);
119 }
120 catch (EmptyResultException e) {
Michael Hanle25dea22015-09-24 19:37:56 +0200121 return;
122 }
123 policies.updateResource(resource);
124 }
125 }
126
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200127
128 public <T extends KustvaktResource> void storeResources (User user,
129 T ... resources) throws KustvaktException, NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200130 for (T resource : resources)
131 SecurityManager.register(resource, user);
132 }
133
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200134
Michael Hanle25dea22015-09-24 19:37:56 +0200135 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200136 public <T extends KustvaktResource> void deleteResources (User user,
137 String ... ids) throws KustvaktException, NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200138 for (String id : ids) {
139 SecurityManager policies;
140 try {
141 policies = SecurityManager.init(id, user,
Michael Hanl88b49db2016-02-16 17:15:43 +0100142 Permissions.Permission.DELETE);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200143 }
144 catch (EmptyResultException e) {
Michael Hanle25dea22015-09-24 19:37:56 +0200145 return;
146 }
147 policies.deleteResource();
148 }
149 }
150
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200151
152 public <T extends KustvaktResource> void deleteResources (User user,
153 T ... resources) throws KustvaktException, NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200154 for (T r : resources) {
Michael Hanldaf86602016-05-12 14:31:52 +0200155 SecurityManager manager;
Michael Hanle25dea22015-09-24 19:37:56 +0200156 try {
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200157 manager = SecurityManager.findbyId(r.getPersistentID(), user,
158 r.getClass(), Permissions.Permission.DELETE);
159 }
160 catch (EmptyResultException e) {
Michael Hanle25dea22015-09-24 19:37:56 +0200161 return;
162 }
Michael Hanldaf86602016-05-12 14:31:52 +0200163 manager.deleteResource();
Michael Hanle25dea22015-09-24 19:37:56 +0200164 }
165 }
166
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200167
Michael Hanle25dea22015-09-24 19:37:56 +0200168 @Deprecated
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200169 public <T extends KustvaktResource> void deleteResources (User user,
170 Integer ... ids) throws KustvaktException, NotAuthorizedException {
Michael Hanle25dea22015-09-24 19:37:56 +0200171 for (Integer id : ids) {
172 SecurityManager policies;
173 try {
174 policies = SecurityManager.findbyId(id, user,
Michael Hanl88b49db2016-02-16 17:15:43 +0100175 Permissions.Permission.DELETE);
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200176 }
177 catch (EmptyResultException e) {
Michael Hanle25dea22015-09-24 19:37:56 +0200178 return;
179 }
180 policies.deleteResource();
181 }
182 }
183}