private HTTPClientResponseResolver createResponseResolver( final HttpMethod httpMethod, final Status status, final Header[] headers) { try { when(httpMethod.getStatusLine()) .thenReturn( new org.apache.commons.httpclient.StatusLine( String.format("HTTP/1.1 %s %s\r\n", status.getCode(), status.getName()))); } catch (HttpException e) { throw new RuntimeException(e); } when(httpMethod.getStatusCode()).thenReturn(status.getCode()); when(httpMethod.getResponseHeaders()).thenReturn(headers); return new TestableHTTPClientResponseResolver(httpMethod); }
public JSONObject get(String path) throws Exception { JSONObject result; HttpMethod method = new GetMethod(repoHostname + ":" + repoHostPort + "/api/data/" + path); method.getParams().setCookiePolicy(CookiePolicy.RFC_2109); method.setRequestHeader("Cookie", "jsonhub-store-key=" + getStoreKey()); // client.getState().setCredentials(new AuthScope(null, repoHostPort, "authentication"), new // UsernamePasswordCredentials(memberId, memberPassword)); // method.setDoAuthentication(true); int statusCode = client.executeMethod(method); if (statusCode != HttpStatus.SC_OK) { System.err.println("Method failed: " + method.getStatusLine()); throw new GeneralException(method.getStatusLine().toString()); } else { Reader reader = new InputStreamReader(method.getResponseBodyAsStream()); CharArrayWriter cdata = new CharArrayWriter(); char buf[] = new char[BUFFER_SIZE]; int ret; while ((ret = reader.read(buf, 0, BUFFER_SIZE)) != -1) cdata.write(buf, 0, ret); reader.close(); System.out.println("get response: " + cdata.toString()); result = JSONObject.fromObject(cdata.toString()); } method.releaseConnection(); if (result.has("error")) throw new GeneralException(result.getString("error")); return result; }