View Javadoc

1   /**
2    *       Copyright 2011 Newcastle University
3    *
4    *          http://research.ncl.ac.uk/smart/
5    *
6    * Licensed to the Apache Software Foundation (ASF) under one or more
7    * contributor license agreements.  See the NOTICE file distributed with
8    * this work for additional information regarding copyright ownership.
9    * The ASF licenses this file to You under the Apache License, Version 2.0
10   * (the "License"); you may not use this file except in compliance with
11   * the License.  You may obtain a copy of the License at
12   *
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   *
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
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       * Lazily read JSON from request
94       *
95       * @throws OAuthProblemException
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 }