blob: 08f73d1cd02c7a3514207e3ab0ff027ea5dc2bbd [file] [log] [blame]
abcpro13df666b2022-11-07 19:37:22 +00001package de.ids_mannheim.korap.utils;
2
abcpro13df666b2022-11-07 19:37:22 +00003import org.glassfish.jersey.message.internal.MediaTypes;
4import org.glassfish.jersey.server.ContainerRequest;
5
margaretha96c309d2023-08-16 12:24:12 +02006import jakarta.ws.rs.container.ContainerRequestContext;
7import jakarta.ws.rs.core.Form;
8import jakarta.ws.rs.core.MediaType;
9
abcpro13df666b2022-11-07 19:37:22 +000010public 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 *
margaretha35e1ca22023-11-16 22:00:01 +010018 * @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.
abcpro13df666b2022-11-07 19:37:22 +000023 */
margaretha35e1ca22023-11-16 22:00:01 +010024 public static Form getFormParameters (
25 ContainerRequestContext requestContext) {
abcpro13df666b2022-11-07 19:37:22 +000026 if (requestContext instanceof ContainerRequest) {
27 return getFormParameters((ContainerRequest) requestContext);
28 }
29 return new Form();
30 }
31
32 private static Form getFormParameters (ContainerRequest request) {
margaretha35e1ca22023-11-16 22:00:01 +010033 if (MediaTypes.typeEqual(MediaType.APPLICATION_FORM_URLENCODED_TYPE,
34 request.getMediaType())) {
abcpro13df666b2022-11-07 19:37:22 +000035 request.bufferEntity();
36 Form form = request.readEntity(Form.class);
37 return (form == null ? new Form() : form);
margaretha35e1ca22023-11-16 22:00:01 +010038 }
39 else {
abcpro13df666b2022-11-07 19:37:22 +000040 return new Form();
41 }
42 }
43}