Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue #1106 #1113

Merged
merged 1 commit into from
Jan 28, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
package cz.incad.kramerius.rest.apiNew.admin.v70;

import com.sun.jersey.api.client.Client;
import cz.incad.kramerius.ObjectPidsPath;
import cz.incad.kramerius.SolrAccess;
import cz.incad.kramerius.fedora.om.RepositoryException;
import cz.incad.kramerius.imaging.ImageStreams;
import cz.incad.kramerius.repository.RepositoryApi;
import cz.incad.kramerius.repository.utils.Utils;
import cz.incad.kramerius.resourceindex.IResourceIndex;
import cz.incad.kramerius.rest.apiNew.client.v70.libs.Instances;
import cz.incad.kramerius.rest.apiNew.client.v70.redirection.utils.IntrospectUtils;
import cz.incad.kramerius.rest.apiNew.exceptions.BadRequestException;
import cz.incad.kramerius.rest.apiNew.exceptions.ForbiddenException;
import cz.incad.kramerius.rest.apiNew.exceptions.InternalErrorException;
Expand All @@ -20,6 +23,7 @@
import cz.incad.kramerius.utils.Dom4jUtils;
import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.XMLUtils;
import cz.incad.kramerius.utils.conf.KConfiguration;
import cz.incad.kramerius.utils.java.Pair;
import org.apache.commons.io.IOUtils;
import org.dom4j.Document;
Expand All @@ -42,6 +46,7 @@

import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.util.*;
import java.util.logging.Level;
import java.util.logging.Logger;
Expand All @@ -54,6 +59,7 @@ public class ItemsResource extends AdminApiResource {

private static final Integer DEFAULT_OFFSET = 0;
private static final Integer DEFAULT_LIMIT = 10;
private final Client client;


@javax.inject.Inject
Expand All @@ -77,6 +83,16 @@ public class ItemsResource extends AdminApiResource {
IResourceIndex resourceIndex;


//TODO: Do it better; divide into two classes
@Inject
private Instances libraries;


public ItemsResource() {
super();
this.client = Client.create();
}


/**
* Returns array of pids (with titles) that have given model. Only partial array with offset & limit.
Expand Down Expand Up @@ -237,6 +253,36 @@ public Response checkItemExists(@PathParam("pid") String pid) {
}
}

@GET
@Path("{pid}/solr/instintrospect")
@Produces(MediaType.APPLICATION_JSON)
public Response introspectPidInInstances(@PathParam("pid") String pid) {
try {
boolean cdkServerMode = KConfiguration.getInstance().getConfiguration().getBoolean("cdk.server.mode");
if (cdkServerMode) {
User user = this.userProvider.get();

if (!userIsAllowedToRead(this.rightsResolver, user, SpecialObjects.REPOSITORY.getPid())) {
throw new ForbiddenException("user '%s' is not allowed to do this (missing action '%s')", user, SecuredActions.A_ADMIN_READ.name()); //403
}
try {
JSONObject obj = IntrospectUtils.introspectSolr(this.client, this.libraries, pid);
return Response.ok(obj.toString()).build();
} catch (UnsupportedEncodingException | JSONException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
} else {
return Response.status(Response.Status.NOT_FOUND).build();
}
} catch (IOException e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
}



@GET
@Path("{pid}/foxml")
@Produces(MediaType.APPLICATION_XML)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,8 +172,8 @@ public boolean acceptElement(Element element) {
errorObject.put("error", "No pid");
return Response.status(Response.Status.BAD_REQUEST).entity(errorObject.toString()).type(MediaType.APPLICATION_JSON_TYPE).build();
}
case delete_tree:
case root:

case delete_tree:
case children:
if (item != null && StringUtils.isAnyString(item.getRootPid()) && StringUtils.isAnyString(item.getOwnPidPath())) {
item.setTimestamp(Instant.now());
Expand All @@ -184,7 +184,9 @@ public boolean acceptElement(Element element) {
errorObject.put("error", "No root pid or own_pid_path");
return Response.status(Response.Status.BAD_REQUEST).entity(errorObject.toString()).type(MediaType.APPLICATION_JSON_TYPE).build();
}



case root:
case new_root:
if (item != null && StringUtils.isAnyString(item.getRootPid())) {
item.setTimestamp(Instant.now());
Expand All @@ -195,6 +197,7 @@ public boolean acceptElement(Element element) {
errorObject.put("error", "No root pid");
return Response.status(Response.Status.BAD_REQUEST).entity(errorObject.toString()).type(MediaType.APPLICATION_JSON_TYPE).build();
}

default:
throw new IllegalStateException(String.format("Uknown type of reharvest %s", item.getTypeOfReharvest()));
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
package cz.incad.kramerius.rest.apiNew.client.v70.redirection.utils;

import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

import cz.incad.kramerius.SolrAccess;
import org.apache.commons.lang3.tuple.Pair;
import org.json.JSONArray;
import org.json.JSONObject;
Expand All @@ -16,6 +19,8 @@
import cz.incad.kramerius.rest.apiNew.client.v70.libs.OneInstance.InstanceType;
import cz.incad.kramerius.utils.conf.KConfiguration;

import static cz.incad.kramerius.utils.IterationUtils.getSortField;

public class IntrospectUtils {

private IntrospectUtils() {}
Expand Down Expand Up @@ -56,58 +61,66 @@ public static Pair<List<String>, List<String>> introspectPid(Client client, Inst

/** introspect utils */
public static JSONObject introspectSolr(Client client, Instances libraries, String pid) throws UnsupportedEncodingException {
JSONObject obj = new JSONObject();
List<OneInstance> instances = libraries.enabledInstances();
for(OneInstance inst:instances) {
String library = inst.getName();
boolean channelAccess = KConfiguration.getInstance().getConfiguration().containsKey("cdk.collections.sources." + library + ".licenses") ? KConfiguration.getInstance().getConfiguration().getBoolean("cdk.collections.sources." + library + ".licenses") : false;
if(channelAccess) {
String channel = KConfiguration.getInstance().getConfiguration().getString("cdk.collections.sources." + library + ".forwardurl");
String solrChannelUrl = ChannelUtils.solrChannelUrl(inst.getInstanceType().name(), channel);
InstanceType instType = inst.getInstanceType();
String solrPid = ChannelUtils.solrChannelPidExistence(client, channel, solrChannelUrl, instType.name(), pid);
if (solrPid != null) {
JSONObject responseObj = null;
switch(instType) {
case V5:
// make solr fields accessible
// PID copy to pid
// fedora.model copy to model
// root_pid copy to root_pid
// pid_path copy to pid_paths
JSONObject k5resp = new JSONObject(solrPid);
JSONObject optJSONObject = k5resp.optJSONObject("response");
if (optJSONObject != null) {
JSONArray docs = optJSONObject.getJSONArray("docs");
for (int i = 0; i < docs.length(); i++) {
JSONObject doc = docs.getJSONObject(i);
if (doc.has("PID")) {
doc.put("pid", doc.getString("PID"));
}
if (doc.has("fedora.model")) {
doc.put("model", doc.getString("fedora.model"));
}

if (doc.has("pid_path")) {
doc.put("pid_paths", doc.getJSONArray("pid_path"));
}

if (doc.has("root_pid")) {
doc.put("root.pid", doc.getString("root_pid"));
}
}
JSONObject obj = new JSONObject();

/** cdk request */
String solrSearchHost = KConfiguration.getInstance().getSolrSearchHost();
String response = ChannelUtils.solrChannelPidExistence(client, null, solrSearchHost, "v7", pid);
obj.put("_cdk_", new JSONObject(response));


/** instances request */
List<OneInstance> instances = libraries.enabledInstances();
for(OneInstance inst:instances) {
String library = inst.getName();
boolean channelAccess = KConfiguration.getInstance().getConfiguration().containsKey("cdk.collections.sources." + library + ".licenses") ? KConfiguration.getInstance().getConfiguration().getBoolean("cdk.collections.sources." + library + ".licenses") : false;
if(channelAccess) {
String channel = KConfiguration.getInstance().getConfiguration().getString("cdk.collections.sources." + library + ".forwardurl");
String solrChannelUrl = ChannelUtils.solrChannelUrl(inst.getInstanceType().name(), channel);
InstanceType instType = inst.getInstanceType();
String solrPid = ChannelUtils.solrChannelPidExistence(client, channel, solrChannelUrl, instType.name(), pid);
if (solrPid != null) {
JSONObject responseObj = null;
switch(instType) {
case V5:
// make solr fields accessible
// PID copy to pid
// fedora.model copy to model
// root_pid copy to root_pid
// pid_path copy to pid_paths
JSONObject k5resp = new JSONObject(solrPid);
JSONObject optJSONObject = k5resp.optJSONObject("response");
if (optJSONObject != null) {
JSONArray docs = optJSONObject.getJSONArray("docs");
for (int i = 0; i < docs.length(); i++) {
JSONObject doc = docs.getJSONObject(i);
if (doc.has("PID")) {
doc.put("pid", doc.getString("PID"));
}
if (doc.has("fedora.model")) {
doc.put("model", doc.getString("fedora.model"));
}

if (doc.has("pid_path")) {
doc.put("pid_paths", doc.getJSONArray("pid_path"));
}

if (doc.has("root_pid")) {
doc.put("root.pid", doc.getString("root_pid"));
}
}
responseObj = k5resp;
break;

default:
responseObj = new JSONObject(solrPid);
break;
}
obj.put(library, responseObj);
}
responseObj = k5resp;
break;

default:
responseObj = new JSONObject(solrPid);
break;
}
obj.put(library, responseObj);
}
}
return obj;
}
return obj;
}
}