-
Notifications
You must be signed in to change notification settings - Fork 7
/
Copy pathPaysApi.java
280 lines (264 loc) · 13.1 KB
/
PaysApi.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
package fr.insee.rmes.api.geo;
import fr.insee.rmes.modeles.geo.territoire.AireDAttractionDesVilles2020;
import fr.insee.rmes.modeles.geo.territoires.PaysS;
import fr.insee.rmes.modeles.geo.territoire.Territoire;
import fr.insee.rmes.modeles.geo.territoires.Territoires;
import fr.insee.rmes.modeles.utils.Date;
import jakarta.ws.rs.*;
import jakarta.ws.rs.core.HttpHeaders;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.Response;
import jakarta.ws.rs.core.Response.Status;
import fr.insee.rmes.modeles.geo.territoire.Pays;
import fr.insee.rmes.modeles.utils.Header;
import fr.insee.rmes.queries.geo.GeoQueries;
import fr.insee.rmes.utils.Constants;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.media.Content;
import io.swagger.v3.oas.annotations.media.Schema;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.tags.Tag;
@Path(ConstGeoApi.PATH_GEO)
@Tag(name = ConstGeoApi.TAG_NAME, description = ConstGeoApi.TAG_DESCRIPTION)
public class PaysApi extends AbstractGeoApi {
private static final String CODE_PATTERN = "/{code: " + ConstGeoApi.PATTERN_PAYS + "}";
private static final String LITTERAL_ID_OPERATION = "getcogpays";
private static final String LITTERAL_OPERATION_SUMMARY =
"Informations sur un pays identifié par son code (cinq chiffres, les deux premiers étant 99)";
private static final String LITTERAL_RESPONSE_DESCRIPTION = "Pays";
private static final String LITTERAL_PARAMETER_TYPE_DESCRIPTION = "Filtre sur le type de territoire renvoyé.";
private static final String LITTERAL_DATE_EXAMPLE = "2000-01-01";
private static final String LITTERAL_CODE_HISTORY_EXAMPLE = "99309";
@Path(ConstGeoApi.PATH_PAYS + CODE_PATTERN)
@GET
@Produces({
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
})
@Operation(
operationId = LITTERAL_ID_OPERATION,
summary = LITTERAL_OPERATION_SUMMARY,
responses = {
@ApiResponse(
content = @Content(schema = @Schema(implementation = Pays.class)),
description = LITTERAL_RESPONSE_DESCRIPTION)
})
public Response getByCode(
@Parameter(
description = ConstGeoApi.PATTERN_PAYS_DESCRIPTION,
required = true,
schema = @Schema(
pattern = ConstGeoApi.PATTERN_PAYS,
type = Constants.TYPE_STRING, example="99132")) @PathParam(Constants.CODE) String code,
@Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header,
@Parameter(
description = "Filtre pour renvoyer le pays actif à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')",
required = false,
schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam(
value = Constants.PARAMETER_DATE) Date date) {
String dateString = null;
if (date !=null) {
dateString = date.getString();
}
if ( ! this.verifyParameterDateIsRightWithoutHistory(dateString)) {
return this.generateBadRequestResponse();
}
else {
return this
.generateResponseATerritoireByCode(
sparqlUtils
.executeSparqlQuery(
GeoQueries.getPaysByCodeAndDate(code, this.formatValidParameterDateIfIsNull(dateString))),
header,
new Pays(code));
}
}
@Path(ConstGeoApi.PATH_PAYS)
@GET
@Produces({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
@Operation(operationId = LITTERAL_ID_OPERATION, summary = "Informations sur tous les pays actifs à la date données. Par défaut c'est la date courante", responses = {
@ApiResponse(
content = @Content(schema = @Schema(implementation = PaysS.class)),
description = LITTERAL_RESPONSE_DESCRIPTION)
})
public Response getListe(
@Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header,
@Parameter(
description = "Filtre pour renvoyer les pays actifs à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')" + LITTERAL_PARAMETER_DATE_WITH_HISTORY,
required = false,
schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam(
value = Constants.PARAMETER_DATE) Date date) {
String dateString = null;
if (date !=null) {
dateString = date.getString();
}
if ( ! this.verifyParameterDateIsRightWithHistory(dateString)) {
return this.generateBadRequestResponse();
}
else {
return this
.generateResponseListOfTerritoire(
sparqlUtils
.executeSparqlQuery(GeoQueries.getListPays(this.formatValidParameterDateIfIsNull(dateString))),
header,
PaysS.class,
Pays.class);
}
}
@Path(ConstGeoApi.PATH_PAYS + CODE_PATTERN + ConstGeoApi.PATH_DESCENDANT)
@GET
@Produces({
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
})
@Operation(
operationId = LITTERAL_ID_OPERATION + ConstGeoApi.ID_OPERATION_DESCENDANTS,
summary = "Informations concernant les territoires inclus dans le pays",
responses = {
@ApiResponse(
content = @Content(schema = @Schema(type = ARRAY, implementation = Territoire.class)),
description = LITTERAL_RESPONSE_DESCRIPTION)
})
public Response getDescendants(
@Parameter(
description = ConstGeoApi.PATTERN_PAYS_DESCRIPTION,
required = true,
schema = @Schema(
pattern = ConstGeoApi.PATTERN_PAYS,
type = Constants.TYPE_STRING, example="99132")) @PathParam(Constants.CODE) String code,
@Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header,
@Parameter(
description = "Filtre pour renvoyer les territoires inclus dans le pays actif à la date donnée. Par défaut, c’est la date courante. (Format : 'AAAA-MM-JJ')",
required = false,
schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE)) @QueryParam(
value = Constants.PARAMETER_DATE) Date date,
@Parameter(
description = LITTERAL_PARAMETER_TYPE_DESCRIPTION,
required = false,
schema = @Schema(type = Constants.TYPE_STRING, example="Territoire")) @QueryParam(
value = Constants.PARAMETER_TYPE) String typeTerritoire) {
String dateString = null;
if (date !=null) {
dateString = date.getString();
}
if ( ! this.verifyParametersTypeAndDateAreValid(typeTerritoire, dateString)) {
return this.generateBadRequestResponse();
}
else {
return this
.generateResponseListOfTerritoire(
sparqlUtils
.executeSparqlQuery(
GeoQueries
.getDescendantsPays(
code,
this.formatValidParameterDateIfIsNull(dateString),
this.formatValidParametertypeTerritoireIfIsNull(typeTerritoire))),
header,
Territoires.class,
Territoire.class);
}
}
@Path(ConstGeoApi.PATH_PAYS + CODE_PATTERN + ConstGeoApi.PATH_SUIVANT)
@GET
@Produces({
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
})
@Operation(
operationId = LITTERAL_ID_OPERATION + ConstGeoApi.ID_OPERATION_SUIVANT,
summary = "Informations concernant les pays qui succèdent au pays",
responses = {
@ApiResponse(
content = @Content(schema = @Schema(implementation = Pays.class)),
description = LITTERAL_RESPONSE_DESCRIPTION)
})
public Response getSuivant(
@Parameter(
description = ConstGeoApi.PATTERN_PAYS_DESCRIPTION,
required = true,
schema = @Schema(
pattern = ConstGeoApi.PATTERN_PAYS,
type = Constants.TYPE_STRING, example="99121")) @PathParam(Constants.CODE) String code,
@Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header,
@Parameter(
description = "Filtre pour préciser le pays de départ. Par défaut, c’est la date courante qui est utilisée. (Format : 'AAAA-MM-JJ')",
required = false,
schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE, example="1950-01-01")) @QueryParam(
value = Constants.PARAMETER_DATE) Date date) {
String dateString = null;
if (date != null){
dateString = date.getString();
}
if ( ! this.verifyParameterDateIsRightWithoutHistory(dateString)) {
return this.generateBadRequestResponse();
}
else {
return this
.generateResponseListOfTerritoire(
sparqlUtils
.executeSparqlQuery(
GeoQueries.getNextPays(code, this.formatValidParameterDateIfIsNull(dateString))),
header,
PaysS.class,
Pays.class);
}
}
@Path(ConstGeoApi.PATH_PAYS + CODE_PATTERN + ConstGeoApi.PATH_PRECEDENT)
@GET
@Produces({
MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML
})
@Operation(
operationId = LITTERAL_ID_OPERATION + ConstGeoApi.ID_OPERATION_PRECEDENT,
summary = "Informations concernant les pays qui précèdent le pays",
responses = {
@ApiResponse(
content = @Content(schema = @Schema(implementation = Pays.class)),
description = LITTERAL_RESPONSE_DESCRIPTION)
})
public Response getPrecedent(
@Parameter(
description = ConstGeoApi.PATTERN_PAYS_DESCRIPTION,
required = true,
schema = @Schema(
pattern = ConstGeoApi.PATTERN_PAYS,
type = Constants.TYPE_STRING, example=LITTERAL_CODE_HISTORY_EXAMPLE)) @PathParam(Constants.CODE) String code,
@Parameter(hidden = true) @HeaderParam(HttpHeaders.ACCEPT) String header,
@Parameter(
description = "Filtre pour préciser le pays de départ. Par défaut, c’est la date courante qui est utilisée. (Format : 'AAAA-MM-JJ')",
required = false,
schema = @Schema(type = Constants.TYPE_STRING, format = Constants.FORMAT_DATE,example = "1963-01-01")) @QueryParam(
value = Constants.PARAMETER_DATE) Date date) {
String dateString = null;
if (date != null){
dateString = date.getString();
}
if ( ! this.verifyParameterDateIsRightWithoutHistory(dateString)) {
return this.generateBadRequestResponse();
}
else {
return this
.generateResponseListOfTerritoire(
sparqlUtils
.executeSparqlQuery(
GeoQueries.getPreviousPays(code, this.formatValidParameterDateIfIsNull(dateString))),
header,
PaysS.class,
Pays.class);
}
}
private boolean isValidCode(String code) {
return code != null && code.matches(ConstGeoApi.PATTERN_PAYS);
}
private String escapeSparql(String input) {
return input.replace("\"", "\\\"").replace("<", "\\u003C").replace(">", "\\u003E");
}
private String sanitizeAndValidateHeader(String header) {
if (header == null || header.isEmpty()) {
return null;
}
if (header.equals(MediaType.APPLICATION_JSON) || header.equals(MediaType.APPLICATION_XML)) {
return header;
}
return null;
}
}