You are viewing an old version of this page. View the current version.

Compare with Current View Page History

Version 1 Next »

If you integrate with, e.g., Google Charts, you will need to fetch the remote image in the background while using HTTPS. Because Google does not support HTTPS and you do not want that annoygin pop-up saying "Unsecure content". So you fetch the image on the background:

public class RemoteImage extends Image {
  /**  */
  final IModel<String> urlModel;

  /**
   * @param id
   * @param urlModel 
   */
  public RemoteImage(String id, IModel<String> urlModel) {
    super(id);
    this.urlModel = urlModel;
  }

  /**
   * @see org.apache.wicket.markup.html.image.Image#getImageResource()
   */
  @Override
  protected Resource getImageResource() {
    return new DynamicImageResource() {
      @Override
      protected byte[] getImageData() {
        try {
          URL url = new URL(urlModel.getObject());
          HttpURLConnection uc = (HttpURLConnection) url.openConnection();
          try {
            final int size = uc.getContentLength();
            int filled = 0;
            byte[] imageData = new byte[size];
            do {
              byte[] tmpData = new byte[size-filled];
              int read = uc.getInputStream().read(tmpData);
              System.arraycopy(tmpData, 0, imageData, filled, read);
              filled += read;
            } while (filled < size);
            return imageData;
          } finally {
            uc.getInputStream().close();
            uc.disconnect();
          }
        } catch (IOException e) {
          Utils.errorLog(RemoteImage.class, "Failed to load remote image", e);
        } 

        return new byte[] {};
      }

      @Override
      protected void setHeaders(WebResponse response) {
        response.setHeader("Pragma", "no-cache");
        response.setHeader("Cache-Control", "no-cache");
        response.setDateHeader("Expires", 0);
      }
    };
  }
}

  • No labels