This Confluence has been LDAP enabled, if you are an ASF Committer, please use your LDAP Credentials to login. Any problems file an INFRA jira ticket please.

Child pages
  • Showing a remote image on Wicket site
Skip to end of metadata
Go to start of metadata

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;
    
            InputStream inputStream;
            {
              int tmp = uc.getContentLength();
          
              if (tmp < 0) {
                inputStream = new BufferedInputStream(uc.getInputStream());
                inputStream.mark(Integer.MAX_VALUE);
                while (0 <= inputStream.read()) {
                  // read
                }
                inputStream.reset();
                size = inputStream.available();
              } else {
                size = tmp;
                inputStream = uc.getInputStream();
              }
            }
        
            int filled = 0;
            byte[] imageData = new byte[size];
            do {
              byte[] tmpData = new byte[size-filled];
              int read = inputStream.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