[Logo] Enterprise Client Community
  [Search] Search   [Recent Topics] Recent Topics   [Members]  Member Listing   [Groups] Back to home page 
[Register] Register / 
[Login] Login 
Problem with images in TextWithLinks  XML
Forum Index -> Development
Author Message
moebus

Power User

Joined: 21/11/2007 12:49:18
Messages: 93
Offline

We use textwithlinks, and in the html text have included some images. In some scenarios (applet) the base url of our page contains a .. path element.

In this situation we have problems displaying images in html text.
We like to specify them with relative url, i.e. <img src="images/cool.jpg">. This src specification makes its way through ExtImageView and comes to new URL(baseUrl,src). This JDK method normalizes the resulting URL, i.e. the ../ stuff is gone. This URL is then presented to TestPaneElement.HTMLImageCache.get() where it is checked if the url of the image starts with the page url. This page url however has not been normalized, just toExternalForm() is called. So the image is not displayed.

It would work if we did specify the correct url in absolute form (so in this case containing the xxx/../yyy stuff), but this is cumbersome especially since then the application code would have to include ifs reflecting the current mode in which the app is viewed, e.g. webstart, or applet.

Regards
Manfred
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

If you say "relative URL" then you mean relative within the webcontent directory?

Regards, Björn

PS: to do some pre-parsing on my side etc. shouldn't be a a big problem...

Björn Müller, CaptainCasa GmbH
moebus

Power User

Joined: 21/11/2007 12:49:18
Messages: 93
Offline

Just another helpful idea:
In webstart case, we can explicitly set page root and start page, as the first two arguments of PageWebstart.

In applet case, we have only one argument available ("page") which then mangled together with applet's contextRoot to form the two parameters for the PageBrowser. It might be helpful to normalize the resulting URL here.
moebus

Power User

Joined: 21/11/2007 12:49:18
Messages: 93
Offline

while composing my previous comment, did not see your last remark.
To answer this: relative url is such that it works in webstart, i.e. it is relative to the path where the faces servlet is situated.
dkhanzhyiev

Active

Joined: 11/11/2015 10:21:27
Messages: 10
Offline

To illustrate the problem here is small sample:
http://pastebin.com/t8kSSijA
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

THere is now an attribute TEXTWITHLINKS-BASEURL which you can set in the component. If you want to set it in general, then please use the style processing.

Hope, this matches you requirements.

Thanks + regards! Björn

Björn Müller, CaptainCasa GmbH
dkhanzhyiev

Active

Joined: 11/11/2015 10:21:27
Messages: 10
Offline

Why not just correctly check URL in
TEXTPANEElement.HTMLImageCache.get()

something like this
Code:
 ...
 68: String str = localURL.toExternalForm();
 69:  if (m_baseUrl != null)
       {
           String normalized = (new URI(m_baseUrl )).normalize().toString();
 71:      if(str.startsWith(normalized)) {
 ...


but str probably should also be normalized.
dkhanzhyiev

Active

Joined: 11/11/2015 10:21:27
Messages: 10
Offline

baseurl attribute does not solve our problem.

Again here is problematic code:

Code:
  class HTMLImageCache extends Hashtable<URL,Image> 
     {
         @Override
         public synchronized Image get(Object key) 
         {
             if (key instanceof URL) 
             {
                 URL url = (URL)key;
                 String urlString = url.toExternalForm();
                 if (m_webappBaseUrl != null)
                 {
                     if (urlString.startsWith(m_webappBaseUrl))
                     {
                         urlString = urlString.substring(m_webappBaseUrl.length());
                         if (urlString.startsWith("/") == false)
                             urlString = "/" + urlString;
                         ImageIcon icon = getPage().loadImageIcon(urlString);
                         return icon.getImage();
                     }
                 }
             }
             return super.get(key);
         }
     }    
 


when
m_webappBaseUrl llooks like: "http://host.com/some1/../some2/"
key is URL constructed from base "http://host.com/some1/../some2" and "img/someImg.gif" - it is normalized by URL class and then

Code:
 String urlString = url.toExternalForm(); // ="http://host.com/some2/img/someImg.gif"
 


So line
Code:
   if (urlString.startsWith(m_webappBaseUrl)) {...
 

fails. And image is not displayed while URL is correct.
dkhanzhyiev

Active

Joined: 11/11/2015 10:21:27
Messages: 10
Offline

UP!
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

...ooops, did not see the activity on this item....
Will check...
Björn

Björn Müller, CaptainCasa GmbH
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

Hi,

thanks for your remark: up to now we only did read the image when feeling explicitly responsible, but it seems that the cache has to read all images. We now added the following implementation and read all images, also these ones not belonging to the scope of our web application.

When do you need the update on this? The regular availability would be next Monday (Dec 28th).

Regards, Björn

Code:
         public synchronized Image get(Object key) 
         {
             if (key instanceof URL) 
             {
                 URL url = (URL)key;
                 String urlString = url.toExternalForm();
                 if (m_webappBaseUrl != null)
                 {
                     if (urlString.startsWith(m_webappBaseUrl))
                     {
                         urlString = urlString.substring(m_webappBaseUrl.length());
                         if (urlString.startsWith("/") == false)
                             urlString = "/" + urlString;
                         ImageIcon icon = getPage().loadImageIcon(urlString);
                         return icon.getImage();
                     }
                 }
                 else
                 {
                     try
                     {
                         // first check in cache
                         if (contains(key))
                             return get(super.get(key));
                         // explicitly read image
                         DataTransfer dt = new DataTransfer(null,null,null);
                         dt.readBytesFromURL(urlString);
                         if (dt.getDataTransferException() != null)
                         {
                             CLog.L.log(CLog.LL_WAR,"Problem loading image: " + urlString,dt.getDataTransferException());
                             put((URL)key,null);
                             return null;
                         }
                         else
                         {
                             byte[] bytes = dt.getResponseBytes();
                             ImageIcon icon = new ImageIcon(bytes);
                             put((URL)key,icon.getImage());
                             return icon.getImage();
                         }
                     }
                     catch (Throwable t)
                     {
                         CLog.L.log(CLog.LL_WAR,"Problem loading image: " + urlString,t);
                         put((URL)key,null);
                         return null;
                     }
                 }
             }
             return super.get(key);
 

Björn Müller, CaptainCasa GmbH
dkhanzhyiev

Active

Joined: 11/11/2015 10:21:27
Messages: 10
Offline

Thanks! Next Monday is Ok.
But it still looks like it will not work.

In our case m_webappBaseUrl is not null and still is not normalized(or is it?) so this code will not work in our case.
CaptainCasa

Power User
[Avatar]

Joined: 21/11/2007 12:23:06
Messages: 5519
Offline

Hey, you are right!!!! - the else-processing belongs to the wrong if statement!

THANKS!!!
Björn

Code:
     class HTMLImageCache extends Hashtable<URL,Image> 
     {
         @Override
         public synchronized Image get(Object key) 
         {
             if (key instanceof URL) 
             {
                 URL url = (URL)key;
                 String urlString = url.toExternalForm();
                 // load via page processing if URL is pointing into the
                 // content of the web application
                 if (m_webappBaseUrl != null)
                 {
                     if (urlString.startsWith(m_webappBaseUrl))
                     {
                         urlString = urlString.substring(m_webappBaseUrl.length());
                         if (urlString.startsWith("/") == false)
                             urlString = "/" + urlString;
                         ImageIcon icon = getPage().loadImageIcon(urlString);
                         return icon.getImage();
                     }
                 }
                 // otherwise: load image through normal http access
                 try
                 {
                     // first check in cache
                     if (contains(key))
                         return get(super.get(key));
                     // explicitly read image
                     DataTransfer dt = new DataTransfer(null,null,null);
                     dt.readBytesFromURL(urlString);
                     if (dt.getDataTransferException() != null)
                     {
                         CLog.L.log(CLog.LL_WAR,"Problem loading image: " + urlString,dt.getDataTransferException());
                         put((URL)key,null);
                         return null;
                     }
                     else
                     {
                         byte[] bytes = dt.getResponseBytes();
                         ImageIcon icon = new ImageIcon(bytes);
                         put((URL)key,icon.getImage());
                         return icon.getImage();
                     }
                 }
                 catch (Throwable t)
                 {
                     CLog.L.log(CLog.LL_WAR,"Problem loading image: " + urlString,t);
                     put((URL)key,null);
                     return null;
                 }
             }
             return super.get(key);
         }
     }    
 

Björn Müller, CaptainCasa GmbH
 
Forum Index -> Development
Go to:   
Powered by JForum 2.1.6 © JForum Team