Search in sources :

Example 46 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project stanbol by apache.

the class RestfulLangidentEngine method activate.

/**
     * Activate and read the properties. Configures and initialises a POSTagger for each language configured in
     * CONFIG_LANGUAGES.
     *
     * @param ce the {@link org.osgi.service.component.ComponentContext}
     */
@Activate
protected void activate(ComponentContext ce) throws ConfigurationException, IOException {
    super.activate(ce);
    log.info("activate {} '{}'", getClass().getSimpleName(), getName());
    @SuppressWarnings("unchecked") Dictionary<String, Object> properties = ce.getProperties();
    Object value = properties.get(ANALYSIS_SERVICE_URL);
    if (value == null) {
        throw new ConfigurationException(ANALYSIS_SERVICE_URL, "The RESTful Language Identification Service URL is missing in the provided configuration!");
    } else {
        try {
            serviceUrl = new URI(value.toString());
            log.info("  ... service: {}", serviceUrl);
        } catch (URISyntaxException e) {
            throw new ConfigurationException(ANALYSIS_SERVICE_URL, "The parsed RESTful Language Identification Service URL '" + value + "'is not a valid URL!", e);
        }
    }
    String usr;
    String pwd;
    value = properties.get(ANALYSIS_SERVICE_USER);
    if (value != null && !value.toString().isEmpty()) {
        usr = value.toString();
        value = properties.get(ANALYSIS_SERVICE_PWD);
        pwd = value == null ? null : value.toString();
    } else {
        // no user set
        usr = null;
        pwd = null;
    }
    //init the http client
    httpParams = new BasicHttpParams();
    httpParams.setParameter(CoreProtocolPNames.USER_AGENT, "Apache Stanbol RESTful Language Identification Engine");
    httpParams.setBooleanParameter(ClientPNames.HANDLE_REDIRECTS, true);
    httpParams.setIntParameter(ClientPNames.MAX_REDIRECTS, 3);
    httpParams.setBooleanParameter(CoreConnectionPNames.SO_KEEPALIVE, true);
    connectionManager = new PoolingClientConnectionManager();
    connectionManager.setMaxTotal(20);
    connectionManager.setDefaultMaxPerRoute(20);
    httpClient = new DefaultHttpClient(connectionManager, httpParams);
    if (usr != null) {
        log.info("  ... setting user to {}", usr);
        httpClient.getCredentialsProvider().setCredentials(AuthScope.ANY, new UsernamePasswordCredentials(usr, pwd));
        // And add request interceptor to have preemptive authentication
        httpClient.addRequestInterceptor(new PreemptiveAuthInterceptor(), 0);
    }
}
Also used : PoolingClientConnectionManager(org.apache.http.impl.conn.PoolingClientConnectionManager) ConfigurationException(org.osgi.service.cm.ConfigurationException) URISyntaxException(java.net.URISyntaxException) BasicHttpParams(org.apache.http.params.BasicHttpParams) URI(java.net.URI) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) UsernamePasswordCredentials(org.apache.http.auth.UsernamePasswordCredentials) Activate(org.apache.felix.scr.annotations.Activate)

Example 47 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project dhis2-core by dhis2.

the class HttpUtils method httpPOST.

/**
     * <pre>
     * <b>Description : </b>
     * Method to make an http POST call to a given URL with/without authentication.
     *
     * @param requestURL
     * @param body
     * @param authorize
     * @param username
     * @param password
     * @param contentType
     * @param timeout
     * @return </pre>
     */
public static DhisHttpResponse httpPOST(String requestURL, Object body, boolean authorize, String username, String password, String contentType, int timeout) throws Exception {
    DefaultHttpClient httpclient = null;
    HttpParams params = new BasicHttpParams();
    List<NameValuePair> pairs = new ArrayList<NameValuePair>();
    DhisHttpResponse dhisHttpResponse = null;
    try {
        HttpConnectionParams.setConnectionTimeout(params, timeout);
        HttpConnectionParams.setSoTimeout(params, timeout);
        httpclient = new DefaultHttpClient(params);
        HttpPost httpPost = new HttpPost(requestURL);
        if (body instanceof Map) {
            @SuppressWarnings("unchecked") Map<String, String> parameters = (Map<String, String>) body;
            for (Map.Entry<String, String> parameter : parameters.entrySet()) {
                if (parameter.getValue() != null) {
                    pairs.add(new BasicNameValuePair(parameter.getKey(), parameter.getValue()));
                }
            }
            httpPost.setEntity(new UrlEncodedFormEntity(pairs, "UTF-8"));
        } else if (body instanceof String) {
            httpPost.setEntity(new StringEntity((String) body));
        }
        if (!StringUtils.isNotEmpty(contentType))
            httpPost.setHeader("Content-Type", contentType);
        if (authorize) {
            httpPost.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
        }
        HttpResponse response = httpclient.execute(httpPost);
        log.info("Successfully got response from http POST.");
        dhisHttpResponse = processResponse(requestURL, username, response);
    } catch (Exception e) {
        log.error("Exception occurred in httpPOST call with username " + username, e);
        throw e;
    } finally {
        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }
    }
    return dhisHttpResponse;
}
Also used : BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) HttpPost(org.apache.http.client.methods.HttpPost) ArrayList(java.util.ArrayList) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException) StringEntity(org.apache.http.entity.StringEntity) BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) BasicHttpParams(org.apache.http.params.BasicHttpParams) Map(java.util.Map)

Example 48 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project dhis2-core by dhis2.

the class HttpUtils method httpGET.

/**
     * <pre>
     * <b>Description : </b>
     * Method to make an http GET call to a given URL with/without authentication.
     *
     * @param requestURL
     * @param authorize
     * @param username
     * @param password
     * @param headers
     * @param timeout
     * @return
     * @throws Exception </pre>
     */
public static DhisHttpResponse httpGET(String requestURL, boolean authorize, String username, String password, Map<String, String> headers, int timeout, boolean processResponse) throws Exception {
    DefaultHttpClient httpclient = null;
    DhisHttpResponse dhisHttpResponse = null;
    HttpParams params = new BasicHttpParams();
    try {
        HttpConnectionParams.setConnectionTimeout(params, timeout);
        HttpConnectionParams.setSoTimeout(params, timeout);
        httpclient = new DefaultHttpClient(params);
        HttpGet httpGet = new HttpGet(requestURL);
        if (headers instanceof Map) {
            for (Map.Entry<String, String> e : headers.entrySet()) {
                httpGet.addHeader(e.getKey(), e.getValue());
            }
        }
        if (authorize) {
            httpGet.setHeader("Authorization", CodecUtils.getBasicAuthString(username, password));
        }
        HttpResponse response = httpclient.execute(httpGet);
        if (processResponse) {
            dhisHttpResponse = processResponse(requestURL, username, response);
        } else {
            dhisHttpResponse = new DhisHttpResponse(response, null, response.getStatusLine().getStatusCode());
        }
    } catch (Exception e) {
        log.error("Exception occurred in the httpGET call with username " + username, e);
        throw e;
    } finally {
        if (httpclient != null) {
            httpclient.getConnectionManager().shutdown();
        }
    }
    return dhisHttpResponse;
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) Map(java.util.Map) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) IOException(java.io.IOException)

Example 49 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project 360-Engine-for-Android by 360.

the class HttpConnectionThread method setHttpClient.

/**
 * Sets HTTP settings.
 */
public void setHttpClient() {
    int connectionTimeout = Settings.HTTP_CONNECTION_TIMEOUT;
    HttpParams myHttpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(myHttpParams, connectionTimeout);
    HttpConnectionParams.setSoTimeout(myHttpParams, connectionTimeout);
    // get http
    mHttpClient = new DefaultHttpClient(myHttpParams);
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 50 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project developNote by cheng2016.

the class HttpUtil method getNewHttpClient.

private static HttpClient getNewHttpClient() {
    try {
        KeyStore trustStore = KeyStore.getInstance(KeyStore.getDefaultType());
        trustStore.load(null, null);
        SSLSocketFactory sf = new SSLSocketFactoryEx(trustStore);
        sf.setHostnameVerifier(SSLSocketFactory.ALLOW_ALL_HOSTNAME_VERIFIER);
        HttpParams params = new BasicHttpParams();
        HttpProtocolParams.setVersion(params, HttpVersion.HTTP_1_1);
        HttpProtocolParams.setContentCharset(params, HTTP.UTF_8);
        SchemeRegistry registry = new SchemeRegistry();
        registry.register(new Scheme("http", PlainSocketFactory.getSocketFactory(), 80));
        registry.register(new Scheme("https", sf, 443));
        ClientConnectionManager ccm = new ThreadSafeClientConnManager(params, registry);
        return new DefaultHttpClient(ccm, params);
    } catch (Exception e) {
        return new DefaultHttpClient();
    }
}
Also used : BasicHttpParams(org.apache.http.params.BasicHttpParams) HttpParams(org.apache.http.params.HttpParams) Scheme(org.apache.http.conn.scheme.Scheme) ThreadSafeClientConnManager(org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager) SchemeRegistry(org.apache.http.conn.scheme.SchemeRegistry) SSLSocketFactory(org.apache.http.conn.ssl.SSLSocketFactory) BasicHttpParams(org.apache.http.params.BasicHttpParams) KeyStore(java.security.KeyStore) ClientConnectionManager(org.apache.http.conn.ClientConnectionManager) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) KeyStoreException(java.security.KeyStoreException) UnrecoverableKeyException(java.security.UnrecoverableKeyException) IOException(java.io.IOException) KeyManagementException(java.security.KeyManagementException) UnknownHostException(java.net.UnknownHostException) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException)

Aggregations

BasicHttpParams (org.apache.http.params.BasicHttpParams)82 HttpParams (org.apache.http.params.HttpParams)62 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)47 IOException (java.io.IOException)33 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)31 Scheme (org.apache.http.conn.scheme.Scheme)30 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)29 HttpResponse (org.apache.http.HttpResponse)27 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)24 ClientProtocolException (org.apache.http.client.ClientProtocolException)15 HttpClient (org.apache.http.client.HttpClient)12 HttpGet (org.apache.http.client.methods.HttpGet)12 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)12 Socket (java.net.Socket)11 HttpPost (org.apache.http.client.methods.HttpPost)10 ConnectException (java.net.ConnectException)8 KeyManagementException (java.security.KeyManagementException)8 NoSuchAlgorithmException (java.security.NoSuchAlgorithmException)8 SSLContext (javax.net.ssl.SSLContext)8 StringEntity (org.apache.http.entity.StringEntity)8