powered by Jive Software

Improved favicon-getter

Hi,

I’'ve noticed that openfire only uses a favicon in the FaviconServlet when it is located at /favicon.ico

FaviconServlet.java:

byte[] bytes = getImage("http://" + host + "/favicon.ico");

For a private project I’'m using a class analysing the other page and then also taking the faviconheader-entry:

package bilder; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL; import org.apache.commons.httpclient.HttpClient;
import org.apache.commons.httpclient.HttpException;
import org.apache.commons.httpclient.HttpMethod;
import org.apache.commons.httpclient.methods.GetMethod; //Author: Torsten Kunkel <torsten@tkunkel.de>
//License: GPL public class FavIconDownloader {
     private Integer status;      private InputStream responseBody = null;      private byte[] body = null;      private String contentType;      private HttpMethod method = null;      public FavIconDownloader() {
     }      public String toString() {
          String rc = "";
          rc += "status:" + status;
          rc += ", ";
          rc += "contentType:" + contentType;
          // rc += "\n";
          return rc;
     }      private void downloadSite(String url) throws HttpException {
          HttpClient client = new HttpClient();           // establish a connection within 5 seconds
          client.getHttpConnectionManager().getParams().setConnectionTimeout(2000);           System.out.println("url:" + url);           method = new GetMethod(url);
          method.setFollowRedirects(true);           responseBody = null;
          try {
               client.executeMethod(method);
               responseBody = method.getResponseBodyAsStream();
          } catch (IOException ioe) {
               throw new HttpException(ioe.getMessage());
          }           status = method.getStatusCode();
          contentType = method.getResponseHeader("Content-Type").getValue();      }      public void disconnect() {
          method.releaseConnection();      }      // XXX this method needs some work
     private String analyseBody(String body) {
          String rc = null;
          try {
               if (body != null) {
                    // System.out.println(body);
                    int start = body.toUpperCase().indexOf("SHORTCUT ICON");
                    if (start > 0) {
                         int end = body.toUpperCase().indexOf(">", start);
                         if (end > 0) {
                              String tmp = body.substring(start, end);
                              // XXX maybe a regex is better
                              tmp = tmp.replaceAll(" ", "");                               // System.out.println(tmp);
                              start = tmp.indexOf("href=", tmp.indexOf(''"''));
                              end = tmp.indexOf("\"", start + 6); // +6 because of ''href="''.length
                              tmp = tmp.substring(start + 4, end);
                              tmp = tmp.replaceAll("\"", "");
                              tmp = tmp.replaceAll("''", "");
                              tmp = tmp.replaceAll("=", "");
                              rc = tmp;
                         }
                    }
               } else {
                    // nothing to do?
               }
          } catch (Exception e) {
          }
          return rc;      }      private String readBaseURL(String complexURL) {
          String rc = null;
          try {
               URL url = new URL(complexURL);                rc = url.getProtocol();
               rc += "://";
               rc += url.getHost();           } catch (MalformedURLException e) {
               e.printStackTrace();
          }
          return rc;
     }      public byte[] downloadFavIcon(String url) {
          byte[] rc = null;
          try {
               downloadSite(url);
               String baseUrl = readBaseURL(url);
               if ((status == 200) && (contentType.startsWith("text"))) {
                    byte[] bytes = new byte[2048];
                    responseBody.read(bytes);
                    String favicon = analyseBody(new String(bytes));
                    
                    System.out.println("favicon: " + favicon);                     if (favicon == null) {
                         downloadSite(baseUrl + "/favicon.ico");                     } else {
                         try {
                              if (!favicon.contains("://")) {
                                   favicon = baseUrl + "/" + favicon;
                              }
                              downloadSite(favicon);
                         } catch (java.lang.IllegalArgumentException e) {
                              // TODO Auto-generated catch block
                              e.printStackTrace();
                         }
                    }
               }                if ((contentType.startsWith("image/x-icon")) || (contentType.startsWith("image/icon"))
                         || (contentType.startsWith("text/plain"))) {
                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] bytes = new byte[2048];
                    int counter = 0;
                    while (counter != -1) {
                         counter = responseBody.read(bytes);
                         if (counter != -1) {
                              baos.write(bytes, 0, counter);
                         }
                    }
                    baos.flush();
                    rc = baos.toByteArray();
                    if (rc.length == 0) {
                         rc = null;
                    }
               } else {
                    if (status != 404) {
                         System.out.println("!image/x-icon => " + contentType);
                         System.out.println("status => " + status);
                    }
               }
          } catch (HttpException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          } catch (IOException e) {
               // TODO Auto-generated catch block
               e.printStackTrace();
          }
          disconnect();
          return rc;
     }
}

and here some testclass:

package test; //Author: Torsten Kunkel <torsten@tkunkel.de>
//License: GPL import bilder.FavIconDownloader; public class Downloadtest {
     public Downloadtest() {
          FavIconDownloader downloader = new FavIconDownloader();
          try {
               byte[] image = downloader.downloadFavIcon("http://10.187.112.166/test/neu.html");
               if (image == null) {
                    System.out.println("No image!");
               } else {
                    System.out.println("Image found.");
               }
          } catch (Exception e) {
               e.printStackTrace();
          }
     }      public static void main(String[] args) {
          Downloadtest downloadtest = new Downloadtest();
     }
}

Maybe it’'s usefull for openfire.