1/*
2 * Copyright 2007 Netflix, Inc.
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package net.oauth;
18
19import java.util.HashMap;
20import java.util.Map;
21import net.oauth.http.HttpMessage;
22import net.oauth.http.HttpResponseMessage;
23
24/**
25 * Describes an OAuth-related problem, using a set of named parameters. One
26 * parameter identifies the basic problem, and the others provide supplementary
27 * diagnostic information. This can be used to capture information from a
28 * response that conforms to the OAuth <a
29 * href="http://wiki.oauth.net/ProblemReporting">Problem Reporting
30 * extension</a>.
31 *
32 * @author John Kristian
33 * @hide
34 */
35public class OAuthProblemException extends OAuthException {
36
37    public static final String OAUTH_PROBLEM = "oauth_problem";
38
39    public OAuthProblemException() {
40    }
41
42    public OAuthProblemException(String problem) {
43        super(problem);
44        if (problem != null) {
45            parameters.put(OAUTH_PROBLEM, problem);
46        }
47    }
48
49    private final Map<String, Object> parameters = new HashMap<String, Object>();
50
51    @Override
52    public String getMessage() {
53        String msg = super.getMessage();
54        if (msg != null)
55            return msg;
56        msg = getProblem();
57        if (msg != null)
58            return msg;
59        Object response = getParameters().get(HttpMessage.RESPONSE);
60        if (response != null) {
61            msg = response.toString();
62            int eol = msg.indexOf("\n");
63            if (eol < 0) {
64                eol = msg.indexOf("\r");
65            }
66            if (eol >= 0) {
67                msg = msg.substring(0, eol);
68            }
69            msg = msg.trim();
70            if (msg.length() > 0) {
71                return msg;
72            }
73        }
74        response = getHttpStatusCode();
75        if (response != null) {
76            return HttpResponseMessage.STATUS_CODE + " " + response;
77        }
78        return null;
79    }
80
81    public void setParameter(String name, Object value) {
82        getParameters().put(name, value);
83    }
84
85    public Map<String, Object> getParameters() {
86        return parameters;
87    }
88
89    public String getProblem() {
90        return (String) getParameters().get(OAUTH_PROBLEM);
91    }
92
93    public int getHttpStatusCode() {
94        Object code = getParameters().get(HttpResponseMessage.STATUS_CODE);
95        if (code == null) {
96            return 200;
97        } else if (code instanceof Number) { // the usual case
98            return ((Number) code).intValue();
99        } else {
100            return Integer.parseInt(code.toString());
101        }
102    }
103
104    private static final long serialVersionUID = 1L;
105
106}
107