use of org.apache.http.entity.BufferedHttpEntity in project c-geo by just-radovan.
the class cgMapImg method getDrawable.
public void getDrawable(String url, int level) {
if (url == null || url.length() == 0) {
return;
}
if (geocode == null || geocode.length() == 0) {
return;
}
final String fileName = settings.getStorage() + geocode + "/map_" + level;
HttpClient client = null;
HttpGet getMethod = null;
HttpResponse httpResponse = null;
HttpEntity entity = null;
BufferedHttpEntity bufferedEntity = null;
boolean ok = false;
for (int i = 0; i < 3; i++) {
if (i > 0)
Log.w(cgSettings.tag, "cgMapImg.getDrawable: Failed to download data, retrying. Attempt #" + (i + 1));
try {
client = new DefaultHttpClient();
getMethod = new HttpGet(url);
httpResponse = client.execute(getMethod);
entity = httpResponse.getEntity();
// if image is to small, don't download and save, there is no map data for this zoom level
long contentSize = entity.getContentLength();
if (contentSize > 0 && contentSize <= MIN_MAP_IMAGE_BYTES) {
break;
}
bufferedEntity = new BufferedHttpEntity(entity);
if (bufferedEntity != null) {
InputStream is = (InputStream) bufferedEntity.getContent();
FileOutputStream fos = new FileOutputStream(fileName);
int fileSize = 0;
try {
byte[] buffer = new byte[4096];
int bytesRead;
while ((bytesRead = is.read(buffer)) != -1) {
fos.write(buffer, 0, bytesRead);
fileSize += bytesRead;
}
ok = true;
} catch (IOException e) {
Log.e(cgSettings.tag, "cgMapImg.getDrawable (saving to cache): " + e.toString());
} finally {
is.close();
fos.flush();
fos.close();
}
bufferedEntity = null;
// delete image if it has no contents
if (ok && fileSize < MIN_MAP_IMAGE_BYTES) {
(new File(fileName)).delete();
}
}
if (ok == true) {
break;
}
} catch (Exception e) {
Log.e(cgSettings.tag, "cgMapImg.getDrawable (downloading from web): " + e.toString());
}
}
}
use of org.apache.http.entity.BufferedHttpEntity in project c-geo by just-radovan.
the class cgAddressImg method getDrawable.
public static BitmapDrawable getDrawable(String url) {
Bitmap imagePre = null;
if (url == null || url.length() == 0)
return null;
HttpClient client = null;
HttpGet getMethod = null;
HttpResponse httpResponse = null;
HttpEntity entity = null;
BufferedHttpEntity bufferedEntity = null;
for (int i = 0; i < 2; i++) {
if (i > 0)
Log.w(cgSettings.tag, "cgAddressImg.getDrawable: Failed to download data, retrying. Attempt #" + (i + 1));
try {
client = new DefaultHttpClient();
getMethod = new HttpGet(url);
httpResponse = client.execute(getMethod);
entity = httpResponse.getEntity();
bufferedEntity = new BufferedHttpEntity(entity);
Log.i(cgSettings.tag, "[" + entity.getContentLength() + "B] Downloading address map " + url);
if (bufferedEntity != null)
imagePre = BitmapFactory.decodeStream(bufferedEntity.getContent(), null, null);
if (imagePre != null)
break;
} catch (Exception e) {
Log.e(cgSettings.tag, "cgAddressImg.getDrawable (downloading from web): " + e.toString());
}
}
if (imagePre == null) {
Log.d(cgSettings.tag, "cgAddressImg.getDrawable: Failed to obtain image");
return null;
}
final BitmapDrawable image = new BitmapDrawable(imagePre);
image.setBounds(new Rect(0, 0, imagePre.getWidth(), imagePre.getHeight()));
// imagePre.recycle();
imagePre = null;
return image;
}
use of org.apache.http.entity.BufferedHttpEntity in project janusgraph by JanusGraph.
the class SolrIndex method configureSolrClientsForKerberos.
private void configureSolrClientsForKerberos() throws PermanentBackendException {
String kerberosConfig = System.getProperty("java.security.auth.login.config");
if (kerberosConfig == null) {
throw new PermanentBackendException("Unable to configure kerberos for solr client. System property 'java.security.auth.login.config' is not set.");
}
logger.debug("Using kerberos configuration file located at '{}'.", kerberosConfig);
try (Krb5HttpClientBuilder krbBuild = new Krb5HttpClientBuilder()) {
SolrHttpClientBuilder kb = krbBuild.getBuilder();
HttpClientUtil.setHttpClientBuilder(kb);
HttpRequestInterceptor bufferedEntityInterceptor = (request, context) -> {
if (request instanceof HttpEntityEnclosingRequest) {
HttpEntityEnclosingRequest enclosingRequest = ((HttpEntityEnclosingRequest) request);
HttpEntity requestEntity = enclosingRequest.getEntity();
enclosingRequest.setEntity(new BufferedHttpEntity(requestEntity));
}
};
HttpClientUtil.addRequestInterceptor(bufferedEntityInterceptor);
HttpRequestInterceptor preemptiveAuth = new PreemptiveAuth(new KerberosScheme());
HttpClientUtil.addRequestInterceptor(preemptiveAuth);
}
}
use of org.apache.http.entity.BufferedHttpEntity in project azure-tools-for-java by Microsoft.
the class ADLSGen2FSOperation method appendData.
private Observable<Long> appendData(String filePath, File src) {
try {
InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(src), -1, ContentType.APPLICATION_OCTET_STREAM);
BufferedHttpEntity entity = new BufferedHttpEntity(reqEntity);
long len = entity.getContentLength();
HttpPatch req = new HttpPatch(filePath);
http.setContentType("application/octet-stream");
return http.executeReqAndCheckStatus(req, entity, this.appendReqParams, Collections.emptyList(), 202).map(ignore -> len);
} catch (FileNotFoundException e) {
throw new RuntimeException(new IllegalArgumentException("Can not find the aritifact"));
} catch (IOException e) {
throw new RuntimeException(new IllegalArgumentException("Can not read the aritfact"));
}
}
use of org.apache.http.entity.BufferedHttpEntity in project azure-tools-for-java by Microsoft.
the class JobUtils method deployArtifact.
public static Observable<String> deployArtifact(@NotNull SparkBatchSubmission submission, @NotNull String destinationRootPath, @NotNull String artifactPath) {
return Observable.fromCallable(() -> {
final File file = new File(artifactPath);
final String webHdfsUploadPath = destinationRootPath + file.getName();
final String redirectUri;
List<NameValuePair> params = new WebHdfsParamsBuilder("CREATE").setOverwrite("true").build();
URIBuilder uriBuilder = new URIBuilder(webHdfsUploadPath);
uriBuilder.addParameters(params);
HttpUriRequest req = RequestBuilder.put(uriBuilder.build()).build();
final CloseableHttpClient httpclient = submission.getHttpClient();
try (final CloseableHttpResponse response = httpclient.execute(req)) {
// two steps to upload via webhdfs
// 1.put request the get 307 redirect uri from response
// 2.put redirect request with file content as setEntity
redirectUri = response.getFirstHeader("Location").getValue();
if (StringUtils.isBlank(redirectUri)) {
throw new UnknownServiceException("can not get valid redirect uri using webhdfs");
}
} catch (final Exception ex) {
throw new UnknownServiceException("using webhdfs encounter problem:" + ex.toString());
}
final InputStreamEntity reqEntity = new InputStreamEntity(new FileInputStream(file), -1, ContentType.APPLICATION_OCTET_STREAM);
reqEntity.setChunked(true);
final BufferedHttpEntity reqEntityBuf = new BufferedHttpEntity(reqEntity);
// setup url with redirect url and entity ,config 100 continue to header
req = RequestBuilder.put(redirectUri).setEntity(reqEntityBuf).setConfig(RequestConfig.custom().setExpectContinueEnabled(true).build()).build();
// execute put request
try (final CloseableHttpResponse putResp = httpclient.execute(req)) {
params = new WebHdfsParamsBuilder("OPEN").build();
uriBuilder = new URIBuilder(webHdfsUploadPath);
uriBuilder.addParameters(params);
// return get file uri
return uriBuilder.build().toString();
}
});
}
Aggregations