diff --git a/src/main/java/org/restheart/hal/AbstractRepresentationFactory.java b/src/main/java/org/restheart/hal/AbstractRepresentationFactory.java index 161250c27e..efb9f93625 100644 --- a/src/main/java/org/restheart/hal/AbstractRepresentationFactory.java +++ b/src/main/java/org/restheart/hal/AbstractRepresentationFactory.java @@ -73,7 +73,10 @@ protected void addSizeAndTotalPagesProperties(final long size, final RequestCont float _pagesize = context.getPagesize() + 0f; rep.addProperty("_size", size); - rep.addProperty("_total_pages", Math.max(1, Math.round(Math.ceil(_size / _pagesize)))); + + if (context.getPagesize() > 0) { + rep.addProperty("_total_pages", Math.max(1, Math.round(Math.ceil(_size / _pagesize)))); + } } } @@ -82,7 +85,7 @@ protected void addReturnedProperty(final List embeddedData, final Repr .filter((props) -> props.keySet().stream() .anyMatch((k) -> k.equals("id") || k.equals("_id"))) .count(); - + rep.addProperty("_returned", count); } diff --git a/src/main/java/org/restheart/hal/Representation.java b/src/main/java/org/restheart/hal/Representation.java index 43ca8d3da5..3d405efa7d 100644 --- a/src/main/java/org/restheart/hal/Representation.java +++ b/src/main/java/org/restheart/hal/Representation.java @@ -55,7 +55,7 @@ public Representation(String href) { links = new BasicDBObject(); properties.append("_links", links); - + links.put("self", new BasicDBObject("href", href)); } @@ -90,19 +90,30 @@ public BasicDBObject asDBObject() { public void addLink(Link link) { links.putAll((BSONObject) link.getDBObject()); } - + /** * - * @param link - * @param inArray + * @param linkArrayRef + * @return the created or existing link array */ - public void addLink(Link link, boolean inArray) { - BasicDBList linkArray = (BasicDBList) links.get(link.getRef()); + public BasicDBList addLinkArray(String linkArrayRef) { + BasicDBList linkArray = (BasicDBList) links.get(linkArrayRef); if (linkArray == null) { linkArray = new BasicDBList(); - links.append(link.getRef(), linkArray); + links.append(linkArrayRef, linkArray); } + + return linkArray; + } + + /** + * + * @param link + * @param inArray + */ + public void addLink(Link link, boolean inArray) { + BasicDBList linkArray = addLinkArray(link.getRef()); linkArray.add(link.getDBObject().get(link.getRef())); diff --git a/src/main/java/org/restheart/handlers/collection/CollectionRepresentationFactory.java b/src/main/java/org/restheart/handlers/collection/CollectionRepresentationFactory.java index 431ce21c46..d0963ad4f0 100644 --- a/src/main/java/org/restheart/handlers/collection/CollectionRepresentationFactory.java +++ b/src/main/java/org/restheart/handlers/collection/CollectionRepresentationFactory.java @@ -58,25 +58,29 @@ public Representation getRepresentation(HttpServerExchange exchange, RequestCont final Representation rep = createRepresentation(exchange, context, requestPath); addEmbeddedData(embeddedData, rep, requestPath, exchange, context); - + if (context.getHalMode() == HAL_MODE.FULL || context.getHalMode() == HAL_MODE.F) { addProperties(rep, context, size); - + addPaginationLinks(exchange, context, size, rep); addLinkTemplates(exchange, context, rep, requestPath); + + // curies + rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL + + "/{rel}.html", true), true); + } else { + // empty curies section. this is needed due to HAL browser issue + // https://github.com/mikekelly/hal-browser/issues/71 + rep.addLinkArray("curies"); } - - addSizeAndTotalPagesProperties(size, context, rep); - // curies - rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL - + "/{rel}.html", true), true); + addSizeAndTotalPagesProperties(size, context, rep); return rep; } - + private void addProperties(final Representation rep, final RequestContext context, long size) { // add the collection properties final DBObject collProps = context.getCollectionProps(); @@ -95,7 +99,7 @@ private void addEmbeddedData(List embeddedData, final Representation r throws IllegalQueryParamenterException { if (embeddedData != null) { addReturnedProperty(embeddedData, rep); - + if (!embeddedData.isEmpty()) { embeddedDocuments(embeddedData, requestPath, exchange, context, rep); } @@ -147,4 +151,4 @@ private void embeddedDocuments(List embeddedData, String requestPath, } } } -} \ No newline at end of file +} diff --git a/src/main/java/org/restheart/handlers/collection/GetCollectionHandler.java b/src/main/java/org/restheart/handlers/collection/GetCollectionHandler.java index 27407f3c68..f73b51beb5 100644 --- a/src/main/java/org/restheart/handlers/collection/GetCollectionHandler.java +++ b/src/main/java/org/restheart/handlers/collection/GetCollectionHandler.java @@ -29,7 +29,6 @@ import org.restheart.handlers.IllegalQueryParamenterException; import org.restheart.handlers.PipedHttpHandler; import org.restheart.handlers.RequestContext; -import org.restheart.handlers.RequestContext.HAL_MODE; import org.restheart.utils.HttpStatus; import org.restheart.utils.ResponseHelper; import org.slf4j.Logger; diff --git a/src/main/java/org/restheart/handlers/database/DBRepresentationFactory.java b/src/main/java/org/restheart/handlers/database/DBRepresentationFactory.java index d970e8fa04..130004ac9c 100644 --- a/src/main/java/org/restheart/handlers/database/DBRepresentationFactory.java +++ b/src/main/java/org/restheart/handlers/database/DBRepresentationFactory.java @@ -57,19 +57,23 @@ public Representation getRepresentation(HttpServerExchange exchange, RequestCont if (context.getHalMode() == HAL_MODE.FULL || context.getHalMode() == HAL_MODE.F) { addProperties(rep, context); - + addPaginationLinks(exchange, context, size, rep); - + addLinkTemplates(exchange, context, rep, requestPath); + + // curies + rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL + + "/{rel}.html", true), true); + } else { + // empty curies section. this is needed due to HAL browser issue + // https://github.com/mikekelly/hal-browser/issues/71 + rep.addLinkArray("curies"); } - - // curies - rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL - + "/{rel}.html", true), true); return rep; } - + private void addProperties(final Representation rep, RequestContext context) { final DBObject dbProps = context.getDbProps(); @@ -83,7 +87,7 @@ private void addProperties(final Representation rep, RequestContext context) { } } } - + private void addEmbeddedData( final List embeddedData, final Representation rep, diff --git a/src/main/java/org/restheart/handlers/document/DocumentRepresentationFactory.java b/src/main/java/org/restheart/handlers/document/DocumentRepresentationFactory.java index 2757e30451..395aabd837 100644 --- a/src/main/java/org/restheart/handlers/document/DocumentRepresentationFactory.java +++ b/src/main/java/org/restheart/handlers/document/DocumentRepresentationFactory.java @@ -122,13 +122,17 @@ public Representation getRepresentation(String href, HttpServerExchange exchange rep.addLink(new Link("rh:document", parentPath + "/{docid}?id_type={type}", true)); } - } - if (!isEmbedded) { - rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL - + "/{rel}.html", true), true); + if (!isEmbedded) { + rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL + + "/{rel}.html", true), true); + } + } else if (!isEmbedded) { + // empty curies section. this is needed due to HAL browser issue + // https://github.com/mikekelly/hal-browser/issues/71 + rep.addLinkArray("curies"); } - + return rep; } diff --git a/src/main/java/org/restheart/handlers/root/RootRepresentationFactory.java b/src/main/java/org/restheart/handlers/root/RootRepresentationFactory.java index ea6c6afd38..8a46bfef6a 100644 --- a/src/main/java/org/restheart/handlers/root/RootRepresentationFactory.java +++ b/src/main/java/org/restheart/handlers/root/RootRepresentationFactory.java @@ -28,7 +28,6 @@ import org.restheart.handlers.IllegalQueryParamenterException; import org.restheart.handlers.RequestContext; import org.restheart.handlers.RequestContext.HAL_MODE; -import org.restheart.handlers.database.DBRepresentationFactory; import org.slf4j.Logger; import org.slf4j.LoggerFactory; @@ -58,11 +57,15 @@ public Representation getRepresentation(HttpServerExchange exchange, RequestCont addPaginationLinks(exchange, context, size, rep); addLinkTemplates(exchange, context, rep, requestPath); - } - //curies link - rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL - + "/{rel}.html", true), true); + //curies + rep.addLink(new Link("rh", "curies", Configuration.RESTHEART_ONLINE_DOC_URL + + "/{rel}.html", true), true); + } else { + // empty curies section. this is needed due to HAL browser issue + // https://github.com/mikekelly/hal-browser/issues/71 + rep.addLinkArray("curies"); + } return rep; }