1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.amber.oauth2.rs;
18
19 import java.io.UnsupportedEncodingException;
20 import java.net.URLDecoder;
21 import java.util.ArrayList;
22 import java.util.HashMap;
23 import java.util.List;
24 import java.util.Map;
25
26 import javax.servlet.http.HttpServletRequest;
27
28 import org.apache.amber.oauth2.common.exception.OAuthSystemException;
29 import org.apache.amber.oauth2.common.message.types.ParameterStyle;
30 import org.apache.amber.oauth2.common.utils.OAuthUtils;
31 import org.apache.amber.oauth2.common.validators.OAuthValidator;
32 import org.apache.amber.oauth2.rs.extractor.TokenExtractor;
33
34 public abstract class ResourceServer {
35
36 protected Map<ParameterStyle, Class> extractors = new HashMap<ParameterStyle, Class>();
37 protected Map<ParameterStyle, Class> validators = new HashMap<ParameterStyle, Class>();
38
39 public OAuthValidator instantiateValidator(ParameterStyle ps) throws OAuthSystemException {
40 Class clazz = validators.get(ps);
41 if (clazz == null) {
42 throw new OAuthSystemException("Cannot instantiate a message validator.");
43 }
44 return (OAuthValidator)OAuthUtils.instantiateClass(clazz);
45 }
46
47 public TokenExtractor instantiateExtractor(ParameterStyle ps) throws OAuthSystemException {
48 Class clazz = extractors.get(ps);
49 if (clazz == null) {
50 throw new OAuthSystemException("Cannot instantiate a token extractor.");
51 }
52 return (TokenExtractor)OAuthUtils.instantiateClass(clazz);
53 }
54
55
56
57
58
59
60
61 public static String[] getQueryParameterValues(HttpServletRequest request, String name) {
62 String query = request.getQueryString();
63 if (query == null) {
64 return null;
65 }
66 List<String> values = new ArrayList<String>();
67 String[] params = query.split("&");
68 for (String param : params) {
69 try {
70 param = URLDecoder.decode(param, "UTF-8");
71 } catch (UnsupportedEncodingException e) {
72
73 }
74 int index = param.indexOf('=');
75 String key = param;
76 String value = null;
77 if (index != -1) {
78 key = param.substring(0, index);
79 value = param.substring(index + 1);
80 }
81 if (key.equals(name)) {
82 values.add(value);
83 }
84 }
85 return values.toArray(new String[values.size()]);
86 }
87
88 public static String getQueryParameterValue(HttpServletRequest request, String name) {
89 String[] values = getQueryParameterValues(request, name);
90 if (values == null || values.length == 0) {
91 return null;
92 }
93 return values[0];
94 }
95 }