Skip to content

Commit

Permalink
In HAL compact mode, the curies section is rendered as an empty array.
Browse files Browse the repository at this point in the history
this is needed to have the HAL browser rendering data correctly. Potential HAL browser bug mikekelly/hal-browser#71
  • Loading branch information
ujibang committed Oct 12, 2015
1 parent 2f343fe commit 8921b6e
Show file tree
Hide file tree
Showing 7 changed files with 66 additions and 38 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -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))));
}
}
}

Expand All @@ -82,7 +85,7 @@ protected void addReturnedProperty(final List<DBObject> embeddedData, final Repr
.filter((props) -> props.keySet().stream()
.anyMatch((k) -> k.equals("id") || k.equals("_id")))
.count();

rep.addProperty("_returned", count);
}

Expand Down
25 changes: 18 additions & 7 deletions src/main/java/org/restheart/hal/Representation.java
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ public Representation(String href) {
links = new BasicDBObject();

properties.append("_links", links);

links.put("self", new BasicDBObject("href", href));
}

Expand Down Expand Up @@ -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()));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -95,7 +99,7 @@ private void addEmbeddedData(List<DBObject> embeddedData, final Representation r
throws IllegalQueryParamenterException {
if (embeddedData != null) {
addReturnedProperty(embeddedData, rep);

if (!embeddedData.isEmpty()) {
embeddedDocuments(embeddedData, requestPath, exchange, context, rep);
}
Expand Down Expand Up @@ -147,4 +151,4 @@ private void embeddedDocuments(List<DBObject> embeddedData, String requestPath,
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Expand All @@ -83,7 +87,7 @@ private void addProperties(final Representation rep, RequestContext context) {
}
}
}

private void addEmbeddedData(
final List<DBObject> embeddedData,
final Representation rep,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 8921b6e

Please sign in to comment.