Example usage for java.net HttpURLConnection getHeaderFields

List of usage examples for java.net HttpURLConnection getHeaderFields

Introduction

In this page you can find the example usage for java.net HttpURLConnection getHeaderFields.

Prototype

public Map<String, List<String>> getHeaderFields() 

Source Link

Document

Returns an unmodifiable Map of the header fields.

Usage

From source file:org.apache.qpid.systest.rest.SaslRestTest.java

public void testCramMD5SaslAuthenticationForValidCredentials() throws Exception {
    startBrokerNow();//from w ww . j av a2s  . com

    // request the challenge for CRAM-MD5
    HttpURLConnection connection = requestSasServerChallenge("CRAM-MD5");
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");

    // authenticate user with correct credentials
    int code = authenticateUser(connection, "admin", "admin", "CRAM-MD5");
    assertEquals("Unexpected response code", 200, code);

    // request authenticated user details
    connection = getRestTestHelper().openManagementConnection("/rest/sasl", "GET");
    applyCookiesToConnection(cookies, connection);
    Map<String, Object> response2 = getRestTestHelper().readJsonResponseAsMap(connection);
    assertEquals("Unexpected user", "admin", response2.get("user"));
}

From source file:org.apache.qpid.systest.rest.SaslRestTest.java

public void testCramMD5SaslAuthenticationForIncorrectPassword() throws Exception {
    startBrokerNow();//w  ww. j av a2s.  c  om

    // request the challenge for CRAM-MD5
    HttpURLConnection connection = requestSasServerChallenge("CRAM-MD5");
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");

    // authenticate user with correct credentials
    int code = authenticateUser(connection, "admin", "incorrect", "CRAM-MD5");
    assertEquals("Unexpected response code", 403, code);

    // request authenticated user details
    connection = getRestTestHelper().openManagementConnection("/rest/sasl", "GET");
    applyCookiesToConnection(cookies, connection);
    Map<String, Object> response2 = getRestTestHelper().readJsonResponseAsMap(connection);
    assertNull("Unexpected user", response2.get("user"));
}

From source file:org.apache.qpid.systest.rest.SaslRestTest.java

public void testCramMD5SaslAuthenticationForNonExistingUser() throws Exception {
    startBrokerNow();/*from w w  w  . ja va  2s.co  m*/

    // request the challenge for CRAM-MD5
    HttpURLConnection connection = requestSasServerChallenge("CRAM-MD5");
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");

    // authenticate user with correct credentials
    int code = authenticateUser(connection, "nonexisting", "admin", "CRAM-MD5");
    assertEquals("Unexpected response code", 403, code);

    // request authenticated user details
    connection = getRestTestHelper().openManagementConnection("/rest/sasl", "GET");
    applyCookiesToConnection(cookies, connection);
    Map<String, Object> response2 = getRestTestHelper().readJsonResponseAsMap(connection);
    assertNull("Unexpected user", response2.get("user"));
}

From source file:org.apache.qpid.systest.rest.SaslRestTest.java

public void testCramMD5HexSaslAuthenticationForIncorrectPassword() throws Exception {
    configureBase64MD5FilePrincipalDatabase();
    startBrokerNow();//  w ww .j a  va  2 s .c o m

    HttpURLConnection connection = requestSasServerChallenge("CRAM-MD5-HEX");
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");

    // try to authenticate user with incorrect passowrd
    int code = authenticateUser(connection, "admin", "incorrect", "CRAM-MD5-HEX");
    assertEquals("Unexpected response code", 403, code);

    // request authenticated user details
    connection = getRestTestHelper().openManagementConnection("/rest/sasl", "GET");
    applyCookiesToConnection(cookies, connection);
    Map<String, Object> response2 = getRestTestHelper().readJsonResponseAsMap(connection);
    assertNull("Unexpected user", response2.get("user"));
}

From source file:org.apache.qpid.systest.rest.SaslRestTest.java

public void testCramMD5HexSaslAuthenticationForNonExistingUser() throws Exception {
    configureBase64MD5FilePrincipalDatabase();
    startBrokerNow();//from   w  w  w  .ja  v  a 2 s  . c om

    HttpURLConnection connection = requestSasServerChallenge("CRAM-MD5-HEX");
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");

    // try to authenticate non-existing user
    int code = authenticateUser(connection, "nonexisting", "admin", "CRAM-MD5-HEX");
    assertEquals("Unexpected response code", 403, code);

    // request authenticated user details
    connection = getRestTestHelper().openManagementConnection("/rest/sasl", "GET");
    applyCookiesToConnection(cookies, connection);
    Map<String, Object> response2 = getRestTestHelper().readJsonResponseAsMap(connection);
    assertNull("Unexpected user", response2.get("user"));
}

From source file:org.apache.qpid.systest.rest.SaslRestTest.java

public void testCramMD5HexSaslAuthenticationForValidCredentials() throws Exception {
    configureBase64MD5FilePrincipalDatabase();
    startBrokerNow();/*  w  w w. j  av a2  s.  c  o  m*/

    // request the challenge for CRAM-MD5-HEX
    HttpURLConnection connection = requestSasServerChallenge("CRAM-MD5-HEX");
    List<String> cookies = connection.getHeaderFields().get("Set-Cookie");

    // authenticate user with correct credentials
    int code = authenticateUser(connection, "admin", "admin", "CRAM-MD5-HEX");
    assertEquals("Unexpected response code", 200, code);

    // request authenticated user details
    connection = getRestTestHelper().openManagementConnection("/rest/sasl", "GET");
    applyCookiesToConnection(cookies, connection);
    Map<String, Object> response2 = getRestTestHelper().readJsonResponseAsMap(connection);
    assertEquals("Unexpected user", "admin", response2.get("user"));
}

From source file:fr.ybonnel.simpleweb4j.util.SimpleWebTestUtil.java

public UrlResponse doMethod(String requestMethod, String path, String body) throws Exception {
    URL url = new URL("http://localhost:" + port + path);
    HttpURLConnection connection = (HttpURLConnection) url.openConnection();
    connection.setRequestMethod(requestMethod);

    if (body != null) {
        connection.setDoOutput(true);/* w  w w  .ja v  a2 s . c  o m*/
        connection.getOutputStream().write(body.getBytes());
    }

    connection.connect();

    UrlResponse response = new UrlResponse();
    response.status = connection.getResponseCode();
    response.headers = connection.getHeaderFields();
    response.contentType = connection.getContentType();

    if (response.status >= 400) {
        if (connection.getErrorStream() != null) {
            response.body = IOUtils.toString(connection.getErrorStream());
        }
    } else {
        if (connection.getInputStream() != null) {
            response.body = IOUtils.toString(connection.getInputStream());
        }
    }
    return response;
}

From source file:com.cloudant.http.internal.interceptors.CookieInterceptor.java

private boolean storeCookiesFromResponse(HttpURLConnection connection) {

    // Store any cookies from the response in the CookieManager
    try {//from  ww  w  . ja  v  a2 s  .  com
        logger.finest("Storing cookie.");
        cookieManager.put(connection.getURL().toURI(), connection.getHeaderFields());
        return true;
    } catch (IOException e) {
        logger.log(Level.SEVERE, "Failed to read cookie response header", e);
        return false;
    } catch (URISyntaxException e) {
        logger.log(Level.SEVERE, "Failed to convert request URL to URI for cookie storage.");
        return false;
    }
}

From source file:com.joyent.manta.client.MantaClientSigningIT.java

@Test
public final void testCanCreateSignedHEADUriFromPath() throws IOException {
    if (config.isClientEncryptionEnabled()) {
        throw new SkipException("Signed URLs are not decrypted by the client");
    }//ww  w. j av  a  2 s  . c  o m

    final String name = UUID.randomUUID().toString();
    final String path = testPathPrefix + name;

    mantaClient.put(path, TEST_DATA);

    // This will throw an error if the newly inserted object isn't present
    mantaClient.head(path);

    Instant expires = Instant.now().plus(1, ChronoUnit.HOURS);
    URI uri = mantaClient.getAsSignedURI(path, "HEAD", expires);

    HttpURLConnection connection = (HttpURLConnection) uri.toURL().openConnection();

    try {
        connection.setReadTimeout(3000);
        connection.setRequestMethod("HEAD");
        connection.connect();

        Map<String, List<String>> headers = connection.getHeaderFields();

        if (connection.getResponseCode() != 200) {
            Assert.fail(IOUtils.toString(connection.getErrorStream(), Charset.defaultCharset()));
        }

        Assert.assertNotNull(headers);
        Assert.assertEquals(TEST_DATA.length(), connection.getContentLength());
    } finally {
        connection.disconnect();
    }
}

From source file:org.opentestsystem.shared.test.interactioncontext.FastInteractionResponse.java

FastInteractionResponse(FastInteractionContext interactionContext, InputStream is, HttpURLConnection connection,
        long timeout, long t_timeout) throws IOException {

    _interactionContext = interactionContext;
    _timeout = timeout;//from   w  w w .j  a  v a  2  s.c  o  m
    _t_timeout = t_timeout;

    // Get the response
    _requestUrl = connection.getURL();
    _bytes = IOUtils.toByteArray(is);
    _responseHeaders = connection.getHeaderFields();
    connection.getContentType();
    String message = connection.getResponseMessage();
    if (message != null) {
        String[] messageParts = StringUtils.split(message, null, 3);
        if (messageParts.length > 0) {
            _httpVersion = messageParts[0];
        }

        if (messageParts.length > 1) {
            try {
                _status = Integer.valueOf(messageParts[1]);
            } catch (Exception e) {
                // Do nothing: an invalid HTTP status code!!
            }
        }
        if (messageParts.length > 2) {
            _statusMessage = messageParts[2];
        }
    }
}