use of org.apache.http.client.HttpClient in project cuba by cuba-platform.
the class FileUploadController method uploadToMiddleware.
protected void uploadToMiddleware(UserSession userSession, InputStream is, FileDescriptor fd) throws FileStorageException, InterruptedIOException {
Object context = serverSelector.initContext();
String selectedUrl = serverSelector.getUrl(context);
if (selectedUrl == null) {
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName());
}
while (true) {
String url = selectedUrl + CORE_FILE_UPLOAD_CONTEXT + "?s=" + userSession.getId() + "&f=" + fd.toUrlParam();
HttpPost method = new HttpPost(url);
InputStreamEntity entity = new InputStreamEntity(is, -1);
method.setEntity(entity);
HttpClient client = new DefaultHttpClient();
try {
HttpResponse coreResponse = client.execute(method);
int statusCode = coreResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.SC_OK) {
break;
} else {
log.debug("Unable to upload file to " + url + "\n" + coreResponse.getStatusLine());
selectedUrl = failAndGetNextUrl(context);
if (selectedUrl == null)
throw new FileStorageException(FileStorageException.Type.fromHttpStatus(statusCode), fd.getName());
}
} catch (InterruptedIOException e) {
log.trace("Uploading has been interrupted");
throw e;
} catch (IOException e) {
log.debug("Unable to upload file to " + url + "\n" + e);
selectedUrl = failAndGetNextUrl(context);
if (selectedUrl == null)
throw new FileStorageException(FileStorageException.Type.IO_EXCEPTION, fd.getName(), e);
} finally {
client.getConnectionManager().shutdown();
}
}
}
use of org.apache.http.client.HttpClient in project cuba by cuba-platform.
the class RestFileDownloadController method writeResponse.
private void writeResponse(HttpServletResponse response, UserSession userSession, FileDescriptor fd) throws IOException {
InputStream is = null;
ServletOutputStream os = response.getOutputStream();
try {
Object context = serverSelector.initContext();
String selectedUrl = serverSelector.getUrl(context);
if (selectedUrl == null) {
log.debug("Unable to download file: no available server URLs");
error(response);
}
while (selectedUrl != null) {
String url = selectedUrl + fileDownloadContext + "?s=" + userSession.getId() + "&f=" + fd.getId().toString();
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
int httpStatus = httpResponse.getStatusLine().getStatusCode();
if (httpStatus == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
is = httpEntity.getContent();
IOUtils.copy(is, os);
os.flush();
break;
} else {
log.debug("Unable to download file from " + url + "\nHttpEntity is null");
selectedUrl = failAndGetNextUrl(context, response);
}
} else {
log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine());
selectedUrl = failAndGetNextUrl(context, response);
}
} catch (IOException ex) {
log.debug("Unable to download file from " + url + "\n" + ex);
selectedUrl = failAndGetNextUrl(context, response);
} finally {
IOUtils.closeQuietly(is);
httpClient.getConnectionManager().shutdown();
}
}
} finally {
IOUtils.closeQuietly(os);
}
}
use of org.apache.http.client.HttpClient in project cuba by cuba-platform.
the class IdpAuthLifecycleManager method pingIdpSessionServer.
protected IdpSessionStatus pingIdpSessionServer(String idpSessionId) {
log.debug("Ping IDP session {}", idpSessionId);
String idpBaseURL = idpConfig.getIdpBaseURL();
if (!idpBaseURL.endsWith("/")) {
idpBaseURL += "/";
}
String idpSessionPingUrl = idpBaseURL + "service/ping";
HttpPost httpPost = new HttpPost(idpSessionPingUrl);
httpPost.setHeader(HttpHeaders.CONTENT_TYPE, ContentType.APPLICATION_FORM_URLENCODED.getMimeType());
UrlEncodedFormEntity formEntity = new UrlEncodedFormEntity(Arrays.asList(new BasicNameValuePair("idpSessionId", idpSessionId), new BasicNameValuePair("trustedServicePassword", idpConfig.getIdpTrustedServicePassword())), StandardCharsets.UTF_8);
httpPost.setEntity(formEntity);
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
HttpClient client = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
try {
HttpResponse httpResponse = client.execute(httpPost);
int statusCode = httpResponse.getStatusLine().getStatusCode();
if (statusCode == HttpStatus.GONE.value()) {
log.debug("IDP session expired {}", idpSessionId);
return IdpSessionStatus.EXPIRED;
}
if (statusCode != HttpStatus.OK.value()) {
log.warn("IDP respond status {} on session ping", statusCode);
}
} catch (IOException e) {
log.warn("Unable to ping IDP {} session {}", idpSessionPingUrl, idpSessionId, e);
} finally {
connectionManager.shutdown();
}
return IdpSessionStatus.ALIVE;
}
use of org.apache.http.client.HttpClient in project cuba by cuba-platform.
the class RestApiDataProvider method provide.
@Override
public InputStream provide() {
String url = restApiUrl + query;
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse response = httpClient.execute(httpGet);
int status = response.getStatusLine().getStatusCode();
if (status == HttpStatus.SC_OK) {
HttpEntity httpEntity = response.getEntity();
if (httpEntity != null) {
inputStream = httpEntity.getContent();
} else {
throw new RuntimeException("Unable to retrieve data using REST API from " + url + "\nHttpEntity is null");
}
} else {
throw new RuntimeException("Unable to retrieve data using REST API from " + url + "\n" + response.getStatusLine());
}
} catch (IOException e) {
throw new RuntimeException(e);
} finally {
connectionManager.shutdown();
}
return inputStream;
}
use of org.apache.http.client.HttpClient in project cuba by cuba-platform.
the class SimpleFileDataProvider method provide.
@Override
public InputStream provide() {
if (filePath == null)
throw new IllegalStateException("File path is null");
Object context = serverSelector.initContext();
String selectedUrl = serverSelector.getUrl(context);
if (selectedUrl == null) {
throw new RuntimeException(String.format("Unable to download file '%s': no available server URLs", filePath));
}
while (true) {
String url = selectedUrl + fileDownloadContext + "?s=" + userSessionSource.getUserSession().getId() + "&p=" + URLEncodeUtils.encodeUtf8(filePath);
HttpClientConnectionManager connectionManager = new BasicHttpClientConnectionManager();
HttpClient httpClient = HttpClientBuilder.create().setConnectionManager(connectionManager).build();
HttpGet httpGet = new HttpGet(url);
try {
HttpResponse httpResponse = httpClient.execute(httpGet);
int httpStatus = httpResponse.getStatusLine().getStatusCode();
if (httpStatus == HttpStatus.SC_OK) {
HttpEntity httpEntity = httpResponse.getEntity();
if (httpEntity != null) {
inputStream = httpEntity.getContent();
break;
} else {
log.debug("Unable to download file from " + url + "\nHttpEntity is null");
selectedUrl = failAndGetNextUrl(context);
if (selectedUrl == null)
throw new RuntimeException(String.format("Unable to download file '%s': HttpEntity is null", filePath));
}
} else {
log.debug("Unable to download file from " + url + "\n" + httpResponse.getStatusLine());
selectedUrl = failAndGetNextUrl(context);
if (selectedUrl == null)
throw new RuntimeException(String.format("Unable to download file '%s': HTTP status is %d", filePath, httpStatus));
}
} catch (IOException ex) {
log.debug("Unable to download file from " + url + "\n" + ex);
selectedUrl = failAndGetNextUrl(context);
if (selectedUrl == null)
throw new RuntimeException(String.format("Unable to download file '%s'", filePath), ex);
}
}
return inputStream;
}
Aggregations