1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21 package org.apache.amber.oauth2.ext.dynamicreg.server.request;
22
23 import java.util.Collections;
24 import java.util.Enumeration;
25 import java.util.HashMap;
26 import java.util.Map;
27 import javax.servlet.ServletInputStream;
28 import javax.servlet.ServletRequest;
29 import javax.servlet.http.HttpServletRequest;
30 import javax.servlet.http.HttpServletRequestWrapper;
31
32 import org.apache.amber.oauth2.common.OAuth;
33 import org.apache.amber.oauth2.common.exception.OAuthProblemException;
34 import org.apache.amber.oauth2.common.exception.OAuthRuntimeException;
35 import org.apache.amber.oauth2.common.utils.OAuthUtils;
36 import org.codehaus.jettison.json.JSONArray;
37 import org.codehaus.jettison.json.JSONException;
38 import org.codehaus.jettison.json.JSONObject;
39 import org.slf4j.Logger;
40 import org.slf4j.LoggerFactory;
41
42
43
44
45 public class JSONHttpServletRequestWrapper extends HttpServletRequestWrapper {
46 private Logger log = LoggerFactory.getLogger(JSONHttpServletRequestWrapper.class);
47 private JSONObject body;
48 private boolean bodyRead = false;
49
50 public JSONHttpServletRequestWrapper(HttpServletRequest request) {
51 super(request);
52 }
53
54 public String getParameter(String name) {
55 final String[] values = getParameterMap().get(name);
56 if (values == null || values.length == 0) {
57 return null;
58 }
59 return values[0];
60 }
61
62 public Map<String, String[]> getParameterMap() {
63 try {
64 readJsonBody();
65 Map<String, String[]> parameters = new HashMap<String, String[]>();
66
67 if (body != null) {
68 final JSONArray attributeNames = body.names();
69 for (int i = 0; i < attributeNames.length(); i++) {
70 final String attributeName = attributeNames.getString(i);
71 final String attributeValue = body.getString(attributeName);
72
73 parameters.put(attributeName, new String[] {attributeValue});
74 }
75 }
76
77 return Collections.unmodifiableMap(parameters);
78 } catch (JSONException e) {
79 log.error("Dynamic client registration error: ", e);
80 throw new OAuthRuntimeException("OAuth server error");
81 }
82 }
83
84 public Enumeration getParameterNames() {
85 return Collections.enumeration(getParameterMap().keySet());
86 }
87
88 public String[] getParameterValues(String name) {
89 return getParameterMap().get(name);
90 }
91
92
93
94
95
96
97 private void readJsonBody() {
98 if (!bodyRead) {
99 bodyRead = true;
100 try {
101 final ServletRequest request = getRequest();
102 String contentType = request.getContentType();
103 final String expectedContentType = OAuth.ContentType.JSON;
104 if (!OAuthUtils.hasContentType(contentType, expectedContentType)) {
105 return;
106 }
107
108 final ServletInputStream inputStream = request.getInputStream();
109 if (inputStream == null) {
110 return;
111 }
112 final String jsonString = OAuthUtils.saveStreamAsString(inputStream);
113 body = new JSONObject(jsonString);
114 } catch (JSONException e) {
115 log.error("Cannot decode request body as a JSON: ", e);
116 } catch (Exception e) {
117 log.error("Dynamic client registration error: ", e);
118 throw new OAuthRuntimeException("OAuth server error");
119 }
120 }
121 }
122 }