Author: adietish
Date: 2010-10-20 16:06:59 -0400 (Wed, 20 Oct 2010)
New Revision: 25957
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.docs/
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/CloudTypeValidator.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/UrlToCloudTypeConverter.java
Log:
[JBIDE-7371] all tests for server type implemented
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-10-20 20:03:04
UTC (rev 25956)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/ChangeLog 2010-10-20 20:06:59
UTC (rev 25957)
@@ -1,6 +1,8 @@
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
+ (DCNS): Added API enum (for api requests)
+ (getServerType): moved to instance method, reuse existing client code
2010-10-12 Jeff Johnston <jjohnstn(a)redhat.com>
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
20:03:04 UTC (rev 25956)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.core/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClient.java 2010-10-20
20:06:59 UTC (rev 25957)
@@ -18,10 +18,8 @@
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;
@@ -54,23 +52,17 @@
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
+ UNKNOWN, MOCK, EC2
}
-
public static Logger logger = Logger.getLogger(DeltaCloudClient.class);
private static enum DCNS {
- INSTANCES, REALMS, IMAGES, HARDWARE_PROFILES, KEYS, START, STOP, REBOOT, DESTROY;
+ API, INSTANCES, REALMS, IMAGES, HARDWARE_PROFILES, KEYS, START, STOP, REBOOT, DESTROY;
@Override
public String toString() {
@@ -86,8 +78,16 @@
private String username;
private String password;
- public DeltaCloudClient(URL url, String username, String password) throws
MalformedURLException {
+ public DeltaCloudClient(String url) throws MalformedURLException {
+ this(new URL(url), null, null);
+ }
+ public DeltaCloudClient(String url, String username, String password) throws
MalformedURLException {
+ this(new URL(url), username, password);
+ }
+
+ public DeltaCloudClient(URL url, String username, String password) {
+
logger.debug("Creating new Delta Cloud Client for Server: " + url);
this.baseUrl = url;
@@ -97,8 +97,11 @@
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));
+ if (username != null && password != null) {
+ httpClient.getCredentialsProvider().setCredentials(
+ new AuthScope(baseUrl.getHost(), baseUrl.getPort()),
+ new UsernamePasswordCredentials(username, password));
+ }
String requestUrl = baseUrl.toString() + path;
logger.debug("Sending Request to: " + requestUrl);
@@ -157,58 +160,29 @@
return "";
}
- public static DeltaCloudType getDeltaCloudType(String url) {
- DeltaCloudType cloudType = DeltaCloudType.UNKNOWN;
+ public DeltaCloudType getServerType() {
+ DeltaCloudType serverType = 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))));
+ String query = "?format=xml";
+ String apiResponse = sendRequest(DCNS.API + query, RequestType.GET);
+ DocumentBuilder db = DocumentBuilderFactory.newInstance().newDocumentBuilder();
+ Document document = db.parse(new InputSource(new StringReader(apiResponse)));
- 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());
- }
+ 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();
+ serverType = DeltaCloudType.valueOf(driverValue.toUpperCase());
}
}
- } catch (MalformedURLException e) {
- cloudType = DeltaCloudType.INVALID_URL;
} catch (Exception e) {
- cloudType = DeltaCloudType.UNKNOWN;
+ serverType = DeltaCloudType.UNKNOWN;
}
- return cloudType;
+ return serverType;
}
- 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 {
String query = "?image_id=" + imageId;
Property changes on: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.docs
___________________________________________________________________
Name: svn:ignore
+ target
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/CloudTypeValidator.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/CloudTypeValidator.java 2010-10-20
20:03:04 UTC (rev 25956)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/CloudTypeValidator.java 2010-10-20
20:06:59 UTC (rev 25957)
@@ -20,8 +20,7 @@
@Override
public IStatus validate(Object value) {
if (value != null
- && !DeltaCloudClient.DeltaCloudType.UNKNOWN.equals(value)
- && !DeltaCloudClient.DeltaCloudType.INVALID_URL.equals(value)) {
+ && !DeltaCloudClient.DeltaCloudType.UNKNOWN.equals(value)) {
return ValidationStatus.ok();
} else {
return
ValidationStatus.error(WizardMessages.getString("IllegalCloudUrl.msg"));
//$NON-NLS-1$
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/UrlToCloudTypeConverter.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/UrlToCloudTypeConverter.java 2010-10-20
20:03:04 UTC (rev 25956)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.ui/src/org/jboss/tools/internal/deltacloud/ui/wizards/UrlToCloudTypeConverter.java 2010-10-20
20:06:59 UTC (rev 25957)
@@ -10,6 +10,8 @@
******************************************************************************/
package org.jboss.tools.internal.deltacloud.ui.wizards;
+import java.net.MalformedURLException;
+
import org.eclipse.core.databinding.conversion.IConverter;
import org.jboss.tools.deltacloud.core.client.DeltaCloudClient;
@@ -31,7 +33,11 @@
@Override
public Object convert(Object fromObject) {
String deltaCloudUrl = (String) fromObject;
- return DeltaCloudClient.getDeltaCloudType(deltaCloudUrl);
+ try {
+ return new DeltaCloudClient(deltaCloudUrl, "",
"").getServerType();
+ } catch (MalformedURLException e) {
+ return null;
+ }
}
}