Search in sources :

Example 46 with HeaderElement

use of org.apache.http.HeaderElement in project vespa by vespa-engine.

the class ApacheGatewayConnectionTest method httpResponse.

private HttpResponse httpResponse(String sessionIdInResult, String version) throws IOException {
    final HttpResponse httpResponseMock = mock(HttpResponse.class);
    StatusLine statusLineMock = mock(StatusLine.class);
    when(httpResponseMock.getStatusLine()).thenReturn(statusLineMock);
    when(statusLineMock.getStatusCode()).thenReturn(200);
    addMockedHeader(httpResponseMock, Headers.SESSION_ID, sessionIdInResult, null);
    addMockedHeader(httpResponseMock, Headers.VERSION, version, null);
    HeaderElement[] headerElements = new HeaderElement[1];
    headerElements[0] = mock(HeaderElement.class);
    final HttpEntity httpEntityMock = mock(HttpEntity.class);
    when(httpResponseMock.getEntity()).thenReturn(httpEntityMock);
    final InputStream inputs = new ByteArrayInputStream("fake response data".getBytes());
    when(httpEntityMock.getContent()).thenReturn(inputs);
    return httpResponseMock;
}
Also used : StatusLine(org.apache.http.StatusLine) HttpEntity(org.apache.http.HttpEntity) ByteArrayInputStream(java.io.ByteArrayInputStream) HeaderElement(org.apache.http.HeaderElement) ByteArrayInputStream(java.io.ByteArrayInputStream) InputStream(java.io.InputStream) HttpResponse(org.apache.http.HttpResponse)

Example 47 with HeaderElement

use of org.apache.http.HeaderElement in project opencast by opencast.

the class TrustedHttpClientImpl method getRealmAndNonce.

/**
 * Perform a request, and extract the realm and nonce values
 *
 * @param request
 *         The request to execute in order to obtain the realm and nonce
 * @return A String[] containing the {realm, nonce}
 */
protected String[] getRealmAndNonce(HttpRequestBase request) throws TrustedHttpClientException {
    HttpClient httpClient = makeHttpClient(DEFAULT_CONNECTION_TIMEOUT, DEFAULT_SOCKET_TIMEOUT);
    HttpResponse response;
    try {
        response = new HttpResponseWrapper(httpClient.execute(request));
    } catch (IOException e) {
        httpClient.getConnectionManager().shutdown();
        throw new TrustedHttpClientException(e);
    }
    Header[] headers = response.getHeaders("WWW-Authenticate");
    if (headers == null || headers.length == 0) {
        logger.warn("URI {} does not support digest authentication", request.getURI());
        httpClient.getConnectionManager().shutdown();
        return null;
    }
    Header authRequiredResponseHeader = headers[0];
    String nonce = null;
    String realm = null;
    for (HeaderElement element : authRequiredResponseHeader.getElements()) {
        if ("nonce".equals(element.getName())) {
            nonce = element.getValue();
        } else if ("Digest realm".equals(element.getName())) {
            realm = element.getValue();
        }
    }
    httpClient.getConnectionManager().shutdown();
    return new String[] { realm, nonce };
}
Also used : HttpResponseWrapper(org.opencastproject.security.util.HttpResponseWrapper) Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) TrustedHttpClient(org.opencastproject.security.api.TrustedHttpClient) HttpClient(org.opencastproject.kernel.http.api.HttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException)

Example 48 with HeaderElement

use of org.apache.http.HeaderElement in project opencast by opencast.

the class StandAloneTrustedHttpClientImpl method getRealmAndNonce.

/**
 * Perform a request, and extract the realm and nonce values
 *
 * @param request
 *         The request to execute in order to obtain the realm and nonce
 * @return A String[] containing the {realm, nonce}
 */
private String[] getRealmAndNonce(HttpRequestBase request) throws TrustedHttpClientException {
    DefaultHttpClient httpClient = new DefaultHttpClient();
    HttpResponse response;
    try {
        response = new HttpResponseWrapper(httpClient.execute(request));
    } catch (IOException e) {
        httpClient.getConnectionManager().shutdown();
        throw new TrustedHttpClientException(e);
    }
    Header[] headers = response.getHeaders("WWW-Authenticate");
    if (headers == null || headers.length == 0) {
        logger.warn("URI {} does not support digest authentication", request.getURI());
        httpClient.getConnectionManager().shutdown();
        return null;
    }
    Header authRequiredResponseHeader = headers[0];
    String nonce = null;
    String realm = null;
    for (HeaderElement element : authRequiredResponseHeader.getElements()) {
        if ("nonce".equals(element.getName())) {
            nonce = element.getValue();
        } else if ("Digest realm".equals(element.getName())) {
            realm = element.getValue();
        }
    }
    httpClient.getConnectionManager().shutdown();
    return new String[] { realm, nonce };
}
Also used : Header(org.apache.http.Header) HeaderElement(org.apache.http.HeaderElement) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) TrustedHttpClientException(org.opencastproject.security.api.TrustedHttpClientException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 49 with HeaderElement

use of org.apache.http.HeaderElement in project JFramework by gugumall.

the class MyConnectionKeepAliveStrategy method getKeepAliveDuration.

/*
	 *  (non-Javadoc)
	 * @see org.apache.http.conn.ConnectionKeepAliveStrategy#getKeepAliveDuration(org.apache.http.HttpResponse, org.apache.http.protocol.HttpContext)
	 */
public long getKeepAliveDuration(HttpResponse response, HttpContext context) {
    // Honor 'keep-alive' header
    HeaderElementIterator it = new BasicHeaderElementIterator(response.headerIterator(HTTP.CONN_KEEP_ALIVE));
    while (it.hasNext()) {
        HeaderElement he = it.nextElement();
        String param = he.getName();
        String value = he.getValue();
        if (value != null && param.equalsIgnoreCase("timeout")) {
            try {
                return Long.parseLong(value) * 1000;
            } catch (NumberFormatException ignore) {
            }
        }
    }
    return 30 * 1000;
}
Also used : BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator) HeaderElementIterator(org.apache.http.HeaderElementIterator) HeaderElement(org.apache.http.HeaderElement) BasicHeaderElementIterator(org.apache.http.message.BasicHeaderElementIterator)

Example 50 with HeaderElement

use of org.apache.http.HeaderElement in project selenium_java by sergueik.

the class RestClient method request.

private JSON request(HttpRequestBase req) throws RestException, IOException {
    req.addHeader("Accept", "application/json");
    if (creds != null)
        creds.authenticate(req);
    HttpResponse resp = httpClient.execute(req);
    HttpEntity ent = resp.getEntity();
    StringBuilder result = new StringBuilder();
    if (ent != null) {
        String encoding = null;
        if (ent.getContentEncoding() != null) {
            encoding = ent.getContentEncoding().getValue();
        }
        if (encoding == null) {
            Header contentTypeHeader = resp.getFirstHeader("Content-Type");
            HeaderElement[] contentTypeElements = contentTypeHeader.getElements();
            for (HeaderElement he : contentTypeElements) {
                NameValuePair nvp = he.getParameterByName("charset");
                if (nvp != null) {
                    encoding = nvp.getValue();
                }
            }
        }
        InputStreamReader isr = encoding != null ? new InputStreamReader(ent.getContent(), encoding) : new InputStreamReader(ent.getContent());
        BufferedReader br = new BufferedReader(isr);
        String line = "";
        while ((line = br.readLine()) != null) result.append(line);
    }
    StatusLine sl = resp.getStatusLine();
    if (sl.getStatusCode() >= 300)
        throw new RestException(sl.getReasonPhrase(), sl.getStatusCode(), result.toString());
    return result.length() > 0 ? JSONSerializer.toJSON(result.toString()) : null;
}
Also used : StatusLine(org.apache.http.StatusLine) NameValuePair(org.apache.http.NameValuePair) HttpEntity(org.apache.http.HttpEntity) Header(org.apache.http.Header) InputStreamReader(java.io.InputStreamReader) HeaderElement(org.apache.http.HeaderElement) BufferedReader(java.io.BufferedReader) HttpResponse(org.apache.http.HttpResponse)

Aggregations

HeaderElement (org.apache.http.HeaderElement)58 Header (org.apache.http.Header)17 NameValuePair (org.apache.http.NameValuePair)14 HeaderElementIterator (org.apache.http.HeaderElementIterator)10 BasicHeaderElementIterator (org.apache.http.message.BasicHeaderElementIterator)10 HttpResponse (org.apache.http.HttpResponse)9 MalformedCookieException (org.apache.http.cookie.MalformedCookieException)9 ParserCursor (org.apache.http.message.ParserCursor)9 ArrayList (java.util.ArrayList)6 IOException (java.io.IOException)5 Cookie (org.apache.http.cookie.Cookie)5 CookieAttributeHandler (org.apache.http.cookie.CookieAttributeHandler)5 HttpContext (org.apache.http.protocol.HttpContext)5 HashMap (java.util.HashMap)4 Map (java.util.Map)4 ConnectionKeepAliveStrategy (org.apache.http.conn.ConnectionKeepAliveStrategy)4 HashSet (java.util.HashSet)3 NoSuchElementException (java.util.NoSuchElementException)3 ParseException (org.apache.http.ParseException)3 ProtocolException (org.apache.http.ProtocolException)3