blob: c9e8a4bf151e2f53f5d90f92c045fb9cb79ac11e [file] [log] [blame]
margarethad3c0fc92017-10-25 15:03:32 +02001package de.ids_mannheim.korap.web.controller;
Michael Hanle56bb892016-05-25 17:34:41 +02002
margarethad8437f12017-03-14 17:09:02 +01003import java.util.List;
4import java.util.Locale;
margarethad8437f12017-03-14 17:09:02 +01005
6import javax.ws.rs.GET;
7import javax.ws.rs.POST;
8import javax.ws.rs.Path;
9import javax.ws.rs.PathParam;
10import javax.ws.rs.Produces;
11import javax.ws.rs.QueryParam;
12import javax.ws.rs.core.Context;
13import javax.ws.rs.core.MediaType;
14import javax.ws.rs.core.Response;
margarethad8437f12017-03-14 17:09:02 +010015
16import org.joda.time.DateTime;
17import org.slf4j.Logger;
18import org.slf4j.LoggerFactory;
19
margaretha62055f72017-04-11 19:17:43 +020020import com.sun.jersey.api.core.HttpContext;
Michael Hanle56bb892016-05-25 17:34:41 +020021import com.sun.jersey.spi.container.ResourceFilters;
margarethad8437f12017-03-14 17:09:02 +010022
Michael Hanle56bb892016-05-25 17:34:41 +020023import de.ids_mannheim.korap.auditing.AuditRecord;
24import de.ids_mannheim.korap.config.BeansFactory;
25import de.ids_mannheim.korap.exceptions.KustvaktException;
26import de.ids_mannheim.korap.exceptions.StatusCodes;
27import de.ids_mannheim.korap.handlers.DocumentDao;
margarethad8437f12017-03-14 17:09:02 +010028import de.ids_mannheim.korap.interfaces.db.AuditingIface;
Michael Hanle56bb892016-05-25 17:34:41 +020029import de.ids_mannheim.korap.resources.Document;
30import de.ids_mannheim.korap.resources.KustvaktResource;
31import de.ids_mannheim.korap.resources.Permissions;
32import de.ids_mannheim.korap.resources.ResourceFactory;
33import de.ids_mannheim.korap.security.PolicyCondition;
34import de.ids_mannheim.korap.security.ac.PolicyBuilder;
margarethaf68daa62017-09-21 02:11:24 +020035import de.ids_mannheim.korap.server.KustvaktServer;
Michael Hanle56bb892016-05-25 17:34:41 +020036import de.ids_mannheim.korap.user.User;
37import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanle56bb892016-05-25 17:34:41 +020038import de.ids_mannheim.korap.utils.TimeUtils;
Michael Hanle56bb892016-05-25 17:34:41 +020039import de.ids_mannheim.korap.web.filter.AdminFilter;
40import de.ids_mannheim.korap.web.filter.PiwikFilter;
41import de.ids_mannheim.korap.web.utils.KustvaktResponseHandler;
Michael Hanle56bb892016-05-25 17:34:41 +020042
43/**
margarethafc2040a2017-04-18 12:07:23 +020044 * @author hanl, margaretha
45 * Created date 6/11/14.
46 * Last update: 04/2017
Michael Hanle56bb892016-05-25 17:34:41 +020047 */
48@Path(KustvaktServer.API_VERSION + "/admin")
49@ResourceFilters({ AdminFilter.class, PiwikFilter.class })
50@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
margarethad3c0fc92017-10-25 15:03:32 +020051public class AdminController {
Michael Hanle56bb892016-05-25 17:34:41 +020052
margarethad3c0fc92017-10-25 15:03:32 +020053 private static Logger jlog = LoggerFactory.getLogger(AdminController.class);
Michael Hanlc0ed00f2016-06-23 14:33:10 +020054
Michael Hanle56bb892016-05-25 17:34:41 +020055 private AuditingIface auditingController;
56 private DocumentDao documentDao;
57
58
margarethad3c0fc92017-10-25 15:03:32 +020059 public AdminController () {
Michael Hanle56bb892016-05-25 17:34:41 +020060 this.auditingController = BeansFactory.getKustvaktContext()
61 .getAuditingProvider();
margarethafc2040a2017-04-18 12:07:23 +020062 this.documentDao = new DocumentDao(
63 BeansFactory.getKustvaktContext().getPersistenceClient());
Michael Hanle56bb892016-05-25 17:34:41 +020064 }
65
margarethafc2040a2017-04-18 12:07:23 +020066 // EM: not documented and tested, not sure what the purpose of the service is
Michael Hanle56bb892016-05-25 17:34:41 +020067 @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 Hanlc0ed00f2016-06-23 14:33:10 +020076 from_date = TimeUtils.getNow();
Michael Hanle56bb892016-05-25 17:34:41 +020077 else
Michael Hanlc0ed00f2016-06-23 14:33:10 +020078 from_date = TimeUtils.getTime(from);
Michael Hanle56bb892016-05-25 17:34:41 +020079 if (until == null)
Michael Hanlc0ed00f2016-06-23 14:33:10 +020080 until_date = TimeUtils.getNow();
Michael Hanle56bb892016-05-25 17:34:41 +020081 else
Michael Hanlc0ed00f2016-06-23 14:33:10 +020082 until_date = TimeUtils.getTime(until);
Michael Hanle56bb892016-05-25 17:34:41 +020083
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,
margarethafc2040a2017-04-18 12:07:23 +0200107 @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 Hanle56bb892016-05-25 17:34:41 +0200142
Michael Hanle56bb892016-05-25 17:34:41 +0200143 try {
Michael Hanl99cb9632016-06-29 16:24:40 +0200144 KustvaktResource resource = ResourceFactory.getResource(type);
145 resource.setPersistentID(persistentid);
146 resource.setDescription(description);
147 resource.setName(name);
148
margarethafc2040a2017-04-18 12:07:23 +0200149 Permissions.Permission[] p = Permissions
150 .read(permissions.toArray(new String[0]));
151
margaretha62055f72017-04-11 19:17:43 +0200152 User user = (User) context.getProperties().get("user");
margarethafc2040a2017-04-18 12:07:23 +0200153
margarethad8437f12017-03-14 17:09:02 +0100154 PolicyBuilder pb = new PolicyBuilder(user)
155 .setConditions(new PolicyCondition(group))
156 .setResources(resource);
margarethafc2040a2017-04-18 12:07:23 +0200157
158 if (loc != null && !loc.isEmpty()){
margarethad8437f12017-03-14 17:09:02 +0100159 pb.setLocation(loc);
margarethafc2040a2017-04-18 12:07:23 +0200160 }
161 if (duration != null && !duration.isEmpty()){
162 long now = TimeUtils.getNow().getMillis();
163 pb.setContext(now,
164 now + TimeUtils.convertTimeToSeconds(duration));
165 }
margarethad8437f12017-03-14 17:09:02 +0100166 pb.setPermissions(p);
167 pb.create();
Michael Hanle56bb892016-05-25 17:34:41 +0200168 }
169 catch (KustvaktException e) {
170 throw KustvaktResponseHandler.throwit(e);
171 }
172
173 return Response.ok().build();
174 }
175
176
Michael Hanle56bb892016-05-25 17:34:41 +0200177 @POST
178 @Path("doc/{id}/add")
Michael Hanlcb2d3f92016-06-02 17:34:06 +0200179 @Deprecated
Michael Hanle56bb892016-05-25 17:34:41 +0200180 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}