Commit b4bf835c authored by redmitry@list.ru's avatar redmitry@list.ru

add json path to get partial object

parent b5a54112
......@@ -175,20 +175,44 @@ public abstract class OpenEBenchService {
@GET
@Path("/{collection}/{id : .*}")
@Path("/Reference/{id : .*}")
@PermitAll
@Produces(MediaType.APPLICATION_JSON)
public Response getObject(@Context SecurityContext sc,
@PathParam("id")
@Parameter(description = "reference id",
example = "doi.org:10.7490/f1000research.1116968.1")
@Encoded final String id) {
final String object = dao().getObject(id, "Reference", sc);
if (object == null) {
return Response.status(Status.NOT_FOUND).build();
}
if ("{}".equals(object)) {
return Response.status(Status.UNAUTHORIZED).build();
}
return Response.ok(object, MediaType.APPLICATION_JSON).build();
}
@GET
@Path("/{collection}/{id:.[^/]*}{path : .*}")
@PermitAll
@Produces(MediaType.APPLICATION_JSON)
public Response getObject(@Context SecurityContext sc,
@PathParam("collection")
@Parameter(description = "OpenEBench collection",
example = "Contact")
@Encoded final String collection,
final String collection,
@PathParam("id")
@Parameter(description = "object id",
example = "OEBD00200001FO")
@Encoded final String id) {
@Encoded final String id,
@PathParam("path")
@Parameter(description = "JSON path ",
example = "")
final String path) {
final String object = dao().getObject(id, collection, sc);
final String object = dao().getObject(id, collection, path, sc);
if (object == null) {
return Response.status(Status.NOT_FOUND).build();
}
......@@ -196,5 +220,5 @@ public abstract class OpenEBenchService {
return Response.status(Status.UNAUTHORIZED).build();
}
return Response.ok(object, MediaType.APPLICATION_JSON).build();
}
}
}
......@@ -37,11 +37,18 @@ import com.mongodb.client.model.Projections;
import es.bsc.inb.elixir.openebench.rest.auth.AbstractAuthorization;
import es.bsc.inb.elixir.openebench.rest.auth.Authorization;
import es.bsc.inb.elixir.openebench.rest.auth.CollectionAuthorizationFilter;
import java.io.StringReader;
import java.io.Writer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.annotation.PreDestroy;
import javax.inject.Inject;
import javax.json.Json;
import javax.json.JsonException;
import javax.json.JsonPointer;
import javax.json.JsonReader;
import javax.json.JsonStructure;
import javax.json.JsonValue;
import javax.servlet.ServletContext;
import javax.ws.rs.core.SecurityContext;
import org.bson.AbstractBsonWriter;
......@@ -140,6 +147,11 @@ public abstract class AbstractDatabase {
}
public String getObject(final String id, final String collection, final SecurityContext sc) {
return getObject(id, collection, null, sc);
}
public String getObject(final String id, final String collection, final String path,
final SecurityContext sc) {
final AbstractAuthorization authz = authorization.getAuthorization(sc, collection);
if (authz == null) {
......@@ -155,8 +167,28 @@ public abstract class AbstractDatabase {
if (iterator.hasNext()) {
final Document doc = iterator.next();
if (doc != null) {
doc.remove(PROVENANCE_PROPERTY);
return doc.toJson();
if (path == null || path.isEmpty()) {
doc.remove(PROVENANCE_PROPERTY);
return doc.toJson();
}
JsonPointer pointer;
try {
pointer = Json.createPointer(path);
} catch(Exception ex) {
Logger.getLogger(AbstractDatabase.class.getName()).log(Level.INFO, null, ex);
return null;
}
try (JsonReader reader = Json.createReader(new StringReader(doc.toJson()))) {
final JsonStructure structure = reader.read();
if (pointer.containsValue(structure)) {
return pointer.getValue(structure).toString();
}
} catch(JsonException ex) {
Logger.getLogger(AbstractDatabase.class.getName()).log(Level.INFO, null, ex);
}
return null;
}
}
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment