Search in sources :

Example 96 with HttpClient

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();
        }
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) FileStorageException(com.haulmont.cuba.core.global.FileStorageException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) InputStreamEntity(org.apache.http.entity.InputStreamEntity)

Example 97 with HttpClient

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);
    }
}
Also used : HttpEntity(org.apache.http.HttpEntity) ServletOutputStream(javax.servlet.ServletOutputStream) InputStream(java.io.InputStream) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) DefaultHttpClient(org.apache.http.impl.client.DefaultHttpClient)

Example 98 with HttpClient

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;
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) HttpClient(org.apache.http.client.HttpClient) HttpResponse(org.apache.http.HttpResponse) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) IOException(java.io.IOException) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 99 with HttpClient

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;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Example 100 with HttpClient

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;
}
Also used : HttpEntity(org.apache.http.HttpEntity) HttpClient(org.apache.http.client.HttpClient) HttpGet(org.apache.http.client.methods.HttpGet) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) HttpClientConnectionManager(org.apache.http.conn.HttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager) BasicHttpClientConnectionManager(org.apache.http.impl.conn.BasicHttpClientConnectionManager)

Aggregations

HttpClient (org.apache.http.client.HttpClient)878 HttpResponse (org.apache.http.HttpResponse)548 HttpGet (org.apache.http.client.methods.HttpGet)394 Test (org.junit.Test)273 DefaultHttpClient (org.apache.http.impl.client.DefaultHttpClient)264 IOException (java.io.IOException)258 HttpPost (org.apache.http.client.methods.HttpPost)197 HttpEntity (org.apache.http.HttpEntity)118 URI (java.net.URI)87 InputStream (java.io.InputStream)81 ArrayList (java.util.ArrayList)64 ClientProtocolException (org.apache.http.client.ClientProtocolException)61 InputStreamReader (java.io.InputStreamReader)59 StringEntity (org.apache.http.entity.StringEntity)58 HttpClientBuilder (org.apache.http.impl.client.HttpClientBuilder)58 BasicNameValuePair (org.apache.http.message.BasicNameValuePair)56 MockResponse (com.google.mockwebserver.MockResponse)48 UrlEncodedFormEntity (org.apache.http.client.entity.UrlEncodedFormEntity)48 BufferedReader (java.io.BufferedReader)46 URISyntaxException (java.net.URISyntaxException)45