Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,16 @@
******************************************************************************/
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;
import java.util.HashMap;
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;
Expand Down Expand Up @@ -90,11 +93,25 @@ private List<IResource> buildList(final String version, List<ModelNode> items, R
return resources;
}

@Override
public <T extends IResource> 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 extends IResource> 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 extends IResource> T create(String version, ResourceKind kind) {
Expand Down
10 changes: 10 additions & 0 deletions src/main/java/com/openshift/restclient/IResourceFactory.java
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
******************************************************************************/
package com.openshift.restclient;

import java.io.InputStream;
import java.util.List;

import com.openshift.restclient.model.IResource;
Expand Down Expand Up @@ -38,6 +39,15 @@ public interface IResourceFactory {
*/
<T extends IResource> 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 extends IResource> T create(InputStream input) ;

/**
* Create a resource for a given version and kind
* @param version
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
******************************************************************************/
package com.openshift.restclient;

/**
* @author jeff.cantrill
*/
public class ResourceFactoryException extends OpenShiftException {

private static final long serialVersionUID = 4215359575300693464L;
Expand Down