Search in sources :

Example 1 with PreemptiveHttpClient

use of org.jfrog.build.client.PreemptiveHttpClient in project build-info by JFrogDev.

the class ArtifactoryDependenciesClient method searchArtifactsByAql.

public AqlSearchResult searchArtifactsByAql(String aql) throws IOException {
    PreemptiveHttpClient client = httpClient.getHttpClient();
    String url = artifactoryUrl + "/api/search/aql";
    HttpPost httpPost = new HttpPost(url);
    StringEntity entity = new StringEntity(aql);
    httpPost.setEntity(entity);
    AqlSearchResult result = readResponse(client.execute(httpPost), new TypeReference<AqlSearchResult>() {
    }, "Failed to search artifact by the aql '" + aql + "'", true);
    return result;
}
Also used : PreemptiveHttpClient(org.jfrog.build.client.PreemptiveHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) StringEntity(org.apache.http.entity.StringEntity) AqlSearchResult(org.jfrog.build.api.search.AqlSearchResult)

Example 2 with PreemptiveHttpClient

use of org.jfrog.build.client.PreemptiveHttpClient in project build-info by JFrogDev.

the class ArtifactoryDependenciesClient method searchArtifactsByProperties.

public PropertySearchResult searchArtifactsByProperties(String properties) throws IOException {
    PreemptiveHttpClient client = httpClient.getHttpClient();
    String replacedProperties = StringUtils.replaceEach(properties, new String[] { ";", "+" }, new String[] { "&", "" });
    String url = artifactoryUrl + "/api/search/prop?" + replacedProperties;
    PropertySearchResult result = readResponse(client.execute(new HttpGet(url)), new TypeReference<PropertySearchResult>() {
    }, "Failed to search artifact by the properties '" + properties + "'", false);
    return result;
}
Also used : PreemptiveHttpClient(org.jfrog.build.client.PreemptiveHttpClient) HttpGet(org.apache.http.client.methods.HttpGet) PropertySearchResult(org.jfrog.build.api.dependency.PropertySearchResult)

Example 3 with PreemptiveHttpClient

use of org.jfrog.build.client.PreemptiveHttpClient in project build-info by JFrogDev.

the class ArtifactoryDependenciesClient method execute.

private HttpResponse execute(String artifactUrl, boolean isHead, Map<String, String> headers) throws IOException {
    PreemptiveHttpClient client = httpClient.getHttpClient();
    artifactUrl = ArtifactoryHttpClient.encodeUrl(artifactUrl);
    HttpRequestBase httpRequest = isHead ? new HttpHead(artifactUrl) : new HttpGet(artifactUrl);
    // Explicitly force keep alive
    httpRequest.setHeader("Connection", "Keep-Alive");
    // Add all required headers to the request
    if (headers != null) {
        for (Map.Entry<String, String> entry : headers.entrySet()) {
            httpRequest.setHeader(entry.getKey(), entry.getValue());
        }
    }
    HttpResponse response = client.execute(httpRequest);
    StatusLine statusLine = response.getStatusLine();
    int statusCode = statusLine.getStatusCode();
    if (statusCode == HttpStatus.SC_NOT_FOUND) {
        throw new FileNotFoundException("Unable to find " + artifactUrl);
    }
    if (statusCode != HttpStatus.SC_OK && statusCode != HttpStatus.SC_PARTIAL_CONTENT) {
        EntityUtils.consume(response.getEntity());
        throw new IOException("Error downloading " + artifactUrl + ". Code: " + statusCode + " Message: " + statusLine.getReasonPhrase());
    }
    return response;
}
Also used : StatusLine(org.apache.http.StatusLine) PreemptiveHttpClient(org.jfrog.build.client.PreemptiveHttpClient) HttpRequestBase(org.apache.http.client.methods.HttpRequestBase) HttpGet(org.apache.http.client.methods.HttpGet) FileNotFoundException(java.io.FileNotFoundException) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) Map(java.util.Map) HttpHead(org.apache.http.client.methods.HttpHead)

Example 4 with PreemptiveHttpClient

use of org.jfrog.build.client.PreemptiveHttpClient in project build-info by JFrogDev.

the class ArtifactoryXrayClient method execute.

private ArtifactoryXrayResponse execute(HttpRequestBase httpRequest) throws InterruptedException, IOException {
    PreemptiveHttpClient client = httpClient.getHttpClient(XRAY_SCAN_CONNECTION_TIMEOUT_SECS);
    int retryNum = 0;
    long lastConnectionAttemptMillis = 0;
    HttpResponse response = null;
    while (true) {
        try {
            lastConnectionAttemptMillis = System.currentTimeMillis();
            retryNum++;
            response = client.execute(httpRequest);
            return parseXrayScanResponse(response);
        } catch (IOException e) {
            if (isStableConnection(lastConnectionAttemptMillis)) {
                retryNum = 0;
                continue;
            }
            if (XRAY_SCAN_RETRY_CONSECUTIVE_RETRIES <= retryNum) {
                throw e;
            }
            log.warn("Xray scan connection lost: " + e.getMessage() + ", attempting to reconnect...");
            // Sleeping before trying to reconnect.
            Thread.sleep(XRAY_SCAN_SLEEP_BETWEEN_RETRIES_MILLIS);
        } finally {
            if (response != null) {
                try {
                    EntityUtils.consume(response.getEntity());
                } catch (IOException e) {
                // Ignore
                }
            }
            httpRequest.releaseConnection();
        }
    }
}
Also used : PreemptiveHttpClient(org.jfrog.build.client.PreemptiveHttpClient) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException)

Example 5 with PreemptiveHttpClient

use of org.jfrog.build.client.PreemptiveHttpClient in project build-info by JFrogDev.

the class ArtifactoryDependenciesClient method searchArtifactsByPattern.

public PatternResultFileSet searchArtifactsByPattern(String pattern) throws IOException {
    PreemptiveHttpClient client = httpClient.getHttpClient();
    String url = artifactoryUrl + "/api/search/pattern?pattern=" + pattern;
    PatternResultFileSet result = readResponse(client.execute(new HttpGet(url)), new TypeReference<PatternResultFileSet>() {
    }, "Failed to search artifact by the pattern '" + pattern + "'", false);
    return result;
}
Also used : PreemptiveHttpClient(org.jfrog.build.client.PreemptiveHttpClient) PatternResultFileSet(org.jfrog.build.api.dependency.PatternResultFileSet) HttpGet(org.apache.http.client.methods.HttpGet)

Aggregations

PreemptiveHttpClient (org.jfrog.build.client.PreemptiveHttpClient)5 HttpGet (org.apache.http.client.methods.HttpGet)3 IOException (java.io.IOException)2 HttpResponse (org.apache.http.HttpResponse)2 FileNotFoundException (java.io.FileNotFoundException)1 Map (java.util.Map)1 StatusLine (org.apache.http.StatusLine)1 HttpHead (org.apache.http.client.methods.HttpHead)1 HttpPost (org.apache.http.client.methods.HttpPost)1 HttpRequestBase (org.apache.http.client.methods.HttpRequestBase)1 StringEntity (org.apache.http.entity.StringEntity)1 PatternResultFileSet (org.jfrog.build.api.dependency.PatternResultFileSet)1 PropertySearchResult (org.jfrog.build.api.dependency.PropertySearchResult)1 AqlSearchResult (org.jfrog.build.api.search.AqlSearchResult)1