Skip to content

Commit 00e989f

Browse files
committed
fix: replace agrold.baseurl by HOST header
Signed-off-by: WoodenMaiden <yann.pomie@laposte.net>
1 parent f5efeac commit 00e989f

File tree

7 files changed

+29
-46
lines changed

7 files changed

+29
-46
lines changed

agrold-javaweb/README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -25,9 +25,8 @@ Le déploiement de l'application se fait premièrement avec des propriétés Jav
2525

2626
| Name | Description | Valeur par défaut |
2727
| :------------------------: | :-----------------------------------------------------------------------------------------------------------------------------------------------: | :---------------------------: |
28-
| `agrold.name` | Nom de l'archive et du contexte (le nom apparait après `agrold.baseurl` , par exemple si on met la valeur à `aldp` on a `https://someurl/agrold`) | `aldp` |
28+
| `agrold.name` | Nom de l'archive et du contexte `aldp` on a `https://<un domaine>/agrold`) | `aldp` |
2929
| `agrold.description` | Description affichée dans Tomcat | :x: |
30-
| `agrold.baseurl` | L'URL de base de l'app | `http://localhost:8080/` |
3130
| `agrold.sparql_endpoint` | Url de l'endpoint SPARQL | `http://sparql.southgreen.fr` |
3231
| `agrold.db_connection_url` | Url de la base de données ex: `[host]:[port]/[db]?[opt]` | :x: (requis) |
3332
| `agrold.db_username` | Utilisateur de la base de données | :x: (requis) |
@@ -90,7 +89,7 @@ docker login 10.9.2.21:8080 -u <user> -p <password>
9089
docker pull 10.9.2.21:8080
9190

9291
# Lancer le conteneur
93-
docker run -p 8080:8080 -e CATALINA_OPTS="-Dagrold.db_connection_url=someurl -Dagrold.db_username=usr -Dagrold.db_password=pwd -Dagrold.baseurl=http://localhost:8080/ -Dagrold.sparql_endpoint=a" <tag>
92+
docker run -p 8080:8080 -e CATALINA_OPTS="-Dagrold.db_connection_url=someurl -Dagrold.db_username=usr -Dagrold.db_password=pwd -Dagrold.sparql_endpoint=a" <tag>
9493
```
9594

9695
> [!NOTE]

agrold-javaweb/charts/agrold-javaweb/templates/_helpers.tpl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -97,9 +97,9 @@ Return the proper CATALINA_OPTS value
9797
{{- define "tomcat.catalinaOpts" -}}
9898
{{- $agrold := include "tomcat.flattenIntoSysProperties" .Values.agroldProperties -}}
9999
{{- if .Values.metrics.jmx.enabled -}}
100-
{{- default "" (cat .Values.catalinaOpts .Values.metrics.jmx.catalinaOpts (printf "-Dagrold.baseurl=%q" (ternary (print "https://" .Values.ingress.hostname) (print "http://" .Values.ingress.hostname) .Values.ingress.tls)) $agrold) | trim -}}
100+
{{- default "" (cat .Values.catalinaOpts .Values.metrics.jmx.catalinaOpts $agrold) | trim -}}
101101
{{- else -}}
102-
{{- default "" (cat .Values.catalinaOpts (printf "-Dagrold.baseurl=%q" (ternary (print "https://" .Values.ingress.hostname) (print "http://" .Values.ingress.hostname) .Values.ingress.tls)) $agrold) | trim -}}
102+
{{- default "" (cat .Values.catalinaOpts $agrold) | trim -}}
103103
{{- end -}}
104104
{{- end -}}
105105

agrold-javaweb/charts/agrold-javaweb/values.yaml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@
88
##
99

1010
agroldProperties:
11-
# The property below is set to ingress.hostname, set this value to override it
12-
# baseurl: "http://agrold.org"
1311
db_connection_url: someurl
1412
db_username: user
1513
db_password: password

agrold-javaweb/src/main/java/agrold/webservices/API.java

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@
3131
import javax.ws.rs.PathParam;
3232
import javax.ws.rs.Produces;
3333
import javax.ws.rs.QueryParam;
34+
import javax.ws.rs.HeaderParam;
3435
import javax.ws.rs.WebApplicationException;
3536
import javax.ws.rs.client.Client;
3637
import javax.ws.rs.client.ClientBuilder;
@@ -95,15 +96,15 @@ String inputStream2String(InputStream incomingData){
9596
@GET
9697
// @Consumes(MediaType.APPLICATION_JSON) // for "in body parameters"
9798
@Path("/customizable/{serviceLocalName}")
98-
public Response genericGet(@PathParam("serviceLocalName") String serviceLocalName, @Context UriInfo uriInfo, @Context HttpHeaders headers) throws IOException {
99+
public Response genericGet(@PathParam("serviceLocalName") String serviceLocalName, @Context UriInfo uriInfo, @Context HttpHeaders headers) throws IOException {
99100
List<MediaType> mediaTypes = headers.getAcceptableMediaTypes();
100101
MediaType reponseMediaType = mediaTypes.get(0);
101102
if (reponseMediaType == null) {
102103
return Response.serverError()
103104
.entity("[AgroLD Web Services] - Format Error: The requested resource is not available in the format \"" + mediaTypes.get(0) + "\"")
104105
.build();
105106
}
106-
String content = CustomizableServicesManager.queryCustomizableService(serviceLocalName, uriInfo.getQueryParameters(), "get", reponseMediaType);
107+
String content = CustomizableServicesManager.queryCustomizableService(serviceLocalName, uriInfo.getQueryParameters(), "get", reponseMediaType, headers.HOST);
107108
return buildResponse(content, reponseMediaType.toString());
108109
}
109110

@@ -122,8 +123,8 @@ public Response genericGet(@PathParam("serviceLocalName") String serviceLocalNam
122123
// generic web service for modifiables ones
123124
@GET
124125
@Path("/webservices")
125-
public Response getAPISpecification() throws IOException {
126-
String content = CustomizableServicesManager.readAPISpecification(Utils.AGROLDAPIJSONURL);
126+
public Response getAPISpecification(@HeaderParam("Host") String host) throws IOException {
127+
String content = CustomizableServicesManager.readAPISpecification(Utils.AGROLDAPIJSONURL, host);
127128
return buildResponse(content, Utils.JSON);
128129
}
129130

@@ -132,24 +133,24 @@ public Response getAPISpecification() throws IOException {
132133
@DELETE
133134
@Produces(MediaType.TEXT_PLAIN)
134135
@Path("/webservices")
135-
public Response deleteService(@QueryParam("name") String name, @QueryParam("httpMethod") String httpMethod) throws IOException {
136-
String content = CustomizableServicesManager.deleteService(name, httpMethod);
136+
public Response deleteService(@QueryParam("name") String name, @QueryParam("httpMethod") String httpMethod, @HeaderParam("Host") String host) throws IOException {
137+
String content = CustomizableServicesManager.deleteService(name, httpMethod, host);
137138
return buildResponse(content, MediaType.TEXT_PLAIN);
138139
}
139140

140141
@RolesAllowed("ADMIN")
141142
@PUT
142143
@Path("/webservices")
143-
public Response addService(@QueryParam("name") String name, @QueryParam("httpMethod") String httpMethod, InputStream specificationDataStream) throws IOException {
144-
String content = CustomizableServicesManager.addService(name, httpMethod, inputStream2String(specificationDataStream));
144+
public Response addService(@QueryParam("name") String name, @QueryParam("httpMethod") String httpMethod, InputStream specificationDataStream, @HeaderParam("Host") String host) throws IOException {
145+
String content = CustomizableServicesManager.addService(name, httpMethod, inputStream2String(specificationDataStream), host);
145146
return buildResponse(content, MediaType.TEXT_PLAIN);
146147
}
147148

148149
@RolesAllowed("ADMIN")
149150
@POST
150151
@Path("/webservices")
151-
public Response updateService(@QueryParam("name") String name, @QueryParam("httpMethod") String httpMethod, InputStream specificationDataStream) throws IOException {
152-
String content = CustomizableServicesManager.updateService(name, httpMethod, inputStream2String(specificationDataStream));
152+
public Response updateService(@QueryParam("name") String name, @QueryParam("httpMethod") String httpMethod, InputStream specificationDataStream, @HeaderParam("Host") String host) throws IOException {
153+
String content = CustomizableServicesManager.updateService(name, httpMethod, inputStream2String(specificationDataStream), host);
153154
return buildResponse(content, MediaType.TEXT_PLAIN);
154155
}
155156

agrold-javaweb/src/main/java/agrold/webservices/dao/CustomizableServicesManager.java

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ public class CustomizableServicesManager {
2828
/*public static String validateName(String name) {
2929
3030
}*/
31-
public static String readAPISpecification(String apiSpecificationPath) {
31+
public static String readAPISpecification(String apiSpecificationPath, String host) {
3232
//JSON parser object to parse read file
3333
JSONObject jsonObj = null;
3434
try (FileReader reader = new FileReader(apiSpecificationPath)) {
@@ -40,9 +40,9 @@ public static String readAPISpecification(String apiSpecificationPath) {
4040
JSONObject tagObj = new JSONObject();
4141

4242
// Host only has hostname and port, meaning no protocol
43-
String strippedUrl = System.getProperty("agrold.baseurl", "localhost:8080").replaceAll("http://", "").replaceAll("https://", "");
43+
String strippedUrl = host.replaceAll("http://", "").replaceAll("https://", "");
4444

45-
// here we will fill the host property defined by the system property agrold.baseurl and agrold.name
45+
// here we will fill the host property defined by the hosts from incoming requests & the system property agrold.name
4646
jsonObj.put("host", strippedUrl);
4747

4848
jsonObj.put("basePath", "/" + System.getProperty("agrold.name", "aldp") + "/api");
@@ -77,8 +77,8 @@ public static void writeAPISpecification(JSONObject apiSpecification, String api
7777
* @param webServiceSpecification : a JSON object with the method as key
7878
* @return the confirmation message
7979
*/
80-
public static String addService(String name, String httpMethod, String webServiceSpecification) {
81-
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL));
80+
public static String addService(String name, String httpMethod, String webServiceSpecification, String host) {
81+
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL, host));
8282
JSONObject newServiceSpec = new JSONObject();
8383
String sparqlPattern = "get";
8484
newServiceSpec.put(httpMethod, new JSONObject(webServiceSpecification));
@@ -90,8 +90,8 @@ public static String addService(String name, String httpMethod, String webServic
9090
return "The service /api/customizable/" + name + " has been created!";
9191
}
9292

93-
public static String deleteService(String name, String httpMethod) {
94-
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL));
93+
public static String deleteService(String name, String httpMethod, String host) {
94+
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL, host));
9595
String servicePath = PATH_FIXED_PART + name.replaceAll("\\s+", "");
9696
if (apiSpecification.getJSONObject("paths").has(servicePath)) {
9797
apiSpecification.getJSONObject("paths").remove(servicePath);
@@ -101,8 +101,8 @@ public static String deleteService(String name, String httpMethod) {
101101
return "The service /api/customizable/" + name + " has been deleted!";
102102
}
103103

104-
public static String updateService(String name, String httpMethod, String webServiceSpecification) {
105-
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL));
104+
public static String updateService(String name, String httpMethod, String webServiceSpecification, String host) {
105+
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL, host));
106106
JSONObject serviceNewSpec = new JSONObject(webServiceSpecification);
107107
//System.out.println(newServiceSpec.toString());
108108
String servicePath = PATH_FIXED_PART + name.replaceAll("\\s+", "");
@@ -120,8 +120,8 @@ public static String updateService(String name, String httpMethod, String webSer
120120
//System.out.println(apiSpecification.toString());
121121
}
122122

123-
public static String queryCustomizableService(String serviceLocalName, MultivaluedMap<String, String> queryParams, String httpMethod, MediaType reponseMediaType) throws IOException {
124-
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL));
123+
public static String queryCustomizableService(String serviceLocalName, MultivaluedMap<String, String> queryParams, String httpMethod, MediaType reponseMediaType, String host) throws IOException {
124+
JSONObject apiSpecification = new JSONObject(readAPISpecification(Utils.AGROLDAPIJSONURL, host));
125125
String servicePath = PATH_FIXED_PART + serviceLocalName.replaceAll("\\s+", "");
126126
JSONObject serviceCurrentSpec = apiSpecification.getJSONObject("paths").getJSONObject(servicePath);
127127
if (serviceCurrentSpec != null) {

agrold-javaweb/src/main/webapp/config/config.js

Lines changed: 3 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,4 @@
1-
2-
//Fuses all parameters into a url
3-
function buildUrl(...url) {
4-
return url.reduce((acc, val) => {
5-
if (acc.endsWith('/') && val.startsWith('/'))
6-
return acc + val.substring(1);
7-
else if (acc.endsWith('/') || val.startsWith('/'))
8-
return acc + val;
9-
else return acc + '/' + val;
10-
})
11-
}
12-
13-
//WEBAPPURL="http://agrold.southgreen.fr/agrold";
14-
CONTEXT= system_context ?? "aldp";
15-
WEBAPPURL= buildUrl(( system_baseurl ?? "http://127.0.0.1:8080/" ), CONTEXT);
1+
WEBAPPURL= "/" + (system_context ?? "aldp")
162

173
// still constant in :
184
// /Users/zadmin/agrold/git/AgroLD/agrold/src/main/webapp/bc_sparqleditor.jsp
@@ -26,11 +12,11 @@ SPARQLENDPOINTURL= system_sparqlendpoint ?? "http://sparql.southgreen.fr";
2612
// WEB-INF/Account/General/AJAX/Admin/_USER_FULL_DATA_LOADER.jsp
2713
// WEB-INF/Account/General/AJAX/History/QuickSearchList.jsp
2814
// /src/main/webapp/quicksearch.jsp
29-
FACETEDURL="http://agrold.southgreen.fr/fct";
15+
FACETEDURL="http://agrold.southgreen.fr/fct"; //TODO
3016

3117
//AGROLDAPIJSONURL=WEBAPPURL + "/config/agrold-api.json";
3218
//AGROLDAPIJSONURL=WEBAPPURL + "/api/agrold-api-specification.json";
33-
AGROLDAPIJSONURL= buildUrl(WEBAPPURL, "api/webservices");
19+
AGROLDAPIJSONURL= WEBAPPURL + "/api/webservices";
3420

3521

3622
// Advanced search default format to query the web services

agrold-javaweb/src/main/webapp/includes.jsp

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,6 @@
3737
}
3838
3939
const system_context = parseJsp('<%= System.getProperty("agrold.name", "aldp") %>')
40-
const system_baseurl = parseJsp('<%= System.getProperty("agrold.baseurl", "http://localhost:8080") %>')
4140
const system_sparqlendpoint = parseJsp('<%= System.getProperty("agrold.sparql_endpoint", "http://sparql.southgreen.fr") %>')
4241
</script>
4342
<script type="text/javascript" src="config/config.js"></script>

0 commit comments

Comments
 (0)