1 package common.http;
2
3 import javax.inject.Inject;
4 import javax.servlet.http.HttpServletRequest;
5 import java.time.LocalDate;
6 import java.time.format.DateTimeParseException;
7 import java.util.Optional;
8 import java.util.OptionalDouble;
9 import java.util.OptionalInt;
10 import java.util.OptionalLong;
11
12 /**
13 * Classe di utilità che permette di accedere a parametri di un {@link HttpServletRequest} nel tipo desiderato
14 */
15 public class ParameterConverter {
16 private final HttpServletRequest request;
17
18 /**
19 * Costruttore unico dell'oggetto.
20 * @param request L'oggetto {@link HttpServletRequest} rappresentante la richiesta in corso
21 */
22 @Inject
23 public ParameterConverter(HttpServletRequest request){
24 this.request = request;
25 }
26
27
28 /**
29 * Restituisce il valore di un parametro di richiesta come intero
30 * @param parameterKey Una stringa che specifica il nome del parametro
31 * @return Un {@link OptionalInt} contenente l'intero; {@link OptionalInt#empty()} se il parametro
32 * non è stato trovato o se il parsing ha avuto esito negativo
33 * @see Integer#parseInt(String)
34 */
35 public OptionalInt getIntParameter(String parameterKey){
36 String parameter = request.getParameter(parameterKey);
37 if(parameter == null || parameter.isBlank())
38 return OptionalInt.empty();
39 try{
40 return OptionalInt.of(Integer.parseInt(parameter.trim()));
41 } catch (NumberFormatException e){
42 return OptionalInt.empty();
43 }
44 }
45
46 /**
47 * Restituisce il valore di un parametro di richiesta come double
48 * @param parameterKey Una stringa che specifica il nome del parametro
49 * @return Un {@link OptionalDouble} contenente il double; {@link OptionalDouble#empty()} se il parametro
50 * non è stato trovato o se il parsing ha avuto esito negativo
51 * @see Double#parseDouble(String)
52 */
53 public OptionalDouble getDoubleParameter(String parameterKey){
54 String parameter = request.getParameter(parameterKey);
55 if(parameter == null || parameter.isBlank())
56 return OptionalDouble.empty();
57 try{
58 return OptionalDouble.of(Double.parseDouble(parameter.trim()));
59 } catch (NumberFormatException e){
60 return OptionalDouble.empty();
61 }
62 }
63
64 /**
65 * Restituisce il valore di un parametro di richiesta come long
66 * @param parameterKey Una stringa che specifica il nome del parametro
67 * @return Un {@link OptionalLong} contenente il long; {@link OptionalLong#empty()} se il parametro
68 * non è stato trovato o se il parsing ha avuto esito negativo
69 * @see Long#parseLong(String)
70 */
71 public OptionalLong getLongParameter(String parameterKey) {
72 String parameter = request.getParameter(parameterKey);
73 if (parameter == null || parameter.isBlank())
74 return OptionalLong.empty();
75 try {
76 return OptionalLong.of(Long.parseLong(parameter.trim()));
77 } catch (NumberFormatException e) {
78 return OptionalLong.empty();
79 }
80 }
81
82 /**
83 * Restituisce il valore di un parametro di richiesta come {@link LocalDate}. La conversione segue il formato "YYYY-mm-DD"
84 * @param parameterKey Una stringa che specifica il nome del parametro
85 * @return Un {@link Optional<LocalDate>} contenente l'oggetto; {@link Optional#empty()} se il parametro
86 * non è stato trovato o se il parsing ha avuto esito negativo
87 * @see LocalDate#parse(CharSequence)
88 */
89 public Optional<LocalDate> getDateParameter(String parameterKey){
90 String parameter = request.getParameter(parameterKey);
91 if (parameter == null || parameter.isBlank())
92 return Optional.empty();
93 try {
94 return Optional.of(LocalDate.parse(parameter.trim()));
95 } catch (DateTimeParseException e) {
96 return Optional.empty();
97 }
98 }
99 }