use of org.apache.http.client.HttpClient in project jena by apache.
the class QueryEngineHTTP method applyServiceConfig.
/**
* <p>
* Helper method which applies configuration from the Context to the query
* engine if a service context exists for the given URI
* </p>
* <p>
* Based off proposed patch for JENA-405 but modified to apply all relevant
* configuration, this is in part also based off of the private
* {@code configureQuery()} method of the {@link Service} class though it
* omits parameter merging since that will be done automatically whenever
* the {@link QueryEngineHTTP} instance makes a query for remote submission.
* </p>
*
* @param serviceURI
* Service URI
*/
private static void applyServiceConfig(String serviceURI, QueryEngineHTTP engine) {
if (engine.context == null)
return;
@SuppressWarnings("unchecked") Map<String, Context> serviceContextMap = (Map<String, Context>) engine.context.get(Service.serviceContext);
if (serviceContextMap != null && serviceContextMap.containsKey(serviceURI)) {
Context serviceContext = serviceContextMap.get(serviceURI);
if (log.isDebugEnabled())
log.debug("Endpoint URI {} has SERVICE Context: {} ", serviceURI, serviceContext);
// Apply behavioral options
engine.setAllowCompression(serviceContext.isTrueOrUndef(Service.queryCompression));
applyServiceTimeouts(engine, serviceContext);
// Apply context-supplied client settings
HttpClient client = serviceContext.get(Service.queryClient);
if (client != null) {
if (log.isDebugEnabled())
log.debug("Using context-supplied HTTP client for endpoint URI {}", serviceURI);
engine.setClient(client);
}
}
}
use of org.apache.http.client.HttpClient in project jena by apache.
the class Service method configureQuery.
/**
* Create and configure the HttpQuery object.
*
* The parentContext is not modified but is used to create a new context
* copy.
*
* @param uri
* The uri of the endpoint
* @param parentContext
* The initial context.
* @param Query
* the Query to execute.
* @return An HttpQuery configured as per the context.
*/
private static HttpQuery configureQuery(String uri, Context parentContext, Query query) {
HttpQuery httpQuery = new HttpQuery(uri);
Context context = new Context(parentContext);
// add the context settings from the service context
@SuppressWarnings("unchecked") Map<String, Context> serviceContextMap = (Map<String, Context>) context.get(serviceContext);
if (serviceContextMap != null) {
Context serviceContext = serviceContextMap.get(uri);
if (serviceContext != null)
context.putAll(serviceContext);
}
// configure the query object.
httpQuery.merge(QueryEngineHTTP.getServiceParams(uri, context));
httpQuery.addParam(HttpParams.pQuery, query.toString());
httpQuery.setAllowCompression(context.isTrueOrUndef(queryCompression));
HttpClient client = context.get(queryClient);
if (client != null)
httpQuery.setClient(client);
setAnyTimeouts(httpQuery, context);
return httpQuery;
}
use of org.apache.http.client.HttpClient in project asterixdb by apache.
the class NCServiceIT method getHttp.
private static String getHttp(String url) throws Exception {
HttpClient client = HttpClients.createDefault();
HttpGet get = new HttpGet(url);
int statusCode;
final HttpResponse httpResponse;
try {
httpResponse = client.execute(get);
statusCode = httpResponse.getStatusLine().getStatusCode();
} catch (Exception e) {
e.printStackTrace();
throw e;
}
String response = EntityUtils.toString(httpResponse.getEntity());
if (statusCode == HttpStatus.SC_OK) {
return response;
} else {
throw new Exception("HTTP error " + statusCode + ":\n" + response);
}
}
use of org.apache.http.client.HttpClient in project Android-Error-Reporter by tomquist.
the class ExceptionReportService method sendReport.
private void sendReport(Intent intent) throws UnsupportedEncodingException, NameNotFoundException {
Log.v(TAG, "Got request to report error: " + intent.toString());
Uri server = getTargetUrl();
boolean isManualReport = intent.getBooleanExtra(EXTRA_MANUAL_REPORT, false);
boolean isReportOnFroyo = isReportOnFroyo();
boolean isFroyoOrAbove = isFroyoOrAbove();
if (isFroyoOrAbove && !isManualReport && !isReportOnFroyo) {
// We don't send automatic reports on froyo or above
Log.d(TAG, "Don't send automatic report on froyo");
return;
}
Set<String> fieldsToSend = getFieldsToSend();
String stacktrace = intent.getStringExtra(EXTRA_STACK_TRACE);
String exception = intent.getStringExtra(EXTRA_EXCEPTION_CLASS);
String message = intent.getStringExtra(EXTRA_MESSAGE);
long availableMemory = intent.getLongExtra(EXTRA_AVAILABLE_MEMORY, -1l);
long totalMemory = intent.getLongExtra(EXTRA_TOTAL_MEMORY, -1l);
String dateTime = intent.getStringExtra(EXTRA_EXCEPTION_TIME);
String threadName = intent.getStringExtra(EXTRA_THREAD_NAME);
String extraMessage = intent.getStringExtra(EXTRA_EXTRA_MESSAGE);
List<NameValuePair> params = new ArrayList<NameValuePair>();
addNameValuePair(params, fieldsToSend, "exStackTrace", stacktrace);
addNameValuePair(params, fieldsToSend, "exClass", exception);
addNameValuePair(params, fieldsToSend, "exDateTime", dateTime);
addNameValuePair(params, fieldsToSend, "exMessage", message);
addNameValuePair(params, fieldsToSend, "exThreadName", threadName);
if (extraMessage != null)
addNameValuePair(params, fieldsToSend, "extraMessage", extraMessage);
if (availableMemory >= 0)
addNameValuePair(params, fieldsToSend, "devAvailableMemory", availableMemory + "");
if (totalMemory >= 0)
addNameValuePair(params, fieldsToSend, "devTotalMemory", totalMemory + "");
PackageManager pm = getPackageManager();
try {
PackageInfo packageInfo = pm.getPackageInfo(getPackageName(), 0);
addNameValuePair(params, fieldsToSend, "appVersionCode", packageInfo.versionCode + "");
addNameValuePair(params, fieldsToSend, "appVersionName", packageInfo.versionName);
addNameValuePair(params, fieldsToSend, "appPackageName", packageInfo.packageName);
} catch (NameNotFoundException e) {
}
addNameValuePair(params, fieldsToSend, "devModel", android.os.Build.MODEL);
addNameValuePair(params, fieldsToSend, "devSdk", android.os.Build.VERSION.SDK);
addNameValuePair(params, fieldsToSend, "devReleaseVersion", android.os.Build.VERSION.RELEASE);
HttpClient httpClient = new DefaultHttpClient();
HttpPost post = new HttpPost(server.toString());
post.setEntity(new UrlEncodedFormEntity(params, HTTP.UTF_8));
Log.d(TAG, "Created post request");
try {
httpClient.execute(post);
Log.v(TAG, "Reported error: " + intent.toString());
} catch (ClientProtocolException e) {
// Ignore this kind of error
Log.e(TAG, "Error while sending an error report", e);
} catch (SSLException e) {
Log.e(TAG, "Error while sending an error report", e);
} catch (IOException e) {
if (e instanceof SocketException && e.getMessage().contains("Permission denied")) {
Log.e(TAG, "You don't have internet permission", e);
} else {
int maximumRetryCount = getMaximumRetryCount();
int maximumExponent = getMaximumBackoffExponent();
// Retry at a later point in time
AlarmManager alarmMgr = (AlarmManager) getSystemService(ALARM_SERVICE);
int exponent = intent.getIntExtra(EXTRA_CURRENT_RETRY_COUNT, 0);
intent.putExtra(EXTRA_CURRENT_RETRY_COUNT, exponent + 1);
PendingIntent operation = PendingIntent.getService(this, 0, intent, PendingIntent.FLAG_CANCEL_CURRENT);
if (exponent >= maximumRetryCount) {
// Discard error
Log.w(TAG, "Error report reached the maximum retry count and will be discarded.\nStacktrace:\n" + stacktrace);
return;
}
if (exponent > maximumExponent) {
exponent = maximumExponent;
}
// backoff in ms
long backoff = (1 << exponent) * 1000;
alarmMgr.set(AlarmManager.ELAPSED_REALTIME, SystemClock.elapsedRealtime() + backoff, operation);
}
}
}
use of org.apache.http.client.HttpClient in project spring-boot by spring-projects.
the class EndpointWebMvcAutoConfigurationTests method assertContent.
private void assertContent(String scheme, String url, int port, Object expected) throws Exception {
SSLConnectionSocketFactory socketFactory = new SSLConnectionSocketFactory(new SSLContextBuilder().loadTrustMaterial(null, new TrustSelfSignedStrategy()).build());
HttpClient httpClient = HttpClients.custom().setSSLSocketFactory(socketFactory).build();
HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient);
ClientHttpRequest request = requestFactory.createRequest(new URI(scheme + "://localhost:" + port + url), HttpMethod.GET);
try {
ClientHttpResponse response = request.execute();
if (HttpStatus.NOT_FOUND.equals(response.getStatusCode())) {
throw new FileNotFoundException();
}
try {
String actual = StreamUtils.copyToString(response.getBody(), Charset.forName("UTF-8"));
if (expected instanceof Matcher) {
assertThat(actual).is(Matched.by((Matcher<?>) expected));
} else {
assertThat(actual).isEqualTo(expected);
}
} finally {
response.close();
}
} catch (Exception ex) {
if (expected == null) {
if (SocketException.class.isInstance(ex) || FileNotFoundException.class.isInstance(ex)) {
return;
}
}
throw ex;
}
}
Aggregations