Author: adietish
Date: 2011-09-12 10:01:34 -0400 (Mon, 12 Sep 2011)
New Revision: 34634
Added:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/JsonSanitizer.java
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/OpenshiftService.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/AbstractOpenshiftJsonResponseUnmarshaller.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ApplicationResponseUnmarshaller.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/DomainResponseUnmarshaller.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ListCartridgesResponseUnmarshaller.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/DomainTest.java
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/ListCartridgesTest.java
Log:
[JBIDE-9510] added json sanitzier to handle invalid json responses from openshift service
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/OpenshiftService.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/OpenshiftService.java 2011-09-12
13:45:27 UTC (rev 34633)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/OpenshiftService.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -40,6 +40,7 @@
import org.jboss.ide.eclipse.as.openshift.internal.core.request.UserInfoRequest;
import
org.jboss.ide.eclipse.as.openshift.internal.core.response.ApplicationResponseUnmarshaller;
import
org.jboss.ide.eclipse.as.openshift.internal.core.response.DomainResponseUnmarshaller;
+import org.jboss.ide.eclipse.as.openshift.internal.core.response.JsonSanitizer;
import
org.jboss.ide.eclipse.as.openshift.internal.core.response.ListCartridgesResponseUnmarshaller;
import org.jboss.ide.eclipse.as.openshift.internal.core.response.OpenshiftResponse;
@@ -79,6 +80,7 @@
* WARNING: the current server implementation returns invalid json.
*
* @see ListCartridgesResponseUnmarshaller
+ * @see JsonSanitizer#sanitize(String)
*/
@Override
public List<Cartridge> getCartridges() throws OpenshiftException {
@@ -89,7 +91,10 @@
new ListCartridgesRequestJsonMarshaller().marshall(listCartridgesRequest);
String request = new OpenshiftJsonRequestFactory(password,
listCartridgesRequestString).create();
String listCatridgesReponse = createHttpClient(url).post(request);
- throw new UnsupportedOperationException();
+ listCatridgesReponse = JsonSanitizer.sanitize(listCatridgesReponse);
+ OpenshiftResponse<List<Cartridge>> response =
+ new ListCartridgesResponseUnmarshaller().unmarshall(listCatridgesReponse);
+ return response.getData();
} catch (MalformedURLException e) {
throw new OpenshiftEndpointException(url, e, "Could not list available cartridges
at \"{0}\"", url);
} catch (HttpClientException e) {
@@ -101,7 +106,7 @@
public SSHKey createKey(String passPhrase, String privateKeyPath, String publicKeyPath)
throws OpenshiftException {
return SSHKey.create(passPhrase, privateKeyPath, publicKeyPath);
}
-
+
@Override
public SSHKey loadKey(String privateKeyPath, String publicKeyPath) throws
OpenshiftException {
return SSHKey.load(privateKeyPath, publicKeyPath);
@@ -121,7 +126,9 @@
new DomainRequestJsonMarshaller().marshall(request))
.create();
String responseString = createHttpClient(url).post(requestString);
- OpenshiftResponse<Domain> response = new
DomainResponseUnmarshaller(responseString, request.getName()).unmarshall();
+ responseString = JsonSanitizer.sanitize(responseString);
+ OpenshiftResponse<Domain> response =
+ new DomainResponseUnmarshaller(request.getName()).unmarshall(responseString);
return response.getData();
} catch (MalformedURLException e) {
throw new OpenshiftEndpointException(url, e, "Could not list available cartridges
at \"{0}\"", url);
@@ -150,8 +157,10 @@
new ApplicationRequestJsonMarshaller().marshall(applicationRequest);
String request = new OpenshiftJsonRequestFactory(password,
applicationRequestString).create();
String response = createHttpClient(url).post(request);
- OpenshiftResponse<Application> openshiftResponse = new
ApplicationResponseUnmarshaller(response, name,
- cartridge).unmarshall();
+
+ response = JsonSanitizer.sanitize(response);
+ OpenshiftResponse<Application> openshiftResponse =
+ new ApplicationResponseUnmarshaller(name, cartridge).unmarshall(response);
return openshiftResponse.getData();
} catch (MalformedURLException e) {
throw new OpenshiftException(
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/AbstractOpenshiftJsonResponseUnmarshaller.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/AbstractOpenshiftJsonResponseUnmarshaller.java 2011-09-12
13:45:27 UTC (rev 34633)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/AbstractOpenshiftJsonResponseUnmarshaller.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -22,11 +22,7 @@
private String response;
- public AbstractOpenshiftJsonResponseUnmarshaller(String response) {
- this.response = response;
- }
-
- public OpenshiftResponse<OPENSHIFTOBJECT> unmarshall() throws OpenshiftException
{
+ public OpenshiftResponse<OPENSHIFTOBJECT> unmarshall(String response) throws
OpenshiftException {
try {
ModelNode node = ModelNode.fromJSONString(response);
boolean debug = node.get(IOpenshiftJsonConstants.PROPERTY_DEBUG).asBoolean();
@@ -42,6 +38,8 @@
}
}
+
+
protected abstract OPENSHIFTOBJECT createOpenshiftObject(ModelNode dataNode);
protected String getResponse() {
@@ -51,6 +49,7 @@
protected String getString(String property, ModelNode node) {
ModelNode propertyNode = node.get(property);
if (propertyNode.getType() == ModelType.UNDEFINED) {
+ // replace "undefined" by null
return null;
}
return propertyNode.asString();
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ApplicationResponseUnmarshaller.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ApplicationResponseUnmarshaller.java 2011-09-12
13:45:27 UTC (rev 34633)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ApplicationResponseUnmarshaller.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -22,8 +22,7 @@
private String applicationName;
private Cartridge cartridge;
- public ApplicationResponseUnmarshaller(String response, String applicationName,
Cartridge cartridge) {
- super(response);
+ public ApplicationResponseUnmarshaller(String applicationName, Cartridge cartridge) {
this.applicationName = applicationName;
this.cartridge = cartridge;
}
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/DomainResponseUnmarshaller.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/DomainResponseUnmarshaller.java 2011-09-12
13:45:27 UTC (rev 34633)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/DomainResponseUnmarshaller.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -22,8 +22,7 @@
private String domainName;
- public DomainResponseUnmarshaller(String response, String domainName) {
- super(response);
+ public DomainResponseUnmarshaller(String domainName) {
this.domainName = domainName;
}
Added:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/JsonSanitizer.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/JsonSanitizer.java
(rev 0)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/JsonSanitizer.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -0,0 +1,37 @@
+package org.jboss.ide.eclipse.as.openshift.internal.core.response;
+
+import java.util.regex.Matcher;
+import java.util.regex.Pattern;
+
+public class JsonSanitizer {
+
+ private static final Pattern QUOTED_JSON_OBJECT_PATTERN =
Pattern.compile("\"\\{(.+)\\}\"");
+ private static final Pattern ESCAPED_QUOTES_PATTERN =
Pattern.compile("\\\"");
+
+ public static String sanitize(String json) {
+ return correctEscapedJsonObjects(json);
+ }
+
+ /**
+ * Corrects erroneously quoted json objects in the given string.
+ * <p>
+ * "{ \"property\": \"value\" }"
+ *
+ * @param json
+ * @return
+ */
+ protected static String correctEscapedJsonObjects(String json) {
+ String sanitizedJson = json;
+ Matcher matcher = QUOTED_JSON_OBJECT_PATTERN.matcher(json);
+ if (matcher.find()
+ && matcher.groupCount() > 0) {
+ sanitizedJson = matcher.replaceAll("{" + unescapeQuotes(matcher.group(1)) +
"}");
+ }
+ return sanitizedJson;
+ }
+
+ private static String unescapeQuotes(String responseFragment) {
+ return
ESCAPED_QUOTES_PATTERN.matcher(responseFragment).replaceAll("\"");
+ }
+
+}
Property changes on:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/JsonSanitizer.java
___________________________________________________________________
Added: svn:mime-type
+ text/plain
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ListCartridgesResponseUnmarshaller.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ListCartridgesResponseUnmarshaller.java 2011-09-12
13:45:27 UTC (rev 34633)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.core/src/org/jboss/ide/eclipse/as/openshift/internal/core/response/ListCartridgesResponseUnmarshaller.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -18,20 +18,14 @@
import org.jboss.ide.eclipse.as.openshift.core.IOpenshiftJsonConstants;
/**
+ * WARNING: the current (9-7-2011) response from the openshift rest service is
+ * invalid. It quotes the nested json object in the data property: '"data"
:
+ * "{'. My current unmarshalling code does not handle this bad json.
+ *
* @author André Dietisheim
*/
public class ListCartridgesResponseUnmarshaller extends
AbstractOpenshiftJsonResponseUnmarshaller<List<Cartridge>> {
- /*
- * WARNING: the current (9-7-2011) response from the openshift rest
- * service is invalid. It quotes the nested json object in the data
- * property: '"data" : "{'. My current unmarshalling code does
not
- * handle this bad json.
- */
- public ListCartridgesResponseUnmarshaller(String response) {
- super(response);
- }
-
@Override
protected List<Cartridge> createOpenshiftObject(ModelNode dataNode) {
List<Cartridge> cartridges = new ArrayList<Cartridge>();
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/DomainTest.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/DomainTest.java 2011-09-12
13:45:27 UTC (rev 34633)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/DomainTest.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -11,17 +11,23 @@
package org.jboss.ide.eclipse.as.openshift.internal.test.core;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
+import org.jboss.ide.eclipse.as.openshift.core.Domain;
import org.jboss.ide.eclipse.as.openshift.core.OpenshiftException;
import org.jboss.ide.eclipse.as.openshift.core.SSHKey;
+import org.jboss.ide.eclipse.as.openshift.core.User;
import
org.jboss.ide.eclipse.as.openshift.core.internal.marshalling.DomainRequestJsonMarshaller;
import org.jboss.ide.eclipse.as.openshift.internal.core.request.ChangeDomainRequest;
import org.jboss.ide.eclipse.as.openshift.internal.core.request.CreateDomainRequest;
import
org.jboss.ide.eclipse.as.openshift.internal.core.request.OpenshiftJsonRequestFactory;
+import
org.jboss.ide.eclipse.as.openshift.internal.core.response.DomainResponseUnmarshaller;
+import org.jboss.ide.eclipse.as.openshift.internal.core.response.JsonSanitizer;
+import org.jboss.ide.eclipse.as.openshift.internal.core.response.OpenshiftResponse;
import org.jboss.ide.eclipse.as.openshift.internal.test.core.fakes.TestSSHKey;
import org.junit.Test;
@@ -32,12 +38,14 @@
private static final String USERNAME = "toolsjboss(a)gmail.com";
private static final String PASSWORD = "1q2w3e";
+ private static final String UUID = "0c82860dae904a4d87f8e5d87a5af840";
@Test
public void canMarshallDomainCreateRequest() throws IOException, OpenshiftException {
SSHKey sshKey = TestSSHKey.create();
- String expectedRequestString = createDomainRequestString(PASSWORD, USERNAME, true,
"myDomain", false, sshKey.getPublicKeyContent());
-
+ String expectedRequestString = createDomainRequestString(PASSWORD, USERNAME, true,
"myDomain", false,
+ sshKey.getPublicKeyContent());
+
CreateDomainRequest request = new CreateDomainRequest("myDomain", sshKey,
USERNAME, true);
String requestString =
new OpenshiftJsonRequestFactory(
@@ -46,12 +54,30 @@
.create();
assertEquals(expectedRequestString, requestString);
}
-
+
@Test
+ public void canUnmarshallDomainCreateResponse() throws IOException, OpenshiftException
{
+ String domainName = "myDomain";
+ String responseString = createDomainResponseString(USERNAME, UUID);
+
+ responseString = JsonSanitizer.sanitize(responseString);
+ OpenshiftResponse<Domain> response = new
DomainResponseUnmarshaller(domainName).unmarshall(responseString);
+
+ assertNotNull(response);
+ Domain domain = response.getData();
+ assertEquals(domainName, domain.getName());
+ User user = domain.getUser();
+ assertNotNull(user);
+ assertEquals(USERNAME, user.getRhlogin());
+ assertEquals(UUID, user.getUuid());
+ }
+
+ @Test
public void canMarshallDomainAlterRequest() throws IOException, OpenshiftException {
SSHKey sshKey = TestSSHKey.create();
- String expectedRequestString = createDomainRequestString(PASSWORD, USERNAME, true,
"myDomain", true, sshKey.getPublicKeyContent());
-
+ String expectedRequestString = createDomainRequestString(PASSWORD, USERNAME, true,
"myDomain", true,
+ sshKey.getPublicKeyContent());
+
ChangeDomainRequest request = new ChangeDomainRequest("myDomain", sshKey,
USERNAME, true);
String requestString =
new OpenshiftJsonRequestFactory(
@@ -61,22 +87,38 @@
assertEquals(expectedRequestString, requestString);
}
- private String createDomainRequestString(String password, String username, boolean
debug, String namespace, boolean alter, String sshPublicKey) throws
UnsupportedEncodingException {
- String request =
- "password="
+ private String createDomainRequestString(String password, String username, boolean
debug, String namespace,
+ boolean alter, String sshPublicKey) throws UnsupportedEncodingException {
+ return "password="
+ password
+ "&json_data=%7B"
+ "%22rhlogin%22+%3A+"
+ "%22"
+ URLEncoder.encode(username, "UTF-8")
+ "%22"
- + "%2C+%22debug%22+%3A+%22" + String.valueOf(debug)+ "%22"
- + "%2C+%22namespace%22+%3A+%22" + URLEncoder.encode(namespace,
"UTF-8")+ "%22"
- + "%2C+%22alter%22+%3A+%22" + String.valueOf(alter)+ "%22"
+ + "%2C+%22debug%22+%3A+%22" + String.valueOf(debug) + "%22"
+ + "%2C+%22namespace%22+%3A+%22" + URLEncoder.encode(namespace,
"UTF-8") + "%22"
+ + "%2C+%22alter%22+%3A+%22" + String.valueOf(alter) + "%22"
+ "%2C+%22ssh%22+%3A+%22"
+ URLEncoder.encode(sshPublicKey, "UTF-8")
+ "%22"
+ "%7D";
- return request;
}
+
+ /**
+ * WARNING: the response this method returns matches the actual response
+ * from the openshift service (9-12-2011). It is not valid json since it quotes the
+ * nested json object
+ * <p>
+ * "data": "{\"rhlogin\": ...
+ */
+ private String createDomainResponseString(String username, String uuid) {
+ return
"{\"messages\":\"\",\"debug\":\"\",\"data\":\""
+ + "{\\\"rhlogin\\\":\\\""
+ + username
+ + "\\\",\\\"uuid\\\":\\\""
+ + uuid
+ + "\\\"}"
+ +
"\",\"api\":\"1.1.1\",\"api_c\":[\"placeholder\"],\"result\":null,\"broker\":\"1.1.1\",\"broker_c\":[\"namespace\",\"rhlogin\",\"ssh\",\"app_uuid\",\"debug\",\"alter\",\"cartridge\",\"cart_type\",\"action\",\"app_name\",\"api\"],\"exit_code\":0}";
+ }
}
Modified:
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/ListCartridgesTest.java
===================================================================
---
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/ListCartridgesTest.java 2011-09-12
13:45:27 UTC (rev 34633)
+++
trunk/as/plugins/org.jboss.ide.eclipse.as.openshift.test/src/org/jboss/ide/eclipse/as/openshift/internal/test/core/ListCartridgesTest.java 2011-09-12
14:01:34 UTC (rev 34634)
@@ -22,6 +22,7 @@
import
org.jboss.ide.eclipse.as.openshift.core.internal.marshalling.ListCartridgesRequestJsonMarshaller;
import org.jboss.ide.eclipse.as.openshift.internal.core.request.ListCartridgesRequest;
import
org.jboss.ide.eclipse.as.openshift.internal.core.request.OpenshiftJsonRequestFactory;
+import org.jboss.ide.eclipse.as.openshift.internal.core.response.JsonSanitizer;
import
org.jboss.ide.eclipse.as.openshift.internal.core.response.ListCartridgesResponseUnmarshaller;
import org.jboss.ide.eclipse.as.openshift.internal.core.response.OpenshiftResponse;
import org.junit.Test;
@@ -46,7 +47,7 @@
assertEquals(expectedRequestString, effectiveRequest);
}
-
+
@Test
public void canUnmarshallCartridgeListResponse() throws OpenshiftException {
String cartridgeListResponse =
@@ -54,16 +55,29 @@
+ "\"messages\":\"\","
+ "\"debug\":\"\","
+ "\"data\":"
- +
"{\"carts\":[\"perl-5.10\",\"jbossas-7.0\",\"wsgi-3.2\",\"rack-1.1\",\"php-5.3\"]},"
+ +
"\"{\\\"carts\\\":[\\\"perl-5.10\\\",\\\"jbossas-7.0\\\",\\\"wsgi-3.2\\\",\\\"rack-1.1\\\",\\\"php-5.3\\\"]}\","
+ "\"api\":\"1.1.1\","
+ "\"api_c\":[\"placeholder\"],"
+ "\"result\":null,"
+ "\"broker\":\"1.1.1\","
- +
"\"broker_c\":[\"namespace\",\"rhlogin\",\"ssh\",\"app_uuid\",\"debug\",\"alter\",\"cartridge\",\"cart_type\",\"action\",\"app_name\",\"api\"],"
+ + "\"broker_c\":["
+ + "\"namespace\","
+ + "\"rhlogin\","
+ + "\"ssh\","
+ + "\"app_uuid\","
+ + "\"debug\","
+ + "\"alter\","
+ + "\"cartridge\","
+ + "\"cart_type\","
+ + "\"action\","
+ + "\"app_name\","
+ + "\"api"
+ + "\"],"
+ "\"exit_code\":0}";
- OpenshiftResponse<List<Cartridge>> response = new
ListCartridgesResponseUnmarshaller(cartridgeListResponse)
- .unmarshall();
+ cartridgeListResponse = JsonSanitizer.sanitize(cartridgeListResponse);
+ OpenshiftResponse<List<Cartridge>> response =
+ new ListCartridgesResponseUnmarshaller().unmarshall(cartridgeListResponse);
assertEquals("", response.getMessages());
assertEquals(false, response.isDebug());