2016-07-18 11:57:12 +02:00
|
|
|
/**
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
package org.gcube.informationsystem.resourceregistry.resources.utils;
|
|
|
|
|
2016-11-21 11:52:56 +01:00
|
|
|
import java.io.IOException;
|
2016-08-02 11:47:15 +02:00
|
|
|
import java.text.SimpleDateFormat;
|
|
|
|
import java.util.Calendar;
|
|
|
|
import java.util.Date;
|
2016-07-18 11:57:12 +02:00
|
|
|
import java.util.UUID;
|
|
|
|
|
|
|
|
import org.gcube.common.authorization.library.provider.AuthorizationProvider;
|
2016-09-12 14:45:40 +02:00
|
|
|
import org.gcube.common.authorization.library.provider.ClientInfo;
|
|
|
|
import org.gcube.common.authorization.library.utils.Caller;
|
2016-10-28 16:25:39 +02:00
|
|
|
import org.gcube.informationsystem.model.embedded.Header;
|
2016-10-25 17:58:40 +02:00
|
|
|
import org.gcube.informationsystem.model.entity.Entity;
|
2016-11-21 11:52:56 +01:00
|
|
|
import org.gcube.informationsystem.model.entity.Resource;
|
2016-12-19 14:59:27 +01:00
|
|
|
import org.gcube.informationsystem.resourceregistry.api.exceptions.ResourceRegistryException;
|
2016-07-18 11:57:12 +02:00
|
|
|
import org.slf4j.Logger;
|
|
|
|
import org.slf4j.LoggerFactory;
|
|
|
|
|
2016-11-21 11:52:56 +01:00
|
|
|
import com.fasterxml.jackson.core.JsonParseException;
|
|
|
|
import com.fasterxml.jackson.databind.JsonMappingException;
|
|
|
|
import com.fasterxml.jackson.databind.JsonNode;
|
2016-07-18 11:57:12 +02:00
|
|
|
import com.tinkerpop.blueprints.Edge;
|
2016-12-19 14:59:27 +01:00
|
|
|
import com.tinkerpop.blueprints.Element;
|
2016-07-18 11:57:12 +02:00
|
|
|
import com.tinkerpop.blueprints.Vertex;
|
|
|
|
|
|
|
|
/**
|
2016-10-12 14:33:23 +02:00
|
|
|
* @author Luca Frosini (ISTI - CNR)
|
2016-07-18 11:57:12 +02:00
|
|
|
*
|
|
|
|
*/
|
2016-11-11 12:01:12 +01:00
|
|
|
public class HeaderUtility {
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
private static final Logger logger = LoggerFactory
|
|
|
|
.getLogger(HeaderUtility.class);
|
|
|
|
|
|
|
|
public static Header createHeader(UUID uuid) {
|
2016-10-28 16:25:39 +02:00
|
|
|
HeaderOrient header = new HeaderOrient();
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
if (uuid == null) {
|
|
|
|
uuid = UUID.randomUUID();
|
|
|
|
}
|
|
|
|
|
|
|
|
header.setUUID(uuid);
|
|
|
|
|
|
|
|
String creator = org.gcube.informationsystem.model.embedded.Header.UNKNOWN_USER;
|
|
|
|
try {
|
2016-09-12 14:45:40 +02:00
|
|
|
Caller caller = AuthorizationProvider.instance.get();
|
2016-09-14 18:09:28 +02:00
|
|
|
if(caller!=null){
|
|
|
|
ClientInfo clientInfo = caller.getClient();
|
|
|
|
String clientId = clientInfo.getId();
|
|
|
|
if (clientId != null && clientId.compareTo("") != 0) {
|
|
|
|
creator = clientId;
|
|
|
|
} else {
|
|
|
|
throw new Exception("Username null or empty");
|
|
|
|
}
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
} catch (Exception e) {
|
2016-11-03 12:16:52 +01:00
|
|
|
logger.error("Unable to retrieve user. {} will be used", creator);
|
2016-07-18 11:57:12 +02:00
|
|
|
}
|
|
|
|
header.setCreator(creator);
|
|
|
|
|
2016-08-02 11:47:15 +02:00
|
|
|
Date date = Calendar.getInstance().getTime();
|
|
|
|
SimpleDateFormat ft = new SimpleDateFormat ("E yyyy.MM.dd 'at' hh:mm:ss a zzz");
|
|
|
|
logger.trace("Setting Last Update and Creation Time to " + ft.format(date));
|
|
|
|
|
|
|
|
header.setCreationTime(date);
|
|
|
|
header.setLastUpdateTime(date);
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
return header;
|
|
|
|
}
|
2016-11-21 11:52:56 +01:00
|
|
|
|
|
|
|
public static Header getHeader(JsonNode jsonNode, boolean creation)
|
|
|
|
throws JsonParseException, JsonMappingException, IOException {
|
|
|
|
if (jsonNode.has(Resource.HEADER_PROPERTY)) {
|
|
|
|
JsonNode headerNode = jsonNode.get(Resource.HEADER_PROPERTY);
|
|
|
|
if (headerNode.isNull()) {
|
|
|
|
return null;
|
|
|
|
}
|
|
|
|
HeaderOrient header = null;
|
|
|
|
if(creation){
|
2016-11-28 18:17:14 +01:00
|
|
|
// If an header is provided MUST contains and UUID otherwise is
|
|
|
|
// an invalid request so that let that an exception is raised
|
2016-11-21 11:52:56 +01:00
|
|
|
UUID uuid = UUID.fromString(headerNode.get(Header.UUID_PROPERTY).asText());
|
|
|
|
header = (HeaderOrient) createHeader(uuid);
|
|
|
|
}else{
|
|
|
|
header = new HeaderOrient();
|
|
|
|
header.fromJSON(headerNode.toString());
|
|
|
|
}
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
return null;
|
|
|
|
}
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
public static Header addHeader(Vertex vertex, UUID uuid) {
|
|
|
|
Header header = createHeader(uuid);
|
|
|
|
vertex.setProperty(Entity.HEADER_PROPERTY, header);
|
|
|
|
return header;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
public static Header addHeader(Edge edge, UUID uuid) {
|
|
|
|
Header header = createHeader(uuid);
|
|
|
|
edge.setProperty(Entity.HEADER_PROPERTY, header);
|
|
|
|
return header;
|
|
|
|
}
|
2016-12-19 14:59:27 +01:00
|
|
|
|
|
|
|
public static Header getHeader(Element element)
|
|
|
|
throws ResourceRegistryException {
|
|
|
|
return Utility.getEmbedded(Header.class, element, Entity.HEADER_PROPERTY);
|
|
|
|
}
|
2016-07-18 11:57:12 +02:00
|
|
|
|
|
|
|
}
|