1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.springframework.security.oauth.common;
18
19 import org.apache.commons.codec.DecoderException;
20 import org.apache.commons.codec.net.URLCodec;
21
22 import java.io.UnsupportedEncodingException;
23 import java.util.BitSet;
24
25
26
27
28
29
30 public class OAuthCodec extends URLCodec {
31
32 protected static final BitSet SAFE_CHARACTERS = (BitSet) URLCodec.WWW_FORM_URL.clone();
33 static {
34
35 SAFE_CHARACTERS.clear('*');
36 SAFE_CHARACTERS.clear(' ');
37 SAFE_CHARACTERS.set('~');
38 }
39
40
41
42
43 private OAuthCodec() {
44 }
45
46
47
48
49
50
51
52 public static String oauthEncode(String value) {
53 if (value == null) {
54 return "";
55 }
56
57 try {
58 return new String(URLCodec.encodeUrl(SAFE_CHARACTERS, value.getBytes("UTF-8")), "US-ASCII");
59 }
60 catch (UnsupportedEncodingException e) {
61 throw new RuntimeException(e);
62 }
63 }
64
65
66
67
68
69
70
71
72 public static String oauthDecode(String value) throws DecoderException {
73 if (value == null) {
74 return "";
75 }
76
77 try {
78 return new String(URLCodec.decodeUrl(value.getBytes("US-ASCII")), "UTF-8");
79 }
80 catch (UnsupportedEncodingException e) {
81 throw new RuntimeException(e);
82 }
83 }
84
85 }