Skip to main content


Eclipse Community Forums
Forum Search:

Search      Help    Register    Login    Home
Home » Eclipse Projects » Remote Application Platform (RAP) » UTF8 characters not displaying in html served by RAP appserver(HTML created within RAP and served as an rwt-resource displays incorrect UTF8 text)
UTF8 characters not displaying in html served by RAP appserver [message #1859210] Fri, 19 May 2023 11:40 Go to next message
John Gymer is currently offline John GymerFriend
Messages: 279
Registered: November 2012
Location: UK
Senior Member
I wonder if anyone can help with a display issue we're having with UTF8 files served by RAP please...

In our RAP application we have some text data that includes various national language characters. In a Table this data is rendered correctly, so no problems there:
index.php/fa/43206/0/

An option in our application is to produce an HTML report of this data, which we do by writing an rwt-resource - a new .html file. Viewing this html file directly with something like NotePad++ in UTF8 encoding shows the text data correctly, so we know the file is good:
index.php/fa/43207/0/

However, if we register that file as an RWT resource and open it up, the browser displays that text wrong (doesn't matter what browser type). Note the URL is rwt-resource so served by RAP:
index.php/fa/43208/0/

It does not appear to be a browser problem because if we navigate Explorer directly to that work directly and open the html file manually then it displays the same file correctly (see the c:\JVM URL):
index.php/fa/43209/0/

Why is the html not displaying correctly when served up through RAP as an RWT-resource, but the file is clearly ok?

We are using RAP 3.12 still, but I didn't see anything in later releases that suggested any change in behaviour. Hopefully the images display ok when I post (the previous doesn't show them!)

Thanks, John


---
Just because you can doesn't mean you should
Re: UTF8 characters not displaying in html served by RAP appserver [message #1859265 is a reply to message #1859210] Thu, 25 May 2023 08:20 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2427
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hi John,

for me this issue is really strange and... interesting. Could you please create a small test project to be able to reproduce it on my end? It's also possible that the issue is related to the servlet container, Java version, operating and file system and the browser. Is the issue reproduceable on different environments? Is it reproducible in Chrome and Firefox? Did you try Tomcat or other container different from Jetty?

If you provide me with a simple test project I would like to debug and test it.

Best regards,
Ivan
Re: UTF8 characters not displaying in html served by RAP appserver [message #1859285 is a reply to message #1859265] Fri, 26 May 2023 14:16 Go to previous messageGo to next message
John Gymer is currently offline John GymerFriend
Messages: 279
Registered: November 2012
Location: UK
Senior Member
Thanks Ivan. Here is a fairly simple self-contained example that demonstrates the issue:

/* Demonstrates character translation issue of resources */
package bug.snippet;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.io.InputStream;

import org.eclipse.rap.rwt.RWT;
import org.eclipse.rap.rwt.client.service.UrlLauncher;
import org.eclipse.rap.rwt.service.ResourceManager;
import org.eclipse.swt.SWT;
import org.eclipse.swt.events.SelectionEvent;
import org.eclipse.swt.events.SelectionListener;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.graphics.RGB;
import org.eclipse.swt.layout.FormAttachment;
import org.eclipse.swt.layout.FormData;
import org.eclipse.swt.layout.FormLayout;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableColumn;
import org.eclipse.swt.widgets.TableItem;

public class Bugsy {
	private Display display;
	private Shell shell;
	Button but;
	Table tab;
	TableColumn col;
	TableItem ti;
	
	public void begin() {
		System.out.println("BugSnippy Starting...");
		
		// create the Shell
		display = new Display();
		shell = new Shell(display, SWT.NONE); //, SWT.TITLE|SWT.CLOSE|SWT.RESIZE);
		shell.setText("Shell");
		shell.setBackground(new Color(null, new RGB(255,192,128)));
		FormLayout layout = new FormLayout();
		layout.marginBottom = 20;
		shell.setLayout(layout);

		but = new Button(shell, SWT.PUSH);
		but.setText("Test");
		FormData fd = new FormData();
		fd.left = new FormAttachment(0, 10);
		fd.width = 200;
		fd.top = new FormAttachment(0, 10);
		fd.height = 20;
		but.setLayoutData(fd);
		but.addSelectionListener(butLnsr);
		
		tab = new Table(shell, SWT.SINGLE);
		col = new TableColumn(tab, SWT.NONE);
		col.setWidth(500);
		col.setText("My Column");

		fd = new FormData();
		fd.left = new FormAttachment(0, 10);
		fd.width = 500;
		fd.top = new FormAttachment(0, 50);
		fd.bottom = new FormAttachment(100, -10);
		tab.setLayoutData(fd);

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("ABC");

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("DEF");

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("GHI");

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("JKL");

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("毛茸茸的鸭子");

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("пушистые уточки");

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("البط الرقيق");

		ti = new TableItem(tab, SWT.NONE);
		ti.setText("kabarık ördekler - αφράτες πάπιες");

		shell.setFullScreen(true);

		shell.open();

		System.out.println("BugSnippy Done!");
	}

	SelectionListener butLnsr = new SelectionListener() {
		@Override
		public void widgetSelected(SelectionEvent ev) {
			System.out.println("but press...");
			// write all the table data to a physical file (html page)
			String clientDir = "c:/temp/MySessionFiles/";		
			File directory = new File(clientDir);
			directory.mkdirs();
			String fileName = clientDir + "listDataAsReport.html";
			try {
				FileWriter fw = new FileWriter(new File(fileName));

				fw.write("<html>\n");
				fw.write("<head>\n");

				fw.write("<style>\n");
				fw.write("body{font-family:Arial, Helvetica, sans-serif;font-size:12px;padding:0;margin:0;}\n");
				fw.write("h1{font-family:Arial, Helvetica, sans-serif;font-size:14px;font-weight: bold;padding:0;margin:0;}\n");
				fw.write("td{border-bottom: 1px solid #EEEEEE;border-right: 1px solid #EEEEEE;color:#0;font-family:Arial, Helvetica, sans-serif;font-size:10px;padding:0;}\n");
				fw.write("td.selected{border-bottom: 1px solid #EEEEEE;border-right: 1px solid #EEEEEE;color:#FFFFFF;background-color: #6E8EFF;font-family:Arial, Helvetica, sans-serif;font-size:10px;padding:0;}\n");
				fw.write("</style>\n");
				
				fw.write("</head>\n");
				fw.write("<body>\n");

				fw.write("<h1>My List Report</h1><br>\n");

				fw.write("<table>\n");

				int rows = tab.getItemCount();
				for (int i = 0; i < rows; i++) {
					String txt = tab.getItem(i).getText();
					System.out.println("Item " + i + " has data '" + txt + "'");
					fw.write("<tr><td>" + txt + "</td></tr>\n");
				}

				fw.write("</table>\n");
				fw.write("</body>\n");
				fw.write("</html>\n");

				fw.close();

				System.out.println("Written to html file: " + fileName);
				
				// register the file as a resource
				String rsc = loadAndRegisterResource(fileName);
				System.out.println("Resource is: " + rsc);
				
				// open that html file in a new tab
				launchURL(rsc);
				
			} catch (IOException e) {
				e.printStackTrace();
			}
		}

		@Override
		public void widgetDefaultSelected(SelectionEvent e) {
		}
	};

	public String loadAndRegisterResource(String fileName) {
		InputStream sourceStream = null;

		sourceStream = this.getClass().getClassLoader().getResourceAsStream(fileName);

		if (sourceStream == null) {
			sourceStream = this.getClass().getClassLoader().getResourceAsStream("rwt-resources/" + fileName);

			if (sourceStream == null) {
				try {
					sourceStream = new FileInputStream(fileName);
				} catch (FileNotFoundException e) {
			    	System.out.println("ERROR: loadAndRegisterResource failed to open new inputStream for: " + fileName);
				}
			}
			if (sourceStream == null) {
				System.out.println("ERROR: loadAndRegisterResource failed to find requested resource: " + fileName);
				return(null);
			}
		}
		
		ResourceManager resourceManager = RWT.getResourceManager();
		
		try {
			System.out.println("A loadAndRegisterResource - Registering resource: " + fileName);
			resourceManager.register(fileName, sourceStream);
		} catch (Exception e) {
			System.out.println("ERROR: loadAndRegisterResource [A:too many attempts] - register error on " + fileName + " - " + e.getMessage());
		}
		
		// determine the resource name that it is registered as
		String resourceSrc = null;
		try {
			resourceSrc = getLocation(resourceManager, fileName);
			System.out.println("loadAndRegisterResource got " + fileName + " location as a resource: " + resourceSrc);
		}
		catch (IllegalArgumentException e) {
			System.out.println("ERROR: loadAndRegisterResource IllegalArgumentException getting resource location: " + e.getMessage());
		}
		
		// close the resource
		try {
			sourceStream.close();
		} catch (IOException e) {
			System.out.println("ERROR: loadAndRegisterResource IOException closing resource: " + e.getMessage());
		}

		System.out.println("DEBUG: loadAndRegisterResource: " + resourceSrc);
		return(resourceSrc);
	}

	public String getLocation(ResourceManager resourceManager, String loadFile) {
		String src = null;
		try {
			src = resourceManager.getLocation(loadFile);
		}
		catch (Exception e) {
			System.out.println("ERROR: getLocation failed to locate resource: " + loadFile + " - " + e.getMessage());
		}
		return(src);
	}

	public int launchURL(String url) {
		UrlLauncher launcher = RWT.getClient().getService(UrlLauncher.class);
		if (launcher == null) {
			System.out.println("ERROR: RWT.launchURL failed to create RWT launcher for launching: " + url);
			return(-1);
		}
		launcher.openURL(url);
		return(0);
	}
}


I recreated it in Firefox, Edge and Chrome - haven't tried anything else.

Running in a different container will take a little longer to setup - we normally use OSGi, then the eventual app is deployed using WAR to TOMEE, JBoss, WildFly, WebLogic or WebSphere (though that is customer choice, so not our environment). I probably won't try to recreate the sample in any of these, but I can try my original application.

Hopefully the above is all you need to recreate it though.
Thanks, John


---
Just because you can doesn't mean you should
Re: UTF8 characters not displaying in html served by RAP appserver [message #1859286 is a reply to message #1859285] Fri, 26 May 2023 14:38 Go to previous messageGo to next message
John Gymer is currently offline John GymerFriend
Messages: 279
Registered: November 2012
Location: UK
Senior Member
Interestingly, under WildFly (I'm using 15), the html file is not written correctly to start with, so a different problem there, possibly in FileWriter - but this isn't the immediately problem that I'm trying to solve.



---
Just because you can doesn't mean you should
Re: UTF8 characters not displaying in html served by RAP appserver [message #1859306 is a reply to message #1859210] Tue, 30 May 2023 06:23 Go to previous messageGo to next message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2427
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hi John,

without testing your snippet yet I found that in the generated HTML the charset definition is missing:
<meta http-equiv="content-type" content="text/html; charset=UTF-8">


Also the
FileWriter
is created without specifying the charset. Try to use
java.io.FileWriter.FileWriter(File, Charset)


Best regards,
Ivan

[Updated on: Tue, 30 May 2023 06:25]

Report message to a moderator

Re: UTF8 characters not displaying in html served by RAP appserver [message #1859326 is a reply to message #1859306] Wed, 31 May 2023 07:57 Go to previous messageGo to next message
John Gymer is currently offline John GymerFriend
Messages: 279
Registered: November 2012
Location: UK
Senior Member
Thanks Ivan, hadn't considered either aspect. I've added the charset meta tag into the html and this has resolved it.
Do you think specifying the Charset on FileWriter is necessary? Would UTF-8 not be the default anyway? The content of the file itself looked OK anyway.


---
Just because you can doesn't mean you should
Re: UTF8 characters not displaying in html served by RAP appserver [message #1859349 is a reply to message #1859210] Thu, 01 June 2023 15:01 Go to previous message
Ivan Furnadjiev is currently offline Ivan FurnadjievFriend
Messages: 2427
Registered: July 2009
Location: Sofia, Bulgaria
Senior Member
Hi John,

my experience shown that if you rely on "defaults", soon or later you will run into trouble. The default "file.encoding" property is platform (OS) specific. To be on the safe side I would suggest to set it.

Best regards,
Ivan
Previous Topic:Sound/Audio through RAP/RWT
Next Topic:Modify Event
Goto Forum:
  


Current Time: Tue Dec 03 15:02:24 GMT 2024

Powered by FUDForum. Page generated in 0.03625 seconds
.:: Contact :: Home ::.

Powered by: FUDforum 3.0.2.
Copyright ©2001-2010 FUDforum Bulletin Board Software

Back to the top