All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.applitools.eyes.ResourceFuture Maven / Gradle / Ivy

There is a newer version: 3.215.0-beta
Show newest version
package com.applitools.eyes;

import com.applitools.eyes.visualgrid.model.RGridResource;
import com.applitools.eyes.visualgrid.services.IResourceFuture;
import com.applitools.utils.GeneralUtils;
import org.apache.commons.io.IOUtils;

import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.List;
import java.util.concurrent.Future;
import java.util.concurrent.TimeUnit;

public class ResourceFuture implements IResourceFuture {

    private Future future;
    private String url;
    private Logger logger;
    private IServerConnector serverConnector;
    private RGridResource rgResource;
    private String userAgent;

    public ResourceFuture(Future future, String url, Logger logger, IServerConnector serverConnector, String userAgent) {
        this.future = future;
        this.url = url;
        this.logger = logger;
        this.serverConnector = serverConnector;
        this.userAgent = userAgent;
    }

    public ResourceFuture(RGridResource rgResource, Logger logger, IServerConnector serverConnector, String userAgent) {
        this.url = rgResource.getUrl();
        this.rgResource = rgResource;
        this.logger = logger;
        this.serverConnector = serverConnector;
        this.userAgent = userAgent;
    }

    @Override
    public boolean cancel(boolean mayInterruptIfRunning) {
        return future.cancel(mayInterruptIfRunning);
    }

    @Override
    public boolean isCancelled() {
        return future.isCancelled();
    }

    @Override
    public boolean isDone() {
        return future.isDone();
    }

    @Override
    public RGridResource get() throws InterruptedException {
        logger.verbose("entering");
        synchronized (url) {
            logger.verbose("enter - this.rgResource: " + this.rgResource);
            if (this.future == null) {
                try {
                    IResourceFuture newFuture = serverConnector.downloadResource(new URL(this.url), userAgent);
                    this.future = ((ResourceFuture) newFuture).future;
                } catch (MalformedURLException malformedUrlException) {
                    GeneralUtils.logExceptionStackTrace(logger, malformedUrlException);
                }
            }
            int retryCount = 3;
            while (this.rgResource == null && retryCount > 0) {
                try {
                    Response response = this.future.get(15, TimeUnit.SECONDS);
                    int status = response.getStatus();
                    List contentLengthHeaders = response.getStringHeaders().get("Content-length");
                    int contentLength = Integer.parseInt(contentLengthHeaders.get(0));

                    logger.verbose("downloading url - : " + url);
                    logger.verbose("Content Length: " + contentLengthHeaders.get(0));
                    if ((status == 200 || status == 201) && (!contentLengthHeaders.isEmpty() && contentLength > 0)) {
                        logger.verbose("response: " + response);
                        byte[] content = downloadFile(response);
                        if (content.length == 0) {
                            throw new Exception("content is empty - url :" + url);
                        }
                        String contentType = Utils.getResponseContentType(response);
                        String contentEncoding = Utils.getResponseContentEncoding(response);
                        if (contentEncoding != null && contentEncoding.contains("gzip")) {
                            content = GeneralUtils.getUnGzipByteArrayOutputStream(content);
                        }
                        rgResource = new RGridResource(url, contentType, content, logger, "ResourceFuture");
                        break;
                    }
                    else{
                        retryCount--;
                    }
                } catch (Throwable e) {
                    GeneralUtils.logExceptionStackTrace(logger, e);
                    retryCount--;
                    logger.verbose("Entering retry");
                    try {
                        Thread.sleep(300);
                        IResourceFuture newFuture = serverConnector.downloadResource(new URL(this.url), userAgent);
                        this.future = ((ResourceFuture) newFuture).future;
                    } catch (MalformedURLException malformedUrlException) {
                        GeneralUtils.logExceptionStackTrace(logger, malformedUrlException);
                    }
                }
            }

        }
        logger.verbose("exit");
        return rgResource;
    }

    @Override
    public RGridResource get(long timeout, TimeUnit unit) {
        try {
            return get();
        } catch (InterruptedException e) {
            GeneralUtils.logExceptionStackTrace(logger, e);
        }
        return null;
    }

    private byte[] downloadFile(Response response) {
        InputStream inputStream = response.readEntity(InputStream.class);
        byte[] bytes = new byte[0];
        try {
            bytes = IOUtils.toByteArray(inputStream);
        } catch (IOException e) {
            GeneralUtils.logExceptionStackTrace(logger, e);
        }
        return bytes;
    }

    @Override
    public String getUrl() {
        return this.url;
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy