Author: adietish
Date: 2011-03-29 09:49:49 -0400 (Tue, 29 Mar 2011)
New Revision: 30101
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/AbstractHttpTransport.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/Base64Coder.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/IHttpTransport.java
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/URLConnectionTransport.java
Removed:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/lib/
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/.classpath
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/ChangeLog
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/META-INF/MANIFEST.MF
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/build.properties
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java
trunk/deltacloud/tests/org.jboss.tools.deltacloud.client.test/src/org/jboss/tools/internal/deltacloud/client/test/core/client/InstanceMockIntegrationTest.java
Log:
[JBIDE-8635] removed http client and replaced by urlConnection
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/.classpath
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/.classpath 2011-03-29
13:48:42 UTC (rev 30100)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/.classpath 2011-03-29
13:49:49 UTC (rev 30101)
@@ -1,13 +1,5 @@
<?xml version="1.0" encoding="UTF-8"?>
<classpath>
- <classpathentry exported="true" kind="lib"
path="lib/apache-mime4j-0.6.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/commons-codec-1.3.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/commons-logging-1.1.1.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/httpclient-4.0.1.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/httpcore-4.0.1.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/httpcore-nio-4.0.1.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/httpmime-4.0.1.jar"/>
- <classpathentry exported="true" kind="lib"
path="lib/log4j-1.2.14.jar"/>
<classpathentry kind="con"
path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.6"/>
<classpathentry kind="con"
path="org.eclipse.pde.core.requiredPlugins"/>
<classpathentry kind="src" path="src"/>
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/ChangeLog
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/ChangeLog 2011-03-29
13:48:42 UTC (rev 30100)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/ChangeLog 2011-03-29
13:49:49 UTC (rev 30101)
@@ -1,3 +1,10 @@
+2011-03-29 André Dietisheim <André Dietisheim@adietisheim-thinkpad>
+
+ * src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java
+ (request):
+ (DeltaCloudClientImpl):
+ [JBIDE-8635] removed http client and replaced by urlConnection
+
2011-03-11 André Dietisheim <André Dietisheim@adietisheim-thinkpad>
* src/org/jboss/tools/deltacloud/core/client/StateAware.java:
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/META-INF/MANIFEST.MF
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/META-INF/MANIFEST.MF 2011-03-29
13:48:42 UTC (rev 30100)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/META-INF/MANIFEST.MF 2011-03-29
13:49:49 UTC (rev 30101)
@@ -8,18 +8,10 @@
Bundle-ActivationPolicy: lazy
Bundle-Localization: plugin
Bundle-RequiredExecutionEnvironment: JavaSE-1.6
-Bundle-ClassPath: .,
- lib/apache-mime4j-0.6.jar,
- lib/commons-codec-1.3.jar,
- lib/commons-logging-1.1.1.jar,
- lib/httpclient-4.0.1.jar,
- lib/httpcore-4.0.1.jar,
- lib/httpcore-nio-4.0.1.jar,
- lib/httpmime-4.0.1.jar,
- lib/log4j-1.2.14.jar
Require-Bundle: org.eclipse.core.runtime
Export-Package: org.jboss.tools.deltacloud.client,
org.jboss.tools.deltacloud.core.client,
org.jboss.tools.deltacloud.core.client.request,
+ org.jboss.tools.deltacloud.core.client.transport,
org.jboss.tools.deltacloud.core.client.unmarshal,
org.jboss.tools.deltacloud.core.client.utils
Modified: trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/build.properties
===================================================================
--- trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/build.properties 2011-03-29
13:48:42 UTC (rev 30100)
+++ trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/build.properties 2011-03-29
13:49:49 UTC (rev 30101)
@@ -1,18 +1,8 @@
-source.. = src/
output.. = bin/
bin.includes = META-INF/,\
- .,\
- lib/apache-mime4j-0.6.jar,\
- lib/commons-codec-1.3.jar,\
- lib/commons-logging-1.1.1.jar,\
- lib/httpclient-4.0.1.jar,\
- lib/httpcore-4.0.1.jar,\
- lib/httpcore-nio-4.0.1.jar,\
- lib/httpmime-4.0.1.jar,\
- lib/log4j-1.2.14.jar,\
about.html,\
pom.xml,\
-src.includes = lib/,\
+ src.includes = lib/,\
pom.xml
jars.extra.classpath = lib/apache-mime4j-0.6.jar,\
lib/commons-codec-1.3.jar,\
Modified:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java 2011-03-29
13:48:42 UTC (rev 30100)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/DeltaCloudClientImpl.java 2011-03-29
13:49:49 UTC (rev 30101)
@@ -10,23 +10,12 @@
*******************************************************************************/
package org.jboss.tools.deltacloud.core.client;
-import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
-import java.net.URL;
-import java.net.UnknownHostException;
import java.text.MessageFormat;
import java.util.ArrayList;
import java.util.List;
-import org.apache.http.HttpResponse;
-import org.apache.http.auth.AuthScope;
-import org.apache.http.auth.UsernamePasswordCredentials;
-import org.apache.http.client.methods.HttpDelete;
-import org.apache.http.client.methods.HttpGet;
-import org.apache.http.client.methods.HttpPost;
-import org.apache.http.client.methods.HttpUriRequest;
-import org.apache.http.impl.client.DefaultHttpClient;
import org.jboss.tools.deltacloud.core.client.API.Driver;
import org.jboss.tools.deltacloud.core.client.request.CreateInstanceRequest;
import org.jboss.tools.deltacloud.core.client.request.CreateKeyRequest;
@@ -43,6 +32,8 @@
import org.jboss.tools.deltacloud.core.client.request.ListRealmsRequest;
import org.jboss.tools.deltacloud.core.client.request.PerformActionRequest;
import org.jboss.tools.deltacloud.core.client.request.TypeRequest;
+import org.jboss.tools.deltacloud.core.client.transport.IHttpTransport;
+import org.jboss.tools.deltacloud.core.client.transport.URLConnectionTransport;
import org.jboss.tools.deltacloud.core.client.unmarshal.APIUnmarshaller;
import org.jboss.tools.deltacloud.core.client.unmarshal.HardwareProfileUnmarshaller;
import org.jboss.tools.deltacloud.core.client.unmarshal.HardwareProfilesUnmarshaller;
@@ -61,127 +52,26 @@
public class DeltaCloudClientImpl implements DeltaCloudClient {
private String baseUrl;
- private String username;
- private String password;
+ private IHttpTransport transport;
- public DeltaCloudClientImpl(String url) throws MalformedURLException,
- DeltaCloudClientException {
+ public DeltaCloudClientImpl(String url) throws MalformedURLException,
DeltaCloudClientException {
this(url, null, null);
}
- public DeltaCloudClientImpl(String url, String username, String password) throws
DeltaCloudClientException {
- this.baseUrl = url;
- this.username = username;
- this.password = password;
+ public DeltaCloudClientImpl(String url, String username, String password) throws
MalformedURLException,
+ DeltaCloudClientException {
+ this(url, new URLConnectionTransport(username, password));
}
- protected InputStream request(DeltaCloudRequest deltaCloudRequest)
- throws DeltaCloudClientException {
- DefaultHttpClient httpClient = new DefaultHttpClient();
- try {
- URL url = deltaCloudRequest.getUrl();
- addCredentials(url, httpClient, username, password);
- HttpUriRequest request = createRequest(deltaCloudRequest);
- HttpResponse httpResponse = httpClient.execute(request);
- throwOnHttpErrors(deltaCloudRequest.getUrl(), httpResponse);
- if (httpResponse.getEntity() == null) {
- return null;
- }
- InputStream in = httpResponse.getEntity().getContent();
-// StringWriter writer = new StringWriter();
-// int data = -1;
-//
-// while(((data = in.read()) != -1)) {
-// writer.write(data);
-// }
-// System.err.println(writer.toString());
- return in;
- } catch (DeltaCloudClientException e) {
- throw e;
- } catch (MalformedURLException e) {
- throw new DeltaCloudClientException(MessageFormat.format(
- "Could not connect to \"{0}\". The url is invalid.",
deltaCloudRequest.toString()), e);
- } catch (IOException e) {
- throw new DeltaCloudClientException(e);
- } catch (Exception e) {
- throw new DeltaCloudClientException(e);
- }
+ public DeltaCloudClientImpl(String url, IHttpTransport transport) throws
DeltaCloudClientException {
+ this.baseUrl = url;
+ this.transport = transport;
}
- private void throwOnHttpErrors(URL requestUrl, HttpResponse httpResponse)
- throws DeltaCloudClientException {
- int statusCode = httpResponse.getStatusLine().getStatusCode();
- if (HttpStatusCode.OK.isStatus(statusCode)) {
- return;
- } else if (HttpStatusCode.UNAUTHORIZED.isStatus(statusCode)) {
- throw new DeltaCloudAuthClientException(
- MessageFormat
- .format("The server reported an authorization error \"{0}\" on
requesting \"{1}\"",
- httpResponse.getStatusLine()
- .getReasonPhrase(), requestUrl));
- } else if (HttpStatusCode.NOT_FOUND.isStatus(statusCode)) {
- throw new DeltaCloudNotFoundClientException(MessageFormat.format(
- "The server could not find the resource \"{0}\"",
- requestUrl));
- } 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));
- }
+ protected InputStream request(DeltaCloudRequest deltaCloudRequest) throws
DeltaCloudClientException {
+ return transport.request(deltaCloudRequest);
}
- /**
- * Returns a request instance for the given request type and url.
- *
- * @param httpMethod
- * the request type to use
- * @param requestUrl
- * the requested url
- * @return the request instance
- * @throws MalformedURLException
- */
- protected HttpUriRequest createRequest(DeltaCloudRequest deltaCloudRequest)
- throws MalformedURLException {
- HttpUriRequest request = null;
- String url = deltaCloudRequest.getUrl().toString();
- HttpMethod httpMethod = deltaCloudRequest.getHttpMethod();
- switch (httpMethod) {
- case POST:
- request = new HttpPost(url);
- break;
- case DELETE:
- request = new HttpDelete(url);
- break;
- case GET:
- default:
- request = new HttpGet(url);
- }
- request.setHeader("Accept", "application/xml;q=1");
- return request;
- }
-
- /**
- * Adds the credentials to the given http client.
- *
- * @param httpClient
- * the http client
- * @return the default http client
- * @throws UnknownHostException
- */
- private DefaultHttpClient addCredentials(URL url,
- DefaultHttpClient httpClient, String username, String password)
- throws UnknownHostException {
- if (username != null && password != null) {
- httpClient.getCredentialsProvider().setCredentials(
- new AuthScope(url.getHost(), url.getPort()),
- new UsernamePasswordCredentials(username, password));
- }
- return httpClient;
- }
-
public Driver getServerType() {
try {
InputStream response = request(new TypeRequest(baseUrl));
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/AbstractHttpTransport.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/AbstractHttpTransport.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/AbstractHttpTransport.java 2011-03-29
13:49:49 UTC (rev 30101)
@@ -0,0 +1,69 @@
+package org.jboss.tools.deltacloud.core.client.transport;
+
+import java.io.InputStream;
+import java.net.MalformedURLException;
+import java.net.URL;
+import java.text.MessageFormat;
+
+import org.jboss.tools.deltacloud.core.client.DeltaCloudAuthClientException;
+import org.jboss.tools.deltacloud.core.client.DeltaCloudClientException;
+import org.jboss.tools.deltacloud.core.client.DeltaCloudNotFoundClientException;
+import org.jboss.tools.deltacloud.core.client.HttpStatusCode;
+import org.jboss.tools.deltacloud.core.client.HttpStatusRange;
+import org.jboss.tools.deltacloud.core.client.request.DeltaCloudRequest;
+
+public abstract class AbstractHttpTransport implements IHttpTransport {
+
+ private String username;
+ private String password;
+
+ public AbstractHttpTransport(String username, String password) {
+ this.username = username;
+ this.password = password;
+ }
+
+ @Override
+ public final InputStream request(DeltaCloudRequest request) throws
DeltaCloudClientException {
+ try {
+ return doRequest(request);
+ } catch (MalformedURLException e) {
+ throw new DeltaCloudClientException(MessageFormat.format(
+ "Could not connect to \"{0}\". The url is invalid.",
request.getUrlString()), e);
+ } catch(DeltaCloudClientException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new DeltaCloudClientException(e);
+ }
+ }
+
+ protected abstract InputStream doRequest(DeltaCloudRequest request) throws Exception;
+
+ protected void throwOnHttpErrors(int statusCode, String statusMessage, URL requestUrl)
+ throws DeltaCloudClientException {
+ if (HttpStatusCode.OK.isStatus(statusCode)) {
+ return;
+ } else if (HttpStatusCode.UNAUTHORIZED.isStatus(statusCode)) {
+ throw new DeltaCloudAuthClientException(
+ MessageFormat.format("The server reported an authorization error
\"{0}\" on requesting \"{1}\"",
+ statusMessage, requestUrl));
+ } else if (HttpStatusCode.NOT_FOUND.isStatus(statusCode)) {
+ throw new DeltaCloudNotFoundClientException(MessageFormat.format(
+ "The server could not find the resource \"{0}\"",
+ requestUrl));
+ } 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}\"",
+ statusMessage, requestUrl));
+ }
+ }
+
+ protected String getUsername() {
+ return username;
+ }
+
+ protected String getPassword() {
+ return password;
+ }
+
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/AbstractHttpTransport.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/Base64Coder.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/Base64Coder.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/Base64Coder.java 2011-03-29
13:49:49 UTC (rev 30101)
@@ -0,0 +1,226 @@
+// Copyright 2003-2010 Christian d'Heureuse, Inventec Informatik AG, Zurich,
Switzerland
+//
www.source-code.biz,
www.inventec.ch/chdh
+//
+// This module is multi-licensed and may be used under the terms
+// of any of the following licenses:
+//
+// EPL, Eclipse Public License, V1.0 or later,
http://www.eclipse.org/legal
+// LGPL, GNU Lesser General Public License, V2.1 or later,
http://www.gnu.org/licenses/lgpl.html
+// GPL, GNU General Public License, V2 or later,
http://www.gnu.org/licenses/gpl.html
+// AL, Apache License, V2.0 or later,
http://www.apache.org/licenses
+// BSD, BSD License,
http://www.opensource.org/licenses/bsd-license.php
+//
+// Please contact the author if you need another license.
+// This module is provided "as is", without warranties of any kind.
+
+package org.jboss.tools.deltacloud.core.client.transport;
+
+/**
+* A Base64 encoder/decoder.
+*
+* <p>
+* This class is used to encode and decode data in Base64 format as described in RFC
1521.
+*
+* <p>
+* Project home page: <a
href="http://www.source-code.biz/base64coder/java/">www.source-code.biz/base64coder/java</a><br>
+* Author: Christian d'Heureuse, Inventec Informatik AG, Zurich,
Switzerland<br>
+* Multi-licensed: EPL / LGPL / GPL / AL / BSD.
+*/
+public class Base64Coder {
+
+// The line separator string of the operating system.
+private static final String systemLineSeparator =
System.getProperty("line.separator");
+
+// Mapping table from 6-bit nibbles to Base64 characters.
+private static char[] map1 = new char[64];
+ static {
+ int i=0;
+ for (char c='A'; c<='Z'; c++) map1[i++] = c;
+ for (char c='a'; c<='z'; c++) map1[i++] = c;
+ for (char c='0'; c<='9'; c++) map1[i++] = c;
+ map1[i++] = '+'; map1[i++] = '/'; }
+
+// Mapping table from Base64 characters to 6-bit nibbles.
+private static byte[] map2 = new byte[128];
+ static {
+ for (int i=0; i<map2.length; i++) map2[i] = -1;
+ for (int i=0; i<64; i++) map2[map1[i]] = (byte)i; }
+
+/**
+* Encodes a string into Base64 format.
+* No blanks or line breaks are inserted.
+* @param s A String to be encoded.
+* @return A String containing the Base64 encoded data.
+*/
+public static String encodeString (String s) {
+ return new String(encode(s.getBytes())); }
+
+/**
+* Encodes a byte array into Base 64 format and breaks the output into lines of 76
characters.
+* This method is compatible with
<code>sun.misc.BASE64Encoder.encodeBuffer(byte[])</code>.
+* @param in An array containing the data bytes to be encoded.
+* @return A String containing the Base64 encoded data, broken into lines.
+*/
+public static String encodeLines (byte[] in) {
+ return encodeLines(in, 0, in.length, 76, systemLineSeparator); }
+
+/**
+* Encodes a byte array into Base 64 format and breaks the output into lines.
+* @param in An array containing the data bytes to be encoded.
+* @param iOff Offset of the first byte in <code>in</code> to be
processed.
+* @param iLen Number of bytes to be processed in <code>in</code>,
starting at <code>iOff</code>.
+* @param lineLen Line length for the output data. Should be a multiple of 4.
+* @param lineSeparator The line separator to be used to separate the output lines.
+* @return A String containing the Base64 encoded data, broken into lines.
+*/
+public static String encodeLines (byte[] in, int iOff, int iLen, int lineLen, String
lineSeparator) {
+ int blockLen = (lineLen*3) / 4;
+ if (blockLen <= 0) throw new IllegalArgumentException();
+ int lines = (iLen+blockLen-1) / blockLen;
+ int bufLen = ((iLen+2)/3)*4 + lines*lineSeparator.length();
+ StringBuilder buf = new StringBuilder(bufLen);
+ int ip = 0;
+ while (ip < iLen) {
+ int l = Math.min(iLen-ip, blockLen);
+ buf.append (encode(in, iOff+ip, l));
+ buf.append (lineSeparator);
+ ip += l; }
+ return buf.toString(); }
+
+/**
+* Encodes a byte array into Base64 format.
+* No blanks or line breaks are inserted in the output.
+* @param in An array containing the data bytes to be encoded.
+* @return A character array containing the Base64 encoded data.
+*/
+public static char[] encode (byte[] in) {
+ return encode(in, 0, in.length); }
+
+/**
+* Encodes a byte array into Base64 format.
+* No blanks or line breaks are inserted in the output.
+* @param in An array containing the data bytes to be encoded.
+* @param iLen Number of bytes to process in <code>in</code>.
+* @return A character array containing the Base64 encoded data.
+*/
+public static char[] encode (byte[] in, int iLen) {
+ return encode(in, 0, iLen); }
+
+/**
+* Encodes a byte array into Base64 format.
+* No blanks or line breaks are inserted in the output.
+* @param in An array containing the data bytes to be encoded.
+* @param iOff Offset of the first byte in <code>in</code> to be processed.
+* @param iLen Number of bytes to process in <code>in</code>, starting at
<code>iOff</code>.
+* @return A character array containing the Base64 encoded data.
+*/
+public static char[] encode (byte[] in, int iOff, int iLen) {
+ int oDataLen = (iLen*4+2)/3; // output length without padding
+ int oLen = ((iLen+2)/3)*4; // output length including padding
+ char[] out = new char[oLen];
+ int ip = iOff;
+ int iEnd = iOff + iLen;
+ int op = 0;
+ while (ip < iEnd) {
+ int i0 = in[ip++] & 0xff;
+ int i1 = ip < iEnd ? in[ip++] & 0xff : 0;
+ int i2 = ip < iEnd ? in[ip++] & 0xff : 0;
+ int o0 = i0 >>> 2;
+ int o1 = ((i0 & 3) << 4) | (i1 >>> 4);
+ int o2 = ((i1 & 0xf) << 2) | (i2 >>> 6);
+ int o3 = i2 & 0x3F;
+ out[op++] = map1[o0];
+ out[op++] = map1[o1];
+ out[op] = op < oDataLen ? map1[o2] : '='; op++;
+ out[op] = op < oDataLen ? map1[o3] : '='; op++; }
+ return out; }
+
+/**
+* Decodes a string from Base64 format.
+* No blanks or line breaks are allowed within the Base64 encoded input data.
+* @param s A Base64 String to be decoded.
+* @return A String containing the decoded data.
+* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
+*/
+public static String decodeString (String s) {
+ return new String(decode(s)); }
+
+/**
+* Decodes a byte array from Base64 format and ignores line separators, tabs and blanks.
+* CR, LF, Tab and Space characters are ignored in the input data.
+* This method is compatible with
<code>sun.misc.BASE64Decoder.decodeBuffer(String)</code>.
+* @param s A Base64 String to be decoded.
+* @return An array containing the decoded data bytes.
+* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
+*/
+public static byte[] decodeLines (String s) {
+ char[] buf = new char[s.length()];
+ int p = 0;
+ for (int ip = 0; ip < s.length(); ip++) {
+ char c = s.charAt(ip);
+ if (c != ' ' && c != '\r' && c != '\n'
&& c != '\t')
+ buf[p++] = c; }
+ return decode(buf, 0, p); }
+
+/**
+* Decodes a byte array from Base64 format.
+* No blanks or line breaks are allowed within the Base64 encoded input data.
+* @param s A Base64 String to be decoded.
+* @return An array containing the decoded data bytes.
+* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
+*/
+public static byte[] decode (String s) {
+ return decode(s.toCharArray()); }
+
+/**
+* Decodes a byte array from Base64 format.
+* No blanks or line breaks are allowed within the Base64 encoded input data.
+* @param in A character array containing the Base64 encoded data.
+* @return An array containing the decoded data bytes.
+* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
+*/
+public static byte[] decode (char[] in) {
+ return decode(in, 0, in.length); }
+
+/**
+* Decodes a byte array from Base64 format.
+* No blanks or line breaks are allowed within the Base64 encoded input data.
+* @param in A character array containing the Base64 encoded data.
+* @param iOff Offset of the first character in <code>in</code> to be
processed.
+* @param iLen Number of characters to process in <code>in</code>, starting
at <code>iOff</code>.
+* @return An array containing the decoded data bytes.
+* @throws IllegalArgumentException If the input is not valid Base64 encoded data.
+*/
+public static byte[] decode (char[] in, int iOff, int iLen) {
+ if (iLen%4 != 0) throw new IllegalArgumentException ("Length of Base64 encoded
input string is not a multiple of 4.");
+ while (iLen > 0 && in[iOff+iLen-1] == '=') iLen--;
+ int oLen = (iLen*3) / 4;
+ byte[] out = new byte[oLen];
+ int ip = iOff;
+ int iEnd = iOff + iLen;
+ int op = 0;
+ while (ip < iEnd) {
+ int i0 = in[ip++];
+ int i1 = in[ip++];
+ int i2 = ip < iEnd ? in[ip++] : 'A';
+ int i3 = ip < iEnd ? in[ip++] : 'A';
+ if (i0 > 127 || i1 > 127 || i2 > 127 || i3 > 127)
+ throw new IllegalArgumentException ("Illegal character in Base64 encoded
data.");
+ int b0 = map2[i0];
+ int b1 = map2[i1];
+ int b2 = map2[i2];
+ int b3 = map2[i3];
+ if (b0 < 0 || b1 < 0 || b2 < 0 || b3 < 0)
+ throw new IllegalArgumentException ("Illegal character in Base64 encoded
data.");
+ int o0 = ( b0 <<2) | (b1>>>4);
+ int o1 = ((b1 & 0xf)<<4) | (b2>>>2);
+ int o2 = ((b2 & 3)<<6) | b3;
+ out[op++] = (byte)o0;
+ if (op<oLen) out[op++] = (byte)o1;
+ if (op<oLen) out[op++] = (byte)o2; }
+ return out; }
+
+// Dummy constructor.
+private Base64Coder() {}
+
+} // end class Base64Coder
\ No newline at end of file
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/Base64Coder.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/IHttpTransport.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/IHttpTransport.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/IHttpTransport.java 2011-03-29
13:49:49 UTC (rev 30101)
@@ -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.transport;
+
+import java.io.InputStream;
+
+import org.jboss.tools.deltacloud.core.client.DeltaCloudClientException;
+import org.jboss.tools.deltacloud.core.client.request.DeltaCloudRequest;
+
+/**
+ * An interface for http transport implementation to be used by the
+ * DeltaCloudClient.
+ *
+ * @author André Dietisheim
+ * @see URLConnectionTransport
+ */
+public interface IHttpTransport {
+
+ public InputStream request(DeltaCloudRequest request) throws DeltaCloudClientException;
+
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/IHttpTransport.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Added:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/URLConnectionTransport.java
===================================================================
---
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/URLConnectionTransport.java
(rev 0)
+++
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/URLConnectionTransport.java 2011-03-29
13:49:49 UTC (rev 30101)
@@ -0,0 +1,108 @@
+package org.jboss.tools.deltacloud.core.client.transport;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayOutputStream;
+import java.io.FileNotFoundException;
+import java.io.IOException;
+import java.io.InputStream;
+import java.net.Authenticator;
+import java.net.HttpURLConnection;
+import java.net.URL;
+import java.net.URLConnection;
+import java.text.MessageFormat;
+
+import org.jboss.tools.deltacloud.core.client.DeltaCloudNotFoundClientException;
+import org.jboss.tools.deltacloud.core.client.HttpMethod;
+import org.jboss.tools.deltacloud.core.client.request.DeltaCloudRequest;
+
+public class URLConnectionTransport extends AbstractHttpTransport {
+
+ private static final char USERNAME_PASSWORD_DELIMITER = ':';
+ private static final String PROPERTY_AUTHORIZATION = "Authorization";
+ private static final String PROPERTY_ACCEPT = "Accept";
+ private static final String PREFIX_BASIC_AUTHENTICATION = "Basic ";
+ private static final int TIMEOUT = 10 * 1024;
+
+ public URLConnectionTransport(String username, String password) {
+ super(username, password);
+ }
+
+ @Override
+ protected InputStream doRequest(DeltaCloudRequest request) throws Exception {
+ try {
+ URL url = request.getUrl();
+ HttpURLConnection connection = (HttpURLConnection) url.openConnection();
+ connection.setUseCaches(false);
+ connection.setDoInput(true);
+ connection.setAllowUserInteraction(false);
+ connection.setConnectTimeout(TIMEOUT);
+ connection.setRequestProperty(PROPERTY_ACCEPT, "application/xml;q=1");
+ connection.setInstanceFollowRedirects(true);
+ addCredentials(connection);
+ setRequestMethod(request, connection);
+ BufferedInputStream in = new BufferedInputStream(connection.getInputStream());
+ throwOnHttpErrors(
+ connection.getResponseCode(), connection.getResponseMessage(), request.getUrl());
+ return in;
+ } catch (FileNotFoundException e) {
+ /*
+ * thrown by #connect when server resonds with 404
+ */
+ throw new DeltaCloudNotFoundClientException(
+ MessageFormat.format("Could not find resource {0}",
request.getUrlString()));
+
+ }
+ }
+
+ private void setRequestMethod(DeltaCloudRequest request, HttpURLConnection connection)
throws IOException {
+ HttpMethod httpMethod = request.getHttpMethod();
+ connection.setRequestMethod(httpMethod.name());
+ switch (httpMethod) {
+ case PUT:
+ case POST:
+ connection.setRequestProperty("Content-Type",
"application/x-www-form-urlencoded");
+ connection.setRequestProperty("Content-Length", "0");//
String.valueOf(request.getParametersLength()));
+ connection.setDoOutput(true);
+ connection.getOutputStream().flush();
+ break;
+ case GET:
+ connection.setDoOutput(false);
+ break;
+ }
+
+ }
+
+ /**
+ * Adds the credentials to the given http url connection.
+ *
+ * The current implementation uses low level API. Alternatively
+ * {@link Authenticator#setDefault(Authenticator)} could be used which would
+ * then rule all url connections in the same jvm.
+ *
+ * @param httpClient
+ * the http client
+ * @return the default http client
+ * @throws IOException
+ */
+ private void addCredentials(URLConnection urlConnection) throws IOException {
+ String username = getUsername();
+ String password = getPassword();
+ if (username != null && password != null) {
+ String credentials = new StringBuilder()
+ .append(PREFIX_BASIC_AUTHENTICATION)
+ .append(getAuthenticationValue(username, password))
+ .toString();
+ urlConnection.setRequestProperty(PROPERTY_AUTHORIZATION, credentials);
+ }
+
+ }
+
+ private String getAuthenticationValue(String username, String password) throws
IOException {
+ ByteArrayOutputStream out = new ByteArrayOutputStream();
+ out.write(username.getBytes());
+ out.write(USERNAME_PASSWORD_DELIMITER);
+ out.write(password.getBytes());
+ char[] encoded = Base64Coder.encode(out.toByteArray());
+ return new String(encoded);
+ }
+}
Property changes on:
trunk/deltacloud/plugins/org.jboss.tools.deltacloud.client/src/org/jboss/tools/deltacloud/core/client/transport/URLConnectionTransport.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified:
trunk/deltacloud/tests/org.jboss.tools.deltacloud.client.test/src/org/jboss/tools/internal/deltacloud/client/test/core/client/InstanceMockIntegrationTest.java
===================================================================
---
trunk/deltacloud/tests/org.jboss.tools.deltacloud.client.test/src/org/jboss/tools/internal/deltacloud/client/test/core/client/InstanceMockIntegrationTest.java 2011-03-29
13:48:42 UTC (rev 30100)
+++
trunk/deltacloud/tests/org.jboss.tools.deltacloud.client.test/src/org/jboss/tools/internal/deltacloud/client/test/core/client/InstanceMockIntegrationTest.java 2011-03-29
13:49:49 UTC (rev 30101)
@@ -13,7 +13,6 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotNull;
-import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import java.io.IOException;
@@ -146,14 +145,14 @@
testSetup.getClient().createInstance("dummy");
}
- @Test
+ @Test(expected=DeltaCloudClientException.class)
public void canDestroy() throws DeltaCloudClientException {
Image image = testSetup.getFirstImage(testSetup.getClient());
DeltaCloudClient client = testSetup.getClient();
Instance instance = client.createInstance(image.getId());
instance.stop(client);
instance.destroy(client);
- assertNull(testSetup.getInstanceById(instance.getId(), testSetup.getClient()));
+ client.listInstances(instance.getId());
}
@Test(expected = DeltaCloudClientException.class)