add weibo src
[freespace.git] / src / weibo4android / http / OAuthToken.java
blob51b9b4ac0575a9f33590c7b2bc13cf9739452385
1 /*
2 Copyright (c) 2007-2009, Yusuke Yamamoto
3 All rights reserved.
5 Redistribution and use in source and binary forms, with or without
6 modification, are permitted provided that the following conditions are met:
7 * Redistributions of source code must retain the above copyright
8 notice, this list of conditions and the following disclaimer.
9 * Redistributions in binary form must reproduce the above copyright
10 notice, this list of conditions and the following disclaimer in the
11 documentation and/or other materials provided with the distribution.
12 * Neither the name of the Yusuke Yamamoto nor the
13 names of its contributors may be used to endorse or promote products
14 derived from this software without specific prior written permission.
16 THIS SOFTWARE IS PROVIDED BY Yusuke Yamamoto ``AS IS'' AND ANY
17 EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
18 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
19 DISCLAIMED. IN NO EVENT SHALL Yusuke Yamamoto BE LIABLE FOR ANY
20 DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
21 (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
22 LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
23 ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
24 (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
25 SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 package weibo4android.http;
29 import weibo4android.WeiboException;
31 import javax.crypto.spec.SecretKeySpec;
33 import android.util.Log;
35 import java.io.Serializable;
37 abstract class OAuthToken implements Serializable {
39 /**
42 private static final long serialVersionUID = 2385887178385032767L;
44 private String token;
45 private String tokenSecret;
46 private transient SecretKeySpec secretKeySpec;
47 String[] responseStr = null;
49 public OAuthToken(String token, String tokenSecret) {
50 this.token = token;
51 this.tokenSecret = tokenSecret;
54 OAuthToken(Response response) throws WeiboException {
55 this(response.asString());
57 OAuthToken(String string) {
58 responseStr = string.split("&");
59 tokenSecret = getParameter("oauth_token_secret");
60 token = getParameter("oauth_token");
63 public String getToken() {
64 return token;
67 public String getTokenSecret() {
68 return tokenSecret;
71 /*package*/ void setSecretKeySpec(SecretKeySpec secretKeySpec) {
72 this.secretKeySpec = secretKeySpec;
75 /*package*/ SecretKeySpec getSecretKeySpec() {
76 return secretKeySpec;
79 public String getParameter(String parameter) {
80 String value = null;
81 for (String str : responseStr) {
82 if (str.startsWith(parameter+'=')) {
83 value = str.split("=")[1].trim();
84 break;
87 return value;
90 @Override
91 public boolean equals(Object o) {
92 if (this == o) return true;
93 if (!(o instanceof OAuthToken)) return false;
95 OAuthToken that = (OAuthToken) o;
97 if (secretKeySpec != null ? !secretKeySpec.equals(that.secretKeySpec) : that.secretKeySpec != null)
98 return false;
99 if (!token.equals(that.token)) return false;
100 if (!tokenSecret.equals(that.tokenSecret)) return false;
102 return true;
105 @Override
106 public int hashCode() {
107 int result = token.hashCode();
108 result = 31 * result + tokenSecret.hashCode();
109 result = 31 * result + (secretKeySpec != null ? secretKeySpec.hashCode() : 0);
110 return result;
113 @Override
114 public String toString() {
115 return "OAuthToken{" +
116 "token='" + token + '\'' +
117 ", tokenSecret='" + tokenSecret + '\'' +
118 ", secretKeySpec=" + secretKeySpec +
119 '}';