1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
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
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
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 }