blob: f3dac4982dce2187c7a3c2acb63f278638f3b16f [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.Locale;
margarethad8437f12017-03-14 17:09:02 +01004
5import javax.ws.rs.GET;
margarethad8437f12017-03-14 17:09:02 +01006import javax.ws.rs.Path;
7import javax.ws.rs.PathParam;
8import javax.ws.rs.Produces;
9import javax.ws.rs.QueryParam;
10import javax.ws.rs.core.Context;
11import javax.ws.rs.core.MediaType;
12import javax.ws.rs.core.Response;
margarethad8437f12017-03-14 17:09:02 +010013
margaretha49cb6882018-07-04 04:19:54 +020014import org.apache.logging.log4j.LogManager;
15import org.apache.logging.log4j.Logger;
margarethad8437f12017-03-14 17:09:02 +010016import org.joda.time.DateTime;
margaretha894a7d72017-11-08 19:24:20 +010017import org.springframework.beans.factory.annotation.Autowired;
18import org.springframework.stereotype.Controller;
margarethad8437f12017-03-14 17:09:02 +010019
abcpro1c0c2e302022-11-07 18:30:52 +000020import de.ids_mannheim.korap.web.utils.ResourceFilters;
margarethad8437f12017-03-14 17:09:02 +010021
Michael Hanle56bb892016-05-25 17:34:41 +020022import de.ids_mannheim.korap.auditing.AuditRecord;
Michael Hanle56bb892016-05-25 17:34:41 +020023import de.ids_mannheim.korap.exceptions.KustvaktException;
24import de.ids_mannheim.korap.exceptions.StatusCodes;
margarethad8437f12017-03-14 17:09:02 +010025import de.ids_mannheim.korap.interfaces.db.AuditingIface;
Michael Hanle56bb892016-05-25 17:34:41 +020026import de.ids_mannheim.korap.utils.JsonUtils;
Michael Hanle56bb892016-05-25 17:34:41 +020027import de.ids_mannheim.korap.utils.TimeUtils;
margarethada3c7852018-06-14 20:35:11 +020028import de.ids_mannheim.korap.web.KustvaktResponseHandler;
margaretha398f4722019-01-09 19:07:20 +010029import de.ids_mannheim.korap.web.filter.APIVersionFilter;
Michael Hanle56bb892016-05-25 17:34:41 +020030import de.ids_mannheim.korap.web.filter.AdminFilter;
31import de.ids_mannheim.korap.web.filter.PiwikFilter;
Michael Hanle56bb892016-05-25 17:34:41 +020032
33/**
margarethafc2040a2017-04-18 12:07:23 +020034 * @author hanl, margaretha
35 * Created date 6/11/14.
margaretha894a7d72017-11-08 19:24:20 +010036 * Last update: 08/11/2017
37 * Last changes:
margarethaee0cbfe2018-08-28 17:47:14 +020038 * - removed DocumentDao (EM)
39 * - added API version filter (EM)
Michael Hanle56bb892016-05-25 17:34:41 +020040 */
margaretha2afb97d2017-12-07 19:18:44 +010041@Deprecated
margaretha894a7d72017-11-08 19:24:20 +010042@Controller
margarethaee0cbfe2018-08-28 17:47:14 +020043@Path("/v0.1/admin")
44@ResourceFilters({APIVersionFilter.class, AdminFilter.class, PiwikFilter.class })
Michael Hanle56bb892016-05-25 17:34:41 +020045@Produces(MediaType.APPLICATION_JSON + ";charset=utf-8")
margarethad3c0fc92017-10-25 15:03:32 +020046public class AdminController {
Michael Hanle56bb892016-05-25 17:34:41 +020047
margaretha49cb6882018-07-04 04:19:54 +020048 private static Logger jlog = LogManager.getLogger(AdminController.class);
margaretha894a7d72017-11-08 19:24:20 +010049 @Autowired
Michael Hanle56bb892016-05-25 17:34:41 +020050 private AuditingIface auditingController;
Michael Hanle56bb892016-05-25 17:34:41 +020051
margaretha894a7d72017-11-08 19:24:20 +010052 @Autowired
margarethada3c7852018-06-14 20:35:11 +020053 private KustvaktResponseHandler kustvaktResponseHandler;
Michael Hanle56bb892016-05-25 17:34:41 +020054
margarethafc2040a2017-04-18 12:07:23 +020055 // EM: not documented and tested, not sure what the purpose of the service is
Michael Hanle56bb892016-05-25 17:34:41 +020056 @GET
57 @Path("audit/{type}")
58 public Response getAudits (@PathParam("type") String type,
59 @QueryParam("from") String from, @QueryParam("until") String until,
60 @QueryParam("day") Boolean day, @QueryParam("limit") String limit,
61 @Context Locale locale) {
62 DateTime from_date, until_date;
63
64 if (from == null)
Michael Hanlc0ed00f2016-06-23 14:33:10 +020065 from_date = TimeUtils.getNow();
Michael Hanle56bb892016-05-25 17:34:41 +020066 else
Michael Hanlc0ed00f2016-06-23 14:33:10 +020067 from_date = TimeUtils.getTime(from);
Michael Hanle56bb892016-05-25 17:34:41 +020068 if (until == null)
Michael Hanlc0ed00f2016-06-23 14:33:10 +020069 until_date = TimeUtils.getNow();
Michael Hanle56bb892016-05-25 17:34:41 +020070 else
Michael Hanlc0ed00f2016-06-23 14:33:10 +020071 until_date = TimeUtils.getTime(until);
Michael Hanle56bb892016-05-25 17:34:41 +020072
73 int integer_limit;
74 boolean dayOnly = Boolean.valueOf(day);
75 try {
76 integer_limit = Integer.valueOf(limit);
77 }
78 catch (NumberFormatException | NullPointerException e) {
margarethada3c7852018-06-14 20:35:11 +020079 throw kustvaktResponseHandler.throwit(StatusCodes.ILLEGAL_ARGUMENT);
Michael Hanle56bb892016-05-25 17:34:41 +020080 }
margarethad4796662017-11-09 16:11:40 +010081 String result="";
82 try {
83 result = JsonUtils.toJSON(auditingController.retrieveRecords(
84 AuditRecord.CATEGORY.valueOf(type.toUpperCase()), from_date,
85 until_date, dayOnly, integer_limit));
86 }
87 catch (KustvaktException e) {
margarethada3c7852018-06-14 20:35:11 +020088 throw kustvaktResponseHandler.throwit(e);
margarethad4796662017-11-09 16:11:40 +010089 }
Michael Hanle56bb892016-05-25 17:34:41 +020090 // limit number of records to return
91 return Response.ok(result).build();
92 }
93
94
margarethaefc18a42018-03-01 16:01:42 +010095// @Deprecated
96// @POST
97// @Path("createPolicies/{id}")
98// public Response addResourcePolicy (@PathParam("id") String persistentid,
99// @QueryParam("type") String type, @QueryParam("name") String name,
100// @QueryParam("description") String description,
101// @QueryParam("group") String group,
102// @QueryParam("perm") List<String> permissions,
103// @QueryParam("loc") String loc,
104// @QueryParam("expire") String duration, @Context HttpContext context)
105// throws KustvaktException {
106//
107// if (type == null | type.isEmpty()) {
108// KustvaktException e = new KustvaktException(
109// StatusCodes.MISSING_ARGUMENT,
110// "The value of parameter type is missing.");
111// throw kustvaktResponseHandler.throwit(e);
112// }
113// else if (name == null | name.isEmpty()) {
114// KustvaktException e = new KustvaktException(
115// StatusCodes.MISSING_ARGUMENT,
116// "The value of parameter name is missing.");
117// throw kustvaktResponseHandler.throwit(e);
118// }
119// else if (description == null | description.isEmpty()) {
120// KustvaktException e = new KustvaktException(
121// StatusCodes.MISSING_ARGUMENT,
122// "The value of parameter description is missing.");
123// throw kustvaktResponseHandler.throwit(e);
124// }
125// else if (group == null | group.isEmpty()) {
126// KustvaktException e = new KustvaktException(
127// StatusCodes.MISSING_ARGUMENT,
128// "The value of parameter group is missing.");
129// throw kustvaktResponseHandler.throwit(e);
130// }
131// else if (permissions == null | permissions.isEmpty()) {
132// KustvaktException e = new KustvaktException(
133// StatusCodes.MISSING_ARGUMENT,
134// "The value of parameter permissions is missing.");
135// throw kustvaktResponseHandler.throwit(e);
136// }
137//
138//
139// try {
140// KustvaktResource resource = ResourceFactory.getResource(type);
141// resource.setPersistentID(persistentid);
142// resource.setDescription(description);
143// resource.setName(name);
144//
145// Permissions.Permission[] p = Permissions
146// .read(permissions.toArray(new String[0]));
147//
148// User user = (User) context.getProperties().get("user");
149//
150// PolicyBuilder pb = new PolicyBuilder(user)
151// .setConditions(new PolicyCondition(group))
152// .setResources(resource);
153//
154// if (loc != null && !loc.isEmpty()){
155// pb.setLocation(loc);
156// }
157// if (duration != null && !duration.isEmpty()){
158// long now = TimeUtils.getNow().getMillis();
159// pb.setContext(now,
160// now + TimeUtils.convertTimeToSeconds(duration));
161// }
162// pb.setPermissions(p);
163// pb.create();
164// }
165// catch (KustvaktException e) {
166// throw kustvaktResponseHandler.throwit(e);
167// }
168//
169// return Response.ok().build();
170// }
Michael Hanle56bb892016-05-25 17:34:41 +0200171
Michael Hanle56bb892016-05-25 17:34:41 +0200172}