Skip to content

Commit

Permalink
Issues #123,
Browse files Browse the repository at this point in the history
  • Loading branch information
pavel-stastny committed Aug 9, 2024
1 parent 7b3cd21 commit 86cea3e
Show file tree
Hide file tree
Showing 5 changed files with 94 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ protected void configureServlets() {
bind(cz.incad.kramerius.rest.apiNew.admin.v70.sync.SDNNTSyncResource.class);
bind(cz.incad.kramerius.rest.apiNew.admin.v70.conf.Configurations.class);
bind(cz.incad.kramerius.rest.apiNew.admin.v70.AdminLockResource.class);
//bind(cz.incad.kramerius.rest.apiNew.admin.v70.processing.ProcessingSupportResource.class);

// OAI endpoint
bind(cz.incad.kramerius.rest.oai.OAIEndpoint.class);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
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.exceptions.BadRequestException;
import cz.incad.kramerius.rest.apiNew.exceptions.ForbiddenException;
import cz.incad.kramerius.rest.apiNew.exceptions.InternalErrorException;
Expand All @@ -31,6 +32,7 @@
import javax.inject.Inject;
import javax.inject.Named;
import javax.inject.Provider;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.*;
import javax.ws.rs.core.Context;
import javax.ws.rs.core.HttpHeaders;
Expand Down Expand Up @@ -72,6 +74,17 @@ public class ItemsResource extends AdminApiResource {
@Inject
@Named("new-index")
private SolrAccess solrAccess;


@Inject
RightsResolver rightsResolver;

@Inject
Provider<HttpServletRequest> requestProvider;

@Inject
IResourceIndex resourceIndex;



/**
Expand Down Expand Up @@ -170,6 +183,40 @@ public Response getItems(@QueryParam("model") String model,
}
}


@SuppressWarnings("deprecation")
@GET
@Path("/models")
@Produces(MediaType.APPLICATION_JSON)
public Response getCollections(@QueryParam("withItem") String itemPid) {
try {
User user = this.userProvider.get();
List<String> roles = Arrays.stream(user.getGroups()).map(Role::getName).collect(Collectors.toList());
// TODO: check if it is necessary
// if (!permitPocessingIndexAcceess(this.rightsResolver, user1)) {
// throw new ForbiddenException("user '%s' is not allowed to read processing index (missing action '%s')", user1.getLoginname(), SecuredActions.A_INDEX); //403
// }

if (!userIsAllowedToRead(this.rightsResolver, user, SpecialObjects.REPOSITORY.getPid())) {
// request doesnt contain user principal
throw new ForbiddenException("user '%s' is not allowed to do this (missing action '%s')", user, SecuredActions.A_ADMIN_READ.name()); //403
}


List<org.apache.commons.lang3.tuple.Pair<String,Long>> allFedoraModelsAsList = this.resourceIndex.getAllFedoraModelsAsList();
JSONObject object = new JSONObject();
for (org.apache.commons.lang3.tuple.Pair<String, Long> pair : allFedoraModelsAsList) {
object.put(pair.getKey(), pair.getRight());
}
return Response.ok(object.toString()).build();
} catch (WebApplicationException e) {
throw e;
} catch (Throwable e) {
LOGGER.log(Level.SEVERE, e.getMessage(), e);
throw new InternalErrorException(e.getMessage());
}
}

@HEAD
@Path("{pid}")
public Response checkItemExists(@PathParam("pid") String pid) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,14 @@ public List<Map<String, String>> search(String query, int limit, int offset) thr
throw new UnsupportedOperationException("Not implemented");
}



@Override
public List<org.apache.commons.lang3.tuple.Pair<String, Long>> getAllFedoraModelsAsList()
throws ResourceIndexException {
throw new UnsupportedOperationException("Not implemented");
}

@Override
public Document getFedoraModels() throws ResourceIndexException {
throw new UnsupportedOperationException("Not implemented");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@

package cz.incad.kramerius.resourceindex;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import cz.incad.kramerius.ObjectPidsPath;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.solr.client.solrj.SolrServerException;
import org.w3c.dom.Document;

/**
Expand Down Expand Up @@ -42,7 +46,10 @@ public interface IResourceIndex {
*/
@Deprecated
public Document getFedoraModels() throws ResourceIndexException;



public List<Pair<String,Long>> getAllFedoraModelsAsList() throws ResourceIndexException;

/**
* Never used; throw it out
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
import cz.incad.kramerius.utils.StringUtils;
import cz.incad.kramerius.utils.solr.SolrUtils;
import cz.incad.kramerius.virtualcollections.CollectionPidUtils;

import org.apache.commons.lang3.tuple.Pair;
import org.apache.solr.client.solrj.SolrClient;
import org.apache.solr.client.solrj.SolrQuery;
import org.apache.solr.client.solrj.SolrServerException;
Expand All @@ -27,6 +29,7 @@
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* SOLR implemenation of the resource index;
Expand Down Expand Up @@ -131,24 +134,40 @@ public List<String> getObjectsByModel(String model, int limit, int offset, Strin
}



public List<Pair<String,Long>> getAllFedoraModelsAsList() throws ResourceIndexException {
try {
QueryResponse response = this.solrClient.query(new SolrQuery("type:description").setRows(0).setFacet(true).addFacetField("model"));
List<Count> values = response.getFacetField("model").getValues();
return values.stream().map(c-> {
long count = c.getCount();
String cname = c.getName();
return Pair.of(cname, count);
}).collect(Collectors.toList());
} catch (SolrServerException | IOException e) {
throw new ResourceIndexException(e);
}
}


@Override
// TODO: rewrite it
public Document getFedoraModels() throws ResourceIndexException {
try {
QueryResponse response = this.solrClient.query(new SolrQuery("type:description").setRows(0).setFacet(true).addFacetField("model"));
//QueryResponse response = this.solrClient.query(new SolrQuery("type:description").setRows(0).setFacet(true).addFacetField("model"));
List<Pair<String,Long>> allModels = getAllFedoraModelsAsList();

DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.newDocument();
Element rootElement = doc.createElementNS(FedoraNamespaces.SPARQL_NAMESPACE_URI, "sparql");
rootElement.appendChild(createHeader(doc, "object", "title"));
rootElement.appendChild(createModelResults(doc, response.getFacetField("model").getValues()));
rootElement.appendChild(createModelResults(doc, allModels));
doc.appendChild(rootElement);
return doc;
} catch (ParserConfigurationException e) {
throw new ResourceIndexException(e);
} catch (SolrServerException e) {
throw new ResourceIndexException(e);
} catch (IOException e) {
throw new ResourceIndexException(e);
} catch (ResourceIndexException e) {
throw e;
}
}

Expand All @@ -162,17 +181,18 @@ private Element createHeader(Document doc, String... variables) {
return head;
}

private Element createModelResults(Document doc, List<Count> values) {
private Element createModelResults(Document doc, List<Pair<String,Long>> values) {
Element results = doc.createElementNS(FedoraNamespaces.SPARQL_NAMESPACE_URI, "results");
for (Count count : values) {
//for (Count count : values) {
for (Pair<String,Long> count : values) {
Element result = doc.createElementNS(FedoraNamespaces.SPARQL_NAMESPACE_URI, "result");
Element object = doc.createElementNS(FedoraNamespaces.SPARQL_NAMESPACE_URI, "object");
//page info:fedora/model:page
object.setAttribute("uri", "info:fedora/model:" + count.getName());
object.setAttribute("uri", "info:fedora/model:" + count.getLeft());
result.appendChild(object);

Element title = doc.createElementNS(FedoraNamespaces.SPARQL_NAMESPACE_URI, "title");
title.setTextContent(count.getName());
title.setTextContent(count.getLeft());
result.appendChild(title);

results.appendChild(result);
Expand Down

0 comments on commit 86cea3e

Please sign in to comment.