View Javadoc

1   /**
2    *       Copyright 2010 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  
22  package org.apache.amber.oauth2.client.response;
23  
24  import java.util.Map;
25  
26  import javax.servlet.http.HttpServletRequest;
27  
28  import org.apache.amber.oauth2.client.validator.CodeTokenValidator;
29  import org.apache.amber.oauth2.client.validator.CodeValidator;
30  import org.apache.amber.oauth2.client.validator.OAuthClientValidator;
31  import org.apache.amber.oauth2.client.validator.TokenValidator;
32  import org.apache.amber.oauth2.common.OAuth;
33  import org.apache.amber.oauth2.common.exception.OAuthProblemException;
34  import org.apache.amber.oauth2.common.utils.OAuthUtils;
35  
36  
37  /**
38   *
39   *
40   *
41   */
42  public class OAuthAuthzResponse extends OAuthClientResponse {
43  
44      private HttpServletRequest request;
45  
46      protected OAuthAuthzResponse(HttpServletRequest request, OAuthClientValidator validator) {
47          this.request = request;
48          Map<String, String[]> params = request.getParameterMap();
49          for (Map.Entry<String, String[]> entry : params.entrySet()) {
50              String key = entry.getKey();
51              String[] values = entry.getValue();
52              if (!OAuthUtils.hasEmptyValues(values)) {
53                  parameters.put(key, values[0]);
54              }
55          }
56          this.validator = validator;
57      }
58  
59      public static OAuthAuthzResponse oauthCodeAuthzResponse(HttpServletRequest request)
60          throws OAuthProblemException {
61          OAuthAuthzResponse response = new OAuthAuthzResponse(request, new CodeValidator());
62          response.validate();
63          return response;
64      }
65  
66      public static OAuthAuthzResponse oAuthCodeAndTokenAuthzResponse(HttpServletRequest request)
67          throws OAuthProblemException {
68          OAuthAuthzResponse response = new OAuthAuthzResponse(request, new CodeTokenValidator());
69          response.validate();
70          return response;
71      }
72  
73      public static OAuthAuthzResponse oauthTokenAuthzResponse(HttpServletRequest request)
74          throws OAuthProblemException {
75          OAuthAuthzResponse response = new OAuthAuthzResponse(request, new TokenValidator());
76          response.validate();
77          return response;
78      }
79  
80      public String getAccessToken() {
81          return getParam(OAuth.OAUTH_ACCESS_TOKEN);
82      }
83  
84      public Long getExpiresIn() {
85          String value = getParam(OAuth.OAUTH_EXPIRES_IN);
86          return value == null? null: Long.valueOf(value);
87      }
88  
89      public String getScope() {
90          return getParam(OAuth.OAUTH_SCOPE);
91      }
92  
93      public String getCode() {
94          return getParam(OAuth.OAUTH_CODE);
95      }
96  
97      public String getState() {
98          return getParam(OAuth.OAUTH_STATE);
99      }
100 
101     public HttpServletRequest getRequest() {
102         return request;
103     }
104 
105     protected void setBody(String body) {
106         this.body = body;
107     }
108 
109     protected void setContentType(String contentType) {
110         this.contentType = contentType;
111     }
112 
113     protected void setResponseCode(int responseCode) {
114         this.responseCode = responseCode;
115     }
116 
117 }