Search in sources :

Example 36 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 37 with BasicHttpParams

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

the class RestfulNlpAnalysisEngine 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());
    config = ce.getProperties();
    Object value = config.get(ANALYSIS_SERVICE_URL);
    if (value == null) {
        throw new ConfigurationException(ANALYSIS_SERVICE_URL, "The RESTful Analysis Service URL is missing in the provided configuration!");
    } else {
        try {
            analysisServiceUrl = new URI(value.toString());
            log.info("  ... service: {}", analysisServiceUrl);
        } catch (URISyntaxException e) {
            throw new ConfigurationException(ANALYSIS_SERVICE_URL, "The parsed RESTful Analysis Service URL '" + value + "'is not a valid URL!", e);
        }
    }
    String usr;
    String pwd;
    value = config.get(ANALYSIS_SERVICE_USER);
    if (value != null && !value.toString().isEmpty()) {
        usr = value.toString();
        value = config.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 NLP Analysis 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);
    // NOTE: The list of supported languages is the combination of the
    // languages enabled by the configuration (#languageConfig) and the
    // languages supported by the RESTful NLP Analysis Service
    // (#supportedLanguages)
    // init the language configuration with the engine configuration
    languageConfig.setConfiguration(config);
    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);
    }
    // STANBOL-1389: deactivated initialization during activation as this can create
    // issues in cases where Stanbol and the NLP service do run in the same
    // servlet container.
    // initRESTfulNlpAnalysisService();
    value = config.get(WRITE_TEXT_ANNOTATIONS_STATE);
    if (value instanceof Boolean) {
        this.writeTextAnnotations = ((Boolean) value).booleanValue();
    } else if (value != null) {
        this.writeTextAnnotations = Boolean.parseBoolean(value.toString());
    } else {
        this.writeTextAnnotations = DEFAULT_WRITE_TEXT_ANNOTATION_STATE;
    }
}
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 38 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project ABPlayer by winkstu.

the class HttpUtil method httpGetHost.

public static String httpGetHost(String url) {
    HttpGet httpget = new HttpGet(url);
    String strResult = "";
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 5000);
    HttpConnectionParams.setSoTimeout(httpParams, 5000);
    HttpClient httpclient;
    try {
        httpclient = new DefaultHttpClient(httpParams);
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            strResult = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
        }
    } catch (ConnectException e) {
        e.printStackTrace();
        System.out.println("hosterror");
    } catch (ClientProtocolException e) {
        System.out.println("Client");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO");
        e.printStackTrace();
    }
    return strResult;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectException(java.net.ConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Example 39 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project ABPlayer by winkstu.

the class HttpUtil method HttpGetBmpInputStream.

public static InputStream HttpGetBmpInputStream(String url) {
    HttpGet httpget = new HttpGet(url);
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
    HttpConnectionParams.setSoTimeout(httpParams, 10000);
    InputStream is = null;
    try {
        HttpClient httpclient = new DefaultHttpClient(httpParams);
        HttpResponse response = httpclient.execute(httpget);
        is = response.getEntity().getContent();
    } catch (Exception e) {
        e.printStackTrace();
    }
    return is;
}
Also used : GZIPInputStream(java.util.zip.GZIPInputStream) BufferedInputStream(java.io.BufferedInputStream) ZipInputStream(java.util.zip.ZipInputStream) InputStream(java.io.InputStream) HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ProtocolException(org.apache.http.ProtocolException) ClientProtocolException(org.apache.http.client.ClientProtocolException) DataFormatException(java.util.zip.DataFormatException) ConnectException(java.net.ConnectException) IOException(java.io.IOException)

Example 40 with BasicHttpParams

use of org.apache.http.params.BasicHttpParams in project ABPlayer by winkstu.

the class HttpUtil method httpGet.

public static String httpGet(String url) {
    System.out.println("httpGet" + url);
    HttpGet httpget = new HttpGet(url);
    String strResult = null;
    BasicHttpParams httpParams = new BasicHttpParams();
    HttpConnectionParams.setConnectionTimeout(httpParams, 10000);
    HttpConnectionParams.setSoTimeout(httpParams, 10000);
    HttpClient httpclient;
    try {
        httpclient = new DefaultHttpClient(httpParams);
        httpget.setHeader("Cookie", cookieName + "=" + cookieValue);
        HttpResponse response = httpclient.execute(httpget);
        System.out.println(response.getStatusLine().getStatusCode());
        if (response.getStatusLine().getStatusCode() == 200) {
            strResult = EntityUtils.toString(response.getEntity(), HTTP.UTF_8);
            // System.out.println(strResult);
            System.out.println("getFinish");
        }
    } catch (ConnectException e) {
        e.printStackTrace();
    } catch (ClientProtocolException e) {
        System.out.println("Client");
        e.printStackTrace();
    } catch (IOException e) {
        System.out.println("IO");
        e.printStackTrace();
    }
    return strResult;
}
Also used : HttpGet(org.apache.http.client.methods.HttpGet) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) BasicHttpParams(org.apache.http.params.BasicHttpParams) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) ConnectException(java.net.ConnectException) ClientProtocolException(org.apache.http.client.ClientProtocolException)

Aggregations

BasicHttpParams (org.apache.http.params.BasicHttpParams)105 HttpParams (org.apache.http.params.HttpParams)75 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)50 IOException (java.io.IOException)41 HttpResponse (org.apache.http.HttpResponse)33 SchemeRegistry (org.apache.http.conn.scheme.SchemeRegistry)32 Scheme (org.apache.http.conn.scheme.Scheme)31 ThreadSafeClientConnManager (org.apache.http.impl.conn.tsccm.ThreadSafeClientConnManager)30 ClientConnectionManager (org.apache.http.conn.ClientConnectionManager)25 ClientProtocolException (org.apache.http.client.ClientProtocolException)16 HttpGet (org.apache.http.client.methods.HttpGet)16 HttpClient (org.apache.http.client.HttpClient)15 Test (org.junit.Test)15 HttpPost (org.apache.http.client.methods.HttpPost)14 SSLSocketFactory (org.apache.http.conn.ssl.SSLSocketFactory)14 Socket (java.net.Socket)11 URI (java.net.URI)11 StringEntity (org.apache.http.entity.StringEntity)11 TestHttpClient (io.undertow.testutils.TestHttpClient)10 KeyManagementException (java.security.KeyManagementException)9