OSDN Git Service

Change authentication from userid/password to oauth
[neighbornote/NeighborNote.git] / src / cx / fbn / nevernote / oauth / OAuthTokenizer.java
1
2 /*
3   * This file is part of NixNote 
4  * Copyright 2012 Randy Baumgarte
5  * 
6  * This file may be licensed under the terms of of the
7  * GNU General Public License Version 2 (the ``GPL'').
8  *
9  * Software distributed under the License is distributed
10  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
11  * express or implied. See the GPL for the specific language
12  * governing rights and limitations.
13  *
14  * You should have received a copy of the GPL along with this
15  * program. If not, go to http://www.gnu.org/licenses/gpl.html
16  * or write to the Free Software Foundation, Inc.,
17  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  *
19 */
20
21
22
23 /* This class is used to parse out the OAuth reply from Evernote */
24
25 package cx.fbn.nevernote.oauth;
26
27 import java.util.ArrayList;
28
29 public class OAuthTokenizer {
30
31           public String oauth_token;
32           public String edam_shard;
33           public String edam_userId;
34           public String edam_expires;
35           public String edam_noteStoreUrl;
36           public String edam_webApiUrlPrefix;
37           
38           public OAuthTokenizer() {
39                   oauth_token = new String();
40                   edam_shard = new String();
41                   edam_userId = new String();
42                   edam_expires = new String();
43                   edam_noteStoreUrl = new String();
44                   edam_webApiUrlPrefix = new String();
45           }
46         
47           public void tokenize(String decoded) {
48                 ArrayList<String> tokens = new ArrayList<String>();
49                 for (;decoded!=null && decoded.length()>0;) {
50                         int i=decoded.indexOf("&");
51                         if (i>0) {
52                                 tokens.add(decoded.substring(0,i));
53                                 decoded=decoded.substring(i+1);
54                         } else {
55                                 tokens.add(decoded);
56                                 decoded="";
57                         }
58                 }
59                 System.out.println("Tokens found:" +tokens.size());
60                 String oauth_tokenString = "oauth_token=";
61                 String edam_shardString = "edam_shard=";
62                 String edam_userIdString = "edam_userid=";
63                 String edam_expiresString = "edam_expires=";
64                 String edam_noteStoreUrlString ="edam_notestoreurl=";
65                 String edam_webApiUrlPrefixString = "edam_webapiurlprefix=";
66                 oauth_token = "";
67                 edam_shard = "";
68                 edam_userId = "";
69                 edam_expires = "";
70                 edam_noteStoreUrl = "";
71                 edam_webApiUrlPrefix = "";
72                 
73                 for (int i=0; i<tokens.size(); i++) {
74                         String token = tokens.get(i);
75                         if (token.toLowerCase().startsWith(oauth_tokenString)) {
76                                 oauth_token = token.substring(oauth_tokenString.length());
77                         }
78                         if (token.toLowerCase().startsWith(edam_shardString)) {
79                                 edam_shard = token.substring(edam_shardString.length());
80                         }
81                         if (token.toLowerCase().startsWith(edam_userIdString)) {
82                                 edam_userId = token.substring(edam_userIdString.length());
83                         }
84                         if (token.toLowerCase().startsWith(edam_expiresString)) {
85                                 edam_expires = token.substring(edam_expiresString.length());
86                         }
87                         if (token.toLowerCase().startsWith(edam_noteStoreUrlString)) {
88                                 edam_noteStoreUrl = token.substring(edam_noteStoreUrlString.length());
89                         }
90                         if (token.toLowerCase().startsWith(edam_webApiUrlPrefixString)) {
91                                 edam_webApiUrlPrefix = token.substring(edam_webApiUrlPrefixString.length());
92                         }
93                 }
94
95         }
96 }