From d92258b6bb638563b11d5760a1ffb20158ed1b25 Mon Sep 17 00:00:00 2001 From: Jeff Cantrill Date: Mon, 18 May 2015 14:26:39 -0400 Subject: [PATCH] [OSJC-167] Modify ResourceFactory to create resources from an inputstream --- .../internal/restclient/ResourceFactory.java | 25 ++++++++++++++++--- .../restclient/IResourceFactory.java | 10 ++++++++ .../restclient/ResourceFactoryException.java | 3 +++ 3 files changed, 34 insertions(+), 4 deletions(-) diff --git a/src/main/java/com/openshift/internal/restclient/ResourceFactory.java b/src/main/java/com/openshift/internal/restclient/ResourceFactory.java index ce3559de..6a9588c6 100644 --- a/src/main/java/com/openshift/internal/restclient/ResourceFactory.java +++ b/src/main/java/com/openshift/internal/restclient/ResourceFactory.java @@ -8,6 +8,8 @@ ******************************************************************************/ package com.openshift.internal.restclient; +import java.io.IOException; +import java.io.InputStream; import java.lang.reflect.Constructor; import java.lang.reflect.InvocationTargetException; import java.util.ArrayList; @@ -15,6 +17,7 @@ import java.util.List; import java.util.Map; +import org.apache.commons.io.IOUtils; import org.jboss.dmr.ModelNode; import com.openshift.internal.restclient.model.Build; @@ -90,11 +93,25 @@ private List buildList(final String version, List items, R return resources; } + @Override + public T create(InputStream input) { + try { + String resource = IOUtils.toString(input, "UTF-8"); + return create(resource); + } catch (IOException e) { + throw new ResourceFactoryException(e, "There was an exception creating the resource from the InputStream"); + } + } + public T create(String response) { - ModelNode node = ModelNode.fromJSONString(response); - String version = node.get(APIVERSION).asString(); - ResourceKind kind = ResourceKind.valueOf(node.get(KIND).asString()); - return create(node, version, kind); + try { + ModelNode node = ModelNode.fromJSONString(response); + String version = node.get(APIVERSION).asString(); + ResourceKind kind = ResourceKind.valueOf(node.get(KIND).asString()); + return create(node, version, kind); + }catch(Exception e) { + throw new ResourceFactoryException(e, "There was an exception creating the resource from: %s", response); + } } public T create(String version, ResourceKind kind) { diff --git a/src/main/java/com/openshift/restclient/IResourceFactory.java b/src/main/java/com/openshift/restclient/IResourceFactory.java index 4987f2e6..1d0525e1 100644 --- a/src/main/java/com/openshift/restclient/IResourceFactory.java +++ b/src/main/java/com/openshift/restclient/IResourceFactory.java @@ -8,6 +8,7 @@ ******************************************************************************/ package com.openshift.restclient; +import java.io.InputStream; import java.util.List; import com.openshift.restclient.model.IResource; @@ -38,6 +39,15 @@ public interface IResourceFactory { */ T create(String response) ; + /** + * Create a resource from a response string + * @param input Read the given input stream which assumes the input + * is parsable JSON representing a valid resource + * @return + * @throws ResourceFactoryException if it is unable to create resources + */ + T create(InputStream input) ; + /** * Create a resource for a given version and kind * @param version diff --git a/src/main/java/com/openshift/restclient/ResourceFactoryException.java b/src/main/java/com/openshift/restclient/ResourceFactoryException.java index ec572edc..c2b0c51e 100644 --- a/src/main/java/com/openshift/restclient/ResourceFactoryException.java +++ b/src/main/java/com/openshift/restclient/ResourceFactoryException.java @@ -8,6 +8,9 @@ ******************************************************************************/ package com.openshift.restclient; +/** + * @author jeff.cantrill + */ public class ResourceFactoryException extends OpenShiftException { private static final long serialVersionUID = 4215359575300693464L;