Author: rombert Date: Tue Sep 23 15:25:55 2014 New Revision: 1627053 URL: http://svn.apache.org/r1627053 Log: SLING-3826 - Do not use HttpMethod.getResponseBodyAsString
Modified: sling/trunk/tooling/ide/api/src/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/BundleStateHelper.java sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ExternalSlingLaunchpad.java sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/RepositoryAccessor.java Modified: sling/trunk/tooling/ide/api/src/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/api/src/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java?rev=1627053&r1=1627052&r2=1627053&view=diff ============================================================================== --- sling/trunk/tooling/ide/api/src/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java (original) +++ sling/trunk/tooling/ide/api/src/org/apache/sling/ide/osgi/impl/HttpOsgiClient.java Tue Sep 23 15:25:55 2014 @@ -20,6 +20,7 @@ package org.apache.sling.ide.osgi.impl; import java.io.ByteArrayOutputStream; import java.io.IOException; import java.io.InputStream; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.List; @@ -44,6 +45,7 @@ import org.apache.sling.ide.transport.Re import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import org.json.JSONTokener; import org.osgi.framework.Version; public class HttpOsgiClient implements OsgiClient { @@ -60,6 +62,7 @@ public class HttpOsgiClient implements O GetMethod method = new GetMethod(repositoryInfo.getUrl() + "system/console/bundles.json"); HttpClient client = getHttpClient(); + InputStream input = null; try { int result = client.executeMethod(method); @@ -67,7 +70,9 @@ public class HttpOsgiClient implements O throw new HttpException("Got status code " + result + " for call to " + method.getURI()); } - JSONObject object = new JSONObject(method.getResponseBodyAsString()); + input = method.getResponseBodyAsStream(); + + JSONObject object = new JSONObject(new JSONTokener(new InputStreamReader(input))); JSONArray bundleData = object.getJSONArray("data"); for (int i = 0; i < bundleData.length(); i++) { @@ -88,6 +93,7 @@ public class HttpOsgiClient implements O } catch (JSONException e) { throw new OsgiClientException(e); } finally { + IOUtils.closeQuietly(input); method.releaseConnection(); } } Modified: sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/BundleStateHelper.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/BundleStateHelper.java?rev=1627053&r1=1627052&r2=1627053&view=diff ============================================================================== --- sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/BundleStateHelper.java (original) +++ sling/trunk/tooling/ide/eclipse-core/src/org/apache/sling/ide/eclipse/core/internal/BundleStateHelper.java Tue Sep 23 15:25:55 2014 @@ -16,6 +16,8 @@ */ package org.apache.sling.ide.eclipse.core.internal; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.jar.Manifest; import org.apache.commons.httpclient.Credentials; @@ -24,6 +26,7 @@ import org.apache.commons.httpclient.Htt import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.io.IOUtils; import org.apache.sling.ide.eclipse.core.ISlingLaunchpadServer; import org.eclipse.core.resources.IFile; import org.eclipse.core.resources.IProject; @@ -34,6 +37,7 @@ import org.eclipse.jdt.core.IJavaProject import org.eclipse.wst.server.core.IServer; import org.json.JSONArray; import org.json.JSONObject; +import org.json.JSONTokener; public class BundleStateHelper { @@ -86,6 +90,9 @@ public class BundleStateHelper { } private static Object doRecalcDecorationState(IServer server, IProject project) { + + InputStream input = null; + try { if (!ProjectHelper.isBundleProject(project)) { return EMPTY_STATE; @@ -121,15 +128,18 @@ public class BundleStateHelper { if (resultCode!=HttpStatus.SC_OK) { return " ["+resultCode+"]"; } - String responseBodyAsString = method.getResponseBodyAsString(); // explicitly not limiting buffer here - even though this results in a warning - // cannot know the size of a bundle in advance, large bundles can have large json - JSONObject result = new JSONObject(responseBodyAsString); + + input = method.getResponseBodyAsStream(); + + JSONObject result = new JSONObject(new JSONTokener(new InputStreamReader(input))); JSONArray dataArray = (JSONArray) result.get("data"); JSONObject firstElement = (JSONObject) dataArray.get(0); return " ["+firstElement.get("state")+"]"; } catch (Exception e) { e.printStackTrace(); return e.getMessage(); + } finally { + IOUtils.closeQuietly(input); } } Modified: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ExternalSlingLaunchpad.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ExternalSlingLaunchpad.java?rev=1627053&r1=1627052&r2=1627053&view=diff ============================================================================== --- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ExternalSlingLaunchpad.java (original) +++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/ExternalSlingLaunchpad.java Tue Sep 23 15:25:55 2014 @@ -16,6 +16,8 @@ */ package org.apache.sling.ide.test.impl.helpers; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -30,8 +32,10 @@ import org.apache.commons.httpclient.Htt import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.io.IOUtils; import org.json.JSONArray; import org.json.JSONObject; +import org.json.JSONTokener; import org.junit.rules.ExternalResource; public class ExternalSlingLaunchpad extends ExternalResource { @@ -105,20 +109,28 @@ public class ExternalSlingLaunchpad exte int status = client.executeMethod(httpMethod); debug("vmstat http call got return code " + status); - if (status == 200) { - String responseBody = httpMethod.getResponseBodyAsString(); - Matcher m = STARTLEVEL_JSON_SNIPPET.matcher(responseBody); - if (m.find()) { - int startLevel = Integer.parseInt(m.group(1)); - debug("vmstat http call got startLevel " + startLevel); - if (startLevel >= EXPECTED_START_LEVEL) { - debug("current startLevel " + startLevel + " >= " + EXPECTED_START_LEVEL + ", we are done here"); - return true; + InputStream input = null; + try { + if (status == 200) { + + String responseBody = IOUtils.toString(httpMethod.getResponseBodyAsStream(), + httpMethod.getResponseCharSet()); + + Matcher m = STARTLEVEL_JSON_SNIPPET.matcher(responseBody); + if (m.find()) { + int startLevel = Integer.parseInt(m.group(1)); + debug("vmstat http call got startLevel " + startLevel); + if (startLevel >= EXPECTED_START_LEVEL) { + debug("current startLevel " + startLevel + " >= " + EXPECTED_START_LEVEL + + ", we are done here"); + return true; + } } - } + } + } finally { + IOUtils.closeQuietly(input); } - return false; } } @@ -137,21 +149,27 @@ public class ExternalSlingLaunchpad exte int status = client.executeMethod(httpMethod); debug("bundles http call got return code " + status); - if (status == 200) { - JSONObject obj = new JSONObject(httpMethod.getResponseBodyAsString()); - - JSONArray bundleStatus = obj.getJSONArray("s"); - - int total = bundleStatus.getInt(0); - int active = bundleStatus.getInt(1); - int fragment = bundleStatus.getInt(2); - - debug("bundle http call status: total = " + total + ", active = " + active + ", fragment = " + fragment); - - if (total == active + fragment) { - debug("All bundles are started, we are done here"); - return true; + InputStream input = null; + try { + if (status == 200) { + input = httpMethod.getResponseBodyAsStream(); + JSONObject obj = new JSONObject(new JSONTokener(new InputStreamReader(input))); + + JSONArray bundleStatus = obj.getJSONArray("s"); + + int total = bundleStatus.getInt(0); + int active = bundleStatus.getInt(1); + int fragment = bundleStatus.getInt(2); + + debug("bundle http call status: total = " + total + ", active = " + active + ", fragment = " + fragment); + + if (total == active + fragment) { + debug("All bundles are started, we are done here"); + return true; + } } + } finally { + IOUtils.closeQuietly(input); } return false; Modified: sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/RepositoryAccessor.java URL: http://svn.apache.org/viewvc/sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/RepositoryAccessor.java?rev=1627053&r1=1627052&r2=1627053&view=diff ============================================================================== --- sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/RepositoryAccessor.java (original) +++ sling/trunk/tooling/ide/eclipse-test/src/org/apache/sling/ide/test/impl/helpers/RepositoryAccessor.java Tue Sep 23 15:25:55 2014 @@ -20,6 +20,7 @@ import static org.junit.Assert.assertTha import java.io.ByteArrayInputStream; import java.io.IOException; +import java.io.InputStream; import javax.jcr.Credentials; import javax.jcr.Node; @@ -32,6 +33,7 @@ import org.apache.commons.httpclient.Htt import org.apache.commons.httpclient.UsernamePasswordCredentials; import org.apache.commons.httpclient.auth.AuthScope; import org.apache.commons.httpclient.methods.GetMethod; +import org.apache.commons.io.IOUtils; import org.apache.jackrabbit.util.Text; import org.apache.sling.ide.jcr.RepositoryUtils; import org.apache.sling.ide.transport.RepositoryInfo; @@ -61,13 +63,19 @@ public class RepositoryAccessor { public void assertGetIsSuccessful(String path, String expectedResult) throws HttpException, IOException { GetMethod m = new GetMethod(config.getUrl() + path); + InputStream input = null; try { int result = client.executeMethod(m); assertThat("Unexpected status call for " + m.getURI(), result, CoreMatchers.equalTo(200)); - assertThat("Unexpected response for " + m.getURI(), m.getResponseBodyAsString(), + + input = m.getResponseBodyAsStream(); + String responseBody = IOUtils.toString(input, m.getRequestCharSet()); + + assertThat("Unexpected response for " + m.getURI(), responseBody, CoreMatchers.equalTo(expectedResult)); } finally { + IOUtils.closeQuietly(input); m.releaseConnection(); } }