Example usage for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsString

List of usage examples for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsString

Introduction

In this page you can find the example usage for org.apache.commons.httpclient HttpMethodBase getResponseBodyAsString.

Prototype

@Override
public String getResponseBodyAsString() throws IOException 

Source Link

Document

Returns the response body of the HTTP method, if any, as a String .

Usage

From source file:org.apache.geronimo.testsuite.requirebundle.resolution.tests.ResolutionOptionalServletTest.java

/**
 * Test 1/*from   w ww  .j a  v a 2  s .  c o m*/
 * Test 
 * Require-Bundle: Resolution=optional in OSGi core spec.
 * @throws Exception
 */
@Test
public void RequireBundleTest() throws Exception {
    String contextroot = System.getProperty("webAppName");
    String root = "http://localhost:8080/" + contextroot;
    int status = 0;
    HttpClient nclient = new HttpClient();
    String url = root + "/checkResolution";
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
    status = nclient.executeMethod(httpMethod);
    Assert.assertEquals(status, 200);
    String response = null;
    if (status == 200) {
        response = new String(httpMethod.getResponseBodyAsString().getBytes("8859_1"));
    }
    Assert.assertTrue(response
            .contains("Hello! Reexport in Require-bundle attribute is effective since this INFO displays."));
    Assert.assertTrue(response.contains("Succeed to resolve Attr \"resolution=optional\""));
    httpMethod.releaseConnection();
}

From source file:org.apache.geronimo.testsuite.requirebundle.tests.RequireBundleCalculatorServletTest.java

/**
 * Test 1/*from w w w .  j a  v a  2 s  . c om*/
 * Test 
 * Require-Bundle in OSGi core spec.
 * @throws Exception
 */
@Test
public void RequireBundleTest() throws Exception {
    String contextroot = System.getProperty("webAppName");
    String root = "http://localhost:8080/" + contextroot;
    int status = 0;
    HttpClient nclient = new HttpClient();
    String url = root + "/CalculatorServlet";
    HttpMethodBase httpMethod;
    httpMethod = new PostMethod(url);
    status = nclient.executeMethod(httpMethod);
    Assert.assertEquals(status, 200);
    String response = null;
    if (status == 200) {
        response = new String(httpMethod.getResponseBodyAsString().getBytes("8859_1"));
    }
    Assert.assertTrue(response
            .contains("Hello! Reexport in Require-bundle attribute is effective since this INFO displays."));
    Assert.assertTrue(response.contains("result of ADD operation [\"10.0 + 8.0 = ?\"] is: 18"));
    Assert.assertTrue(response.contains("result of SUB operation [\"10.0 - 8.0 = ?\"] is: 2"));
    httpMethod.releaseConnection();
}

From source file:org.apache.kylin.jdbc.KylinClient.java

private IOException asIOException(HttpMethodBase method) throws IOException {
    return new IOException(method + " failed, error code " + method.getStatusCode() + " and response: "
            + method.getResponseBodyAsString());
}

From source file:org.apache.solr.servlet.CacheHeaderTestBase.java

protected void checkResponseBody(String method, HttpMethodBase resp) throws Exception {
    String responseBody = resp.getResponseBodyAsString();
    if ("GET".equals(method)) {
        switch (resp.getStatusCode()) {
        case 200:
            assertTrue("Response body was empty for method " + method,
                    responseBody != null && responseBody.length() > 0);
            break;
        case 304:
            assertTrue("Response body was not empty for method " + method,
                    responseBody == null || responseBody.length() == 0);
            break;
        case 412:
            assertTrue("Response body was not empty for method " + method,
                    responseBody == null || responseBody.length() == 0);
            break;
        default:/*from   w  w  w  .ja  v  a2  s  .c om*/
            System.err.println(responseBody);
            assertEquals("Unknown request response", 0, resp.getStatusCode());
        }
    }
    if ("HEAD".equals(method)) {
        assertTrue("Response body was not empty for method " + method,
                responseBody == null || responseBody.length() == 0);
    }
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritClient.java

public void publishComments(String reviewId, int patchSetId, final String message,
        final Set<ApprovalCategoryValue.Id> approvals, IProgressMonitor monitor) throws GerritException {
    final PatchSet.Id id = new PatchSet.Id(new Change.Id(id(reviewId)), patchSetId);
    ReviewInput reviewInput = new ReviewInput(message);
    Map<String, CommentInfo[]> drafts = listDrafts(id, monitor);
    Map<String, CommentInput[]> comments = convert(drafts);
    if (!comments.isEmpty()) {
        reviewInput.setComments(comments);
    }//from w  w  w.  ja  v a2 s.  c  om
    reviewInput.setApprovals(approvals);
    final String uri = "/a/changes/" + id.getParentKey().get() + "/revisions/" + id.get() + "/review"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    executePostRestRequest(uri, reviewInput, ReviewInfo.class, new ErrorHandler() {

        @Override
        public void handleError(HttpMethodBase method) throws GerritException {
            if (method.getStatusCode() == HttpURLConnection.HTTP_FORBIDDEN) {
                String msg = getResponseBodyAsString(method);
                if (msg.startsWith("Applying label") && msg.endsWith("is restricted")) { //$NON-NLS-1$ //$NON-NLS-2$
                    throw new GerritException(msg);
                }
            }
        }

        private String getResponseBodyAsString(HttpMethodBase method) {
            try {
                String msg = method.getResponseBodyAsString();
                return msg.trim();
            } catch (IOException e) {
                // ignore
            }
            return null;
        }
    }, monitor);
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritClient.java

private ChangeDetail submitRest(PatchSet.Id id, IProgressMonitor monitor) throws GerritException {
    final String uri = "/a/changes/" + id.getParentKey().get() + "/revisions/" + id.get() + "/submit"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$
    executePostRestRequest(uri, new SubmitInput(true), SubmitInfo.class, new ErrorHandler() {
        @Override/*ww  w. java  2 s  . com*/
        public void handleError(HttpMethodBase method) throws GerritException {
            String errorMsg = getResponseBodyAsString(method);
            if (isNotPermitted(method, errorMsg) || isConflict(method)) {
                throw new GerritException(NLS.bind("Cannot submit change: {0}", errorMsg)); //$NON-NLS-1$
            }
        }

        private String getResponseBodyAsString(HttpMethodBase method) {
            try {
                return method.getResponseBodyAsString();
            } catch (IOException e) {
                return null;
            }
        }

        private boolean isNotPermitted(HttpMethodBase method, String msg) {
            return method.getStatusCode() == HttpURLConnection.HTTP_FORBIDDEN
                    && "submit not permitted\n".equals(msg); //$NON-NLS-1$
        }

        private boolean isConflict(HttpMethodBase method) {
            return method.getStatusCode() == HttpURLConnection.HTTP_CONFLICT;
        }
    }, monitor);
    return getChangeDetail(id.getParentKey().get(), monitor);
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritClient.java

public Version getVersion(IProgressMonitor monitor) throws GerritException {
    return execute(monitor, new Operation<Version>() {
        @Override//from   ww  w.  j  a  v  a 2s  . c o m
        public void execute(IProgressMonitor monitor) throws GerritException {
            try {
                Request<String> request = new Request<String>() {
                    @Override
                    public HttpMethodBase createMethod() throws IOException {
                        return new GetMethod(client.getUrl() + "/tools/hooks/"); //$NON-NLS-1$
                    }

                    @Override
                    public String process(HttpMethodBase method) throws IOException {
                        String content = method.getResponseBodyAsString();
                        Matcher matcher = GERRIT_VERSION_PATTERN.matcher(content);
                        if (matcher.find()) {
                            return matcher.group(1);
                        }
                        return null;
                    }
                };
                String result = client.execute(request, false, monitor);
                Version version = GerritVersion.parseGerritVersion(result);
                onSuccess(version);
            } catch (Exception e) {
                onFailure(e);
            }
        }
    });
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritClient28.java

@Override
public ChangeDetail cherryPick(String reviewId, int patchSetId, final String message, final String destBranch,
        IProgressMonitor monitor) throws GerritException {
    final PatchSet.Id id = new PatchSet.Id(new Change.Id(id(reviewId)), patchSetId);
    String url = "/changes/" + id.getParentKey() + "/revisions/" + id.get() + "/cherrypick"; //$NON-NLS-1$//$NON-NLS-2$//$NON-NLS-3$
    CherryPickInput input = new CherryPickInput(message, destBranch);
    ChangeInfo result = executePostRestRequest(url, input, ChangeInfo.class, new ErrorHandler() {
        @Override//w  w  w  . j a va 2  s.  c o m
        public void handleError(HttpMethodBase method) throws GerritException {
            String errorMsg = getResponseBodyAsString(method);
            if (isNotPermitted(method, errorMsg)) {
                errorMsg = NLS.bind("Cannot cherry pick: {0}", errorMsg); //$NON-NLS-1$
            } else if (isConflict(method)) {
                errorMsg = NLS.bind("Request Conflict: {0}", errorMsg); //$NON-NLS-1$
            } else if (isBadRequest(method)) {
                errorMsg = NLS.bind("Bad Request: {0}", errorMsg); //$NON-NLS-1$
            }
            throw new GerritException(errorMsg);
        }

        private String getResponseBodyAsString(HttpMethodBase method) {
            try {
                return method.getResponseBodyAsString();
            } catch (IOException e) {
                return null;
            }
        }

        private boolean isNotPermitted(HttpMethodBase method, String msg) {
            return method.getStatusCode() == HttpURLConnection.HTTP_FORBIDDEN
                    && msg.toLowerCase().startsWith("cherry pick not permitted"); //$NON-NLS-1$
        }

        private boolean isConflict(HttpMethodBase method) {
            return method.getStatusCode() == HttpURLConnection.HTTP_CONFLICT;
        }

        private boolean isBadRequest(HttpMethodBase method) {
            return method.getStatusCode() == HttpURLConnection.HTTP_BAD_REQUEST;
        }
    }, monitor);

    return getChangeDetail(result.getNumber(), monitor);
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritClient29.java

@Override
public ChangeDetail rebase(String reviewId, int patchSetId, IProgressMonitor monitor) throws GerritException {
    final PatchSet.Id id = new PatchSet.Id(new Change.Id(id(reviewId)), patchSetId);
    final String uri = "/a/changes/" + id.getParentKey().get() + "/revisions/" + id.get() + "/rebase"; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$

    executePostRestRequest(uri, new ChangeInfo28(), ChangeInfo28.class, new ErrorHandler() {
        @Override/*  w  w  w.j  a  va2s  .c  o  m*/
        public void handleError(HttpMethodBase method) throws GerritException {
            String errorMsg = getResponseBodyAsString(method);
            if (isConflict(method)) {
                throw new GerritException(errorMsg);
            }
        }

        private String getResponseBodyAsString(HttpMethodBase method) {
            try {
                return method.getResponseBodyAsString();
            } catch (IOException e) {
                return null;
            }
        }

        private boolean isConflict(HttpMethodBase method) {
            return method.getStatusCode() == HttpURLConnection.HTTP_CONFLICT;
        }
    }, monitor);
    return getChangeDetail(id.getParentKey().get(), monitor);
}

From source file:org.eclipse.mylyn.internal.gerrit.core.client.GerritClient29.java

private List<SubmitRecord> currentSubmitRecord(String uri, IProgressMonitor monitor) throws GerritException {
    List<SubmitRecord> submitRecordList = new ArrayList<SubmitRecord>();
    SubmitRecord[] submitRecordArray = executePostRestRequest(uri, new SubmitRecord(), SubmitRecord[].class,
            new ErrorHandler() {
                @Override// www. j  a v a  2s  .  c o m
                public void handleError(HttpMethodBase method) throws GerritException {
                    String errorMsg = getResponseBodyAsString(method);
                    if (isNotPermitted(method, errorMsg) || isConflict(method)) {
                        throw new GerritException(NLS.bind("Cannot get submit change: {0}", errorMsg)); //$NON-NLS-1$
                    }
                }

                private String getResponseBodyAsString(HttpMethodBase method) {
                    try {
                        return method.getResponseBodyAsString();
                    } catch (IOException e) {
                        return null;
                    }
                }

                private boolean isNotPermitted(HttpMethodBase method, String msg) {
                    return method.getStatusCode() == HttpURLConnection.HTTP_FORBIDDEN
                            && "submit not permitted\n".equals(msg); //$NON-NLS-1$
                }

                private boolean isConflict(HttpMethodBase method) {
                    return method.getStatusCode() == HttpURLConnection.HTTP_CONFLICT;
                }
            }, monitor);
    for (SubmitRecord element : submitRecordArray) {
        List<SubmitRecord.Label> list = null;
        if (element.getStatus().equalsIgnoreCase("OK")) { //$NON-NLS-1$
            list = element.createLabel(element, element.getOkMap(), OK);
        } else if (element.getStatus().equalsIgnoreCase("NOT_READY")) { //$NON-NLS-1$
            list = element.createLabel(element, element.getNeedMap(), NEED);
        } else if (element.getStatus().equalsIgnoreCase("REJECT")) { //$NON-NLS-1$
            list = element.createLabel(element, element.getRejectMap(), REJECT);
        } else if (element.getStatus().equalsIgnoreCase("MAY")) { //$NON-NLS-1$
            list = element.createLabel(element, element.getMayMap(), MAY);
        }
        element.setLabels(list);
        submitRecordList.add(element);
    }

    return submitRecordList;
}