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.message;
23  
24  import java.util.HashMap;
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.exception.OAuthProblemException;
30  import org.apache.amber.oauth2.common.exception.OAuthSystemException;
31  import org.apache.amber.oauth2.common.parameters.BodyURLEncodedParametersApplier;
32  import org.apache.amber.oauth2.common.parameters.JSONBodyParametersApplier;
33  import org.apache.amber.oauth2.common.parameters.OAuthParametersApplier;
34  import org.apache.amber.oauth2.common.parameters.QueryParameterApplier;
35  import org.apache.amber.oauth2.common.parameters.WWWAuthHeaderParametersApplier;
36  
37  /**
38   *
39   *
40   *
41   */
42  public class OAuthResponse implements OAuthMessage {
43  
44      protected int responseStatus;
45      protected String uri;
46      protected String body;
47  
48      protected Map<String, String> headers = new HashMap<String, String>();
49  
50      protected OAuthResponse(String uri, int responseStatus) {
51          this.uri = uri;
52          this.responseStatus = responseStatus;
53      }
54  
55      public static OAuthResponseBuilder status(int code) {
56          return new OAuthResponseBuilder(code);
57      }
58  
59      public static OAuthErrorResponseBuilder errorResponse(int code) {
60          return new OAuthErrorResponseBuilder(code);
61      }
62  
63      @Override
64      public String getLocationUri() {
65          return uri;
66      }
67  
68      @Override
69      public void setLocationUri(String uri) {
70          this.uri = uri;
71      }
72  
73      @Override
74      public String getBody() {
75          return body;
76      }
77  
78      @Override
79      public void setBody(String body) {
80          this.body = body;
81      }
82  
83      @Override
84      public String getHeader(String name) {
85          return headers.get(name);
86      }
87  
88      @Override
89      public Map<String, String> getHeaders() {
90          return headers;
91      }
92  
93      @Override
94      public void setHeaders(Map<String, String> headers) {
95          this.headers = headers;
96      }
97  
98      public int getResponseStatus() {
99          return responseStatus;
100     }
101 
102     @Override
103     public void addHeader(String name, String header) {
104         headers.put(name, header);
105     }
106 
107     public static class OAuthResponseBuilder {
108 
109         protected OAuthParametersApplier applier;
110         protected Map<String, Object> parameters = new HashMap<String, Object>();
111         protected int responseCode;
112         protected String location;
113 
114         public OAuthResponseBuilder(int responseCode) {
115             this.responseCode = responseCode;
116         }
117 
118         public OAuthResponseBuilder location(String location) {
119             this.location = location;
120             return this;
121         }
122 
123         public OAuthResponseBuilder setScope(String value) {
124             this.parameters.put(OAuth.OAUTH_SCOPE, value);
125             return this;
126         }
127 
128         public OAuthResponseBuilder setParam(String key, String value) {
129             this.parameters.put(key, value);
130             return this;
131         }
132 
133         public OAuthResponse buildQueryMessage() throws OAuthSystemException {
134             OAuthResponse msg = new OAuthResponse(location, responseCode);
135             this.applier = new QueryParameterApplier();
136             return (OAuthResponse)applier.applyOAuthParameters(msg, parameters);
137         }
138 
139         public OAuthResponse buildBodyMessage() throws OAuthSystemException {
140             OAuthResponse msg = new OAuthResponse(location, responseCode);
141             this.applier = new BodyURLEncodedParametersApplier();
142             return (OAuthResponse)applier.applyOAuthParameters(msg, parameters);
143         }
144 
145         public OAuthResponse buildJSONMessage() throws OAuthSystemException {
146             OAuthResponse msg = new OAuthResponse(location, responseCode);
147             this.applier = new JSONBodyParametersApplier();
148             return (OAuthResponse)applier.applyOAuthParameters(msg, parameters);
149         }
150 
151         public OAuthResponse buildHeaderMessage() throws OAuthSystemException {
152             OAuthResponse msg = new OAuthResponse(location, responseCode);
153             this.applier = new WWWAuthHeaderParametersApplier();
154             return (OAuthResponse)applier.applyOAuthParameters(msg, parameters);
155         }
156     }
157 
158     public static class OAuthErrorResponseBuilder extends OAuthResponseBuilder {
159 
160         public OAuthErrorResponseBuilder(int responseCode) {
161             super(responseCode);
162         }
163 
164         public OAuthErrorResponseBuilder error(OAuthProblemException ex) {
165             this.parameters.put(OAuthError.OAUTH_ERROR, ex.getError());
166             this.parameters.put(OAuthError.OAUTH_ERROR_DESCRIPTION, ex.getDescription());
167             this.parameters.put(OAuthError.OAUTH_ERROR_URI, ex.getUri());
168             this.parameters.put(OAuth.OAUTH_STATE, ex.getState());
169             return this;
170         }
171 
172         public OAuthErrorResponseBuilder setError(String error) {
173             this.parameters.put(OAuthError.OAUTH_ERROR, error);
174             return this;
175         }
176 
177         public OAuthErrorResponseBuilder setErrorDescription(String desc) {
178             this.parameters.put(OAuthError.OAUTH_ERROR_DESCRIPTION, desc);
179             return this;
180         }
181 
182         public OAuthErrorResponseBuilder setErrorUri(String state) {
183             this.parameters.put(OAuthError.OAUTH_ERROR_URI, state);
184             return this;
185         }
186 
187         public OAuthErrorResponseBuilder setState(String state) {
188             this.parameters.put(OAuth.OAUTH_STATE, state);
189             return this;
190         }
191 
192         public OAuthErrorResponseBuilder setRealm(String realm) {
193             this.parameters.put(OAuth.WWWAuthHeader.REALM, realm);
194             return this;
195         }
196 
197         public OAuthErrorResponseBuilder location(String location) {
198             this.location = location;
199             return this;
200         }
201     }
202 
203 }