| abcpro1 | 3df666b | 2022-11-07 19:37:22 +0000 | [diff] [blame] | 1 | package de.ids_mannheim.korap.utils; |
| 2 | |
| abcpro1 | 3df666b | 2022-11-07 19:37:22 +0000 | [diff] [blame] | 3 | import org.glassfish.jersey.message.internal.MediaTypes; |
| 4 | import org.glassfish.jersey.server.ContainerRequest; |
| 5 | |
| margaretha | 96c309d | 2023-08-16 12:24:12 +0200 | [diff] [blame] | 6 | import jakarta.ws.rs.container.ContainerRequestContext; |
| 7 | import jakarta.ws.rs.core.Form; |
| 8 | import jakarta.ws.rs.core.MediaType; |
| 9 | |
| abcpro1 | 3df666b | 2022-11-07 19:37:22 +0000 | [diff] [blame] | 10 | public class JerseyUtils { |
| 11 | |
| 12 | /** |
| 13 | * Get the form parameters of the request entity. |
| 14 | * <p> |
| 15 | * This method will ensure that the request entity is buffered |
| 16 | * such that it may be consumed by the application. |
| 17 | * |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 18 | * @return the form parameters, if there is a request entity and |
| 19 | * the |
| 20 | * content type is "application/x-www-form-urlencoded", |
| 21 | * otherwise an |
| 22 | * instance containing no parameters will be returned. |
| abcpro1 | 3df666b | 2022-11-07 19:37:22 +0000 | [diff] [blame] | 23 | */ |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 24 | public static Form getFormParameters ( |
| 25 | ContainerRequestContext requestContext) { |
| abcpro1 | 3df666b | 2022-11-07 19:37:22 +0000 | [diff] [blame] | 26 | if (requestContext instanceof ContainerRequest) { |
| 27 | return getFormParameters((ContainerRequest) requestContext); |
| 28 | } |
| 29 | return new Form(); |
| 30 | } |
| 31 | |
| 32 | private static Form getFormParameters (ContainerRequest request) { |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 33 | if (MediaTypes.typeEqual(MediaType.APPLICATION_FORM_URLENCODED_TYPE, |
| 34 | request.getMediaType())) { |
| abcpro1 | 3df666b | 2022-11-07 19:37:22 +0000 | [diff] [blame] | 35 | request.bufferEntity(); |
| 36 | Form form = request.readEntity(Form.class); |
| 37 | return (form == null ? new Form() : form); |
| margaretha | 35e1ca2 | 2023-11-16 22:00:01 +0100 | [diff] [blame] | 38 | } |
| 39 | else { |
| abcpro1 | 3df666b | 2022-11-07 19:37:22 +0000 | [diff] [blame] | 40 | return new Form(); |
| 41 | } |
| 42 | } |
| 43 | } |