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.common.parameters;
23  
24  import java.util.LinkedHashMap;
25  import java.util.Map;
26  
27  import org.apache.amber.oauth2.common.OAuth;
28  import org.apache.amber.oauth2.common.error.OAuthError;
29  import org.apache.amber.oauth2.common.message.OAuthMessage;
30  import org.apache.amber.oauth2.common.utils.OAuthUtils;
31  
32  /**
33   *
34   *
35   *
36   */
37  public class QueryParameterApplier implements OAuthParametersApplier {
38  
39      public OAuthMessage applyOAuthParameters(OAuthMessage message, Map<String, Object> params) {
40  
41          String messageUrl = message.getLocationUri();
42          if (messageUrl != null) {
43              boolean containsQuestionMark = messageUrl.contains("?");
44              StringBuffer url = new StringBuffer(messageUrl);
45  
46              //apply uri fragment component if exist access_toke param
47              Map<String, Object> fragmentParams = new LinkedHashMap<String, Object>();
48              if (params.containsKey(OAuth.OAUTH_ACCESS_TOKEN)) {
49                  fragmentParams.put(OAuth.OAUTH_ACCESS_TOKEN, params.remove(OAuth.OAUTH_ACCESS_TOKEN));
50  
51                  // State should be in the fragment too
52                  if (params.containsKey(OAuth.OAUTH_STATE)) {
53                      fragmentParams.put(OAuth.OAUTH_STATE, params.remove(OAuth.OAUTH_STATE));
54                  }
55                  
56                  if (params.containsKey(OAuth.OAUTH_EXPIRES_IN)) {
57                      fragmentParams.put(OAuth.OAUTH_EXPIRES_IN, params.remove(OAuth.OAUTH_EXPIRES_IN));
58                  }
59                  
60                  if (params.containsKey(OAuth.OAUTH_TOKEN_TYPE)) {
61                      fragmentParams.put(OAuth.OAUTH_TOKEN_TYPE, params.remove(OAuth.OAUTH_TOKEN_TYPE));
62                  }
63                  
64                  if (params.containsKey(OAuth.OAUTH_SCOPE)) {
65                      fragmentParams.put(OAuth.OAUTH_SCOPE, params.remove(OAuth.OAUTH_SCOPE));
66                  }
67                  
68                  if (params.containsKey(OAuthError.OAUTH_ERROR)) {
69                      fragmentParams.put(OAuthError.OAUTH_ERROR, params.remove(OAuthError.OAUTH_ERROR));
70                  }
71                  
72                  if (params.containsKey(OAuthError.OAUTH_ERROR_DESCRIPTION)) {
73                      fragmentParams.put(OAuthError.OAUTH_ERROR_DESCRIPTION, params.remove(OAuthError.OAUTH_ERROR_DESCRIPTION));
74                  }
75                  
76                  if (params.containsKey(OAuthError.OAUTH_ERROR_URI)) {
77                      fragmentParams.put(OAuthError.OAUTH_ERROR_URI, params.remove(OAuthError.OAUTH_ERROR_URI));
78                  }
79                  
80              }
81  
82              StringBuffer query = new StringBuffer(OAuthUtils.format(params.entrySet(), "UTF-8"));
83              String fragmentQuery = "";
84              if (fragmentParams.containsKey(OAuth.OAUTH_ACCESS_TOKEN)) {
85                  fragmentQuery = OAuthUtils.format(fragmentParams.entrySet(), "UTF-8");
86              }
87  
88              if (!OAuthUtils.isEmpty(query.toString())) {
89                  if (containsQuestionMark) {
90                      url.append("&").append(query);
91                  } else {
92                      url.append("?").append(query);
93                  }
94              }
95  
96              if (!OAuthUtils.isEmpty(fragmentQuery)) {
97              	if (fragmentParams.size()>1){
98              		url.append("#").append(fragmentQuery);
99              	}else{
100             		if (containsQuestionMark) {
101                         url.append("&").append(fragmentQuery);
102                     } else {
103                         url.append("?").append(fragmentQuery);
104                     }
105             	}
106             }
107 
108             message.setLocationUri(url.toString());
109         }
110         return message;
111     }
112 }