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);
}
}
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;
}
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;
}
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);
}
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();
}
}
Aggregations