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.exception;
23  
24  import java.util.HashMap;
25  import java.util.Map;
26  
27  import org.apache.amber.oauth2.common.utils.OAuthUtils;
28  
29  /**
30   *
31   *
32   *
33   */
34  public final class OAuthProblemException extends Exception {
35  
36      private String error;
37      private String description;
38      private String uri;
39      private String state;
40      private String scope;
41      private String redirectUri;
42  
43      private int responseStatus;
44  
45      private Map<String, String> parameters = new HashMap<String, String>();
46  
47      private OAuthProblemException(String error) {
48          this(error, "");
49      }
50  
51      private OAuthProblemException(String error, String description) {
52          super(error + " " + description);
53          this.description = description;
54          this.error = error;
55      }
56  
57  
58      public static OAuthProblemException error(String error) {
59          return new OAuthProblemException(error);
60      }
61  
62      public static OAuthProblemException error(String error, String description) {
63          return new OAuthProblemException(error, description);
64      }
65  
66      public OAuthProblemException description(String description) {
67          this.description = description;
68          return this;
69      }
70  
71      public OAuthProblemException uri(String uri) {
72          this.uri = uri;
73          return this;
74      }
75  
76      public OAuthProblemException state(String state) {
77          this.state = state;
78          return this;
79      }
80  
81      public OAuthProblemException scope(String scope) {
82          this.scope = scope;
83          return this;
84      }
85  
86      public OAuthProblemException responseStatus(int responseStatus) {
87          this.responseStatus = responseStatus;
88          return this;
89      }
90  
91      public OAuthProblemException setParameter(String name, String value) {
92          parameters.put(name, value);
93          return this;
94      }
95  
96      public String getError() {
97          return error;
98      }
99  
100     public String getDescription() {
101         return description;
102     }
103 
104     public String getUri() {
105         return uri;
106     }
107 
108     public String getState() {
109         return state;
110     }
111 
112     public String getScope() {
113         return scope;
114     }
115 
116     public int getResponseStatus() {
117         return responseStatus == 0 ? 400 : responseStatus;
118     }
119 
120     public String get(String name) {
121         return parameters.get(name);
122     }
123 
124     public Map<String, String> getParameters() {
125         return parameters;
126     }
127 
128     public String getRedirectUri() {
129         return redirectUri;
130     }
131 
132     public void setRedirectUri(String redirectUri) {
133         this.redirectUri = redirectUri;
134     }
135 
136     @Override
137     public String getMessage() {
138         StringBuffer b = new StringBuffer();
139         if (!OAuthUtils.isEmpty(error)) {
140             b.append(error);
141         }
142 
143         if (!OAuthUtils.isEmpty(description)) {
144             b.append(", ").append(description);
145         }
146 
147 
148         if (!OAuthUtils.isEmpty(uri)) {
149             b.append(", ").append(uri);
150         }
151 
152 
153         if (!OAuthUtils.isEmpty(state)) {
154             b.append(", ").append(state);
155         }
156 
157         if (!OAuthUtils.isEmpty(scope)) {
158             b.append(", ").append(scope);
159         }
160 
161         return b.toString();
162     }
163 
164     @Override
165     public String toString() {
166         return "OAuthProblemException{"
167             + "description='" + description + '\''
168             + ", error='" + error + '\''
169             + ", uri='" + uri + '\''
170             + ", state='" + state + '\''
171             + ", scope='" + scope + '\''
172             + '}';
173     }
174 }