Author: adietish
Date: 2010-10-20 07:10:48 -0400 (Wed, 20 Oct 2010)
New Revision: 25938
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
Log:
[JBIDE-7371] moved deltacloud url checking to deltacloud client, added tests
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-10-20 11:10:19
UTC (rev 25937)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-10-20 11:10:48
UTC (rev 25938)
@@ -1,3 +1,7 @@
+2010-10-20 André Dietisheim <adietish(a)redhat.com>
+
+ * src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java (getDeltaCloudType):
moved from UI to deltacloud client
+
2010-10-12 Jeff Johnston <jjohnstn(a)redhat.com>
* build.properties: Remove lib/deltacloudclient jar specification as it
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF 2010-10-20
11:10:19 UTC (rev 25937)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/META-INF/MANIFEST.MF 2010-10-20
11:10:48 UTC (rev 25938)
@@ -10,7 +10,8 @@
org.jboss.tools.usage;bundle-version="1.0.0";resolution:=optional
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
Bundle-ActivationPolicy: lazy
-Export-Package:
org.jboss.tools.deltacloud.core;x-friends:="org.jboss.tools.deltacloud.ui"
+Export-Package:
org.jboss.tools.deltacloud.core;x-friends:="org.jboss.tools.deltacloud.ui",
+ org.jboss.tools.deltacloud.core.client
Bundle-ClassPath: .,
lib/apache-mime4j-0.6.jar,
lib/commons-codec-1.3.jar,
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java 2010-10-20
11:10:19 UTC (rev 25937)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java 2010-10-20
11:10:48 UTC (rev 25938)
@@ -18,8 +18,10 @@
import java.io.InputStreamReader;
import java.io.StringReader;
import java.io.StringWriter;
+import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
+import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
@@ -50,83 +52,81 @@
import org.w3c.dom.NodeList;
import org.xml.sax.InputSource;
-public class DeltaCloudClient implements API
-{
+public class DeltaCloudClient implements API {
+
+ private static final String REQUEST_URL_API = "/api?format=xml";
+
+ private static final String HTTPHEADER_KEY_ACCEPT = "Accept"; //$NON-NLS-1$
+ private static final String HTTPHEADER_VALUE_ACCEPTXML =
"application/xml;q=1.0"; //$NON-NLS-1$
+ private static final String DOCUMENT_ELEMENT_DRIVER = "driver"; //$NON-NLS-1$
+ private static final String DOCUMENT_ELEMENT_API = "api"; //$NON-NLS-1$
+ private static final String URLCONNECTION_ENCODING = "UTF-8"; //$NON-NLS-1$
+
+ public static enum DeltaCloudType {
+ INVALID_URL, UNKNOWN, MOCK, EC2
+ }
+
+
public static Logger logger = Logger.getLogger(DeltaCloudClient.class);
-
- private static enum DCNS
- {
+
+ private static enum DCNS {
INSTANCES, REALMS, IMAGES, HARDWARE_PROFILES, KEYS, START, STOP, REBOOT, DESTROY;
-
+
@Override
- public String toString()
- {
+ public String toString() {
return "/" + name().toLowerCase();
}
- }
-
- private static enum RequestType { POST, GET, DELETE };
-
+ }
+
+ private static enum RequestType {
+ POST, GET, DELETE
+ };
+
private URL baseUrl;
-
private String username;
-
private String password;
-
- public DeltaCloudClient(URL url, String username, String password) throws
MalformedURLException
- {
-
+
+ public DeltaCloudClient(URL url, String username, String password) throws
MalformedURLException {
+
logger.debug("Creating new Delta Cloud Client for Server: " + url);
-
+
this.baseUrl = url;
-
this.username = username;
-
this.password = password;
}
- private String sendRequest(String path, RequestType requestType) throws
DeltaCloudClientException
- {
+ private String sendRequest(String path, RequestType requestType) throws
DeltaCloudClientException {
DefaultHttpClient httpClient = new DefaultHttpClient();
- httpClient.getCredentialsProvider().setCredentials(new
AuthScope(baseUrl.getHost(), baseUrl.getPort()), new UsernamePasswordCredentials(username,
password));
-
+ httpClient.getCredentialsProvider().setCredentials(new AuthScope(baseUrl.getHost(),
baseUrl.getPort()),
+ new UsernamePasswordCredentials(username, password));
+
String requestUrl = baseUrl.toString() + path;
logger.debug("Sending Request to: " + requestUrl);
-
- try
- {
+
+ try {
HttpUriRequest request = null;
- if(requestType == RequestType.POST)
- {
+ if (requestType == RequestType.POST) {
request = new HttpPost(requestUrl);
- }
- else if (requestType == RequestType.DELETE)
- {
+ } else if (requestType == RequestType.DELETE) {
request = new HttpDelete(requestUrl);
- }
- else
- {
+ } else {
request = new HttpGet(requestUrl);
}
-
+
request.setHeader("Accept", "application/xml");
HttpResponse httpResponse = httpClient.execute(request);
-
+
HttpEntity entity = httpResponse.getEntity();
-
-
- if (entity != null)
- {
+
+ if (entity != null) {
InputStream is = entity.getContent();
String xml = readInputStreamToString(is);
httpClient.getConnectionManager().shutdown();
-
+
logger.debug("Response\n" + xml);
return xml;
}
- }
- catch(IOException e)
- {
+ } catch (IOException e) {
logger.error("Error processing request to: " + requestUrl, e);
throw new DeltaCloudClientException("Error processing request to: " +
requestUrl, e);
} catch (Exception e) {
@@ -134,58 +134,100 @@
}
throw new DeltaCloudClientException("Could not execute request to:" +
requestUrl);
}
-
- private static String readInputStreamToString(InputStream is) throws
DeltaCloudClientException
- {
- try
- {
- try
- {
- if (is != null)
- {
+
+ private static String readInputStreamToString(InputStream is) throws
DeltaCloudClientException {
+ try {
+ try {
+ if (is != null) {
StringBuilder sb = new StringBuilder();
String line;
-
+
BufferedReader reader = new BufferedReader(new InputStreamReader(is,
"UTF-8"));
- while ((line = reader.readLine()) != null)
- {
- sb.append(line).append("\n");
+ while ((line = reader.readLine()) != null) {
+ sb.append(line).append("\n");
}
return sb.toString();
}
- }
- finally
- {
+ } finally {
is.close();
}
- }
- catch(Exception e)
- {
+ } catch (Exception e) {
throw new DeltaCloudClientException("Error converting Response to String",
e);
}
return "";
}
-
+
+ public static DeltaCloudType getDeltaCloudType(String url) {
+ DeltaCloudType cloudType = DeltaCloudType.UNKNOWN;
+ try {
+ Object o = getURLContent(url + REQUEST_URL_API); //$NON-NLS-1$
+ if (o instanceof InputStream) {
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ Document document = db.parse(
+ new InputSource(new StringReader(getXML((InputStream) o))));
+
+ NodeList elements = document.getElementsByTagName(DOCUMENT_ELEMENT_API);
+ if (elements.getLength() > 0) {
+ Node n = elements.item(0);
+ Node driver = n.getAttributes().getNamedItem(DOCUMENT_ELEMENT_DRIVER);
+ if (driver != null) {
+ String driverValue = driver.getNodeValue();
+ cloudType = DeltaCloudType.valueOf(driverValue.toUpperCase());
+ }
+ }
+ }
+ } catch (MalformedURLException e) {
+ cloudType = DeltaCloudType.INVALID_URL;
+ } catch (Exception e) {
+ cloudType = DeltaCloudType.UNKNOWN;
+ }
+ return cloudType;
+ }
+
+ private static Object getURLContent(String url) throws IOException {
+ URL u = new URL(url);
+ URLConnection connection = u.openConnection();
+ connection.setRequestProperty(HTTPHEADER_KEY_ACCEPT, HTTPHEADER_VALUE_ACCEPTXML);
+ return connection.getContent();
+ }
+
+ private static String getXML(InputStream is) throws UnsupportedEncodingException,
IOException {
+ try {
+ if (is == null) {
+ return "";
+ }
+ StringBuilder sb = new StringBuilder();
+ String line = "";
+ BufferedReader reader = new BufferedReader(new InputStreamReader(is,
URLCONNECTION_ENCODING));
+ while ((line = reader.readLine()) != null) {
+ sb.append(line).append("\n"); //$NON-NLS-1$
+ }
+ return sb.toString();
+ } finally {
+ is.close();
+ }
+ }
+
@Override
- public Instance createInstance(String imageId) throws DeltaCloudClientException
- {
+ public Instance createInstance(String imageId) throws DeltaCloudClientException {
String query = "?image_id=" + imageId;
return buildInstance(sendRequest(DCNS.INSTANCES + query, RequestType.POST));
}
@Override
- public Instance createInstance(String imageId, String profileId, String realmId, String
name) throws DeltaCloudClientException
- {
+ public Instance createInstance(String imageId, String profileId, String realmId, String
name)
+ throws DeltaCloudClientException {
return createInstance(imageId, profileId, realmId, name, null, null, null);
}
- public Instance createInstance(String imageId, String profileId, String realmId, String
name, String memory, String storage) throws DeltaCloudClientException
- {
+ public Instance createInstance(String imageId, String profileId, String realmId, String
name, String memory,
+ String storage) throws DeltaCloudClientException {
return createInstance(imageId, profileId, realmId, name, null, memory, storage);
}
- public Instance createInstance(String imageId, String profileId, String realmId, String
name, String keyname, String memory, String storage) throws DeltaCloudClientException
- {
+ public Instance createInstance(String imageId, String profileId, String realmId, String
name, String keyname,
+ String memory, String storage) throws DeltaCloudClientException {
String query = "?image_id=" + imageId + "&hwp_id=" + profileId
+ "&realm_id=" + realmId + "&name=" + name;
if (memory != null)
query += "&hwp_memory=" + memory;
@@ -198,52 +240,44 @@
}
@Override
- public HardwareProfile listProfile(String profileId) throws DeltaCloudClientException
- {
+ public HardwareProfile listProfile(String profileId) throws DeltaCloudClientException {
String request = DCNS.HARDWARE_PROFILES + "/" + profileId;
return JAXB.unmarshal(sendRequest(request, RequestType.GET), HardwareProfile.class);
}
@Override
- public List<HardwareProfile> listProfiles() throws DeltaCloudClientException
- {
+ public List<HardwareProfile> listProfiles() throws DeltaCloudClientException {
return listDeltaCloudObjects(HardwareProfile.class, DCNS.HARDWARE_PROFILES.toString(),
"hardware_profile");
}
-
+
@Override
- public List<Image> listImages() throws DeltaCloudClientException
- {
+ public List<Image> listImages() throws DeltaCloudClientException {
return listDeltaCloudObjects(Image.class, DCNS.IMAGES.toString(), "image");
}
@Override
- public Image listImages(String imageId) throws DeltaCloudClientException
- {
+ public Image listImages(String imageId) throws DeltaCloudClientException {
return JAXB.unmarshal(sendRequest(DCNS.IMAGES + "/" + imageId,
RequestType.GET), Image.class);
}
@Override
- public List<Instance> listInstances() throws DeltaCloudClientException
- {
-
+ public List<Instance> listInstances() throws DeltaCloudClientException {
+
return listDeltaCloudObjects(Instance.class, DCNS.INSTANCES.toString(),
"instance");
}
@Override
- public Instance listInstances(String instanceId) throws DeltaCloudClientException
- {
+ public Instance listInstances(String instanceId) throws DeltaCloudClientException {
return buildInstance(sendRequest(DCNS.INSTANCES + "/" + instanceId,
RequestType.GET));
}
-
+
@Override
- public List<Realm> listRealms() throws DeltaCloudClientException
- {
+ public List<Realm> listRealms() throws DeltaCloudClientException {
return listDeltaCloudObjects(Realm.class, DCNS.REALMS.toString(), "realm");
}
@Override
- public Realm listRealms(String realmId) throws DeltaCloudClientException
- {
+ public Realm listRealms(String realmId) throws DeltaCloudClientException {
return JAXB.unmarshal(sendRequest(DCNS.REALMS + "/" + realmId,
RequestType.GET), Realm.class);
}
@@ -263,13 +297,12 @@
StringBuffer sb = new StringBuffer();
String line;
BufferedReader reader = new BufferedReader(new StringReader(keyText.get(0)));
- while ((line = reader.readLine()) != null)
- {
+ while ((line = reader.readLine()) != null) {
// We must trim off the white-space from the xml
// Complete white-space lines are to be ignored.
String trimmedLine = line.trim();
if (trimmedLine.length() > 0)
- sb.append(trimmedLine).append("\n");
+ sb.append(trimmedLine).append("\n");
}
FileWriter w = new FileWriter(keyFile);
w.write(sb.toString());
@@ -282,108 +315,95 @@
public void deleteKey(String keyname) throws DeltaCloudClientException {
sendRequest(DCNS.KEYS + "/" + keyname, RequestType.DELETE);
}
-
+
@Override
- public void rebootInstance(String instanceId) throws DeltaCloudClientException
- {
+ public void rebootInstance(String instanceId) throws DeltaCloudClientException {
sendRequest(DCNS.INSTANCES + "/" + instanceId + DCNS.REBOOT,
RequestType.GET);
}
@Override
- public void shutdownInstance(String instanceId) throws DeltaCloudClientException
- {
+ public void shutdownInstance(String instanceId) throws DeltaCloudClientException {
sendRequest(DCNS.INSTANCES + "/" + instanceId + DCNS.STOP, RequestType.GET);
}
-
+
@Override
- public void startInstance(String instanceId) throws DeltaCloudClientException
- {
+ public void startInstance(String instanceId) throws DeltaCloudClientException {
sendRequest(DCNS.INSTANCES + "/" + instanceId + DCNS.START,
RequestType.GET);
}
@Override
- public void destroyInstance(String instanceId) throws DeltaCloudClientException
- {
+ public void destroyInstance(String instanceId) throws DeltaCloudClientException {
sendRequest(DCNS.INSTANCES + "/" + instanceId, RequestType.DELETE);
}
-
- private void checkForErrors(Document d) throws DeltaCloudClientException
- {
+
+ private void checkForErrors(Document d) throws DeltaCloudClientException {
NodeList n = d.getElementsByTagName("error");
- for (int i = 0; i < n.getLength(); ++i)
- {
+ for (int i = 0; i < n.getLength(); ++i) {
Node node = n.item(i);
Node statusNode = node.getAttributes().getNamedItem("status");
if (statusNode != null) {
- String status = node.getAttributes().getNamedItem("status").getNodeValue();
- if (status.equals("403"))
- throw new DeltaCloudAuthException("Authorization error");
- else if (status.equals("404"))
- throw new DeltaCloudClientException("Not found");
- else
- throw new DeltaCloudClientException("Connection error");
+ String status =
node.getAttributes().getNamedItem("status").getNodeValue();
+ if (status.equals("403"))
+ throw new DeltaCloudAuthException("Authorization error");
+ else if (status.equals("404"))
+ throw new DeltaCloudClientException("Not found");
+ else
+ throw new DeltaCloudClientException("Connection error");
}
}
}
-
- private Instance buildInstance(String xml) throws DeltaCloudClientException
- {
- try
- {
+
+ private Instance buildInstance(String xml) throws DeltaCloudClientException {
+ try {
Instance instance = JAXB.unmarshal(new StringReader(xml), Instance.class);
-
+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(xml)));
-
+
checkForErrors(document);
-
+
instance.setImageId(getIdFromHref(getAttributeValues(document, "image",
"href").get(0))); //$NON-NLS-1$ //$NON-NLS-2$
instance.setProfileId(getIdFromHref(getAttributeValues(document,
"hardware_profile", "href").get(0))); //$NON-NLS-1$ //$NON-NLS-2$
getProfileProperties(instance, getPropertyNodes(document,
"hardware_profile")); //$NON-NLS-1$
instance.setRealmId(getIdFromHref(getAttributeValues(document, "realm",
"href").get(0))); //$NON-NLS-1$ //$NON-NLS-2$
instance.setState(getElementText(document, "state").get(0)); //$NON-NLS-1$
getAuthentication(document, instance);
-
+
ArrayList<Instance.Action> actions = new ArrayList<Instance.Action>();
- for(String s : getAttributeValues(document, "link", "rel"))
//$NON-NLS-1$ //$NON-NLS-2$
+ for (String s : getAttributeValues(document, "link", "rel"))
//$NON-NLS-1$ //$NON-NLS-2$
{
actions.add(Instance.Action.valueOf(s.toUpperCase()));
}
instance.setActions(actions);
-
+
return instance;
- }
- catch (DeltaCloudClientException e) {
+ } catch (DeltaCloudClientException e) {
throw e;
- }
- catch(Exception e)
- {
+ } catch (Exception e) {
DeltaCloudClientException newException = new
DeltaCloudClientException(e.getLocalizedMessage());
throw newException;
}
}
- private HardwareProfile buildHardwareProfile(String xml) throws
DeltaCloudClientException
- {
- try
- {
+ private HardwareProfile buildHardwareProfile(String xml) throws
DeltaCloudClientException {
+ try {
HardwareProfile profile = JAXB.unmarshal(new StringReader(xml),
HardwareProfile.class);
-
+
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(new InputSource(new StringReader(xml)));
-
+
checkForErrors(document);
-
+
List<Node> nodes = getPropertyNodes(document, "hardware_profile");
//$NON-NLS-1$
-
+
for (Node n : nodes) {
Property p = new Property();
p.setName(n.getAttributes().getNamedItem("name").getNodeValue());
//$NON-NLS-1$
p.setValue(n.getAttributes().getNamedItem("value").getNodeValue());
//$NON-NLS-1$
p.setUnit(n.getAttributes().getNamedItem("unit").getNodeValue());
//$NON-NLS-1$
- p.setKind(n.getAttributes().getNamedItem("kind").getNodeValue()) ;
//$NON-NLS-1$
+ p.setKind(n.getAttributes().getNamedItem("kind").getNodeValue());
//$NON-NLS-1$
if (p.getKind().equals("range")) { //$NON-NLS-1$
NodeList children = n.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
@@ -394,8 +414,7 @@
p.setRange(first, last);
}
}
- }
- else if (p.getKind().equals("enum")) { //$NON-NLS-1$
+ } else if (p.getKind().equals("enum")) { //$NON-NLS-1$
ArrayList<String> enums = new ArrayList<String>();
NodeList children = n.getChildNodes();
for (int i = 0; i < children.getLength(); ++i) {
@@ -415,45 +434,36 @@
profile.getProperties().add(p);
}
return profile;
- }
- catch(Exception e)
- {
+ } catch (Exception e) {
e.printStackTrace();
}
return null;
}
- private List<String> getAttributeValues(Document document, String elementName,
String attributeName)
- {
+ private List<String> getAttributeValues(Document document, String elementName,
String attributeName) {
NodeList elements = document.getElementsByTagName(elementName);
ArrayList<String> values = new ArrayList<String>();
- for(int i = 0; i < elements.getLength(); i++)
- {
+ for (int i = 0; i < elements.getLength(); i++) {
values.add(elements.item(i).getAttributes().getNamedItem(attributeName).getTextContent());
}
return values;
}
-
- private List<String> getElementText(Document document, String elementName)
- {
+
+ private List<String> getElementText(Document document, String elementName) {
NodeList elements = document.getElementsByTagName(elementName);
ArrayList<String> values = new ArrayList<String>();
- for(int i = 0; i < elements.getLength(); i++)
- {
+ for (int i = 0; i < elements.getLength(); i++) {
values.add(elements.item(i).getTextContent());
}
return values;
}
-
- private List<Node> getPropertyNodes(Document document, String elementName)
- {
+
+ private List<Node> getPropertyNodes(Document document, String elementName) {
NodeList elements = document.getElementsByTagName(elementName);
ArrayList<Node> values = new ArrayList<Node>();
- for(int i = 0; i < elements.getLength(); i++)
- {
+ for (int i = 0; i < elements.getLength(); i++) {
NodeList children = elements.item(i).getChildNodes();
- for (int j = 0; j < children.getLength(); ++j)
- {
+ for (int j = 0; j < children.getLength(); ++j) {
Node child = children.item(j);
if (child.getNodeName().equals("property")) { //$NON-NLS-1$
values.add(child);
@@ -462,11 +472,10 @@
}
return values;
}
-
+
private void getAuthentication(Document document, Instance instance) {
NodeList elements = document.getElementsByTagName("authentication");
- for (int i = 0; i < elements.getLength(); i++)
- {
+ for (int i = 0; i < elements.getLength(); i++) {
Node element = elements.item(i);
NamedNodeMap attrs = element.getAttributes();
Node type = attrs.getNamedItem("type"); //$NON-NLS-1$
@@ -487,7 +496,7 @@
}
}
}
-
+
private void getProfileProperties(Instance instance, List<Node> propertyNodes) {
if (propertyNodes != null) {
for (Iterator<Node> i = propertyNodes.iterator(); i.hasNext();) {
@@ -496,13 +505,13 @@
String name = attrs.getNamedItem("name").getNodeValue(); //$NON-NLS-1$
if (name.equals("memory")) { //$NON-NLS-1$
String memory = attrs.getNamedItem("value").getNodeValue(); //$NON-NLS-1$
- if (attrs.getNamedItem("unit") != null) { //$NON-NLS-1$
+ if (attrs.getNamedItem("unit") != null) { //$NON-NLS-1$
memory += " " + attrs.getNamedItem("unit").getNodeValue();
//$NON-NLS-1$
}
instance.setMemory(memory);
} else if (name.equals("storage")) { //$NON-NLS-1$
String storage = attrs.getNamedItem("value").getNodeValue();
//$NON-NLS-1$
- if (attrs.getNamedItem("unit") != null) { //$NON-NLS-1$
+ if (attrs.getNamedItem("unit") != null) { //$NON-NLS-1$
storage += " " + attrs.getNamedItem("unit").getNodeValue();
//$NON-NLS-1$
}
instance.setStorage(storage);
@@ -513,86 +522,68 @@
}
}
}
-
- private String getIdFromHref(String href)
- {
+
+ private String getIdFromHref(String href) {
return href.substring(href.lastIndexOf("/") + 1, href.length());
}
-
- private <T extends DeltaCloudObject> List<T>
listDeltaCloudObjects(Class<T> clazz, String path, String elementName) throws
DeltaCloudClientException
- {
- try
- {
+
+ private <T extends DeltaCloudObject> List<T>
listDeltaCloudObjects(Class<T> clazz, String path, String elementName)
+ throws DeltaCloudClientException {
+ try {
InputSource is = new InputSource(new StringReader(sendRequest(path,
RequestType.GET)));
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document document = db.parse(is);
-
+
checkForErrors(document);
-
- document.getElementsByTagName(path).toString();
-
+
+ document.getElementsByTagName(path).toString();
+
ArrayList<T> dco = new ArrayList<T>();
-
+
NodeList nodeList = document.getElementsByTagName(elementName);
- for(int i = 0; i < nodeList.getLength(); i ++)
- {
+ for (int i = 0; i < nodeList.getLength(); i++) {
dco.add(buildDeltaCloudObject(clazz, nodeList.item(i)));
}
return dco;
- } catch (DeltaCloudClientException e)
- {
+ } catch (DeltaCloudClientException e) {
throw e;
- }
- catch(Exception e)
- {
+ } catch (Exception e) {
throw new DeltaCloudClientException("Could not list object of type " +
clazz, e);
}
}
-
+
@SuppressWarnings("unchecked")
- private <T extends Object> T buildDeltaCloudObject(Class<T> clazz, Node
node) throws DeltaCloudClientException
- {
- if(clazz.equals(Instance.class))
- {
+ private <T extends Object> T buildDeltaCloudObject(Class<T> clazz, Node
node) throws DeltaCloudClientException {
+ if (clazz.equals(Instance.class)) {
return (T) buildInstance(nodeToString(node));
- }
- else if (clazz.equals(HardwareProfile.class))
- {
+ } else if (clazz.equals(HardwareProfile.class)) {
return (T) buildHardwareProfile(nodeToString(node));
- }
- else
- {
+ } else {
return JAXB.unmarshal(new StringReader(nodeToString(node)), clazz);
}
}
- public boolean performInstanceAction(String instanceId, String action) throws
DeltaCloudClientException
- {
- Instance instance = listInstances(instanceId);
- if(instance.getActionNames().contains(action))
- {
- String request = DCNS.INSTANCES + "/" + instanceId +
"/" + action.toLowerCase();
- sendRequest(request, RequestType.POST);
- return true;
- }
- return false;
- }
-
- private String nodeToString(Node node) throws DeltaCloudClientException
- {
- try
- {
+ public boolean performInstanceAction(String instanceId, String action) throws
DeltaCloudClientException {
+ Instance instance = listInstances(instanceId);
+ if (instance.getActionNames().contains(action)) {
+ String request = DCNS.INSTANCES + "/" + instanceId + "/" +
action.toLowerCase();
+ sendRequest(request, RequestType.POST);
+ return true;
+ }
+ return false;
+ }
+
+ private String nodeToString(Node node) throws DeltaCloudClientException {
+ try {
StringWriter writer = new StringWriter();
Transformer t = TransformerFactory.newInstance().newTransformer();
t.transform(new DOMSource(node), new StreamResult(writer));
return writer.toString();
- }
- catch (TransformerException e)
- {
+ } catch (TransformerException e) {
throw new DeltaCloudClientException("Error transforming node to string",
e);
}
-
+
}
}