This is an automated email from the ASF dual-hosted git repository.

danhaywood pushed a commit to branch maint-1.16.2
in repository https://gitbox.apache.org/repos/asf/isis.git

commit b9175ae6d95485561d48e7f12f6f879b75908345
Author: Dan Haywood <d...@haywood-associates.co.uk>
AuthorDate: Tue Mar 13 07:05:00 2018 +0000

    ISIS-1913: adds support for object-icon link in REST API
---
 .../guides/ugvro/_ugvro_layout-resources.adoc      | 27 ++++++-
 .../isis/viewer/restfulobjects/applib/Rel.java     |  1 +
 .../restfulobjects/applib/RepresentationType.java  |  4 ++
 .../restfulobjects/applib/RestfulMediaType.java    |  2 +
 .../applib/domainobjects/DomainObjectResource.java | 17 +++++
 .../domainobjects/DomainObjectReprRenderer.java    | 18 ++++-
 .../server/RestfulObjectsApplication.java          |  2 +
 .../resources/DomainObjectResourceServerside.java  | 82 ++++++++++++++++++++++
 .../server/resources/ImageResourceServerside.java  | 50 +++++++++++++
 9 files changed, 200 insertions(+), 3 deletions(-)

diff --git 
a/adocs/documentation/src/main/asciidoc/guides/ugvro/_ugvro_layout-resources.adoc
 
b/adocs/documentation/src/main/asciidoc/guides/ugvro/_ugvro_layout-resources.adoc
index 8bc924a..75025c3 100644
--- 
a/adocs/documentation/src/main/asciidoc/guides/ugvro/_ugvro_layout-resources.adoc
+++ 
b/adocs/documentation/src/main/asciidoc/guides/ugvro/_ugvro_layout-resources.adoc
@@ -110,6 +110,30 @@ This can also be obtained in JSON format in the usual way 
(by specifying an HTTP
 
 
 
+[[_ugvro_layout-resources_domain-object-icon]]
+== Domain Object Icon
+
+The representation returned by the domain object resource (section 14.4 of the 
RO spec v1.0) has been extended to provide a link to the icon image (`.png`) to 
use:
+
+[source,javascript]
+----
+{
+  "links": [
+    ...
+    {
+      "rel": "urn:org.apache.isis.restfulobjects:rels/object-layout",
+      "href": 
"http://localhost:8080/restful/objects/helloworld.HelloWorldObject/0/object-icon";,
+      "method": "GET",
+      "type": "image/png",
+    },
+  ],
+  ...
+}
+----
+
+Note that because of dynamic icons (the 
xref:../../guides/rgcms/rgcms.adoc#_rgcms_methods_reserved_iconName[`iconName()`]
 supporting method) the image returned can vary on an instance-by-instance 
basis.
+
+
 [[_ugvro_layout-resources_domain-object-layout]]
 == Domain Object Layout
 
@@ -129,8 +153,7 @@ The representation returned by the domain object resource 
(section 14.4 of the R
       "rel": "urn:org.apache.isis.restfulobjects:rels/object-layout",
       "href": 
"http://localhost:8080/restful/objects/helloworld.HelloWorldObject/0/object-layout";,
       "method": "GET",
-      "type": 
"application/json;profile='urn:org.restfulobjects:repr-types/object'",
-      "title": "Object: a"
+      "type": 
"application/json;profile='urn:org.restfulobjects:repr-types/object-layout-bs3'",
     },
   ],
   ...
diff --git 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/Rel.java
 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/Rel.java
index b2d123e..b0794d2 100644
--- 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/Rel.java
+++ 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/Rel.java
@@ -60,6 +60,7 @@ public enum Rel {
     // implementation specific
     CONTRIBUTED_BY(RelDefinition.IMPL, "contributed-by"),
     OBJECT_LAYOUT(RelDefinition.IMPL, "object-layout"),
+    OBJECT_ICON(RelDefinition.IMPL, "object-icon"),
     LAYOUT(RelDefinition.IMPL, "layout"),
     MENUBARS(RelDefinition.IMPL, "menuBars"),
     LOGOUT(RelDefinition.IMPL, "logout");
diff --git 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java
 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java
index 1a6cf5d..b1fea7b 100644
--- 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java
+++ 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RepresentationType.java
@@ -101,6 +101,10 @@ public enum RepresentationType {
             RestfulMediaType.APPLICATION_JSON_OBJECT_LAYOUT_BS3,
             RestfulMediaType.APPLICATION_XML_OBJECT_LAYOUT_BS3,
             null),
+    OBJECT_IMAGE(
+            "image/png",
+            "image/png",
+            null),
     MENUBARS(
             RestfulMediaType.APPLICATION_JSON_LAYOUT_MENUBARS,
             RestfulMediaType.APPLICATION_XML_LAYOUT_MENUBARS,
diff --git 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RestfulMediaType.java
 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RestfulMediaType.java
index 7199e32..99d5a17 100644
--- 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RestfulMediaType.java
+++ 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/RestfulMediaType.java
@@ -86,4 +86,6 @@ public final class RestfulMediaType {
     public final static String APPLICATION_XML_ERROR = XML_BASE + "error";
 //    public final static String APPLICATION_XML_TYPE_ACTION_RESULT = XML_BASE 
+ "type-action-result";
 
+    public final static String IMAGE_PNG = "image/png";
+
 }
diff --git 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/domainobjects/DomainObjectResource.java
 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/domainobjects/DomainObjectResource.java
index aa29ef7..e15f465 100644
--- 
a/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/domainobjects/DomainObjectResource.java
+++ 
b/core/viewer-restfulobjects-applib/src/main/java/org/apache/isis/viewer/restfulobjects/applib/domainobjects/DomainObjectResource.java
@@ -83,6 +83,23 @@ public interface DomainObjectResource {
 
 
     // //////////////////////////////////////////////////////////
+    // domain object image
+    // //////////////////////////////////////////////////////////
+
+    @GET
+    @Path("/{domainType}/{instanceId}/image")
+    @Consumes({ MediaType.WILDCARD })
+    @Produces({
+            "image/png"
+    })
+    public Response image(
+            @PathParam("domainType")
+            final String domainType,
+            @PathParam("instanceId")
+            final String instanceId);
+
+
+    // //////////////////////////////////////////////////////////
     // domain object layout
     // //////////////////////////////////////////////////////////
 
diff --git 
a/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/DomainObjectReprRenderer.java
 
b/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/DomainObjectReprRenderer.java
index c3161b9..0e50982 100644
--- 
a/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/DomainObjectReprRenderer.java
+++ 
b/core/viewer-restfulobjects-rendering/src/main/java/org/apache/isis/viewer/restfulobjects/rendering/domainobjects/DomainObjectReprRenderer.java
@@ -58,7 +58,15 @@ public class DomainObjectReprRenderer extends 
ReprRendererAbstract<DomainObjectR
         String domainType = OidUtils.getDomainType(objectAdapter);
         String instanceId = OidUtils.getInstanceId(objectAdapter);
         final String url = "objects/" + domainType + "/" + instanceId + 
"/object-layout";
-        return LinkBuilder.newBuilder(rendererContext, rel.getName(), 
RepresentationType.DOMAIN_OBJECT, 
url).withTitle(objectAdapter.titleString(null));
+        return LinkBuilder.newBuilder(rendererContext, rel.getName(), 
RepresentationType.OBJECT_LAYOUT, url);
+    }
+
+    public static LinkBuilder newLinkToObjectIconBuilder(final RendererContext 
rendererContext, final ObjectAdapter objectAdapter) {
+        final Rel rel = Rel.OBJECT_ICON;
+        String domainType = OidUtils.getDomainType(objectAdapter);
+        String instanceId = OidUtils.getInstanceId(objectAdapter);
+        final String url = "objects/" + domainType + "/" + instanceId + 
"/image";
+        return LinkBuilder.newBuilder(rendererContext, rel.getName(), 
RepresentationType.OBJECT_IMAGE, url);
     }
 
     private static enum Mode {
@@ -175,6 +183,7 @@ public class DomainObjectReprRenderer extends 
ReprRendererAbstract<DomainObjectR
         if (mode.includeDescribedBy() && 
!rendererContext.suppressDescribedByLinks()) {
             addLinkToDescribedBy();
             addLinkToObjectLayout();
+            addLinkToObjectIcon();
         }
         if(isService && mode.includeUp()) {
             addLinkToUp();
@@ -237,6 +246,13 @@ public class DomainObjectReprRenderer extends 
ReprRendererAbstract<DomainObjectR
         getLinks().arrayAdd(link);
     }
 
+    private void addLinkToObjectIcon() {
+        final LinkBuilder linkBuilder = DomainObjectReprRenderer
+                .newLinkToObjectIconBuilder(getRendererContext(), 
objectAdapter);
+        final JsonRepresentation link = linkBuilder.build();
+        getLinks().arrayAdd(link);
+    }
+
     private void addLinkToUp() {
         final JsonRepresentation link = 
LinkBuilder.newBuilder(rendererContext, Rel.UP.getName(), 
RepresentationType.LIST, "services").build();
         getLinks().arrayAdd(link);
diff --git 
a/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/RestfulObjectsApplication.java
 
b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/RestfulObjectsApplication.java
index 7c33c52..1e13b32 100644
--- 
a/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/RestfulObjectsApplication.java
+++ 
b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/RestfulObjectsApplication.java
@@ -27,6 +27,7 @@ import 
org.apache.isis.viewer.restfulobjects.server.resources.DomainObjectResour
 import 
org.apache.isis.viewer.restfulobjects.server.resources.DomainServiceResourceServerside;
 import 
org.apache.isis.viewer.restfulobjects.server.resources.DomainTypeResourceServerside;
 import 
org.apache.isis.viewer.restfulobjects.server.resources.HomePageResourceServerside;
+import 
org.apache.isis.viewer.restfulobjects.server.resources.ImageResourceServerside;
 import 
org.apache.isis.viewer.restfulobjects.server.resources.MenuBarsResourceServerside;
 import 
org.apache.isis.viewer.restfulobjects.server.resources.SwaggerSpecResource;
 import 
org.apache.isis.viewer.restfulobjects.server.resources.UserResourceServerside;
@@ -41,6 +42,7 @@ public class RestfulObjectsApplication extends 
AbstractJaxRsApplication {
         addClass(DomainTypeResourceServerside.class);
         addClass(UserResourceServerside.class);
         addClass(MenuBarsResourceServerside.class);
+        addClass(ImageResourceServerside.class);
         addClass(DomainObjectResourceServerside.class);
         addClass(DomainServiceResourceServerside.class);
         addClass(VersionResourceServerside.class);
diff --git 
a/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/resources/DomainObjectResourceServerside.java
 
b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/resources/DomainObjectResourceServerside.java
index 938dfa5..853fcc9 100644
--- 
a/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/resources/DomainObjectResourceServerside.java
+++ 
b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/resources/DomainObjectResourceServerside.java
@@ -16,7 +16,9 @@
  */
 package org.apache.isis.viewer.restfulobjects.server.resources;
 
+import java.io.IOException;
 import java.io.InputStream;
+import java.net.URL;
 import java.util.List;
 
 import javax.ws.rs.Consumes;
@@ -31,6 +33,9 @@ import javax.ws.rs.QueryParam;
 import javax.ws.rs.core.MediaType;
 import javax.ws.rs.core.Response;
 
+import com.google.common.base.Strings;
+import com.google.common.io.Resources;
+
 import org.apache.isis.applib.annotation.Where;
 import org.apache.isis.applib.layout.component.ActionLayoutData;
 import org.apache.isis.applib.layout.component.CollectionLayoutData;
@@ -188,6 +193,83 @@ public class DomainObjectResourceServerside extends 
ResourceAbstract implements
     // domain object layout
     // //////////////////////////////////////////////////////////
 
+    static class IconKey {
+        private final Class<?> domainClass;
+        private final String modifier;
+
+        IconKey(final Class<?> domainClass, final String modifier) {
+            this.domainClass = domainClass;
+            this.modifier = modifier;
+        }
+
+        String getImageName() {
+            final StringBuilder buf = new 
StringBuilder(domainClass.getSimpleName());
+            if(!Strings.isNullOrEmpty(modifier)) {
+                buf.append("-").append(modifier);
+            }
+            buf.append(".png");
+            return buf.toString();
+        }
+
+        @Override
+        public boolean equals(final Object o) {
+            if (this == o)
+                return true;
+            if (o == null || getClass() != o.getClass())
+                return false;
+
+            final IconKey iconKey = (IconKey) o;
+
+            if (domainClass != null ? !domainClass.equals(iconKey.domainClass) 
: iconKey.domainClass != null) {
+                return false;
+            }
+            return modifier != null ? modifier.equals(iconKey.modifier) : 
iconKey.modifier == null;
+        }
+
+        @Override
+        public int hashCode() {
+            int result = domainClass != null ? domainClass.hashCode() : 0;
+            result = 31 * result + (modifier != null ? modifier.hashCode() : 
0);
+            return result;
+        }
+
+        byte[] toBytes() {
+            String imageName =  getImageName();
+            URL resource = Resources.getResource(domainClass, imageName);
+            try {
+                return Resources.toByteArray(resource);
+            } catch (IOException e) {
+                return null;
+            }
+        }
+    }
+
+    @Override
+    @GET
+    @Path("/{domainType}/{instanceId}/image")
+    @Consumes({ MediaType.WILDCARD })
+    @Produces({
+            "image/png"
+    })
+    @PrettyPrinting
+    public Response image(
+            @PathParam("domainType")
+            final String domainType,
+            @PathParam("instanceId")
+            final String instanceId) {
+
+        init(RepresentationType.OBJECT_LAYOUT, Where.ANYWHERE, 
RepresentationService.Intent.NOT_APPLICABLE);
+
+        final ObjectAdapter objectAdapter = 
getObjectAdapterElseThrowNotFound(domainType, instanceId);
+        final ObjectSpecification objectSpec = 
objectAdapter.getSpecification();
+        final String iconName = objectSpec.getIconName(objectAdapter);
+        final Class<?> correspondingClass = objectSpec.getCorrespondingClass();
+        final IconKey iconKey = new IconKey(correspondingClass, iconName);
+        final byte[] bytes = iconKey.toBytes();
+        return bytes != null
+                ? Response.ok(bytes).build()
+                : Response.status(Response.Status.NOT_FOUND).build();
+    }
 
     @Override
     @GET
diff --git 
a/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/resources/ImageResourceServerside.java
 
b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/resources/ImageResourceServerside.java
new file mode 100644
index 0000000..04712f1
--- /dev/null
+++ 
b/core/viewer-restfulobjects-server/src/main/java/org/apache/isis/viewer/restfulobjects/server/resources/ImageResourceServerside.java
@@ -0,0 +1,50 @@
+/*
+ *  Licensed to the Apache Software Foundation (ASF) under one
+ *  or more contributor license agreements.  See the NOTICE file
+ *  distributed with this work for additional information
+ *  regarding copyright ownership.  The ASF licenses this file
+ *  to you under the Apache License, Version 2.0 (the
+ *  "License"); you may not use this file except in compliance
+ *  with the License.  You may obtain a copy of the License at
+ *
+ *        http://www.apache.org/licenses/LICENSE-2.0
+ *
+ *  Unless required by applicable law or agreed to in writing,
+ *  software distributed under the License is distributed on an
+ *  "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ *  KIND, either express or implied.  See the License for the
+ *  specific language governing permissions and limitations
+ *  under the License.
+ */
+package org.apache.isis.viewer.restfulobjects.server.resources;
+
+import java.io.IOException;
+import java.net.URL;
+
+import javax.ws.rs.GET;
+import javax.ws.rs.Path;
+import javax.ws.rs.Produces;
+import javax.ws.rs.core.Response;
+
+import com.google.common.io.Resources;
+
+@Path("/image")
+public class ImageResourceServerside extends ResourceAbstract {
+
+    @GET
+    @Path("/")
+    @Produces("image/png")
+    public Response image() throws IOException {
+
+        URL resource = Resources.getResource(getClass(), "SimpleObject.png");
+        byte[] bytes = Resources.toByteArray(resource);
+
+//        Response.ResponseBuilder response = Response.ok(file);
+//        response.header("Content-Disposition",
+//                "attachment; filename=SimpleObject.png");
+//        return response.build();
+
+        return Response.ok(bytes).build();
+
+    }
+}

-- 
To stop receiving notification emails like this one, please contact
danhayw...@apache.org.

Reply via email to