Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[gwt] unicode character transfer not possible #476

Closed
eix128 opened this issue Jul 4, 2012 · 8 comments
Closed

[gwt] unicode character transfer not possible #476

eix128 opened this issue Jul 4, 2012 · 8 comments
Assignees
Labels

Comments

@eix128
Copy link

eix128 commented Jul 4, 2012

Hello when i tried to send data from

package client;

import java.io.Serializable;
import java.util.Date;

/**
 * @author p.havelaar
 */
public class EventObject implements Serializable {

    private String author;
    private String message;
    private String time;

    public EventObject() {
    }

    public EventObject(String author, String data) {
        this.author = author;
        this.message = data;
        Date date = new Date();

        this.time = Utils.getInstance().getDateTimeFormatter().format(date);
    }

    public String getAuthor() {
        return author;
    }

    public void setAuthor(String author) {
        this.author = author;
    }

    public String getTime() {
        return time;
    }

    public void setTime(String time) {
        this.time = time;
    }

    public String getMessage() {
        return message;
    }

    public void setMessage(String msg) {
        this.message = msg;
    }
}

it the string contains any unicode utf-8 characters , they got broken.
I tried to send turkish charset but the object i got back seems broken

@pierreh
Copy link

pierreh commented Jul 5, 2012

Hi I have not yet tested proper UTF-8 encoding, so I cant say much about this

@pierreh
Copy link

pierreh commented Jul 5, 2012

The outputstream is however set to UTF-8 encoding

@eix128
Copy link
Author

eix128 commented Jul 7, 2012

how? no way i tried on server side

sun-web.xml
/




web.xml

encodingFilter
org.springframework.web.filter.CharacterEncodingFilter

encoding
UTF-8

    <init-param>
        <param-name>forceEncoding</param-name>
        <param-value>true</param-value>
    </init-param>
</filter>

<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>

page.html

i added this inside head tag

but i still get ?? when i post ü single char.Every single char gives double question mark.

I'm using gwt atmosphereclient api

@eix128
Copy link
Author

eix128 commented Jul 7, 2012

i post message like this:

@Override
public void doPost(HttpServletRequest postRequest, HttpServletResponse postResponse, List<Serializable> messages, GwtAtmosphereResource cometResource) {
    logger.info("Comet doPost:"+((EventObject)messages.get(0)).getMessage());


    //HttpSession session = postRequest.getSession(false);
    //broadcast(messages, cometResource);
    try{
        ServletOutputStream resp = postResponse.getOutputStream();
        postResponse.setCharacterEncoding("UTF-8");
        postResponse.setContentType("\"text/html; charset=UTF-8\"");
        postResponse.setHeader("Content-Type", "text/html; charset=utf-8");

        postRequest.setCharacterEncoding("UTF-8");


        cometResource.getAtmosphereResource().write(resp, messages.get(0));
    } catch (Exception epx) {

    }
}

@eix128
Copy link
Author

eix128 commented Jul 9, 2012

i ve found temporary solution its not good solution maybe but it works.
Instead of doing encoding , before i send data i call
String sendData = stringToHTMLString(realData);
then on getting data on CometListener:

String recvData = htmlToString(realData);

This sends data on html encoded unicode values , so on server the data doesn't broke up.

public static String htmlToString(String str) {
    Element element = DOM.createDiv();
    element.setInnerHTML(str);
    String str2 = element.getInnerText();
    str2 = str2.replaceAll("\\<.*?>","");
    return str2;

}


public static String stringToHTMLString(String string) {
    StringBuilder sb = new StringBuilder(string.length());
    // true if last char was blank
    boolean lastWasBlankChar = false;
    int len = string.length();
    char c;

    for (int i = 0; i < len; i++)
    {
        c = string.charAt(i);
        if (c == ' ') {
            // blank gets extra work,
            // this solves the problem you get if you replace all
            // blanks with &nbsp;, if you do that you loss
            // word breaking
            if (lastWasBlankChar) {
                lastWasBlankChar = false;
                sb.append("&nbsp;");
            }
            else {
                lastWasBlankChar = true;
                sb.append(' ');
            }
        }
        else {
            lastWasBlankChar = false;
            //
            // HTML Special Chars
            if (c == '"')
                sb.append("&quot;");
            else if (c == '&')
                sb.append("&amp;");
            else if (c == '<')
                sb.append("&lt;");
            else if (c == '>')
                sb.append("&gt;");
            else if (c == '\n')
                // Handle Newline
                sb.append("&lt;br/&gt;");
            else {
                int ci = 0xffff & c;
                if (ci < 160 )
                    // nothing special only 7 Bit
                    sb.append(c);
                else {
                    // Not 7 Bit use the unicode system
                    sb.append("&#");
                    sb.append(new Integer(ci).toString());
                    sb.append(';');
                }
            }
        }
    }
    return sb.toString();
}

@pierreh
Copy link

pierreh commented Jul 9, 2012

You are using the outputstream. This is outside of the intended use case
of writing data to the client.
You should create a message object and broadcast this object to the
client. GWT will then kick in to do the serialization for you.

On 7-7-2012 14:50, jduke32 wrote:

i post message like this:

 @Override
 public void doPost(HttpServletRequest postRequest, HttpServletResponse postResponse, List<Serializable>  messages, GwtAtmosphereResource cometResource) {
     logger.info("Comet doPost:"+((EventObject)messages.get(0)).getMessage());


     //HttpSession session = postRequest.getSession(false);
     //broadcast(messages, cometResource);
     try{
         ServletOutputStream resp = postResponse.getOutputStream();
         postResponse.setCharacterEncoding("UTF-8");
         postResponse.setContentType("\"text/html; charset=UTF-8\"");
         postResponse.setHeader("Content-Type", "text/html; charset=utf-8");

         postRequest.setCharacterEncoding("UTF-8");


         cometResource.getAtmosphereResource().write(resp, messages.get(0));
     } catch (Exception epx) {

     }
 }

Reply to this email directly or view it on GitHub:
#476 (comment)

@eix128
Copy link
Author

eix128 commented Jul 10, 2012

but broadcast is broadcasting to all clients , i will just send single client.
why i use broadcast for ?
why no 1 client post?

@pierreh
Copy link

pierreh commented Jul 10, 2012

then you should do cometResource.post

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants