blob: 920d38bd523da6ca5fee28cb7ae0f795281fcd24 [file] [log] [blame]
margaretha56e8e552017-12-05 16:31:21 +01001package de.ids_mannheim.korap.web;
Michael Hanlca740d72015-06-16 10:04:58 +02002
Michael Hanl482f30d2015-09-25 12:39:46 +02003import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlca740d72015-06-16 10:04:58 +02004import de.ids_mannheim.korap.exceptions.StatusCodes;
Michael Hanlca740d72015-06-16 10:04:58 +02005import de.ids_mannheim.korap.response.Notifications;
margaretha96c309d2023-08-16 12:24:12 +02006import jakarta.ws.rs.WebApplicationException;
7import jakarta.ws.rs.core.Response;
Michael Hanlca740d72015-06-16 10:04:58 +02008
Michael Hanlca740d72015-06-16 10:04:58 +02009/**
margaretha894a7d72017-11-08 19:24:20 +010010 * @author hanl, margaretha
Michael Hanlca740d72015-06-16 10:04:58 +020011 * @date 29/01/2014
margaretha56e8e552017-12-05 16:31:21 +010012 * @last 04/12/2017
Michael Hanlca740d72015-06-16 10:04:58 +020013 */
margaretha56e8e552017-12-05 16:31:21 +010014public class CoreResponseHandler {
Michael Hanlca740d72015-06-16 10:04:58 +020015
margaretha894a7d72017-11-08 19:24:20 +010016 public WebApplicationException throwit (KustvaktException e) {
17 Response s;
margaretha20f31232018-07-09 17:49:39 +020018 if (e.hasNotification()) {
19 if (e.getStatusCode() != null) {
20 s = Response.status(getStatus(e.getStatusCode()))
21 .entity(e.getNotification()).build();
22 }
23 // KustvaktException just wraps another exception
24 else {
margaretha35e1ca22023-11-16 22:00:01 +010025 s = Response.status(Response.Status.BAD_REQUEST)
margaretha20f31232018-07-09 17:49:39 +020026 .entity(e.getNotification()).build();
27 }
margaretha894a7d72017-11-08 19:24:20 +010028 }
margaretha20f31232018-07-09 17:49:39 +020029 else {
margaretha894a7d72017-11-08 19:24:20 +010030 s = Response.status(getStatus(e.getStatusCode()))
31 .entity(buildNotification(e)).build();
32 }
Michael Hanl7368aa42016-02-05 18:15:47 +010033 return new WebApplicationException(s);
Michael Hanlca740d72015-06-16 10:04:58 +020034 }
35
margaretha894a7d72017-11-08 19:24:20 +010036 public WebApplicationException throwit (int code) {
Michael Hanl7368aa42016-02-05 18:15:47 +010037 return new WebApplicationException(Response.status(getStatus(code))
Michael Hanl482f30d2015-09-25 12:39:46 +020038 .entity(buildNotification(code, "", "")).build());
Michael Hanlca740d72015-06-16 10:04:58 +020039 }
40
margaretha894a7d72017-11-08 19:24:20 +010041 public WebApplicationException throwit (int code, String message,
Michael Hanlca740d72015-06-16 10:04:58 +020042 String entity) {
Michael Hanl7368aa42016-02-05 18:15:47 +010043 return new WebApplicationException(Response.status(getStatus(code))
Michael Hanl482f30d2015-09-25 12:39:46 +020044 .entity(buildNotification(code, message, entity)).build());
Michael Hanlca740d72015-06-16 10:04:58 +020045 }
46
margaretha894a7d72017-11-08 19:24:20 +010047 public WebApplicationException throwit (int code, String notification) {
margaretha20f31232018-07-09 17:49:39 +020048 return new WebApplicationException(
49 Response.status(getStatus(code)).entity(notification).build());
margarethab7864112017-06-29 16:36:13 +020050 }
margaretha20f31232018-07-09 17:49:39 +020051
margaretha56e8e552017-12-05 16:31:21 +010052 protected String buildNotification (KustvaktException e) {
Michael Hanlca740d72015-06-16 10:04:58 +020053 return buildNotification(e.getStatusCode(), e.getMessage(),
54 e.getEntity());
55 }
56
margaretha2afb97d2017-12-07 19:18:44 +010057 public static String buildNotification (int code, String message,
Michael Hanlca740d72015-06-16 10:04:58 +020058 String entity) {
59 Notifications notif = new Notifications();
60 notif.addError(code, message, entity);
61 return notif.toJsonString() + "\n";
62 }
63
margaretha56e8e552017-12-05 16:31:21 +010064 protected Response.Status getStatus (int code) {
Michael Hanl7368aa42016-02-05 18:15:47 +010065 Response.Status status = Response.Status.BAD_REQUEST;
66 switch (code) {
margaretha20f31232018-07-09 17:49:39 +020067 // case StatusCodes.NO_VALUE_FOUND:
68 // status = Response.Status.NO_CONTENT;
69 // break;
Michael Hanl7368aa42016-02-05 18:15:47 +010070 case StatusCodes.ILLEGAL_ARGUMENT:
71 status = Response.Status.NOT_ACCEPTABLE;
72 break;
Michael Hanlc4446022016-02-12 18:03:17 +010073 case StatusCodes.STATUS_OK:
74 status = Response.Status.OK;
75 break;
margarethab5e1e0a2019-01-29 22:11:57 +010076 // EM: Added
77 case StatusCodes.NO_RESOURCE_FOUND:
78 status = Response.Status.NOT_FOUND;
79 break;
margaretha52ee9e32019-12-11 16:36:14 +010080 case StatusCodes.CACHING_VC:
81 status = Response.Status.SERVICE_UNAVAILABLE;
margaretha35e1ca22023-11-16 22:00:01 +010082 break;
Michael Hanl7368aa42016-02-05 18:15:47 +010083 }
84 return status;
Michael Hanlca740d72015-06-16 10:04:58 +020085 }
Michael Hanlca740d72015-06-16 10:04:58 +020086}