View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements.  See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache License, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License.  You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the License for the specific language governing permissions and
15   * limitations under the License.
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       * A replacement for HttpServletRequest.getParameter() as it will mess up with HTTP POST body
57       * @param request
58       * @param name
59       * @return
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                  // Ignore
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  }