class FaviconServlet extends HttpServlet {

    private static final Logger LOGGER = LoggerFactory.getLogger(FaviconServlet.class);

    /**
     * The content-type of the images to return.
     */
    private static final String CONTENT_TYPE = "image/x-icon";
    /**
     * Bytes of the default favicon to return when one was not found on a host.
     */
    private byte[] defaultBytes;
    /**
     * Pool of HTTP connections to use to get the favicons
     */
    private CloseableHttpClient client;
    /**
     * Cache the domains that a favicon was not found.
     */
    private Cache<String, Integer> missesCache;
    /**
     * Cache the favicons that we've found.
     */
    private Cache<String, byte[]> hitsCache;

    @Override
    public void init(ServletConfig config) throws ServletException {
        super.init(config);
        // Create a pool of HTTP connections to use to get the favicons
        client = HttpClientBuilder.create()
            .setConnectionManager(new PoolingHttpClientConnectionManager())
            .setRedirectStrategy(new LaxRedirectStrategy())
            .build();
        // Load the default favicon to use when no favicon was found of a remote host
        try {
            defaultBytes = Files.readAllBytes(JiveGlobals.getHomePath().resolve("plugins").resolve("admin").resolve("webapp").resolve("images").resolve("server_16x16.gif"));
        }
        catch (final IOException e) {
            LOGGER.warn("Unable to retrieve default favicon", e);
        }
        // Initialize caches.
        missesCache = CacheFactory.createCache("Favicon Misses");
        hitsCache = CacheFactory.createCache("Favicon Hits");
    }

    @Override
    public void destroy() {
        try {
            client.close();
        } catch (IOException e) {
            LOGGER.warn("Unable to close HTTP client", e);
        }
    }

    /**
     * Retrieve the image based on it's name.
     *
     * @param request the httpservletrequest.
     * @param response the httpservletresponse.
     */
    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response) {
        final String host = request.getParameter("host");

        // OF-1885: Ensure that the provided value is a valid hostname.
        if (!InetAddresses.isInetAddress(host) && !InternetDomainName.isValid(host)) {
            LOGGER.info("Request for favicon of hostname that can't be parsed as a valid hostname '{}' is ignored.", host);
            writeBytesToStream(defaultBytes, response);
            return;
        }

        // Validate that we're connected to the host
        final SessionManager sessionManager = SessionManager.getInstance();
        final Optional<String> optionalHost = Stream
            .concat(sessionManager.getIncomingServers().stream(), sessionManager.getOutgoingServers().stream())
            .filter(remoteServerHost -> remoteServerHost.equalsIgnoreCase(host))
            .findAny();
        if (!optionalHost.isPresent()) {
            LOGGER.info("Request to unconnected host {} ignored - using default response", host);
            writeBytesToStream(defaultBytes, response);
            return;
        }

        // Check special cases where we need to change host to get a favicon
        final String hostToUse = "gmail.com".equals(host) ? "google.com" : host;
        byte[] bytes = getImage(hostToUse, defaultBytes);
        if (bytes != null) {
            writeBytesToStream(bytes, response);
        }
    }

    /**
     * Writes out a <code>byte</code> to the ServletOuputStream.
     *
     * @param bytes the bytes to write to the <code>ServletOutputStream</code>.
     */
    private void writeBytesToStream(byte[] bytes, HttpServletResponse response) {
        response.setContentType(CONTENT_TYPE);

        // Send image
        try (ServletOutputStream sos = response.getOutputStream()) {
            sos.write(bytes);
            sos.flush();
        }
        catch (IOException e) {
            // Do nothing
        }
    }

    /**
     * Returns the favicon image bytes of the specified host.
     *
     * @param host the name of the host to get its favicon.
     * @return the image bytes found, otherwise null.
     */
    private byte[] getImage(String host, byte[] defaultImage) {
        // If we've already attempted to get the favicon twice and failed,
        // return the default image.
        if (missesCache.get(host) != null && missesCache.get(host) > 1) {
            // Domain does not have a favicon so return default icon
            return defaultImage;
        }
        // See if we've cached the favicon.
        if (hitsCache.containsKey(host)) {
            return hitsCache.get(host);
        }
        byte[] bytes = getImage(host);
        if (bytes == null) {
            // Cache that the requested domain does not have a favicon. Check if this
            // is the first cache miss or the second.
            if (missesCache.get(host) != null) {
                missesCache.put(host, 2);
            }
            else {
                missesCache.put(host, 1);
            }
            // Return byte of default icon
            bytes = defaultImage;
        }
        // Cache the favicon.
        else {
            hitsCache.put(host, bytes);
        }
        return bytes;
    }

    private byte[] getImage(@Nonnull final String host) {<FILL_FUNCTION_BODY>}

}

        final Set<URI> urls = new HashSet<>();

        try {
            // Using a builder to reduce the impact of using user-provided values to generate a URL request.
            urls.add(new URIBuilder().setScheme("https").setHost(host).setPath("favicon.ico").build());
            urls.add(new URIBuilder().setScheme("http").setHost(host).setPath("favicon.ico").build());
        } catch (URISyntaxException e) {
            LOGGER.debug("An exception occurred while trying to obtain an image from: {}", host, e);
            return null;
        }
        // Try to get the favicon from the url using an HTTP connection from the pool
        // that also allows configuring timeout values (e.g. connect and get data)
        final RequestConfig requestConfig = RequestConfig.custom()
            .setConnectTimeout(5000)
            .setSocketTimeout(5000)
            .build();

        for (final URI url : urls) {
            final HttpUriRequest getRequest = RequestBuilder.get(url)
                .setConfig(requestConfig)
                .build();

            try (final CloseableHttpResponse response = client.execute(getRequest)) {
                if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
                    final byte[] result = EntityUtils.toByteArray(response.getEntity());

                    // Prevent SSRF by checking result (OF-1885)
                    if (!GraphicsUtils.isImage(result)) {
                        LOGGER.info("Ignoring response to an HTTP request that should have returned an image (but returned something else): {}", url);
                        continue;
                    }
                    return result;
                }
            } catch (final IOException ex) {
                LOGGER.debug("An exception occurred while trying to obtain an image from: {}", url, ex);
            }
        }
        return null;
    
<no_super_class>