Browse files

Major refactoring of the internals

  • Loading branch information...
1 parent 793e2f3 commit 62b4229abec6597e32a3b4372ea21c93b6bd3491 @renner renner committed Feb 22, 2012
View
46 src/com/suse/studio/client/net/Request.java
@@ -1,46 +0,0 @@
-package com.suse.studio.client.net;
-
-import java.io.IOException;
-import java.net.HttpURLConnection;
-import java.net.MalformedURLException;
-import java.net.URL;
-
-public class Request {
-
- public static HttpURLConnection get(String uri) throws IOException {
- return request("GET", uri);
- }
-
- public static HttpURLConnection get(String uri, String encodedCredentials) throws IOException {
- return request("GET", uri, encodedCredentials);
- }
-
- public static HttpURLConnection post(String uri) throws IOException {
- return request("POST", uri);
- }
-
- public static HttpURLConnection post(String uri, String encodedCredentials) throws IOException {
- return request("POST", uri, encodedCredentials);
- }
-
- private static HttpURLConnection request(String method, String uri) throws IOException {
- URL url;
- try {
- url = new URL(uri);
- } catch (MalformedURLException e) {
- throw new RuntimeException(e);
- }
-
- HttpURLConnection connection = (HttpURLConnection) url.openConnection();
- connection.setRequestMethod(method);
-
- return connection;
- }
-
- private static HttpURLConnection request(String method, String uri, String encodedCredentials) throws IOException {
- HttpURLConnection connection = request(method, uri);
- connection.setRequestProperty("Authorization", "BASIC " + encodedCredentials);
- return connection;
- }
-
-}
View
70 src/com/suse/studio/client/net/RequestFactory.java
@@ -0,0 +1,70 @@
+package com.suse.studio.client.net;
+
+import java.io.IOException;
+import java.net.HttpURLConnection;
+import java.net.MalformedURLException;
+import java.net.URL;
+
+public class RequestFactory {
+
+ private static RequestFactory instance = new RequestFactory();
+
+ private RequestFactory() {
+ }
+
+ public static RequestFactory getInstance() {
+ return instance;
+ }
+
+ /**
+ * Create a {@link HttpURLConnection} for performing a GET request.
+ *
+ * @param uri
+ * @param encodedCredentials
+ * @return
+ * @throws IOException
+ */
+ public HttpURLConnection get(String uri, String encodedCredentials)
+ throws IOException {
+ return initConnection("GET", uri, encodedCredentials);
+ }
+
+ /**
+ * Create a {@link HttpURLConnection} for performing a POST request.
+ *
+ * @param uri
+ * @param encodedCredentials
+ * @return
+ * @throws IOException
+ */
+ public HttpURLConnection post(String uri, String encodedCredentials)
+ throws IOException {
+ return initConnection("POST", uri, encodedCredentials);
+ }
+
+ /**
+ * Init a {@link HttpURLConnection} object from a given uri.
+ *
+ * @param method
+ * @param uri
+ * @param encodedCredentials
+ * @return
+ * @throws IOException
+ */
+ private HttpURLConnection initConnection(String method, String uri,
+ String encodedCredentials) throws IOException {
+ try {
+ URL url = new URL(uri);
+ HttpURLConnection connection = (HttpURLConnection) url
+ .openConnection();
+ connection.setRequestMethod(method);
+ if (encodedCredentials != null) {
+ connection.setRequestProperty("Authorization", "BASIC "
+ + encodedCredentials);
+ }
+ return connection;
+ } catch (MalformedURLException e) {
+ throw new RuntimeException(e);
+ }
+ }
+}
View
87 src/com/suse/studio/client/net/StudioConnection.java
@@ -4,65 +4,56 @@
import java.io.InputStream;
import java.net.HttpURLConnection;
-import org.simpleframework.xml.Serializer;
-import org.simpleframework.xml.core.Persister;
-
+import com.suse.studio.util.ParserUtils;
import com.suse.studio.util.StudioConfig;
public class StudioConnection {
-
- private HttpURLConnection connection;
- private InputStream responseBodyStream;
- private String uri = null;
- private String encodedCredentials = null;
+
+ private String uri = null;
+ private String encodedCredentials = null;
/**
- * Init a connection to SUSE Studio with given configuration.
+ * Init a connection to SUSE Studio with a given configuration.
+ *
+ * @param uri
* @param config
*/
public StudioConnection(String uri, StudioConfig config) {
this.uri = config.getBaseURL() + uri;
this.encodedCredentials = config.getEncodedCredentials();
}
- public <T> T get(Class<T> class1) throws IOException {
- if (encodedCredentials == null) {
- connection = Request.get(uri);
- } else {
- connection = Request.get(uri, encodedCredentials);
- }
- connection.connect();
- responseBodyStream = connection.getInputStream();
-
- T result = parseBodyStream(class1);
- connection.disconnect();
- return result;
- }
-
- public <T> T post(Class<T> class1) throws IOException {
- if (encodedCredentials == null) {
- connection = Request.post(uri);
- } else {
- connection = Request.post(uri, encodedCredentials);
- }
- connection.connect();
- responseBodyStream = connection.getInputStream();
-
- T result = parseBodyStream(class1);
- connection.disconnect();
- return result;
- }
-
- private <T> T parseBodyStream(Class<T> class1) {
- T result = null;
- Serializer serializer = new Persister();
- try {
- result = serializer.read(class1, responseBodyStream);
- } catch (Exception e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- return result;
- }
+ /**
+ * Perform a GET request and parse the result into given {@link Class}.
+ *
+ * @param class1
+ * @return instance of class1
+ * @throws IOException
+ */
+ public <T> T get(Class<T> class1) throws IOException {
+ HttpURLConnection connection = RequestFactory.getInstance().get(uri,
+ encodedCredentials);
+ connection.connect();
+ InputStream responseBodyStream = connection.getInputStream();
+ T result = ParserUtils.parseBodyStream(class1, responseBodyStream);
+ connection.disconnect();
+ return result;
+ }
+ /**
+ * FIXME as soon as it's needed.
+ *
+ * @param class1
+ * @return instance of class1
+ * @throws IOException
+ */
+ public <T> T post(Class<T> class1) throws IOException {
+ HttpURLConnection connection = RequestFactory.getInstance().post(uri,
+ encodedCredentials);
+ connection.connect();
+ InputStream responseBodyStream = connection.getInputStream();
+ T result = ParserUtils.parseBodyStream(class1, responseBodyStream);
+ connection.disconnect();
+ return result;
+ }
}
View
53 src/com/suse/studio/util/ParserUtils.java
@@ -0,0 +1,53 @@
+package com.suse.studio.util;
+
+import java.io.ByteArrayInputStream;
+import java.io.InputStream;
+import java.util.Scanner;
+
+import org.simpleframework.xml.Serializer;
+import org.simpleframework.xml.core.Persister;
+
+/**
+ * A wrapper around the Simple XML library plus some utility methods for working
+ * with Streams.
+ */
+public class ParserUtils {
+
+ /**
+ * Parse a given {@link InputStream} into a {@link Class}.
+ *
+ * @param clazz
+ * @param stream
+ * @return
+ */
+ public static <T> T parseBodyStream(Class<T> clazz, InputStream stream) {
+ T result = null;
+ Serializer serializer = new Persister();
+ try {
+ result = serializer.read(clazz, stream);
+ } catch (Exception e) {
+ e.printStackTrace();
+ }
+ return result;
+ }
+
+ /**
+ * Convert a given {@link InputStream} to a {@link String}.
+ *
+ * @param is
+ * @return
+ */
+ public static String convertStreamToString(InputStream is) {
+ return new Scanner(is).useDelimiter("\\A").next();
+ }
+
+ /**
+ * Convert a given {@link String} to an {@link InputStream}.
+ *
+ * @param is
+ * @return
+ */
+ public static InputStream convertStringToStream(String s) {
+ return new ByteArrayInputStream(s.getBytes());
+ }
+}

0 comments on commit 62b4229

Please sign in to comment.