Search in sources :

Example 6 with BasicHeader

use of org.apache.http.message.BasicHeader in project SmartAndroidSource by jaychou2012.

the class HurlStack method performRequest.

@Override
public HttpResponse performRequest(Request<?> request, Map<String, String> additionalHeaders) throws IOException, AuthFailureError {
    String url = request.getUrl();
    HashMap<String, String> map = new HashMap<String, String>();
    map.putAll(request.getHeaders());
    map.putAll(additionalHeaders);
    if (mUrlRewriter != null) {
        String rewritten = mUrlRewriter.rewriteUrl(url);
        if (rewritten == null) {
            throw new IOException("URL blocked by rewriter: " + url);
        }
        url = rewritten;
    }
    URL parsedUrl = new URL(url);
    HttpURLConnection connection = openConnection(parsedUrl, request);
    for (String headerName : map.keySet()) {
        connection.addRequestProperty(headerName, map.get(headerName));
    }
    setConnectionParametersForRequest(connection, request);
    // Initialize HttpResponse with data from the HttpURLConnection.
    ProtocolVersion protocolVersion = new ProtocolVersion("HTTP", 1, 1);
    int responseCode = connection.getResponseCode();
    if (responseCode == -1) {
        // Signal to the caller that something was wrong with the connection.
        throw new IOException("Could not retrieve response code from HttpUrlConnection.");
    }
    StatusLine responseStatus = new BasicStatusLine(protocolVersion, connection.getResponseCode(), connection.getResponseMessage());
    BasicHttpResponse response = new BasicHttpResponse(responseStatus);
    response.setEntity(entityFromConnection(connection));
    for (Entry<String, List<String>> header : connection.getHeaderFields().entrySet()) {
        if (header.getKey() != null) {
            Header h = new BasicHeader(header.getKey(), header.getValue().get(0));
            response.addHeader(h);
        }
    }
    return response;
}
Also used : HashMap(java.util.HashMap) IOException(java.io.IOException) ProtocolVersion(org.apache.http.ProtocolVersion) URL(java.net.URL) BasicStatusLine(org.apache.http.message.BasicStatusLine) BasicStatusLine(org.apache.http.message.BasicStatusLine) StatusLine(org.apache.http.StatusLine) BasicHttpResponse(org.apache.http.message.BasicHttpResponse) HttpURLConnection(java.net.HttpURLConnection) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) List(java.util.List) BasicHeader(org.apache.http.message.BasicHeader)

Example 7 with BasicHeader

use of org.apache.http.message.BasicHeader in project ribbon by Netflix.

the class NFHttpClient method init.

void init(IClientConfig config, boolean registerMonitor) {
    HttpParams params = getParams();
    HttpProtocolParams.setContentCharset(params, "UTF-8");
    params.setParameter(ClientPNames.CONNECTION_MANAGER_FACTORY_CLASS_NAME, ThreadSafeClientConnManager.class.getName());
    HttpClientParams.setRedirecting(params, config.getPropertyAsBoolean(CommonClientConfigKey.FollowRedirects, true));
    // set up default headers
    List<Header> defaultHeaders = new ArrayList<Header>();
    defaultHeaders.add(new BasicHeader("Netflix.NFHttpClient.Version", "1.0"));
    defaultHeaders.add(new BasicHeader("X-netflix-httpclientname", name));
    params.setParameter(ClientPNames.DEFAULT_HEADERS, defaultHeaders);
    connPoolCleaner = new ConnectionPoolCleaner(name, this.getConnectionManager(), connectionPoolCleanUpScheduler);
    this.retriesProperty = DynamicPropertyFactory.getInstance().getIntProperty(this.name + ".nfhttpclient" + ".retries", 3);
    this.sleepTimeFactorMsProperty = DynamicPropertyFactory.getInstance().getIntProperty(this.name + ".nfhttpclient" + ".sleepTimeFactorMs", 10);
    setHttpRequestRetryHandler(new NFHttpMethodRetryHandler(this.name, this.retriesProperty.get(), false, this.sleepTimeFactorMsProperty.get()));
    tracer = Monitors.newTimer(EXECUTE_TRACER + "-" + name, TimeUnit.MILLISECONDS);
    if (registerMonitor) {
        Monitors.registerObject(name, this);
    }
    maxTotalConnectionProperty = new DynamicIntProperty(this.name + "." + config.getNameSpace() + "." + CommonClientConfigKey.MaxTotalHttpConnections.key(), DefaultClientConfigImpl.DEFAULT_MAX_TOTAL_HTTP_CONNECTIONS);
    maxTotalConnectionProperty.addCallback(new Runnable() {

        @Override
        public void run() {
            ((ThreadSafeClientConnManager) getConnectionManager()).setMaxTotal(maxTotalConnectionProperty.get());
        }
    });
    maxConnectionPerHostProperty = new DynamicIntProperty(this.name + "." + config.getNameSpace() + "." + CommonClientConfigKey.MaxHttpConnectionsPerHost.key(), DefaultClientConfigImpl.DEFAULT_MAX_HTTP_CONNECTIONS_PER_HOST);
    maxConnectionPerHostProperty.addCallback(new Runnable() {

        @Override
        public void run() {
            ((ThreadSafeClientConnManager) getConnectionManager()).setDefaultMaxPerRoute(maxConnectionPerHostProperty.get());
        }
    });
}
Also used : HttpParams(org.apache.http.params.HttpParams) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) Header(org.apache.http.Header) BasicHeader(org.apache.http.message.BasicHeader) ArrayList(java.util.ArrayList) BasicHeader(org.apache.http.message.BasicHeader) DynamicIntProperty(com.netflix.config.DynamicIntProperty)

Example 8 with BasicHeader

use of org.apache.http.message.BasicHeader in project android_frameworks_base by ParanoidAndroid.

the class DefaultHttpClientTest method authenticateDigestAlgorithm.

private void authenticateDigestAlgorithm(String algorithm) throws Exception {
    String challenge = "Digest realm=\"protected area\", " + "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", " + "algorithm=" + algorithm;
    DigestScheme digestScheme = new DigestScheme();
    digestScheme.processChallenge(new BasicHeader("WWW-Authenticate", challenge));
    HttpGet get = new HttpGet();
    digestScheme.authenticate(new UsernamePasswordCredentials("username", "password"), get);
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) HttpGet(org.apache.http.client.methods.HttpGet) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 9 with BasicHeader

use of org.apache.http.message.BasicHeader in project android_frameworks_base by ResurrectionRemix.

the class DefaultHttpClientTest method authenticateDigestAlgorithm.

private void authenticateDigestAlgorithm(String algorithm) throws Exception {
    String challenge = "Digest realm=\"protected area\", " + "nonce=\"dcd98b7102dd2f0e8b11d0f600bfb0c093\", " + "algorithm=" + algorithm;
    DigestScheme digestScheme = new DigestScheme();
    digestScheme.processChallenge(new BasicHeader("WWW-Authenticate", challenge));
    HttpGet get = new HttpGet();
    digestScheme.authenticate(new UsernamePasswordCredentials("username", "password"), get);
}
Also used : DigestScheme(org.apache.http.impl.auth.DigestScheme) HttpGet(org.apache.http.client.methods.HttpGet) BasicHeader(org.apache.http.message.BasicHeader) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials)

Example 10 with BasicHeader

use of org.apache.http.message.BasicHeader in project ats-framework by Axway.

the class GGSSchemeBase method authenticate.

@Override
public Header authenticate(final Credentials credentials, final HttpRequest request, final HttpContext context) throws AuthenticationException {
    if (request == null) {
        throw new IllegalArgumentException("HTTP request may not be null");
    }
    switch(state) {
        case UNINITIATED:
            throw new AuthenticationException(getSchemeName() + " authentication has not been initiated");
        case FAILED:
            throw new AuthenticationException(getSchemeName() + " authentication has failed");
        case CHALLENGE_RECEIVED:
            try {
                token = generateToken(token);
                state = State.TOKEN_GENERATED;
            } catch (GSSException gsse) {
                state = State.FAILED;
                if (gsse.getMajor() == GSSException.DEFECTIVE_CREDENTIAL || gsse.getMajor() == GSSException.CREDENTIALS_EXPIRED)
                    throw new InvalidCredentialsException(gsse.getMessage(), gsse);
                if (gsse.getMajor() == GSSException.NO_CRED)
                    throw new InvalidCredentialsException(gsse.getMessage(), gsse);
                if (gsse.getMajor() == GSSException.DEFECTIVE_TOKEN || gsse.getMajor() == GSSException.DUPLICATE_TOKEN || gsse.getMajor() == GSSException.OLD_TOKEN)
                    throw new AuthenticationException(gsse.getMessage(), gsse);
                // other error
                throw new AuthenticationException(gsse.getMessage());
            }
        // continue to next case block
        case TOKEN_GENERATED:
            String tokenstr = new String(base64codec.encode(token));
            if (log.isDebugEnabled()) {
                log.debug("Sending response '" + tokenstr + "' back to the auth server");
            }
            return new BasicHeader("Authorization", "Negotiate " + tokenstr);
        default:
            throw new IllegalStateException("Illegal state: " + state);
    }
}
Also used : GSSException(org.ietf.jgss.GSSException) AuthenticationException(org.apache.http.auth.AuthenticationException) InvalidCredentialsException(org.apache.http.auth.InvalidCredentialsException) BasicHeader(org.apache.http.message.BasicHeader)

Aggregations

BasicHeader (org.apache.http.message.BasicHeader)233 Header (org.apache.http.Header)120 IOException (java.io.IOException)54 HttpResponse (org.apache.http.HttpResponse)50 Test (org.junit.Test)50 StringEntity (org.apache.http.entity.StringEntity)36 List (java.util.List)25 HashMap (java.util.HashMap)24 URISyntaxException (java.net.URISyntaxException)23 HttpGet (org.apache.http.client.methods.HttpGet)22 StatusLine (org.apache.http.StatusLine)20 HttpPost (org.apache.http.client.methods.HttpPost)20 BasicStatusLine (org.apache.http.message.BasicStatusLine)19 RestResponse (com.google.gerrit.acceptance.RestResponse)18 ArrayList (java.util.ArrayList)18 ProtocolVersion (org.apache.http.ProtocolVersion)18 AbstractDaemonTest (com.google.gerrit.acceptance.AbstractDaemonTest)17 File (java.io.File)17 HttpEntity (org.apache.http.HttpEntity)17 BasicHttpResponse (org.apache.http.message.BasicHttpResponse)17