Skip to content

Commit

Permalink
Updates sep 24 (#20)
Browse files Browse the repository at this point in the history
* repackaging + adding REST api endpoints

* adding verification for object version and an About button

* bugfix: multiple DOR occurence in object creation list + better PropertyView + buttons to download xml/json files

* bugfix at object creation + raw xml editor + bugfiw in console event handler

* using bootstrap 5
  • Loading branch information
valentin-gauthier-geosiris authored Sep 11, 2024
1 parent b2f1c52 commit 6558b86
Show file tree
Hide file tree
Showing 85 changed files with 3,504 additions and 1,547 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ The WebStudio is a web application that allows to manipulate energyml file (such
- Bugfix
- About button added in the interface to link to github
- Verifications:
- Verification on objectVerion in DOR
- Verification on objectVersion in DOR

## License

Expand Down
1 change: 1 addition & 0 deletions docker/webstudio-local.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM tomcat:10.1-jdk11-openjdk-slim as base
# FROM tomcat:10.1-jdk21-openjdk-slim as base

LABEL maintainer="valentin.gauthier@geosiris.com"

Expand Down
1 change: 1 addition & 0 deletions docker/webstudio.dockerfile
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
FROM tomcat:10.1-jdk11-openjdk-slim as base
# FROM tomcat:10.1-jdk21-openjdk-slim as base

LABEL maintainer="valentin.gauthier@geosiris.com"

Expand Down
9 changes: 6 additions & 3 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<groupId>com.geosiris</groupId>
<artifactId>webstudio</artifactId>
<name>${project.groupId}:${project.artifactId}</name>
<version>1.0.15</version>
<version>1.0.16</version>
<organization>
<name>Geosiris</name>
<url>http://www.geosiris.com</url>
Expand Down Expand Up @@ -40,6 +40,9 @@
<maven.compiler.source>11</maven.compiler.source>
<maven.compiler.target>11</maven.compiler.target>
<java.version>11</java.version>
<!-- <maven.compiler.source>20</maven.compiler.source>-->
<!-- <maven.compiler.target>20</maven.compiler.target>-->
<!-- <java.version>20</java.version>-->
<webapp.dir>${project.basedir}/src/main/webapp</webapp.dir>
<tomcat.version>10.1.0-M15</tomcat.version>
<failOnMissingWebXml>true</failOnMissingWebXml>
Expand All @@ -63,12 +66,12 @@
<dependency>
<groupId>com.geosiris</groupId>
<artifactId>energyml-utils</artifactId>
<version>1.0.9</version>
<version>1.0.12</version>
</dependency>
<dependency>
<groupId>com.geosiris</groupId>
<artifactId>etpproto-java</artifactId>
<version>1.0.1</version>
<version>1.0.5</version>
</dependency>
<!-- Energyml deps -->
<dependency>
Expand Down
204 changes: 204 additions & 0 deletions src/main/java/com/geosiris/webstudio/etp/ETPWorkspace.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,204 @@
/*
Copyright 2019 GEOSIRIS
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package com.geosiris.webstudio.etp;

import Energistics.Etp.v12.Datatypes.DataArrayTypes.DataArray;
import Energistics.Etp.v12.Datatypes.Object.ContextScopeKind;
import Energistics.Etp.v12.Protocol.DataArray.GetDataArraysResponse;
import com.geosiris.energyml.exception.ObjectNotFoundNotError;
import com.geosiris.energyml.pkg.EPCFile;
import com.geosiris.energyml.pkg.EPCPackage;
import com.geosiris.energyml.utils.EnergymlWorkspace;
import com.geosiris.energyml.utils.ObjectController;
import com.geosiris.etp.utils.ETPHelper;
import com.geosiris.etp.utils.ETPHelperREST;
import com.geosiris.etp.utils.ETPUri;
import com.geosiris.etp.websocket.ETPClient;
import com.geosiris.webstudio.servlet.Editor;
import jakarta.xml.bind.JAXBElement;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.*;
import java.util.stream.Collectors;

import static com.geosiris.energyml.utils.EnergymlWorkspaceHelper.getHdfReference;

public class ETPWorkspace implements EnergymlWorkspace {
private static final Logger logger = LoggerFactory.getLogger(ETPWorkspace.class);

private ETPClient client;
private String dataspace;

private HashMap<String, CacheData<String>> uuidUri_cache = new HashMap<>();

private static HashMap<String, CacheData<String>> xml_cache = new HashMap<>();

public class CacheData<T>{

public static final long TIMEOUT = 1000*60*10; // 10 min
public T data;
public long epoch;

public CacheData(T data){
this.data = data;
this.epoch = System.currentTimeMillis();
}

public boolean outDated(){
return System.currentTimeMillis() - this.epoch > TIMEOUT;
}
}

public ETPWorkspace(String dataspace, ETPClient client) {
this.dataspace = dataspace;
this.client = client;
this.uuidUri_cache = new HashMap<>();
}

public String getDataspace() {
return dataspace;
}

@Override
public Object getObject(String uuid, String objectVersion) {
return getObjectByIdentifier(EPCFile.getIdentifier(uuid, objectVersion));
}

@Override
public Object getObjectByIdentifier(String identifier) {
if(identifier.startsWith("eml:///")){
return getEnergisticsObject(identifier);
}else{
String uuid = EPCFile.getUuidFromIdentifier(identifier);
return getObjectByUUID(uuid);
}
}

@Override
public Object getObjectByUUID(String uuid) {
return getEnergisticsObject(getObjectUriFromUuid(uuid));
}

@Override
public List<?> readExternalArray(Object energymlArray, Object rootObj, String pathInRoot) throws ObjectNotFoundNotError {
String pathInExternal = getHdfReference(energymlArray).get(0);
String uri = getUriFromObject(rootObj, dataspace).toString();
try {
GetDataArraysResponse resp = ETPHelperREST.getMultipleDataArrays(client, uri, List.of(pathInExternal));
List<?> res = dataArraysToNumbers(resp.getDataArrays().entrySet().stream()
.sorted(Comparator.comparing(a -> a.getKey().toString()))
// .sorted(Comparator.comparingInt(e -> Integer.getInteger(e.getKey().toString())))
.map(Map.Entry::getValue)
.collect(Collectors.toList()));
// logger.info("@readExternalArray values {} ", res.subList(0,9));
return res;
} catch (Exception _ignore) {_ignore.printStackTrace();}
return null;
}

public String getEnergisticsXML(String uri) {
if(xml_cache.containsKey(uri) && !xml_cache.get(uri).outDated()){
logger.info("using cache for {}", uri);
return xml_cache.get(uri).data;
}else {
ETPUri etpUri = ETPUri.parse(uri);
etpUri.setDataspace(dataspace);
logger.info("@getEnergisticsXML > {}", uri);
List<String> m = ETPHelper.sendGetDataObjects_pretty(client, Collections.singletonList(etpUri.toString()), "xml", 50000);
if (!m.isEmpty()) {
xml_cache.put(uri, new CacheData<>(m.get(0)));
return m.get(0);
}
return null;
}
}

public Object getEnergisticsObject(String uri) {
String xml = getEnergisticsXML(uri);
if(xml != null){
JAXBElement<?> elt = Editor.pkgManager.unmarshal(xml);
return elt.getValue();
}
return null;
}

public String getObjectUriFromUuid(String uuid){
if(uuidUri_cache.containsKey(uuid)) {
return uuidUri_cache.get(uuid).data;
}else{
logger.info("@getObjectUriFromUuid {} {}", uuid);
List<String> uris = ETPHelper.sendGetRessources_pretty(client, new ETPUri(dataspace).toString(), 1, ContextScopeKind.self, 5000);
for (String uri : uris) {
if (uri.contains(uuid)) {
uuidUri_cache.put(uuid, new CacheData<>(uri));
return uri;
}
}
}
return null;
}

public static ETPUri getUriFromObject(Object obj, String dataspace){
EPCPackage epc_pkg = Editor.pkgManager.getMatchingPackage(obj.getClass());
ETPUri uri = new ETPUri();
uri.setDataspace(dataspace);
uri.setUuid((String) ObjectController.getObjectAttributeValue(obj, "uuid"));
uri.setDomain(epc_pkg.getDomain());
uri.setDomainVersion(epc_pkg.getVersionNum().replace(".", "").substring(0,2));
uri.setObjectType(obj.getClass().getSimpleName());
uri.setVersion((String) ObjectController.getObjectAttributeValue(obj, "version"));
return uri;
}


public static List<?> dataArraysToNumbers(List<DataArray> das){
return das.stream()
.map(da -> {
try {
List<?> values = ((List<?>) ObjectController.getObjectAttributeValue(da.getData().getItem(), "values"));
if(values == null){
// cas of object filled by json from http and deserialized by Gson :
// [Energistics.Etp.v12.Datatypes.ArrayOfInt, {values=[48.0, ...] } ]
// this is translated by Gson as : [String = "Energistics.Etp.v12.Datatypes.ArrayOfInt", com.google.gson.internal.LinkedTreeMap = {'values': [[48.0, ...]]}]
values = (List<?>) ((com.google.gson.internal.LinkedTreeMap) ((List)da.getData().getItem()).get(1)).get("values");
}
// logger.info("@dataArraysToNumbers values {} ", values.subList(0,9));
return values;
} catch (Exception ex) {
throw new RuntimeException(ex);
}
})
.flatMap(List::stream).collect(Collectors.toList());
/*
return das.stream()
.map(da -> {
try {
List<?> values = ((List<?>) ObjectController.getObjectAttributeValue(da.getData().getItem(), "values"));
if(values == null){
// cas of object filled by json from http and deserialized by Gson :
// [Energistics.Etp.v12.Datatypes.ArrayOfInt, {values=[48.0, ...] } ]
// this is translated by Gson as : [String = "Energistics.Etp.v12.Datatypes.ArrayOfInt", com.google.gson.internal.LinkedTreeMap = {'values': [[48.0, ...]]}]
values = (List<?>) ((com.google.gson.internal.LinkedTreeMap) ((List)da.getData().getItem()).get(1)).get("values");
}
return values.stream().map(v-> v instanceof String ? Double.parseDouble((String) v) : ((Number)v).doubleValue()).collect(Collectors.toList());
} catch (Exception ex) {
throw new RuntimeException(ex);
}
})
.flatMap(List::stream).collect(Collectors.toList());*/
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.geosiris.webstudio.exeptions;

public class ETPUploadFailed extends Exception{
public ETPUploadFailed(String msg){
super("ETPUploadFailed : " + msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
package com.geosiris.webstudio.exeptions;

public class NotValidInputException extends Exception{
public NotValidInputException(String msg){
super("NotValidInputException : " + msg);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package com.geosiris.webstudio.exeptions;

public class NotValidRequestException extends Exception{

public NotValidRequestException(String msg){
super("NotValidRequestException : " + msg);
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ public static void main(String[] arv) {
// logger.info(new WebStudioProperties());
logger.info(System.getenv("WS_CONFIG_INI_FILE_PATH"));
WebStudioProperties ws = new WebStudioProperties();
System.out.println(ws);
// logger.info(ws.getXSDFilePathFromPkgName("common2_0"));
// System.out.println(ws);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,10 @@
*/
package com.geosiris.webstudio.servlet.editing;

import com.geosiris.energyml.utils.EPCGenericManager;
import com.geosiris.energyml.utils.ExportVersion;
import com.geosiris.webstudio.servlet.Editor;
import com.geosiris.webstudio.utils.HttpSender;
import com.geosiris.webstudio.utils.SessionUtility;
import jakarta.servlet.ServletException;
import jakarta.servlet.annotation.MultipartConfig;
Expand All @@ -27,6 +30,7 @@
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.PrintWriter;
import java.util.Map;
Expand Down Expand Up @@ -60,17 +64,28 @@ protected void doGet(HttpServletRequest request, HttpServletResponse response)

Map<String, Object> map = SessionUtility.getResqmlObjects(session);
String uuid = request.getParameter("uuid");
String answer = "";

if(map.containsKey(uuid)) {
Object energymlObj = map.get(uuid);
answer = Editor.pkgManager.marshal(energymlObj);
String answer = Editor.pkgManager.marshal(energymlObj);
if("true".equals(request.getParameter("download"))){
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bos.write(answer.getBytes());
HttpSender.writeFileAsRequestResponse(response, EPCGenericManager.genPathInEPC(energymlObj, ExportVersion.CLASSIC), "application/xml", bos);
}else {
PrintWriter out = response.getWriter();
response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");
out.write(answer);
out.flush();
}
}else{
PrintWriter out = response.getWriter();
response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");
out.write("Unknown uuid : " + uuid);
out.flush();
}
// SessionUtility.log(session, new ServerLogMessage(MessageType.LOG, answer, SessionUtility.EDITOR_NAME));
PrintWriter out = response.getWriter();
response.setContentType("application/xml");
response.setCharacterEncoding("UTF-8");
out.write(answer);
out.flush();
}

/**
Expand Down
Loading

0 comments on commit 6558b86

Please sign in to comment.