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.io.IOException;
20import java.io.Serializable;
21import java.net.URISyntaxException;
22import java.util.Collection;
23import java.util.HashMap;
24import java.util.Map;
25import net.oauth.http.HttpMessage;
26
27/**
28 * Properties of one User of an OAuthConsumer. Properties may be added freely,
29 * e.g. to support extensions.
30 *
31 * @author John Kristian
32 * @hide
33 */
34public class OAuthAccessor implements Serializable {
35
36    private static final long serialVersionUID = 5590788443138352999L;
37
38    public final OAuthConsumer consumer;
39    public String requestToken;
40    public String accessToken;
41    public String tokenSecret;
42
43    public OAuthAccessor(OAuthConsumer consumer) {
44        this.consumer = consumer;
45        this.requestToken = null;
46        this.accessToken = null;
47        this.tokenSecret = null;
48    }
49
50    private final Map<String, Object> properties = new HashMap<String, Object>();
51
52    public Object getProperty(String name) {
53        return properties.get(name);
54    }
55
56    public void setProperty(String name, Object value) {
57        properties.put(name, value);
58    }
59
60    /**
61     * Construct a request message containing the given parameters but no body.
62     * Don't send the message, merely construct it. The caller will ordinarily
63     * send it, for example by calling OAuthClient.invoke.
64     *
65     * @param method
66     *            the HTTP request method. If this is null, use the default
67     *            method; that is getProperty("httpMethod") or (if that's null)
68     *            consumer.getProperty("httpMethod") or (if that's null)
69     *            OAuthMessage.GET.
70     */
71    public OAuthMessage newRequestMessage(String method, String url,
72            Collection<? extends Map.Entry> parameters)
73    throws OAuthException, IOException, URISyntaxException {
74        if (method == null) {
75            method = (String) this.getProperty("httpMethod");
76            if (method == null) {
77                method = (String) this.consumer.getProperty("httpMethod");
78                if (method == null) {
79                    method = OAuthMessage.GET;
80                }
81            }
82        }
83        OAuthMessage message = new OAuthMessage(method, url, parameters);
84        message.addRequiredParameters(this);
85        Object accepted = consumer.getProperty(OAuthConsumer.ACCEPT_ENCODING);
86        if (accepted != null) {
87            message.getHeaders().add(new OAuth.Parameter(HttpMessage.ACCEPT_ENCODING, accepted.toString()));
88        }
89        return message;
90    }
91
92}
93