Author: adietish
Date: 2010-10-29 03:56:02 -0400 (Fri, 29 Oct 2010)
New Revision: 26118
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusCode.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusRange.java
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
Log:
[JBIDE-7437] corrected client to pass test on missing resource
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-29
07:55:41 UTC (rev 26117)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java 2010-10-29
07:56:02 UTC (rev 26118)
@@ -56,12 +56,8 @@
public class DeltaCloudClient implements API {
private static final String PEM_FILE_SUFFIX = "pem";
- private static final int HTTP_STATUSCODE_NOTFOUND = 404;
- private static final int HTTP_STATUSCODE_FORBIDDEN = 403;
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 int HTTP_STATUSCODE_SERVERERROR = 500;
- private static final int HTTP_STATUSCODE_CLIENTERROR = 400;
public static Logger logger = Logger.getLogger(DeltaCloudClient.class);
@@ -145,38 +141,24 @@
private void throwOnHttpErrors(String requestUrl, HttpResponse httpResponse) throws
DeltaCloudClientException {
int statusCode = httpResponse.getStatusLine().getStatusCode();
- if (isHttpForbiddenError(statusCode)) {
+ if (HttpStatusCode.OK.isStatus(statusCode)) {
+ return;
+ }
+ else if (HttpStatusCode.FORBIDDEN.isStatus(statusCode)) {
throw new DeltaCloudAuthException(
MessageFormat.format("The server reported an authorization error
\"{0}\" on requesting \"{1}\"",
httpResponse.getStatusLine().getReasonPhrase(), requestUrl));
- } else if (isHttpNotFoundError(statusCode)) {
+ } else if (HttpStatusCode.NOT_FOUND.isStatus(statusCode)) {
throw new DeltaCloudNotFoundException(
MessageFormat.format("The server could not find the resource
\"{0}\"", requestUrl));
- } else if (isHttpClientError(statusCode) || isHttpServerError(statusCode)) {
+ } else if (HttpStatusRange.CLIENT_ERROR.isInRange(statusCode)
+ || HttpStatusRange.SERVER_ERROR.isInRange(statusCode)) {
throw new DeltaCloudClientException(
MessageFormat.format("The server reported an error \"{0}\" on
requesting \"{1}\"",
httpResponse.getStatusLine().getReasonPhrase(), requestUrl));
}
}
- private boolean isHttpNotFoundError(int statusCode) {
- return statusCode == HTTP_STATUSCODE_NOTFOUND;
- }
-
- private boolean isHttpForbiddenError(int statusCode) {
- return statusCode == HTTP_STATUSCODE_FORBIDDEN;
- }
-
- private boolean isHttpClientError(int statusCode) {
- return (statusCode - HTTP_STATUSCODE_CLIENTERROR) >= 0
- && (statusCode - HTTP_STATUSCODE_CLIENTERROR) < 100;
- }
-
- private boolean isHttpServerError(int statusCode) {
- return (statusCode - HTTP_STATUSCODE_SERVERERROR) >= 0
- && (statusCode - HTTP_STATUSCODE_SERVERERROR) < 100;
- }
-
private String getResponse(HttpEntity entity) throws IOException,
DeltaCloudClientException {
if (entity == null) {
@@ -440,25 +422,6 @@
sendRequest(DCNS.INSTANCES + "/" + instanceId, RequestType.DELETE);
}
- // private void checkForErrors(Document d) throws DeltaCloudClientException
- // {
- // NodeList n = d.getElementsByTagName("error");
- // 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");
- // }
- // }
- // }
-
private Instance buildInstance(String xml) throws DeltaCloudClientException {
try {
Instance instance = JAXB.unmarshal(new StringReader(xml), Instance.class);
@@ -636,37 +599,37 @@
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 document = getDocument(path);
ArrayList<T> dco = new ArrayList<T>();
-
NodeList nodeList = document.getElementsByTagName(elementName);
for (int i = 0; i < nodeList.getLength(); i++) {
- dco.add(buildDeltaCloudObject(clazz, nodeList.item(i)));
+ dco.add(buildDeltaCloudObject(clazz, nodeToString(nodeList.item(i))));
}
return dco;
- } catch (DeltaCloudClientException e) {
+ } catch(DeltaCloudClientException e) {
throw e;
} catch (Exception e) {
throw new DeltaCloudClientException("Could not list object of type " +
clazz, e);
}
}
+ private Document getDocument(String path) throws DeltaCloudClientException,
ParserConfigurationException,
+ SAXException, IOException {
+ InputSource is = new InputSource(new StringReader(sendRequest(path,
RequestType.GET)));
+ DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
+ DocumentBuilder db = dbf.newDocumentBuilder();
+ Document document = db.parse(is);
+ return document;
+ }
+
@SuppressWarnings("unchecked")
- private <T extends Object> T buildDeltaCloudObject(Class<T> clazz, Node
node) throws DeltaCloudClientException {
+ private <T extends Object> T buildDeltaCloudObject(Class<T> clazz, String
node) throws DeltaCloudClientException {
if (clazz.equals(Instance.class)) {
- return (T) buildInstance(nodeToString(node));
+ return (T) buildInstance(node);
} else if (clazz.equals(HardwareProfile.class)) {
- return (T) buildHardwareProfile(nodeToString(node));
+ return (T) buildHardwareProfile(node);
} else {
- return JAXB.unmarshal(new StringReader(nodeToString(node)), clazz);
+ return JAXB.unmarshal(new StringReader(node), clazz);
}
}
@@ -689,7 +652,5 @@
} catch (TransformerException e) {
throw new DeltaCloudClientException("Error transforming node to string",
e);
}
-
}
-
}
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusCode.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusCode.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusCode.java 2010-10-29
07:56:02 UTC (rev 26118)
@@ -0,0 +1,29 @@
+/*******************************************************************************
+ * Copyright (c) 2010 Red Hat, Inc.
+ * Distributed under license by Red Hat, Inc. All rights reserved.
+ * This program is made available under the terms of the
+ * Eclipse Public License v1.0 which accompanies this distribution,
+ * and is available at
http://www.eclipse.org/legal/epl-v10.html
+ *
+ * Contributors:
+ * Red Hat, Inc. - initial API and implementation
+ ******************************************************************************/
+package org.jboss.tools.deltacloud.core.client;
+public enum HttpStatusCode {
+
+ OK(200), NOT_FOUND(404), FORBIDDEN(403);
+
+ private int code;
+
+ private HttpStatusCode(int code) {
+ this.code = code;
+ }
+
+ public int getCode() {
+ return code;
+ }
+
+ public boolean isStatus(int statusCode) {
+ return code == statusCode;
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusCode.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusRange.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusRange.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusRange.java 2010-10-29
07:56:02 UTC (rev 26118)
@@ -0,0 +1,18 @@
+package org.jboss.tools.deltacloud.core.client;
+
+public enum HttpStatusRange {
+ CLIENT_ERROR(400, 499), SERVER_ERROR(500, 599);
+
+ private int start;
+ private int stop;
+
+ HttpStatusRange(int start, int stop) {
+ this.start = start;
+ this.stop = stop;
+ }
+
+ public boolean isInRange(int statusCode) {
+ return statusCode >= start
+ && statusCode <= stop;
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/HttpStatusRange.java
___________________________________________________________________
Name: svn:mime-type
+ text/plain