Search in sources :

Example 1 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project zeppelin by apache.

the class NotebookSecurityRestApiTest method userTryGetNote.

private void userTryGetNote(String noteId, String user, String pwd, Matcher<? super HttpMethodBase> m) throws IOException {
    GetMethod get = httpGet("/notebook/" + noteId, user, pwd);
    assertThat(get, m);
    get.releaseConnection();
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod)

Example 2 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project zeppelin by apache.

the class ZeppelinRestApiTest method testexportNote.

@Test
public void testexportNote() throws IOException {
    LOG.info("testexportNote");
    Note note = ZeppelinServer.notebook.createNote(anonymous);
    assertNotNull("can't create new note", note);
    note.setName("source note for export");
    Paragraph paragraph = note.addParagraph(AuthenticationInfo.ANONYMOUS);
    Map config = paragraph.getConfig();
    config.put("enabled", true);
    paragraph.setConfig(config);
    paragraph.setText("%md This is my new paragraph in my new note");
    note.persist(anonymous);
    String sourceNoteId = note.getId();
    // Call export Note REST API
    GetMethod get = httpGet("/notebook/export/" + sourceNoteId);
    LOG.info("testNoteExport \n" + get.getResponseBodyAsString());
    assertThat("test note export method:", get, isAllowed());
    Map<String, Object> resp = gson.fromJson(get.getResponseBodyAsString(), new TypeToken<Map<String, Object>>() {
    }.getType());
    String exportJSON = (String) resp.get("body");
    assertNotNull("Can not find new notejson", exportJSON);
    LOG.info("export JSON:=" + exportJSON);
    ZeppelinServer.notebook.removeNote(sourceNoteId, anonymous);
    get.releaseConnection();
}
Also used : TypeToken(com.google.gson.reflect.TypeToken) Note(org.apache.zeppelin.notebook.Note) GetMethod(org.apache.commons.httpclient.methods.GetMethod) Map(java.util.Map) Paragraph(org.apache.zeppelin.notebook.Paragraph) Test(org.junit.Test)

Example 3 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project zeppelin by apache.

the class AbstractTestRestApi method checkIfServerIsRunning.

protected static boolean checkIfServerIsRunning() {
    GetMethod request = null;
    boolean isRunning = true;
    try {
        request = httpGet("/version");
        isRunning = request.getStatusCode() == 200;
    } catch (IOException e) {
        LOG.error("AbstractTestRestApi.checkIfServerIsRunning() fails .. ZeppelinServer is not running");
        isRunning = false;
    } finally {
        if (request != null) {
            request.releaseConnection();
        }
    }
    return isRunning;
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException)

Example 4 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project pinot by linkedin.

the class SchemaUtils method getSchema.

/**
   * Given host, port and schema name, send a http GET request to download the {@link Schema}.
   *
   * @return schema on success.
   * <P><code>null</code> on failure.
   */
@Nullable
public static Schema getSchema(@Nonnull String host, int port, @Nonnull String schemaName) {
    Preconditions.checkNotNull(host);
    Preconditions.checkNotNull(schemaName);
    try {
        URL url = new URL("http", host, port, "/schemas/" + schemaName);
        GetMethod httpGet = new GetMethod(url.toString());
        try {
            int responseCode = HTTP_CLIENT.executeMethod(httpGet);
            String response = httpGet.getResponseBodyAsString();
            if (responseCode >= 400) {
                // File not find error code.
                if (responseCode == 404) {
                    LOGGER.info("Cannot find schema: {} from host: {}, port: {}", schemaName, host, port);
                } else {
                    LOGGER.warn("Got error response code: {}, response: {}", responseCode, response);
                }
                return null;
            }
            return Schema.fromString(response);
        } finally {
            httpGet.releaseConnection();
        }
    } catch (Exception e) {
        LOGGER.error("Caught exception while getting the schema: {} from host: {}, port: {}", schemaName, host, port, e);
        return null;
    }
}
Also used : GetMethod(org.apache.commons.httpclient.methods.GetMethod) URL(java.net.URL) IOException(java.io.IOException) Nullable(javax.annotation.Nullable)

Example 5 with GetMethod

use of org.apache.commons.httpclient.methods.GetMethod in project pinot by linkedin.

the class MultiGetRequestTest method testMultiGet.

@Test
public void testMultiGet() {
    MultiGetRequest mget = new MultiGetRequest(Executors.newCachedThreadPool(), new MultiThreadedHttpConnectionManager());
    List<String> urls = Arrays.asList("http://localhost:" + String.valueOf(portStart) + URI_PATH, "http://localhost:" + String.valueOf(portStart + 1) + URI_PATH, "http://localhost:" + String.valueOf(portStart + 2) + URI_PATH, // 2nd request to the same server
    "http://localhost:" + String.valueOf(portStart) + URI_PATH);
    // timeout value needs to be less than 5000ms set above for
    // third server
    final int requestTimeoutMs = 1000;
    CompletionService<GetMethod> completionService = mget.execute(urls, requestTimeoutMs);
    int success = 0;
    int errors = 0;
    int timeouts = 0;
    for (int i = 0; i < urls.size(); i++) {
        GetMethod getMethod = null;
        try {
            getMethod = completionService.take().get();
            if (getMethod.getStatusCode() >= 300) {
                ++errors;
                Assert.assertEquals(getMethod.getResponseBodyAsString(), ERROR_MSG);
            } else {
                ++success;
                Assert.assertEquals(getMethod.getResponseBodyAsString(), SUCCESS_MSG);
            }
        } catch (InterruptedException e) {
            LOGGER.error("Interrupted", e);
            ++errors;
        } catch (ExecutionException e) {
            if (Throwables.getRootCause(e) instanceof SocketTimeoutException) {
                LOGGER.debug("Timeout");
                ++timeouts;
            } else {
                LOGGER.error("Error", e);
                ++errors;
            }
        } catch (IOException e) {
            ++errors;
        }
    }
    Assert.assertEquals(2, success);
    Assert.assertEquals(1, errors);
    Assert.assertEquals(1, timeouts);
}
Also used : SocketTimeoutException(java.net.SocketTimeoutException) MultiThreadedHttpConnectionManager(org.apache.commons.httpclient.MultiThreadedHttpConnectionManager) GetMethod(org.apache.commons.httpclient.methods.GetMethod) IOException(java.io.IOException) ExecutionException(java.util.concurrent.ExecutionException) Test(org.testng.annotations.Test) AfterTest(org.testng.annotations.AfterTest) BeforeTest(org.testng.annotations.BeforeTest)

Aggregations

GetMethod (org.apache.commons.httpclient.methods.GetMethod)547 HttpClient (org.apache.commons.httpclient.HttpClient)220 IOException (java.io.IOException)114 Test (org.junit.Test)113 HttpMethod (org.apache.commons.httpclient.HttpMethod)97 InputStream (java.io.InputStream)80 AbstractHttpTest (org.xwiki.test.rest.framework.AbstractHttpTest)58 HashMap (java.util.HashMap)37 HttpException (org.apache.commons.httpclient.HttpException)35 JSONParser (org.json.simple.parser.JSONParser)34 JSONObject (org.json.simple.JSONObject)32 ArrayList (java.util.ArrayList)31 PostMethod (org.apache.commons.httpclient.methods.PostMethod)31 UsernamePasswordCredentials (org.apache.commons.httpclient.UsernamePasswordCredentials)29 Header (org.apache.commons.httpclient.Header)27 PutMethod (org.apache.commons.httpclient.methods.PutMethod)27 Map (java.util.Map)26 AuthRequestHandler (org.apache.commons.httpclient.server.AuthRequestHandler)22 HttpRequestHandlerChain (org.apache.commons.httpclient.server.HttpRequestHandlerChain)22 HttpServiceHandler (org.apache.commons.httpclient.server.HttpServiceHandler)22