protected JSONObject postQuery(HttpClient httpClient, String url, JSONObject body)
      throws UnsupportedEncodingException, IOException, HttpException, URIException, JSONException {
    PostMethod post = new PostMethod(url);
    if (body.toString().length() > DEFAULT_SAVEPOST_BUFFER) {
      post.getParams().setBooleanParameter(HttpMethodParams.USE_EXPECT_CONTINUE, true);
    }
    post.setRequestEntity(
        new ByteArrayRequestEntity(body.toString().getBytes("UTF-8"), "application/json"));

    try {
      httpClient.executeMethod(post);

      if (post.getStatusCode() == HttpStatus.SC_MOVED_PERMANENTLY
          || post.getStatusCode() == HttpStatus.SC_MOVED_TEMPORARILY) {
        Header locationHeader = post.getResponseHeader("location");
        if (locationHeader != null) {
          String redirectLocation = locationHeader.getValue();
          post.setURI(new URI(redirectLocation, true));
          httpClient.executeMethod(post);
        }
      }

      if (post.getStatusCode() != HttpServletResponse.SC_OK) {
        throw new LuceneQueryParserException(
            "Request failed " + post.getStatusCode() + " " + url.toString());
      }

      Reader reader =
          new BufferedReader(
              new InputStreamReader(post.getResponseBodyAsStream(), post.getResponseCharSet()));
      // TODO - replace with streaming-based solution e.g. SimpleJSON ContentHandler
      JSONObject json = new JSONObject(new JSONTokener(reader));

      if (json.has("status")) {
        JSONObject status = json.getJSONObject("status");
        if (status.getInt("code") != HttpServletResponse.SC_OK) {
          throw new LuceneQueryParserException("SOLR side error: " + status.getString("message"));
        }
      }
      return json;
    } finally {
      post.releaseConnection();
    }
  }
示例#2
0
  @Test
  public void testHandleWithCorrectConfiguration() throws Exception {
    HttpClient client = new HttpClient();
    PostMethod post = new PostMethod();
    post.setURI(
        new URI(
            "http://localhost:"
                + TEST_PORT
                + "/"
                + CONTEXT
                + "/"
                + CoverageConstants.COVERAGE_SERVICE_BPELUNIT_NAME,
            false));
    post.setRequestHeader("Content-Type", "text/xml");
    InputStream soapMsgStream = getClass().getResourceAsStream("mark.soap.xml");
    assertNotNull("mark.soap.xml exists in test resources", soapMsgStream);
    post.setRequestEntity(new InputStreamRequestEntity(soapMsgStream));
    client.executeMethod(post);

    assertEquals(3, dummyInstrumenter.markers.size());
    assertEquals("M1", dummyInstrumenter.markers.get(0));
    assertEquals("M2", dummyInstrumenter.markers.get(1));
    assertEquals("M3", dummyInstrumenter.markers.get(2));
  }