diff --git a/BUILD-README.txt b/BUILD-README.txt index c5f1cd50f0..a296194b6e 100644 --- a/BUILD-README.txt +++ b/BUILD-README.txt @@ -1,4 +1,4 @@ -For build project, we use gradle build tool. For more information, see http://www.gradle.org/. +For build project, we use gradle build tool (version 2.+). For more information, see http://www.gradle.org/. Display all tasks: diff --git a/build.gradle b/build.gradle index 8e0da97ad5..f4d094ea2e 100644 --- a/build.gradle +++ b/build.gradle @@ -80,7 +80,7 @@ subprojects { ext.guiceversion ='3.0' ext.postgresversion = '8.4-702.jdbc4' ext.slf4jversion='1.6.0' - ext.jerseyversion = '1.12' + ext.jerseyversion = '1.17.1' ext.generatedSources = "generated-sources" diff --git a/common/build.gradle b/common/build.gradle index 2ade734704..7692400b13 100644 --- a/common/build.gradle +++ b/common/build.gradle @@ -63,23 +63,17 @@ dependencies { compile "commons-dbcp:commons-dbcp:1.4" compile "net.sf.json-lib:json-lib:2.3:jdk15" - /* - compile "com.sun.jersey.contribs:jersey-guice:${ext.jerseyversion}" - compile "com.sun.jersey:jersey-client:${ext.jerseyversion}" - compile "com.sun.jersey.contribs:jersey-apache-client:${ext.jerseyversion}" - compile "com.sun.jersey:jersey-json:${ext.jerseyversion}" - */ compile 'javax.ws.rs:jsr311-api:1.1.1' + + compile "com.sun.jersey:jersey-server:${jerseyversion}" + compile "com.sun.jersey:jersey-servlet:${jerseyversion}" + compile "com.sun.jersey:jersey-client:${jerseyversion}" + + compile "com.sun.jersey.contribs:jersey-apache-client:${jerseyversion}" + compile "com.sun.jersey.contribs:jersey-guice:${jerseyversion}" + // dependencies in maven profile - compile module("com.sun.jersey:jersey-server:1.12") { - dependency("com.sun.jersey:jersey-core:1.12") - dependency("com.sun.jersey:jersey-servlet:1.12") - dependency("com.sun.jersey:jersey-client:1.12") - dependency("com.sun.jersey.contribs:jersey-apache-client:1.12") - dependency("com.sun.jersey:jersey-json:1.12") - dependency("com.sun.jersey.contribs:jersey-guice:1.12") - } testCompile "xmlunit:xmlunit:1.3" diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/guice/ApiServletModule.java b/rest/src/main/java/cz/incad/kramerius/rest/api/guice/ApiServletModule.java index 11d832805a..625538827d 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/guice/ApiServletModule.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/guice/ApiServletModule.java @@ -40,7 +40,7 @@ import cz.incad.kramerius.rest.api.k5.client.impl.SolrMemoizationImpl; import cz.incad.kramerius.rest.api.k5.client.item.ItemResource; import cz.incad.kramerius.rest.api.k5.client.item.decorators.CollectionsDecorator; -import cz.incad.kramerius.rest.api.k5.client.item.decorators.RelsExtRightsFlag; +import cz.incad.kramerius.rest.api.k5.client.item.decorators.SolrRightsFlag; import cz.incad.kramerius.rest.api.k5.client.item.decorators.HandleDecorate; import cz.incad.kramerius.rest.api.k5.client.item.decorators.ItemSolrRootModelDecorate; import cz.incad.kramerius.rest.api.k5.client.item.decorators.ItemSolrRootPidDecorate; @@ -131,7 +131,7 @@ public static void decoratorsBindings(Multibinder decs) { decs.addBinding().to(SolrDataNode.class); decs.addBinding().to(CollectionsDecorator.class); decs.addBinding().to(ReplicatedFromDecorator.class); - decs.addBinding().to(RelsExtRightsFlag.class); + decs.addBinding().to(SolrRightsFlag.class); // item, display diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/JSONDecoratorsAggregate.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/JSONDecoratorsAggregate.java index c08a2a2b22..0211aaee61 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/JSONDecoratorsAggregate.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/JSONDecoratorsAggregate.java @@ -26,30 +26,29 @@ import com.google.inject.Inject; - public class JSONDecoratorsAggregate { - Map decoratorsMap = new HashMap(); - List keys = new ArrayList(); - + Map decoratorsMap = new HashMap(); + List keys = new ArrayList(); + @Inject public JSONDecoratorsAggregate(Set decs) { super(); for (JSONDecorator p : decs) { - String k = p.getKey(); - if (keys.contains(k)) { - keys.remove(k); - } - keys.add(k); - this.decoratorsMap.put(k, p); + String k = p.getKey(); + if (keys.contains(k)) { + keys.remove(k); + } + keys.add(k); + this.decoratorsMap.put(k, p); } } public List getDecorators() { - List decorators = new ArrayList(); - for (String k: this.keys) { - decorators.add(this.decoratorsMap.get(k)); - } - return decorators; + List decorators = new ArrayList(); + for (String k : this.keys) { + decorators.add(this.decoratorsMap.get(k)); + } + return decorators; } } diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/SolrMemoization.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/SolrMemoization.java new file mode 100644 index 0000000000..7782690ef4 --- /dev/null +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/SolrMemoization.java @@ -0,0 +1,39 @@ +package cz.incad.kramerius.rest.api.k5.client; + +import java.io.IOException; +import java.util.Map; + +import org.w3c.dom.Element; + +/** + * SOLR memoization support + * @author pavels + */ +public interface SolrMemoization { + + /** + * Remember document from index + * @param pid PID + * @param elm W3C Element represents indexed document + */ + public void rememberIndexedDoc(String pid, Element elm); + + /** + * Returns remembered document + * @param pid PID + */ + public Element getRememberedIndexedDoc(String pid); + + /** + * Ask SOLR for the index document + * @param pid PID + * @return + * @throws IOException + */ + public Element askForIndexDocument(String pid) throws IOException; + + /** + * Clear remembered elements + */ + public void clearMemo(); +} diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/SolrResultsAware.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/SolrResultsAware.java new file mode 100644 index 0000000000..06c9b19e8d --- /dev/null +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/SolrResultsAware.java @@ -0,0 +1,8 @@ +package cz.incad.kramerius.rest.api.k5.client; + +import java.util.List; + +public interface SolrResultsAware { + + public List getFieldList(); +} diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/feeder/FeederResource.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/feeder/FeederResource.java index fa36d7e7e8..59fbb864ab 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/feeder/FeederResource.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/feeder/FeederResource.java @@ -29,6 +29,7 @@ import cz.incad.kramerius.processes.annotations.DefaultParameterValue; import cz.incad.kramerius.rest.api.exceptions.GenericApplicationException; import cz.incad.kramerius.rest.api.k5.client.JSONDecoratorsAggregate; +import cz.incad.kramerius.rest.api.k5.client.SolrMemoization; import cz.incad.kramerius.rest.api.k5.client.utils.JSONUtils; import cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils; import cz.incad.kramerius.security.User; @@ -63,6 +64,9 @@ public class FeederResource { @Inject SolrAccess solrAccess; + @Inject + SolrMemoization solrMemo; + @Inject Provider userProvider; @@ -115,7 +119,7 @@ public boolean acceptElement(Element element) { .fromResource(FeederResource.class) .path("newest").build(pid).toString(); JSONObject mdis = JSONUtils.pidAndModelDesc(pid, - fedoraAccess, uriString, + uriString, this.solrMemo, this.decoratorsAggregate, uriString); jsonArray.add(mdis); } catch (IOException ex) { @@ -161,8 +165,8 @@ public Response mostdesirable( String uriString = UriBuilder .fromResource(FeederResource.class) .path("mostdesirable").build(pid).toString(); - JSONObject mdis = JSONUtils.pidAndModelDesc(pid, fedoraAccess, - uriString, this.decoratorsAggregate, uriString); + JSONObject mdis = JSONUtils.pidAndModelDesc(pid, + uriString, this.solrMemo, this.decoratorsAggregate, uriString); jsonArray.add(mdis); } catch (Exception e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/impl/SolrMemoizationImpl.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/impl/SolrMemoizationImpl.java new file mode 100644 index 0000000000..142cc1a6a9 --- /dev/null +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/impl/SolrMemoizationImpl.java @@ -0,0 +1,54 @@ +package cz.incad.kramerius.rest.api.k5.client.impl; + +import java.io.IOException; +import java.util.HashMap; +import java.util.Map; + +import org.w3c.dom.Document; +import org.w3c.dom.Element; + +import com.google.inject.Inject; + +import cz.incad.kramerius.SolrAccess; +import cz.incad.kramerius.rest.api.k5.client.SolrMemoization; +import cz.incad.kramerius.rest.api.k5.client.utils.PIDSupport; +import cz.incad.kramerius.utils.XMLUtils; + +public class SolrMemoizationImpl implements SolrMemoization{ + + @Inject + SolrAccess solrAccess; + + private Map elms = new HashMap(); + + @Override + public void rememberIndexedDoc(String pid, Element elm) { + this.elms.put(pid, elm); + } + + @Override + public Element getRememberedIndexedDoc(String pid) { + return elms.get(pid); + } + + @Override + public synchronized Element askForIndexDocument(String pid) throws IOException { + if (!this.elms.containsKey(pid)) { + if (PIDSupport.isComposedPID(pid)) { + pid = PIDSupport.convertToSOLRType(pid); + } + Document doc = solrAccess.getSolrDataDocument(pid); + Element result = XMLUtils.findElement(doc.getDocumentElement(), "result"); + if (result != null) { + Element d = XMLUtils.findElement(result, "doc"); + this.elms.put(pid, d); + } + } + return this.elms.get(pid); + } + + @Override + public void clearMemo() { + this.elms.clear(); + } +} diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/ItemResource.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/ItemResource.java index d040d6ac94..06d8b4e56c 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/ItemResource.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/ItemResource.java @@ -187,7 +187,7 @@ public Response children(@PathParam("pid") String pid) { String uri = UriBuilder.fromResource(ItemResource.class) .path("{pid}/children").build(pid).toString(); JSONObject jsonObject = JSONUtils.pidAndModelDesc(repPid, - fedoraAccess, uri.toString(), + uri.toString(),this.solrMemoization, this.decoratorsAggregate, uri); jsonArray.add(jsonObject); } @@ -249,8 +249,8 @@ private JSON siblings(String pid, ObjectPidsPath onePath) String uriString = UriBuilder.fromResource(ItemResource.class) .path("{pid}/siblings").build(pid).toString(); p = PIDSupport.convertToK4Type(p); - JSONObject jsonObject = JSONUtils.pidAndModelDesc(p, fedoraAccess, - uriString, this.decoratorsAggregate, uriString); + JSONObject jsonObject = JSONUtils.pidAndModelDesc(p, + uriString,this.solrMemoization, this.decoratorsAggregate, uriString); pathArray.add(jsonObject); } object.put("path", pathArray); @@ -261,8 +261,7 @@ private JSON siblings(String pid, ObjectPidsPath onePath) String uriString = UriBuilder.fromResource(ItemResource.class) .path("{pid}/siblings").build(pid).toString(); p = PIDSupport.convertToK4Type(p); - JSONObject jsonObject = JSONUtils.pidAndModelDesc(p, fedoraAccess, - uriString, this.decoratorsAggregate, uriString); + JSONObject jsonObject = JSONUtils.pidAndModelDesc(p, uriString, this.solrMemoization, this.decoratorsAggregate, uriString); jsonObject.put("selected", p.equals(pid)); jsonArray.add(jsonObject); @@ -375,8 +374,7 @@ public Response basic(@PathParam("pid") String pid) { JSONObject jsonObject = new JSONObject(); String uriString = basicURL(pid); - JSONUtils.pidAndModelDesc(pid, jsonObject, - this.fedoraAccess, uriString, + JSONUtils.pidAndModelDesc(pid, jsonObject,uriString, this.solrMemoization, this.decoratorsAggregate, null); return Response.ok().entity(jsonObject.toString()).build(); @@ -389,7 +387,7 @@ public Response basic(@PathParam("pid") String pid) { String uriString = basicURL(pid); JSONUtils.pidAndModelDesc(pid, jsonObject, - this.fedoraAccess, uriString, + uriString, this.solrMemoization, this.decoratorsAggregate, null); return Response.ok().entity(jsonObject.toString()) @@ -424,7 +422,6 @@ public static String basicURL(String pid) { private List solrChildren(String parentPid, List fList) throws IOException { - LOGGER.info("\t children"); List docs = new ArrayList(); List> ll = new ArrayList>(); int rows = 10000; @@ -487,8 +484,9 @@ public int compare(Map o1, Map o2) { }); List values = new ArrayList(); - for (Map m : ll) { LOGGER.info("index =="+m.get("index")); - values.add(m.get("pid"));} + for (Map m : ll) { + values.add(m.get("pid")); + } return values; } diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/ReplicatedFromDecorator.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/ReplicatedFromDecorator.java index abc4b86d7b..713f117b20 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/ReplicatedFromDecorator.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/ReplicatedFromDecorator.java @@ -44,69 +44,80 @@ public class ReplicatedFromDecorator extends AbstractItemDecorator { - public static final Logger LOGGER = Logger.getLogger(ReplicatedFromDecorator.class.getName()); + public static final Logger LOGGER = Logger + .getLogger(ReplicatedFromDecorator.class.getName()); - public static final String REPLICATIONS_DECORATOR_KEY = AbstractItemDecorator.key("REPLICATIONS"); + public static final String REPLICATIONS_DECORATOR_KEY = AbstractItemDecorator + .key("REPLICATIONS"); @Inject @Named("securedFedoraAccess") FedoraAccess fedoraAccess; - - @Override - public String getKey() { - return REPLICATIONS_DECORATOR_KEY; - } - public static List findReplicatedFrom(Document doc) { - List retval = new ArrayList(); - Element rdfElm = XMLUtils.findElement(doc.getDocumentElement(), "RDF",FedoraNamespaces.RDF_NAMESPACE_URI); - if (rdfElm != null) { - Element description = XMLUtils.findElement(rdfElm, "Description", FedoraNamespaces.RDF_NAMESPACE_URI); - if (description != null) { - List elements = XMLUtils.getElements(description, new XMLUtils.ElementsFilter() { + @Override + public String getKey() { + return REPLICATIONS_DECORATOR_KEY; + } - @Override - public boolean acceptElement(Element element) { - return (element.getLocalName().equals("replicatedFrom") && element.getNamespaceURI().equals(FedoraNamespaces.KRAMERIUS_URI)); - } - }); - for (Element el : elements) { retval.add(el); } - } - } - return retval; - } + public static List findReplicatedFrom(Document doc) { + List retval = new ArrayList(); + Element rdfElm = XMLUtils.findElement(doc.getDocumentElement(), "RDF", + FedoraNamespaces.RDF_NAMESPACE_URI); + if (rdfElm != null) { + Element description = XMLUtils.findElement(rdfElm, "Description", + FedoraNamespaces.RDF_NAMESPACE_URI); + if (description != null) { + List elements = XMLUtils.getElements(description, + new XMLUtils.ElementsFilter() { + @Override + public boolean acceptElement(Element element) { + return (element.getLocalName().equals( + "replicatedFrom") && element + .getNamespaceURI().equals( + FedoraNamespaces.KRAMERIUS_URI)); + } + }); + for (Element el : elements) { + retval.add(el); + } + } + } + return retval; + } - @Override - public void decorate(JSONObject jsonObject, - Map runtimeContext) { + @Override + public void decorate(JSONObject jsonObject, + Map runtimeContext) { - try { - if (jsonObject.containsKey("pid")) { - String pid = jsonObject.getString("pid"); - if (!PIDSupport.isComposedPID(pid)) { - Document relsExtDoc = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, context, this.fedoraAccess); - List replicated = findReplicatedFrom(relsExtDoc); - if (!replicated.isEmpty()) { - JSONArray replicatedJSON = new JSONArray(); - for (Element colElm : replicated) { - String rep = colElm.getTextContent(); - replicatedJSON.add(rep); - } - jsonObject.put("replicatedFrom", replicatedJSON); - } - } - } - } catch (DOMException e) { - LOGGER.log(Level.SEVERE,e.getMessage(),e); - } catch (IOException e) { - LOGGER.log(Level.SEVERE,e.getMessage(),e); - } - } + try { + if (jsonObject.containsKey("pid")) { + String pid = jsonObject.getString("pid"); + if (!PIDSupport.isComposedPID(pid)) { + Document relsExtDoc = RELSEXTDecoratorUtils + .getRELSEXTPidDocument(pid, context, + this.fedoraAccess); + List replicated = findReplicatedFrom(relsExtDoc); + if (!replicated.isEmpty()) { + JSONArray replicatedJSON = new JSONArray(); + for (Element colElm : replicated) { + String rep = colElm.getTextContent(); + replicatedJSON.add(rep); + } + jsonObject.put("replicatedFrom", replicatedJSON); + } + } + } + } catch (DOMException e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + } + } - @Override - public boolean apply(JSONObject jsonObject, String context) { - TokenizedPath tpath = super.itemContext(tokenize(context)); - return tpath.isParsed() && tpath.getRestPath().isEmpty(); - } + @Override + public boolean apply(JSONObject jsonObject, String context) { + TokenizedPath tpath = super.itemContext(tokenize(context)); + return tpath.isParsed() && tpath.getRestPath().isEmpty(); + } } diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/RelsExtRightsFlag.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/SolrRightsFlag.java similarity index 62% rename from rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/RelsExtRightsFlag.java rename to rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/SolrRightsFlag.java index dd4db3b0e9..866fb57e6b 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/RelsExtRightsFlag.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/SolrRightsFlag.java @@ -15,20 +15,31 @@ import cz.incad.kramerius.FedoraAccess; import cz.incad.kramerius.FedoraNamespaces; +import cz.incad.kramerius.SolrAccess; +import cz.incad.kramerius.rest.api.k5.client.SolrMemoization; import cz.incad.kramerius.rest.api.k5.client.utils.RELSEXTDecoratorUtils; +import cz.incad.kramerius.rest.api.k5.client.utils.SOLRUtils; import cz.incad.kramerius.utils.XMLUtils; -public class RelsExtRightsFlag extends AbstractItemDecorator { +public class SolrRightsFlag extends AbstractItemDecorator { public static final Logger LOGGER = Logger.getLogger(SolrDataNode.class .getName()); public static final String KEY = AbstractItemDecorator.key("DC_RIGHT");// "DC_RIGHT"; + @Inject @Named("securedFedoraAccess") FedoraAccess fedoraAccess; + @Inject + SolrAccess saccess; + + @Inject + SolrMemoization solrMemo; + + @Override public String getKey() { return KEY; @@ -40,21 +51,17 @@ public void decorate(JSONObject jsonObject, if (jsonObject.containsKey("pid")) { String pid = jsonObject.getString("pid"); try { - Document relsExt = RELSEXTDecoratorUtils.getRELSEXTPidDocument( - pid, runtimeContext, this.fedoraAccess); - Element topElm = XMLUtils.findElement( - relsExt.getDocumentElement(), "Description", - FedoraNamespaces.RDF_NAMESPACE_URI); - Element publicElm = XMLUtils.findElement(topElm, "policy", - FedoraNamespaces.KRAMERIUS_URI); - if (publicElm != null) { - String policyContent = publicElm.getTextContent(); - if (policyContent.contains(":")) { - jsonObject.put("policy", policyContent.split(":")[1]); - } else { - jsonObject.put("policy", policyContent); - } + + Element indexDoc = this.solrMemo.getRememberedIndexedDoc(pid); + if (indexDoc == null) { + indexDoc = this.solrMemo.askForIndexDocument(pid); + } + + String dostupnost = SOLRUtils.value(indexDoc, "dostupnost", String.class); + if (dostupnost != null) { + jsonObject.put("policy", dostupnost); } + } catch (IOException e) { LOGGER.log(Level.SEVERE, e.getMessage(), e); } diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/display/ZoomDecorate.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/display/ZoomDecorate.java index a5742299c3..f5a4c3c88a 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/display/ZoomDecorate.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/item/decorators/display/ZoomDecorate.java @@ -43,10 +43,10 @@ public class ZoomDecorate extends AbstractDisplayDecorate { - public static final Logger LOGGER = Logger.getLogger(ZoomDecorate.class.getName()); - - public static final String ZOOM_KEY = AbstractDisplayDecorate.key("ZOOM"); + public static final Logger LOGGER = Logger.getLogger(ZoomDecorate.class + .getName()); + public static final String ZOOM_KEY = AbstractDisplayDecorate.key("ZOOM"); @Inject @Named("securedFedoraAccess") @@ -54,45 +54,52 @@ public class ZoomDecorate extends AbstractDisplayDecorate { @Inject Provider requestProvider; - - @Override - public String getKey() { - return ZOOM_KEY; - } - - private Object zoom(String vpid) { - JSONObject options = new JSONObject(); - String url = ApplicationURL.applicationURL(this.requestProvider.get()).toString()+"/deepZoom/"+vpid; - options.put("url", url); - options.put("type", KConfiguration.getInstance().getProperty("zoom.viewer","zoomify")); - - return options; - } - - @Override - public void decorate(JSONObject jsonObject, Map context) { - try { - if (containsPidInJSON(jsonObject)) { - String pid = getPidFromJSON(jsonObject); - if (!PIDSupport.isComposedPID(pid)) { - Document relsExt = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, context, this.fedoraAccess); - String url = RelsExtHelper.getRelsExtTilesUrl(relsExt, fedoraAccess); - if (url != null) { - jsonObject.put("zoom", zoom(pid)); - } - } - } - } catch (XPathExpressionException e) { - LOGGER.log(Level.SEVERE, e.getMessage(),e); - } catch (IOException e) { - LOGGER.log(Level.SEVERE, e.getMessage(),e); - } - } - - @Override - public boolean apply(JSONObject jsonObject, String context) { - TokenizedPath tpath = super.itemContext(tokenize(context)); - return (tpath.isParsed() && tpath.getRestPath().isEmpty()); - } - + + @Override + public String getKey() { + return ZOOM_KEY; + } + + private Object zoom(String vpid) { + JSONObject options = new JSONObject(); + String url = ApplicationURL.applicationURL(this.requestProvider.get()) + .toString() + "/deepZoom/" + vpid; + options.put("url", url); + options.put( + "type", + KConfiguration.getInstance().getProperty("zoom.viewer", + "zoomify")); + + return options; + } + + @Override + public void decorate(JSONObject jsonObject, Map context) { + try { + if (containsPidInJSON(jsonObject)) { + String pid = getPidFromJSON(jsonObject); + if (!PIDSupport.isComposedPID(pid)) { + Document relsExt = RELSEXTDecoratorUtils + .getRELSEXTPidDocument(pid, context, + this.fedoraAccess); + String url = RelsExtHelper.getRelsExtTilesUrl(relsExt, + fedoraAccess); + if (url != null) { + jsonObject.put("zoom", zoom(pid)); + } + } + } + } catch (XPathExpressionException e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + } catch (IOException e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + } + } + + @Override + public boolean apply(JSONObject jsonObject, String context) { + TokenizedPath tpath = super.itemContext(tokenize(context)); + return (tpath.isParsed() && tpath.getRestPath().isEmpty()); + } + } diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/utils/JSONUtils.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/utils/JSONUtils.java index b174d98285..41e55af01c 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/utils/JSONUtils.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/utils/JSONUtils.java @@ -25,223 +25,64 @@ import cz.incad.kramerius.TreeNodeProcessor; import cz.incad.kramerius.rest.api.k5.client.JSONDecorator; import cz.incad.kramerius.rest.api.k5.client.JSONDecoratorsAggregate; +import cz.incad.kramerius.rest.api.k5.client.SolrMemoization; import cz.incad.kramerius.utils.XMLUtils; public class JSONUtils { - public static final Logger LOGGER = Logger.getLogger(JSONUtils.class.getName()); - - - public static JSONObject link(JSONObject obj, String key, String link ) { - JSONObject json = new JSONObject(); - json.put("href", link); - obj.put(key, json); - return obj; - } - - public static JSONObject pidAndModelDesc(String pid, JSONObject jsonObject, FedoraAccess fedoraAccess,String callContext, JSONDecoratorsAggregate decoratorsAggregate, String baseLink) - throws IOException { + public static final Logger LOGGER = Logger.getLogger(JSONUtils.class + .getName()); - - Map m = new HashMap(); - jsonObject.put("pid", pid); - if (PIDSupport.isComposedPID(pid)) { - // page model - jsonObject.put("model", "page"); - } else { - Document relsExtDoc = RELSEXTDecoratorUtils.getRELSEXTPidDocument(pid, m, fedoraAccess); - jsonObject.put("model", fedoraAccess.getKrameriusModelName(relsExtDoc)); - } + public static JSONObject link(JSONObject obj, String key, String link) { + JSONObject json = new JSONObject(); + json.put("href", link); + obj.put(key, json); + return obj; + } - // apply decorator - if (callContext != null && decoratorsAggregate != null) { - List ldecs = decoratorsAggregate.getDecorators(); - for (JSONDecorator d : ldecs) { d.before(m); } - for (JSONDecorator d : ldecs) { - if (d.apply(jsonObject, callContext)) { - d.decorate(jsonObject, m); - } - } - for (JSONDecorator d : ldecs) { d.after(); } - } - return jsonObject; - } + public static JSONObject pidAndModelDesc(String pid, JSONObject jsonObject, + String callContext,SolrMemoization soMemo, + JSONDecoratorsAggregate decoratorsAggregate, String baseLink) + throws IOException { - public static JSONObject pidAndModelDesc(String pid, FedoraAccess fedoraAccess, String callContext, JSONDecoratorsAggregate decoratorsAggregate, String baseUrl) - throws IOException { - return pidAndModelDesc(pid, new JSONObject(),fedoraAccess, callContext, decoratorsAggregate, baseUrl); - } - - public static JSONObject miniature(String pid, FedoraAccess fedoraAccess, JSONObject jsonObject) { - throw new UnsupportedOperationException(); - } - - - - - public static JSONArray children(final String pid, FedoraAccess fedoraAccess, SolrAccess solrAccess) { - try { - - JSONArray jsonArray = new JSONArray(); - + Map m = new HashMap(); + jsonObject.put("pid", pid); + if (PIDSupport.isComposedPID(pid)) { + // page model + jsonObject.put("model", "page"); + } else { + Element indexDoc = soMemo.getRememberedIndexedDoc(pid); + if (indexDoc == null) { + indexDoc = soMemo.askForIndexDocument(pid); + } + String fedoraModel = SOLRUtils.value(indexDoc, "fedora.model", String.class); + jsonObject.put("model",fedoraModel); + } - final List children = new ArrayList(); - fedoraAccess.processSubtree(pid, new TreeNodeProcessor() { + // apply decorator + if (callContext != null && decoratorsAggregate != null) { + List ldecs = decoratorsAggregate.getDecorators(); + for (JSONDecorator d : ldecs) { + d.before(m); + } + for (JSONDecorator d : ldecs) { + if (d.apply(jsonObject, callContext)) { + d.decorate(jsonObject, m); + } + } + for (JSONDecorator d : ldecs) { + d.after(); + } + } + return jsonObject; + } - - @Override - public boolean skipBranch(String p, int level) { - return level > 1; - } - - @Override - public void process(String p, int level) throws ProcessSubtreeException { - if (level == 1) { - children.add(p); - } - } - - @Override - public boolean breakProcessing(String p, int level) { - return false; - } - }); - for (String chpid : children) { - jsonArray.add(chpid); - } - return jsonArray; - - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return new JSONArray(); - } catch (ProcessSubtreeException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return new JSONArray(); - } - - } - + public static JSONObject pidAndModelDesc(String pid,String callContext,SolrMemoization solrMemoization, + JSONDecoratorsAggregate decoratorsAggregate, String baseUrl) + throws IOException { + return pidAndModelDesc(pid, new JSONObject(), + callContext, solrMemoization, decoratorsAggregate, baseUrl); + } - private static ObjectPidsPath selectPath(ObjectPidsPath[] paths) { - return paths.length > 0 ? paths[0] : null; - } - - -// public static JSONArray context(String pid, SolrAccess solrAccess, FedoraAccess fedoraAccess) throws IOException { -// JSONArray pathsArr = new JSONArray(); -// ObjectPidsPath[] path = solrAccess.getPath(pid); -// for (ObjectPidsPath objectPidsPath : path) { -// JSONArray onePathArray = new JSONArray(); -// String[] pathFromRootToLeaf = objectPidsPath.getPathFromRootToLeaf(); -// for (String ppid : pathFromRootToLeaf) { -// onePathArray.add(pidAndModelDesc(ppid, fedoraAccess)); -// } -// pathsArr.add(onePathArray); -// } -// return pathsArr; -// } - - - - public static JSONElementTree elementTree(Document doc) { - Map trees = new HashMap(); - Stack stack = new Stack(); - stack.push(doc.getDocumentElement()); - while(!stack.isEmpty()) { - Element topElement = stack.pop(); - JSONElementTree theTree = trees.get(topElement); - if (theTree == null) { - if (topElement.getParentNode() != null && topElement.getParentNode().getNodeType() == Node.ELEMENT_NODE) { - theTree = new JSONElementTree(trees.get(topElement.getParentNode()), topElement); - trees.get(topElement.getParentNode()).addChild(theTree); - } else { - theTree = new JSONElementTree(null, topElement); - } - } - - trees.put(topElement, theTree); - - List elms = XMLUtils.getElements(topElement); - for (Element le : elms) { stack.push(le); } - } - return trees.get(doc.getDocumentElement()); - } - - - public static class JSONElementTree { - - private Element element; - private JSONElementTree parent; - private List children = new ArrayList(); - - public JSONElementTree(JSONElementTree parent, Element elm) { - this.parent = parent; - this.element = elm; - } - - public Element getElement() { - return element; - } - - public JSONElementTree getParent() { - return parent; - } - - public List getChildren() { - return children; - } - - public void addChild(JSONElementTree ch) { - this.children.add(ch); - } - - public void removeChild(JSONElementTree ch) { - this.children.remove(ch); - } - - public String getKey() { - return this.element.getLocalName(); - } - - public JSONObject toJSON(JSONObject parent) { - JSONObject jsonObj = new JSONObject(); - if (XMLUtils.getElements(this.element).isEmpty()) { - jsonObj.put("textcontent", this.element.getTextContent()); - } - JSONObject attributes = new JSONObject(); - NamedNodeMap nmp = this.element.getAttributes(); - for (int i = 0,ll=nmp.getLength(); i < ll; i++) { - Node node = nmp.item(i); - if (node.getNodeType() == Node.ATTRIBUTE_NODE) { - Attr attr = (Attr) node; - attributes.put(attr.getName(), attr.getValue()); - } - } - if (nmp.getLength() > 0) { - jsonObj.put("attributes", attributes); - } - - for (JSONElementTree theTree : this.children) { - JSONObject child = theTree.toJSON(jsonObj); - if (!jsonObj.containsKey(theTree.getKey())) { - jsonObj.put(theTree.getKey(), child); - } else { - JSON json = (JSON) jsonObj.get(theTree.getKey()); - if (json.isArray()) { - ((JSONArray)json).add(child); - } else { - JSONArray jsonArray = new JSONArray(); - jsonArray.add(json); - jsonArray.add(child); - jsonObj.put(theTree.getKey(), jsonArray); - } - } - } - return jsonObj; - } - - } } diff --git a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/virtualcollection/ClientVirtualCollections.java b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/virtualcollection/ClientVirtualCollections.java index ae4815f447..af053d6220 100644 --- a/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/virtualcollection/ClientVirtualCollections.java +++ b/rest/src/main/java/cz/incad/kramerius/rest/api/k5/client/virtualcollection/ClientVirtualCollections.java @@ -49,7 +49,8 @@ @Path("/v5.0/vc") public class ClientVirtualCollections { - public static final Logger LOGGER = Logger.getLogger(ClientVirtualCollections.class.getName()); + public static final Logger LOGGER = Logger + .getLogger(ClientVirtualCollections.class.getName()); @Inject VirtualCollectionsManager manager; @@ -58,42 +59,47 @@ public class ClientVirtualCollections { @Named("securedFedoraAccess") FedoraAccess fedoraAccess; - - @GET - @Path("{pid}") + @GET + @Path("{pid}") @Consumes - @Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8") - public Response oneVirtualCollection(@PathParam("pid")String pid) { - try { - VirtualCollection vc = VirtualCollectionsResource.findVirtualCollection(this.fedoraAccess, pid); - if (vc != null) { - return Response.ok().entity(VirtualCollectionsResource.virtualCollectionTOJSON(vc)).build(); - } else { - throw new ObjectNotFound("cannot find vc '"+pid+"'"); - } - } catch (ObjectNotFound e) { - throw e; - } catch (Exception e) { - LOGGER.log(Level.SEVERE, e.getMessage(),e); - throw new GenericApplicationException(e.getMessage()); - } - } + @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") + public Response oneVirtualCollection(@PathParam("pid") String pid) { + try { + VirtualCollection vc = VirtualCollectionsResource + .findVirtualCollection(this.fedoraAccess, pid); + if (vc != null) { + return Response + .ok() + .entity(VirtualCollectionsResource + .virtualCollectionTOJSON(vc)).build(); + } else { + throw new ObjectNotFound("cannot find vc '" + pid + "'"); + } + } catch (ObjectNotFound e) { + throw e; + } catch (Exception e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + throw new GenericApplicationException(e.getMessage()); + } + } + + @GET + @Produces(MediaType.APPLICATION_JSON + ";charset=utf-8") + public Response get() { + try { + List vcs = VirtualCollectionsManager + .getVirtualCollections(fedoraAccess, + new ArrayList()); + JSONArray jsonArr = new JSONArray(); + for (VirtualCollection vc : vcs) { + jsonArr.add(VirtualCollectionsResource + .virtualCollectionTOJSON(vc)); + } + return Response.ok().entity(jsonArr.toString()).build(); + } catch (Exception e) { + LOGGER.log(Level.SEVERE, e.getMessage(), e); + throw new GenericApplicationException(e.getMessage()); + } + } - - @GET - @Produces(MediaType.APPLICATION_JSON+ ";charset=utf-8") - public Response get() { - try { - List vcs = VirtualCollectionsManager.getVirtualCollections(fedoraAccess, new ArrayList()); - JSONArray jsonArr = new JSONArray(); - for (VirtualCollection vc : vcs) { - jsonArr.add(VirtualCollectionsResource.virtualCollectionTOJSON(vc)); - } - return Response.ok().entity(jsonArr.toString()).build(); - } catch (Exception e) { - LOGGER.log(Level.SEVERE,e.getMessage(),e); - throw new GenericApplicationException(e.getMessage()); - } - } - } diff --git a/search/mvn.debug b/search/mvn.debug deleted file mode 100755 index cefb3d291f..0000000000 --- a/search/mvn.debug +++ /dev/null @@ -1,4 +0,0 @@ -# !/bin/sh -MAVEN_OPTS="-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,suspend=n,server=y -Xms256m -Xmx512m -XX:MaxPermSize=128m" -export MAVEN_OPTS -mvn tomcat:run diff --git a/search/mvn.debug.bat b/search/mvn.debug.bat deleted file mode 100644 index f1e1dcada5..0000000000 --- a/search/mvn.debug.bat +++ /dev/null @@ -1,4 +0,0 @@ - -set MAVEN_OPTS=-Xdebug -Xrunjdwp:transport=dt_socket,address=9999,suspend=n,server=y -Xms256m -Xmx512m -XX:MaxPermSize=128m - -mvn tomcat:run