| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web.controller; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 2 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 3 | import java.util.List; |
| 4 | import java.util.Locale; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 5 | |
| 6 | import javax.ws.rs.GET; |
| 7 | import javax.ws.rs.POST; |
| 8 | import javax.ws.rs.Path; |
| 9 | import javax.ws.rs.PathParam; |
| 10 | import javax.ws.rs.Produces; |
| 11 | import javax.ws.rs.QueryParam; |
| 12 | import javax.ws.rs.core.Context; |
| 13 | import javax.ws.rs.core.MediaType; |
| 14 | import javax.ws.rs.core.Response; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 15 | |
| 16 | import org.joda.time.DateTime; |
| 17 | import org.slf4j.Logger; |
| 18 | import org.slf4j.LoggerFactory; |
| 19 | |
| margaretha | 62055f7 | 2017-04-11 19:17:43 +0200 | [diff] [blame] | 20 | import com.sun.jersey.api.core.HttpContext; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 21 | import com.sun.jersey.spi.container.ResourceFilters; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 22 | |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 23 | import de.ids_mannheim.korap.auditing.AuditRecord; |
| 24 | import de.ids_mannheim.korap.config.BeansFactory; |
| 25 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| 26 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| 27 | import de.ids_mannheim.korap.handlers.DocumentDao; |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 28 | import de.ids_mannheim.korap.interfaces.db.AuditingIface; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 29 | import de.ids_mannheim.korap.resources.Document; |
| 30 | import de.ids_mannheim.korap.resources.KustvaktResource; |
| 31 | import de.ids_mannheim.korap.resources.Permissions; |
| 32 | import de.ids_mannheim.korap.resources.ResourceFactory; |
| 33 | import de.ids_mannheim.korap.security.PolicyCondition; |
| 34 | import de.ids_mannheim.korap.security.ac.PolicyBuilder; |
| margaretha | f68daa6 | 2017-09-21 02:11:24 +0200 | [diff] [blame] | 35 | import de.ids_mannheim.korap.server.KustvaktServer; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 36 | import de.ids_mannheim.korap.user.User; |
| 37 | import de.ids_mannheim.korap.utils.JsonUtils; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 38 | import de.ids_mannheim.korap.utils.TimeUtils; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 39 | import de.ids_mannheim.korap.web.filter.AdminFilter; |
| 40 | import de.ids_mannheim.korap.web.filter.PiwikFilter; |
| 41 | import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler; |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 42 | |
| 43 | /** |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 44 | * @author hanl, margaretha |
| 45 | * Created date 6/11/14. |
| 46 | * Last update: 04/2017 |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 47 | */ |
| 48 | @Path(KustvaktServer.API_VERSION + "/admin") |
| 49 | @ResourceFilters({ AdminFilter.class, PiwikFilter.class }) |
| 50 | @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 51 | public class AdminController { |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 52 | |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 53 | private static Logger jlog = LoggerFactory.getLogger(AdminController.class); |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 54 | |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 55 | private AuditingIface auditingController; |
| 56 | private DocumentDao documentDao; |
| 57 | |
| 58 | |
| margaretha | d3c0fc9 | 2017-10-25 15:03:32 +0200 | [diff] [blame] | 59 | public AdminController () { |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 60 | this.auditingController = BeansFactory.getKustvaktContext() |
| 61 | .getAuditingProvider(); |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 62 | this.documentDao = new DocumentDao( |
| 63 | BeansFactory.getKustvaktContext().getPersistenceClient()); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 64 | } |
| 65 | |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 66 | // EM: not documented and tested, not sure what the purpose of the service is |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 67 | @GET |
| 68 | @Path("audit/{type}") |
| 69 | public Response getAudits (@PathParam("type") String type, |
| 70 | @QueryParam("from") String from, @QueryParam("until") String until, |
| 71 | @QueryParam("day") Boolean day, @QueryParam("limit") String limit, |
| 72 | @Context Locale locale) { |
| 73 | DateTime from_date, until_date; |
| 74 | |
| 75 | if (from == null) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 76 | from_date = TimeUtils.getNow(); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 77 | else |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 78 | from_date = TimeUtils.getTime(from); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 79 | if (until == null) |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 80 | until_date = TimeUtils.getNow(); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 81 | else |
| Michael Hanl | c0ed00f | 2016-06-23 14:33:10 +0200 | [diff] [blame] | 82 | until_date = TimeUtils.getTime(until); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 83 | |
| 84 | int integer_limit; |
| 85 | boolean dayOnly = Boolean.valueOf(day); |
| 86 | try { |
| 87 | integer_limit = Integer.valueOf(limit); |
| 88 | } |
| 89 | catch (NumberFormatException | NullPointerException e) { |
| 90 | throw KustvaktResponseHandler.throwit(StatusCodes.ILLEGAL_ARGUMENT); |
| 91 | } |
| 92 | String result = JsonUtils.toJSON(auditingController.retrieveRecords( |
| 93 | AuditRecord.CATEGORY.valueOf(type.toUpperCase()), from_date, |
| 94 | until_date, dayOnly, integer_limit)); |
| 95 | // limit number of records to return |
| 96 | return Response.ok(result).build(); |
| 97 | } |
| 98 | |
| 99 | |
| 100 | @POST |
| 101 | @Path("createPolicies/{id}") |
| 102 | public Response addResourcePolicy (@PathParam("id") String persistentid, |
| 103 | @QueryParam("type") String type, @QueryParam("name") String name, |
| 104 | @QueryParam("description") String description, |
| 105 | @QueryParam("group") String group, |
| 106 | @QueryParam("perm") List<String> permissions, |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 107 | @QueryParam("loc") String loc, |
| 108 | @QueryParam("expire") String duration, @Context HttpContext context) |
| 109 | throws KustvaktException { |
| 110 | |
| 111 | if (type == null | type.isEmpty()) { |
| 112 | KustvaktException e = new KustvaktException( |
| 113 | StatusCodes.MISSING_ARGUMENT, |
| 114 | "The value of parameter type is missing."); |
| 115 | throw KustvaktResponseHandler.throwit(e); |
| 116 | } |
| 117 | else if (name == null | name.isEmpty()) { |
| 118 | KustvaktException e = new KustvaktException( |
| 119 | StatusCodes.MISSING_ARGUMENT, |
| 120 | "The value of parameter name is missing."); |
| 121 | throw KustvaktResponseHandler.throwit(e); |
| 122 | } |
| 123 | else if (description == null | description.isEmpty()) { |
| 124 | KustvaktException e = new KustvaktException( |
| 125 | StatusCodes.MISSING_ARGUMENT, |
| 126 | "The value of parameter description is missing."); |
| 127 | throw KustvaktResponseHandler.throwit(e); |
| 128 | } |
| 129 | else if (group == null | group.isEmpty()) { |
| 130 | KustvaktException e = new KustvaktException( |
| 131 | StatusCodes.MISSING_ARGUMENT, |
| 132 | "The value of parameter group is missing."); |
| 133 | throw KustvaktResponseHandler.throwit(e); |
| 134 | } |
| 135 | else if (permissions == null | permissions.isEmpty()) { |
| 136 | KustvaktException e = new KustvaktException( |
| 137 | StatusCodes.MISSING_ARGUMENT, |
| 138 | "The value of parameter permissions is missing."); |
| 139 | throw KustvaktResponseHandler.throwit(e); |
| 140 | } |
| 141 | |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 142 | |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 143 | try { |
| Michael Hanl | 99cb963 | 2016-06-29 16:24:40 +0200 | [diff] [blame] | 144 | KustvaktResource resource = ResourceFactory.getResource(type); |
| 145 | resource.setPersistentID(persistentid); |
| 146 | resource.setDescription(description); |
| 147 | resource.setName(name); |
| 148 | |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 149 | Permissions.Permission[] p = Permissions |
| 150 | .read(permissions.toArray(new String[0])); |
| 151 | |
| margaretha | 62055f7 | 2017-04-11 19:17:43 +0200 | [diff] [blame] | 152 | User user = (User) context.getProperties().get("user"); |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 153 | |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 154 | PolicyBuilder pb = new PolicyBuilder(user) |
| 155 | .setConditions(new PolicyCondition(group)) |
| 156 | .setResources(resource); |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 157 | |
| 158 | if (loc != null && !loc.isEmpty()){ |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 159 | pb.setLocation(loc); |
| margaretha | fc2040a | 2017-04-18 12:07:23 +0200 | [diff] [blame] | 160 | } |
| 161 | if (duration != null && !duration.isEmpty()){ |
| 162 | long now = TimeUtils.getNow().getMillis(); |
| 163 | pb.setContext(now, |
| 164 | now + TimeUtils.convertTimeToSeconds(duration)); |
| 165 | } |
| margaretha | d8437f1 | 2017-03-14 17:09:02 +0100 | [diff] [blame] | 166 | pb.setPermissions(p); |
| 167 | pb.create(); |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 168 | } |
| 169 | catch (KustvaktException e) { |
| 170 | throw KustvaktResponseHandler.throwit(e); |
| 171 | } |
| 172 | |
| 173 | return Response.ok().build(); |
| 174 | } |
| 175 | |
| 176 | |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 177 | @POST |
| 178 | @Path("doc/{id}/add") |
| Michael Hanl | cb2d3f9 | 2016-06-02 17:34:06 +0200 | [diff] [blame] | 179 | @Deprecated |
| Michael Hanl | e56bb89 | 2016-05-25 17:34:41 +0200 | [diff] [blame] | 180 | public Response addDocument (@PathParam("id") String id) { |
| 181 | Document document = new Document(id); |
| 182 | try { |
| 183 | this.documentDao.storeResource(document, null); |
| 184 | } |
| 185 | catch (KustvaktException e) { |
| 186 | throw KustvaktResponseHandler.throwit(e); |
| 187 | } |
| 188 | return Response.ok().build(); |
| 189 | } |
| 190 | |
| 191 | } |