| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 1 | package de.ids_mannheim.korap.web; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 2 | |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 3 | import de.ids_mannheim.korap.exceptions.KustvaktException; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 4 | import de.ids_mannheim.korap.exceptions.StatusCodes; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 5 | import de.ids_mannheim.korap.response.Notifications; |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 6 | import jakarta.ws.rs.WebApplicationException; |
| 7 | import jakarta.ws.rs.core.Response; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 8 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 9 | /** |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 10 | * @author hanl, margaretha |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 11 | * @date 29/01/2014 |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 12 | * @last 04/12/2017 |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 13 | */ |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 14 | public class CoreResponseHandler { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 15 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 16 | public WebApplicationException throwit (KustvaktException e) { |
| 17 | Response s; |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 18 | 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 { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 25 | s = Response.status(Response.Status.BAD_REQUEST) |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 26 | .entity(e.getNotification()).build(); |
| 27 | } |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 28 | } |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 29 | else { |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 30 | s = Response.status(getStatus(e.getStatusCode())) |
| 31 | .entity(buildNotification(e)).build(); |
| 32 | } |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 33 | return new WebApplicationException(s); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 34 | } |
| 35 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 36 | public WebApplicationException throwit (int code) { |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 37 | return new WebApplicationException(Response.status(getStatus(code)) |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 38 | .entity(buildNotification(code, "", "")).build()); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 39 | } |
| 40 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 41 | public WebApplicationException throwit (int code, String message, |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 42 | String entity) { |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 43 | return new WebApplicationException(Response.status(getStatus(code)) |
| Michael Hanl | 482f30d | 2015-09-25 12:39:46 +0200 | [diff] [blame] | 44 | .entity(buildNotification(code, message, entity)).build()); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 45 | } |
| 46 | |
| margaretha | 894a7d7 | 2017-11-08 19:24:20 +0100 | [diff] [blame] | 47 | public WebApplicationException throwit (int code, String notification) { |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 48 | return new WebApplicationException( |
| 49 | Response.status(getStatus(code)).entity(notification).build()); |
| margaretha | b786411 | 2017-06-29 16:36:13 +0200 | [diff] [blame] | 50 | } |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 51 | |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 52 | protected String buildNotification (KustvaktException e) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 53 | return buildNotification(e.getStatusCode(), e.getMessage(), |
| 54 | e.getEntity()); |
| 55 | } |
| 56 | |
| margaretha | 2afb97d | 2017-12-07 19:18:44 +0100 | [diff] [blame] | 57 | public static String buildNotification (int code, String message, |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 58 | String entity) { |
| 59 | Notifications notif = new Notifications(); |
| 60 | notif.addError(code, message, entity); |
| 61 | return notif.toJsonString() + "\n"; |
| 62 | } |
| 63 | |
| margaretha | 56e8e55 | 2017-12-05 16:31:21 +0100 | [diff] [blame] | 64 | protected Response.Status getStatus (int code) { |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 65 | Response.Status status = Response.Status.BAD_REQUEST; |
| 66 | switch (code) { |
| margaretha | 20f3123 | 2018-07-09 17:49:39 +0200 | [diff] [blame] | 67 | // case StatusCodes.NO_VALUE_FOUND: |
| 68 | // status = Response.Status.NO_CONTENT; |
| 69 | // break; |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 70 | case StatusCodes.ILLEGAL_ARGUMENT: |
| 71 | status = Response.Status.NOT_ACCEPTABLE; |
| 72 | break; |
| Michael Hanl | c444602 | 2016-02-12 18:03:17 +0100 | [diff] [blame] | 73 | case StatusCodes.STATUS_OK: |
| 74 | status = Response.Status.OK; |
| 75 | break; |
| margaretha | b5e1e0a | 2019-01-29 22:11:57 +0100 | [diff] [blame] | 76 | // EM: Added |
| 77 | case StatusCodes.NO_RESOURCE_FOUND: |
| 78 | status = Response.Status.NOT_FOUND; |
| 79 | break; |
| margaretha | 52ee9e3 | 2019-12-11 16:36:14 +0100 | [diff] [blame] | 80 | case StatusCodes.CACHING_VC: |
| 81 | status = Response.Status.SERVICE_UNAVAILABLE; |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 82 | break; |
| Michael Hanl | 7368aa4 | 2016-02-05 18:15:47 +0100 | [diff] [blame] | 83 | } |
| 84 | return status; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 85 | } |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 86 | } |