Menu

#25 Encoding problems when rewriting

open
nobody
Exception (1)
5
2010-08-10
2010-08-10
No

Hi,
first of all: nice work and quite useful. I needed a proxy with some additional behavior and your J2EP fits perfectly to achieve that. However, I think there's a small encoding trap in your link rewriting code. My extended version of J2EP runs on a linux machine and when rewriting is activated I receive the following error:

java.io.CharConversionException: Not an ISO 8859-1 character: ?
javax.servlet.ServletOutputStream.print(ServletOutputStream.java:89)
net.sf.j2ep.UrlRewritingOutputStream.rewrite(UrlRewritingOutputStream.java:177)
net.sf.j2ep.UrlRewritingResponseWrapper.processStream(UrlRewritingResponseWrapper.java:251)
net.sf.j2ep.RewriteFilter.doFilter(RewriteFilter.java:88)

This error could not be reproduced on a windows machine and it happens only when the server responds some special characters like german umlauts. To cut to the chase, the following code works perfectly for me:

// get the char encoding from response by using response.getCharacterEncoding()
//
// I simply put another String argument to the constructor of UrlRewritingOutputStream and additionally
// edited UrlRewritingResponseWrapper to provide response.getCharacterEncoding() when calling the constructor of UrlRewritingOutputStream.

In UrlRewritingOutputStream change:
156: StringBuffer page = new StringBuffer();
157: Charset charset = Charset.forName(encoding);
158: CharsetEncoder encoder = charset.newEncoder();
159: CharsetDecoder decoder = charset.newDecoder();
160: ByteBuffer buf = encoder.encode(CharBuffer.wrap(stream.toString(encoding)));
170: Matcher matcher = linkPattern.matcher(decoder.decode(buf));

Maybe it is helpful.

Best regards,
Steffen

Discussion


Log in to post a comment.