You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@isis.apache.org by da...@apache.org on 2018/01/24 15:16:43 UTC

[isis] 07/20: ISIS-1569: adds a convenience Util class for ContentMappingService to remove boilerplate in subclass implementations (parsing the acceptable media types).

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

danhaywood pushed a commit to branch ISIS-1569-replay-commands
in repository https://gitbox.apache.org/repos/asf/isis.git

commit 02f03b001716bfb0457a2ea1a78970c16a6042f7
Author: Dan Haywood <da...@haywood-associates.co.uk>
AuthorDate: Thu Jan 18 10:30:46 2018 +0000

    ISIS-1569: adds a convenience Util class for ContentMappingService to remove boilerplate in subclass implementations (parsing the acceptable media types).
---
 .../isis/applib/conmap/ContentMappingService.java  | 27 ++++++++++++++++++++++
 1 file changed, 27 insertions(+)

diff --git a/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingService.java b/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingService.java
index d577dfb..49b0eb5 100644
--- a/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingService.java
+++ b/core/applib/src/main/java/org/apache/isis/applib/conmap/ContentMappingService.java
@@ -19,9 +19,12 @@
 package org.apache.isis.applib.conmap;
 
 import java.util.List;
+import java.util.Map;
 
 import javax.ws.rs.core.MediaType;
 
+import com.google.common.base.Joiner;
+
 import org.apache.isis.applib.annotation.Programmatic;
 
 public interface ContentMappingService {
@@ -32,4 +35,28 @@ public interface ContentMappingService {
     @Programmatic
     Object map(Object object, final List<MediaType> acceptableMediaTypes);
 
+    /**
+     * Convenience utilities for implementations of {@link ContentMappingService}.
+     */
+    public static class Util {
+
+        public static String determineDomainType(final List<MediaType> acceptableMediaTypes) {
+            for (MediaType acceptableMediaType : acceptableMediaTypes) {
+                final Map<String, String> parameters = acceptableMediaType.getParameters();
+                final String domainType = parameters.get("x-ro-domain-type");
+                if(domainType != null) {
+                    return domainType;
+                }
+            }
+            throw new IllegalArgumentException(
+                    "Could not locate x-ro-domain-type parameter in any of the provided media types; got: " + Joiner.on(", ").join(acceptableMediaTypes));
+        }
+
+        public static boolean isSupported(
+                final Class<?> clazz,
+                final List<MediaType> acceptableMediaTypes) {
+            final String domainType = determineDomainType(acceptableMediaTypes);
+            return clazz.getName().equals(domainType);
+        }
+    }
 }

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