Skip to content

Commit

Permalink
bugfix for external servers
Browse files Browse the repository at this point in the history
  • Loading branch information
valentin-gauthier-geosiris committed Nov 25, 2024
1 parent 6063a7b commit caa163a
Show file tree
Hide file tree
Showing 5 changed files with 82 additions and 28 deletions.
33 changes: 22 additions & 11 deletions src/main/java/com/geosiris/webstudio/servlet/etp/ETPConnexion.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,13 @@
*/
package com.geosiris.webstudio.servlet.etp;

import Energistics.Etp.v12.Datatypes.Object.ContextScopeKind;
import com.geosiris.etp.utils.ETPHelper;
import com.geosiris.etp.websocket.ETPClient;
import com.geosiris.webstudio.utils.ETPRequestUtils;
import com.geosiris.webstudio.utils.ETPUtils;
import com.geosiris.webstudio.utils.SessionUtility;
import com.google.gson.Gson;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.WebServlet;
import jakarta.servlet.http.HttpServlet;
Expand All @@ -37,14 +41,15 @@
import java.io.PrintWriter;
import java.util.HashMap;
import java.util.Locale;
import java.util.Map;

/**
* Servlet implementation class ETPConnexion
*/
@WebServlet("/ETPConnexion")
public class ETPConnexion extends HttpServlet {
private static final long serialVersionUID = 1L;
public static Logger logger = LogManager.getLogger(ETPConnexion.class);
public static Logger logger = LogManager.getLogger(ETPConnexion.class);

/**
* @see HttpServlet#HttpServlet()
Expand Down Expand Up @@ -73,10 +78,10 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
answer = "unkown request";
}
PrintWriter out = response.getWriter();
response.setContentType("application/text");
response.setCharacterEncoding("UTF-8");
out.write(answer);
out.flush();
response.setContentType("application/text");
response.setCharacterEncoding("UTF-8");
out.write(answer);
out.flush();
}

/**
Expand All @@ -91,6 +96,7 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)

String userName = null;
String password = null;
Map<String, String> headers = new HashMap<>();
boolean askConnection = true;
HttpURI host_uri = null;

Expand Down Expand Up @@ -123,6 +129,9 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
if (value.compareToIgnoreCase("disconnect") == 0) {
askConnection = false;
}
}else if ("etp-server-headers".compareToIgnoreCase(item.getFieldName()) == 0) {
Gson gson = new Gson();
headers.putAll(gson.fromJson(value, HashMap.class));
}
}
}
Expand All @@ -146,20 +155,22 @@ protected void doPost(HttpServletRequest request, HttpServletResponse response)
if (value.compareToIgnoreCase("disconnect") == 0) {
askConnection = false;
}
}else if ("etp-server-headers".compareToIgnoreCase(k) == 0) {
Gson gson = new Gson();
headers.putAll(gson.fromJson(value, HashMap.class));
}
}
}

logger.info("#ETP : host " + host_uri);
logger.info(request.getSession(false));
ETPClient client = ETPUtils.establishConnexion(request.getSession(false), host_uri, userName, password, null, new HashMap<>(), askConnection);
ETPClient client = ETPUtils.establishConnexion(request.getSession(false), host_uri, userName, password, null, headers, askConnection);

PrintWriter out = response.getWriter();
response.setContentType("application/text");
response.setCharacterEncoding("UTF-8");
out.write(String.valueOf(client != null));
out.flush();
response.setContentType("application/text");
response.setCharacterEncoding("UTF-8");
out.write(String.valueOf(client != null));
out.flush();
}


}
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,9 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)
dataspacesNames.add(searchDataspaceNameFromUri(ds.getUri()+""));
}
}
if(dataspacesNames.isEmpty()){
dataspacesNames.add("eml:///");
}

PrintWriter out = response.getWriter();
response.setContentType("application/json");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@
import jakarta.servlet.http.HttpSession;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.eclipse.jetty.http.HttpURI;

import javax.xml.datatype.XMLGregorianCalendar;
import java.io.IOException;
Expand Down Expand Up @@ -119,6 +120,9 @@ private String manageETPRequest(Map<String, List<String>> parameterMap, HttpSess
if(parameterMap.containsKey("dataspace")){
dataspace = parameterMap.get("dataspace").get(0);
}
if(dataspace!=null && (dataspace.isEmpty() || dataspace.compareToIgnoreCase("eml:///") == 0)){
dataspace = null;
}

boolean ask_aknowledge = parameterMap.containsKey("ask_aknowledge");

Expand All @@ -133,7 +137,7 @@ private String manageETPRequest(Map<String, List<String>> parameterMap, HttpSess
try {
StringBuilder req_result = new StringBuilder();
if (request.toLowerCase().startsWith("getresource")) {

logger.info("Getting resources : " + new ETPUri(dataspace));
GetResources getRess = ETPDefaultProtocolBuilder.buildGetResources(new ETPUri(dataspace).toString(),
ContextScopeKind.self, new ArrayList<>());

Expand Down Expand Up @@ -406,4 +410,5 @@ private Pair<Map<CharSequence, DataObject>, String> getDataObjectMaptoURI(List<S
}
return new Pair<>(mapResult, logs.toString());
}

}
56 changes: 41 additions & 15 deletions src/main/java/com/geosiris/webstudio/utils/ETPUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@
*/
package com.geosiris.webstudio.utils;

import Energistics.Etp.v12.Datatypes.DataValue;
import Energistics.Etp.v12.Datatypes.*;
import Energistics.Etp.v12.Datatypes.Object.*;
import Energistics.Etp.v12.Datatypes.ServerCapabilities;
import Energistics.Etp.v12.Protocol.Discovery.GetResources;
import Energistics.Etp.v12.Protocol.Discovery.GetResourcesResponse;
import Energistics.Etp.v12.Protocol.Store.PutDataObjects;
Expand Down Expand Up @@ -138,16 +137,16 @@ public static ETPClient establishConnexion(
Boolean askConnection
) {
Map<String, String> parsedHeaders = new HashMap<>();
try{
Gson gson = new Gson();
parsedHeaders = gson.fromJson(headers, HashMap.class);
}catch (Exception ignore){}
try{
Gson gson = new Gson();
parsedHeaders = gson.fromJson(headers, HashMap.class);
}catch (Exception ignore){}
if(parsedHeaders == null){
parsedHeaders = new HashMap<>();
}
for(Map.Entry<String, String> he: parsedHeaders.entrySet()){
logger.info("Headers " + he.getKey() + " " + he.getValue());
}
for(Map.Entry<String, String> he: parsedHeaders.entrySet()){
logger.info("Headers " + he.getKey() + " " + he.getValue());
}
return establishConnexion(session, host, userName, password, token, parsedHeaders, askConnection);
}

Expand Down Expand Up @@ -302,6 +301,19 @@ public static String getDataObjectFromUuid(HttpSession session, String dataspace
return result;
}

public static List<SupportedProtocol> computeSupportedProtocols(Map<CommunicationProtocol, ProtocolHandler> protocolHandlers){
List<SupportedProtocol> supported = new ArrayList<>();
for(Map.Entry<CommunicationProtocol, ProtocolHandler> ph: protocolHandlers.entrySet()){
supported.add(new SupportedProtocol(
ph.getKey().id,
Version.newBuilder().setMajor(1).setMinor(2).build(), // TODO: change version when etp changes
"store",
new HashMap<>()
));
}

return supported;
}

private static ETPClient establishConnexionForClient(
HttpSession session,
Expand All @@ -318,27 +330,41 @@ private static ETPClient establishConnexionForClient(
// mapCaps.put("MaxWebSocketMessagePayloadSize", DataValue.newBuilder().setItem(40000).build());
ServerCapabilities caps = new ServerCapabilities();
caps.setEndpointCapabilities(mapCaps);

// List<SupportedDataObject> sdo = new ArrayList<>();
// Map<CharSequence, DataValue> doc_eml = new HashMap<>();
// doc_eml.put("SupportsDelete", DataValue.newBuilder().setItem(true).build());
// doc_eml.put("SupportsPut", DataValue.newBuilder().setItem(true).build());
// doc_eml.put("SupportsGet", DataValue.newBuilder().setItem(true).build());
// sdo.add(SupportedDataObject.newBuilder().setQualifiedType("eml20.*").setDataObjectCapabilities(doc_eml).build());
// caps.setSupportedDataObjects(sdo);

Map<CommunicationProtocol, ProtocolHandler> protocolHandlers = new HashMap<>();
if(useDefaultHandler){
protocolHandlers.put(CoreHandler_WebStudio.protocol, new CoreHandler_WebStudio());
protocolHandlers.put(StoreHandler.protocol, new StoreHandler());
protocolHandlers.put(DataspaceHandler.protocol, new DataspaceHandler());
protocolHandlers.put(DiscoveryHandler.protocol, new DiscoveryHandler());
protocolHandlers.put(StoreHandler.protocol, new StoreHandler());
if(host.getHost().contains("geosiris"))
protocolHandlers.put(DataspaceHandler.protocol, new DataspaceHandler());
protocolHandlers.put(DataArrayHandler.protocol, new DataArrayHandler_WebStudio());

}else{
protocolHandlers.put(CoreHandler_WebStudio.protocol, new CoreHandler_WebStudio());
protocolHandlers.put(StoreHandler_WebStudio.protocol, new StoreHandler_WebStudio(session));
protocolHandlers.put(DataspaceHandler_WebStudio.protocol, new DataspaceHandler_WebStudio(session));
protocolHandlers.put(DiscoveryHandler_WebStudio.protocol, new DiscoveryHandler_WebStudio());
protocolHandlers.put(StoreHandler_WebStudio.protocol, new StoreHandler_WebStudio(session));
if(host.getHost().contains("geosiris"))
protocolHandlers.put(DataspaceHandler_WebStudio.protocol, new DataspaceHandler_WebStudio(session));
protocolHandlers.put(DataArrayHandler.protocol, new DataArrayHandler_WebStudio());
}

caps.setSupportedProtocols(computeSupportedProtocols(protocolHandlers)); // TODO : remove when etpproto-java is updated

ETPConnection etpConnection = new ETPConnection(ConnectionType.CLIENT, caps, clientInfo, protocolHandlers);

if(token != null && !token.trim().isEmpty()){
return ETPClient.getInstanceWithAuth_Token(host, etpConnection, 5000, token, headers);
return ETPClient.getInstanceWithAuth_Token(host, etpConnection, 5000, token, 1 << 22, headers);
}else {
return ETPClient.getInstanceWithAuth_Basic(host, etpConnection, 5000, userName, password, headers);
return ETPClient.getInstanceWithAuth_Basic(host, etpConnection, 5000, userName, password, 1 << 22, headers);
}
}

Expand Down
11 changes: 10 additions & 1 deletion src/main/webapp/ressources/script/modules/etp/etp_connection.js
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,14 @@ export function geosiris_createETP_connector_form(fun_isConnected, fun_isDisconn

form.appendChild(inputGroup);

const headers = document.createElement("textarea");
headers.classList.add('form-control');
headers.setAttribute('name', 'etp-server-headers');
headers.setAttribute('row', '6');
headers.value = '{\n\t"data-partition-id": "osdu"\n}';
form.appendChild(headers);


const connectButton = document.createElement('input');
connectButton.setAttribute('type', 'button');
connectButton.setAttribute('name', 'request-type');
Expand Down Expand Up @@ -217,7 +225,6 @@ export function create_dataspace_input(callback_func, inputs_classes_to_update){
}
});


selectDataspace.update = function (dataspacesNamesArray){
var selectedValue = null;
// keep previous selected value during updates
Expand All @@ -228,13 +235,15 @@ export function create_dataspace_input(callback_func, inputs_classes_to_update){
while(selectDataspace.firstChild){
selectDataspace.firstChild.remove();
}

if(dataspacesNamesArray.length > 0){
var option = document.createElement("option");
option.value = "";
option.appendChild(document.createTextNode("*default-dataspace*"));
selectDataspace.appendChild(option);
}

dataspacesNamesArray = dataspacesNamesArray.filter((ds) => ds.length > 0 && ds != "eml:///");
[].forEach.call(dataspacesNamesArray, (elt, idx) =>{
var option = document.createElement("option");
option.value = elt;
Expand Down

0 comments on commit caa163a

Please sign in to comment.