001/* 002 * Copyright (c) 2012, the Last.fm Java Project and Committers 003 * All rights reserved. 004 * 005 * Redistribution and use of this software in source and binary forms, with or without modification, are 006 * permitted provided that the following conditions are met: 007 * 008 * - Redistributions of source code must retain the above 009 * copyright notice, this list of conditions and the 010 * following disclaimer. 011 * 012 * - Redistributions in binary form must reproduce the above 013 * copyright notice, this list of conditions and the 014 * following disclaimer in the documentation and/or other 015 * materials provided with the distribution. 016 * 017 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED 018 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A 019 * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR 020 * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 021 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR 023 * TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 024 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 025 */ 026 027package de.umass.lastfm; 028 029import de.umass.xml.DomElement; 030 031/** 032 * Contains Session data relevant for making API calls which require authentication. 033 * A <code>Session</code> instance is passed to all methods requiring previous authentication. 034 * 035 * @author Janni Kovacs 036 * @see de.umass.lastfm.Authenticator 037 */ 038public class Session { 039 040 private String apiKey; 041 private String secret; 042 private String username; 043 private String key; 044 private boolean subscriber; 045 046 private Session() { 047 } 048 049 /** 050 * Restores a Session instance with the given session key. 051 * 052 * @param apiKey An api key 053 * @param secret A secret 054 * @param sessionKey The previously obtained session key 055 * @return a Session instance 056 */ 057 public static Session createSession(String apiKey, String secret, String sessionKey) { 058 return createSession(apiKey, secret, sessionKey, null, false); 059 } 060 061 /** 062 * Restores a Session instance with the given session key. 063 * 064 * @param apiKey An api key 065 * @param secret A secret 066 * @param sessionKey The previously obtained session key 067 * @param username A Last.fm username 068 * @param subscriber Subscriber status 069 * @return a Session instance 070 */ 071 public static Session createSession(String apiKey, String secret, String sessionKey, String username, 072 boolean subscriber) { 073 Session s = new Session(); 074 s.apiKey = apiKey; 075 s.secret = secret; 076 s.key = sessionKey; 077 s.username = username; 078 s.subscriber = subscriber; 079 return s; 080 } 081 082 public String getSecret() { 083 return secret; 084 } 085 086 public String getApiKey() { 087 return apiKey; 088 } 089 090 public String getKey() { 091 return key; 092 } 093 094 public boolean isSubscriber() { 095 return subscriber; 096 } 097 098 public String getUsername() { 099 return username; 100 } 101 102 @Override 103 public String toString() { 104 return "Session[" + 105 "apiKey=" + apiKey + 106 ", secret=" + secret + 107 ", username=" + username + 108 ", key=" + key + 109 ", subscriber=" + subscriber + 110 ']'; 111 } 112 113 static Session sessionFromElement(DomElement element, String apiKey, String secret) { 114 if (element == null) 115 return null; 116 String user = element.getChildText("name"); 117 String key = element.getChildText("key"); 118 boolean subsc = element.getChildText("subscriber").equals("1"); 119 return createSession(apiKey, secret, key, user, subsc); 120 } 121}