Scala Play 2.4.x wide character handling via anorm (MySQL) in Java Mail

I was under the impression that UTF-8 was the answer to everything: 0

Problem: Using a playable playout form to navigate from a webpage (basic HTML textarea input field) to a MySQL database via the Anorm abstraction layer (so that's correct escaped

), then reading the database to collect that data and create an email with using the JavaMail API to send HTML email with alternate characters (like characters like eg. (I would post more, but I suspect there might be strange artifacts here too). I'll try it in the comment below, maybe )

I can use a moderate character set and create an email TEXT (edited with Atom and put on the stream directly at the code level) and it goes through the email with all the characters I selected tactfully.

I haven't systematically worked with symbols yet, which I just used a relatively random sample as an initial test.

I put the same set of characters in a textbox and am trying to store them in the database and I can only store 1 out of 5 or less.

The errors look like this:

SQLException: Incorrect string value: '\xC4\x93\x0D\x0A\x0D\x0A...' for column 'content' at row 1

I suspect I am going to find out a ton of new information about Play and / or UTF-8 or HTML or whatever part of the chain where this is off the rails.

Now my question is, is there an idiomatic example of reproducing how to handle UTF-8 end-to-end via Anorm and in Java Mail?

(I think I expected it to be "inline", but then I expected the LOT to bake more into the main product ...)

I want / need both TEXT and HTML path for the email part. (I can write BOTH and they work fine - the problem is with the alternating characters moving though the channels are as above).

+1


source to share


2 answers


This seems to be a reliable candidate - I am studying it now to the end.

import org.apache.commons.lang3._

def htmlEncode(input: String) = htmlEncode_sb(input).toString    

def htmlEncode_sb(input: String, stringBuilder: StringBuilder = new StringBuilder()) = {
    stringBuilder.synchronized {
      for ((c, i) <- input.zipWithIndex) {
        if (CharUtils.isAscii(c)) {
          // Encode common HTML equivalent characters
          stringBuilder.append(StringEscapeUtils.escapeHtml4(c.toString()))
        } else {
          // Why isn't this done in escapeHtml4()?
          stringBuilder.append(s"""&#${Character.codePointAt(input, i)};""")
        }
      }
      stringBuilder
    }
  }

      

To make it work inside Play you will need this in the file build.sbt

  "org.apache.commons" % "commons-lang3" % "3.4",

      

This blog post led me to write this code: https://objectpartners.com/2013/04/24/html-encoding-utf-8-characters/



Update: Confirmed to work end-to-end.

Putting a web page as a TextArea inside a form saved in a MySQL database, escaped by Anorm, re-read from the database, and rendered inside a TextArea on a web page with extended characters (visually) rendered exactly as input.

You will need to call @Html(htmlContentString)

Twirl inside the template to re-render this as original HTML, but the browser (Safari 8.0.7) rendered what I gave it after traveling back and forth from the database.

One caveat - it generates machine readable HTML, not human readable HTML. It would be nice if it didn't encode the angle brackets and thus it looks more like the HTML we would expect. I'm sure a pattern matching block will be added next to the exception that :)

0


source


Currently I see the answer :

https://objectpartners.com/2013/04/24/html-encoding-utf-8-characters/



However, at the moment this is an obstacle ...

How can I disable a specific Implicit in Scala that prevents code from being compiled due to overloaded methods?

0


source







All Articles