Java에서 Javascript의 encodeURIComponent 구현하기

자바에서는 URLEncoder를 사용해서 인코딩을 할 수 있는데 자바스크립트에서 사용하는 encodeURIComponent와 살짝 다르다.

Java의 URLEncoder:

  • literal characters (regex representation): [-a-zA-Z0-9._*]
  • the space character " " is converted into a plus sign "+".

JavaScript의 encodeURIComponent():

  • literal characters (regex representation): [-a-zA-Z0-9._*~'()!]

즉, 자바에서는 빈칸을 ‘+‘로 바꾸고 자바스크립트는 빈칸을 ‘%20‘으로 변환,
그리고 자바스크립트에서는 ‘~'()!‘을 그대로 두지만 자바에서는 각각 UTF-8 인코딩시킨다.

결국 빈칸과 ~'()!을 변환시키면 URLEncoder의 결과를 encodeURIComponent의 결과와 동일하게 만들 수 있다.

 

public static String encodeURIComponent(String s)
  {
    String result = null;
 
    try
    {
      result = URLEncoder.encode(s, "UTF-8")
                         .replaceAll("\\+", "%20")
                         .replaceAll("\\%21", "!")
                         .replaceAll("\\%27", "'")
                         .replaceAll("\\%28", "(")
                         .replaceAll("\\%29", ")")
                         .replaceAll("\\%7E", "~");
    }
 
    // This exception should never occur.
    catch (UnsupportedEncodingException e)
    {
      result = s;
    }
 
    return result;
  }

안드로이드에서 개발한 앱과 서버간의 통신이 제대로 안되어 헤맸었는데 빈칸이 다르게 변환되는 것이 문제였다.

Java equivalent to JavaScript’s encodeURIComponent that produces identical output?

애플에게 삼성에게 사과하라고 했던 판사가 삼성을 위해 일하게 되다

images

영국 법원에서 애플에게 삼성이 아이패드의 디자인을 훔치지 않았다고 홈페이지에 게시하라고 했던 판사 Sir Robin Jacob을 삼성에서 고용했다고 한다.

UK judge that forced Apple to apologize to Samsung now works for… Samsung

법률 전문가들이니 문제는 없겠지만..

“integrity”(진실성)을 문제 삼았던 판사였다는 점에서 더 잡음을 일으키는 것 같다.

 

The Judge Who Forced Apple to Publicly Apologise to Samsung Now Works for… Samsung

Judge who accused Apple of ‘lack of integrity’ joins Samsung’s legal team (update)

UK judge who said Apple lacked integrity hired by Samsung [u]