Fix UnicodeEncodeError when calling urlencode with a dict that contains accents to close #CMIS-995

git-svn-id: https://svn.apache.org/repos/asf/chemistry/cmislib/trunk@1759001 13f79535-47bb-0310-9956-ffa450edef68
diff --git a/src/cmislib/browser/binding.py b/src/cmislib/browser/binding.py
index 4feb124..9c4ffc1 100644
--- a/src/cmislib/browser/binding.py
+++ b/src/cmislib/browser/binding.py
@@ -364,7 +364,7 @@
 
         # invoke the URL
         result = self._cmisClient.binding.post(updateUrl.encode('utf-8'),
-                                               urlencode(props),
+                                               safe_urlencode(props),
                                                'application/x-www-form-urlencoded',
                                                self._cmisClient.username,
                                                self._cmisClient.password)
@@ -393,7 +393,7 @@
 
         # invoke the URL
         self._cmisClient.binding.post(moveUrl.encode('utf-8'),
-                                      urlencode(props),
+                                      safe_urlencode(props),
                                       'application/x-www-form-urlencoded',
                                       self._cmisClient.username,
                                       self._cmisClient.password)
@@ -421,7 +421,7 @@
 
         # invoke the URL
         self._cmisClient.binding.post(delUrl.encode('utf-8'),
-                                      urlencode(props),
+                                      safe_urlencode(props),
                                       'application/x-www-form-urlencoded',
                                       self._cmisClient.username,
                                       self._cmisClient.password,
@@ -1667,7 +1667,7 @@
 
         # invoke the URL
         result = self._cmisClient.binding.post(coUrl.encode('utf-8'),
-                                               urlencode(props),
+                                               safe_urlencode(props),
                                                'application/x-www-form-urlencoded',
                                                self._cmisClient.username,
                                                self._cmisClient.password)
@@ -1694,7 +1694,7 @@
 
         # invoke the URL
         self._cmisClient.binding.post(coUrl.encode('utf-8'),
-                                      urlencode(props),
+                                      safe_urlencode(props),
                                       'application/x-www-form-urlencoded',
                                       self._cmisClient.username,
                                       self._cmisClient.password)
@@ -1794,7 +1794,7 @@
 
         # invoke the URL
         result = self._cmisClient.binding.post(ciUrl.encode('utf-8'),
-                                               urlencode(props),
+                                               safe_urlencode(props),
                                                'application/x-www-form-urlencoded',
                                                self._cmisClient.username,
                                                self._cmisClient.password,
@@ -1945,7 +1945,7 @@
 
         # invoke the URL
         self._cmisClient.binding.post(delUrl.encode('utf-8'),
-                                      urlencode(props),
+                                      safe_urlencode(props),
                                       'application/x-www-form-urlencoded',
                                       self._cmisClient.username,
                                       self._cmisClient.password)
@@ -2063,7 +2063,7 @@
 
         # invoke the URL
         result = self._cmisClient.binding.post(createFolderUrl.encode('utf-8'),
-                                               urlencode(props),
+                                               safe_urlencode(props),
                                                'application/x-www-form-urlencoded',
                                                self._cmisClient.username,
                                                self._cmisClient.password,
@@ -2281,7 +2281,7 @@
 
         # invoke the URL
         self._cmisClient.binding.post(delUrl.encode('utf-8'),
-                                      urlencode(props),
+                                      safe_urlencode(props),
                                       'application/x-www-form-urlencoded',
                                       self._cmisClient.username,
                                       self._cmisClient.password,
@@ -2321,7 +2321,7 @@
 
         # invoke the URL
         result = self._cmisClient.binding.post(addUrl.encode('utf-8'),
-                                               urlencode(props),
+                                               safe_urlencode(props),
                                                'application/x-www-form-urlencoded',
                                                self._cmisClient.username,
                                                self._cmisClient.password,
@@ -2344,7 +2344,7 @@
 
         # invoke the URL
         result = self._cmisClient.binding.post(remUrl.encode('utf-8'),
-                                               urlencode(props),
+                                               safe_urlencode(props),
                                                'application/x-www-form-urlencoded',
                                                self._cmisClient.username,
                                                self._cmisClient.password)
@@ -3056,10 +3056,12 @@
 
 
 def setProps(properties, props, initialIndex=0):
+
     """
     Transform key, value from properties into props list items in the format
     expected by the HTTP POST request
     """
+
     i = initialIndex
     for key, val in properties.items():
         props["propertyId[%s]" % i] = key
@@ -3101,11 +3103,13 @@
 
 
 def encode_multipart_formdata(fields, contentFile, contentType):
+
     """
     fields is a sequence of (name, value) elements for regular form fields.
     files is a sequence of (name, filename, value) elements for data to be uploaded as files
     Return (content_type, body) ready for httplib.HTTP instance
     """
+
     boundary = 'aPacHeCheMIStrycMisLIb%s' % (int(time.time()))
     crlf = '\r\n'
     L = []
@@ -3135,6 +3139,27 @@
     return content_type, body
 
 
+def safe_urlencode(in_dict):
+
+    """
+    Safe encoding of values taking care of unicode values
+    urllib.urlencode doesn't like unicode values
+    """
+
+    def encoded_dict(in_dict):
+        out_dict = {}
+        for k, v in in_dict.iteritems():
+            if isinstance(v, unicode):
+                v = v.encode('utf8')
+            elif isinstance(v, str):
+                # Must be encoded in UTF-8
+                v.decode('utf8')
+            out_dict[k] = v
+        return out_dict
+
+    return urlencode(encoded_dict(in_dict))
+
+
 class ResultsSerializer(object):
 
     """