Search in sources :

Example 96 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestHttpExceptionUtils method testValidateResponseJsonErrorKnownException.

@Test
public void testValidateResponseJsonErrorKnownException() throws IOException {
    Map<String, Object> json = new HashMap<String, Object>();
    json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, IllegalStateException.class.getSimpleName());
    json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, IllegalStateException.class.getName());
    json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX");
    Map<String, Object> response = new HashMap<String, Object>();
    response.put(HttpExceptionUtils.ERROR_JSON, json);
    ObjectMapper jsonMapper = new ObjectMapper();
    String msg = jsonMapper.writeValueAsString(response);
    InputStream is = new ByteArrayInputStream(msg.getBytes());
    HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
    Mockito.when(conn.getErrorStream()).thenReturn(is);
    Mockito.when(conn.getResponseMessage()).thenReturn("msg");
    Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST);
    try {
        HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
        Assert.fail();
    } catch (IllegalStateException ex) {
        Assert.assertEquals("EX", ex.getMessage());
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 97 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestHttpExceptionUtils method testValidateResponseOK.

@Test
public void testValidateResponseOK() throws IOException {
    HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
    Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_CREATED);
    HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Test(org.junit.Test)

Example 98 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestHttpExceptionUtils method testValidateResponseJsonErrorUnknownException.

@Test
public void testValidateResponseJsonErrorUnknownException() throws IOException {
    Map<String, Object> json = new HashMap<String, Object>();
    json.put(HttpExceptionUtils.ERROR_EXCEPTION_JSON, "FooException");
    json.put(HttpExceptionUtils.ERROR_CLASSNAME_JSON, "foo.FooException");
    json.put(HttpExceptionUtils.ERROR_MESSAGE_JSON, "EX");
    Map<String, Object> response = new HashMap<String, Object>();
    response.put(HttpExceptionUtils.ERROR_JSON, json);
    ObjectMapper jsonMapper = new ObjectMapper();
    String msg = jsonMapper.writeValueAsString(response);
    InputStream is = new ByteArrayInputStream(msg.getBytes());
    HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
    Mockito.when(conn.getErrorStream()).thenReturn(is);
    Mockito.when(conn.getResponseMessage()).thenReturn("msg");
    Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST);
    try {
        HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
        Assert.fail();
    } catch (IOException ex) {
        Assert.assertTrue(ex.getMessage().contains("EX"));
        Assert.assertTrue(ex.getMessage().contains("foo.FooException"));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) HashMap(java.util.HashMap) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) Test(org.junit.Test)

Example 99 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class TestHttpExceptionUtils method testValidateResponseNonJsonErrorMessage.

@Test
public void testValidateResponseNonJsonErrorMessage() throws IOException {
    String msg = "stream";
    InputStream is = new ByteArrayInputStream(msg.getBytes());
    HttpURLConnection conn = Mockito.mock(HttpURLConnection.class);
    Mockito.when(conn.getErrorStream()).thenReturn(is);
    Mockito.when(conn.getResponseMessage()).thenReturn("msg");
    Mockito.when(conn.getResponseCode()).thenReturn(HttpURLConnection.HTTP_BAD_REQUEST);
    try {
        HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_CREATED);
        Assert.fail();
    } catch (IOException ex) {
        Assert.assertTrue(ex.getMessage().contains("msg"));
        Assert.assertTrue(ex.getMessage().contains("" + HttpURLConnection.HTTP_BAD_REQUEST));
    }
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ByteArrayInputStream(java.io.ByteArrayInputStream) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) IOException(java.io.IOException) Test(org.junit.Test)

Example 100 with HttpURLConnection

use of java.net.HttpURLConnection in project hadoop by apache.

the class HttpFSFileSystem method getHomeDirectory.

/**
   * Return the current user's home directory in this filesystem.
   * The default implementation returns "/user/$USER/".
   */
@Override
public Path getHomeDirectory() {
    Map<String, String> params = new HashMap<String, String>();
    params.put(OP_PARAM, Operation.GETHOMEDIRECTORY.toString());
    try {
        HttpURLConnection conn = getConnection(Operation.GETHOMEDIRECTORY.getMethod(), params, new Path(getUri().toString(), "/"), false);
        HttpExceptionUtils.validateResponse(conn, HttpURLConnection.HTTP_OK);
        JSONObject json = (JSONObject) HttpFSUtils.jsonParse(conn);
        return new Path((String) json.get(HOME_DIR_JSON));
    } catch (IOException ex) {
        throw new RuntimeException(ex);
    }
}
Also used : Path(org.apache.hadoop.fs.Path) HttpURLConnection(java.net.HttpURLConnection) JSONObject(org.json.simple.JSONObject) HashMap(java.util.HashMap) IOException(java.io.IOException)

Aggregations

HttpURLConnection (java.net.HttpURLConnection)3831 URL (java.net.URL)2447 IOException (java.io.IOException)1634 InputStream (java.io.InputStream)1082 InputStreamReader (java.io.InputStreamReader)692 Test (org.junit.Test)650 BufferedReader (java.io.BufferedReader)573 OutputStream (java.io.OutputStream)466 MalformedURLException (java.net.MalformedURLException)372 URLConnection (java.net.URLConnection)248 HashMap (java.util.HashMap)216 OutputStreamWriter (java.io.OutputStreamWriter)208 Map (java.util.Map)199 Gson (com.google.gson.Gson)190 ByteArrayOutputStream (java.io.ByteArrayOutputStream)186 ArrayList (java.util.ArrayList)168 ExecutionException (java.util.concurrent.ExecutionException)161 File (java.io.File)159 AsyncTask (android.os.AsyncTask)158 HttpsURLConnection (javax.net.ssl.HttpsURLConnection)157