forked from artem-zinnatullin/android-wail-app
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
switched login to RestTemplate (XML) + GreenRobot EventBus, refs arte…
- Loading branch information
1 parent
379042f
commit 22227fc
Showing
19 changed files
with
525 additions
and
173 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
wail-app/src/main/java/com/artemzin/android/wail/api/lastfm/CustomMessageConverter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package com.artemzin.android.wail.api.lastfm; | ||
|
||
import org.springframework.http.HttpInputMessage; | ||
import org.springframework.http.HttpOutputMessage; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.converter.FormHttpMessageConverter; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.converter.HttpMessageNotReadableException; | ||
import org.springframework.http.converter.HttpMessageNotWritableException; | ||
import org.springframework.http.converter.xml.SimpleXmlHttpMessageConverter; | ||
import org.springframework.util.MultiValueMap; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.List; | ||
|
||
/** | ||
* @author Ilya Murzinov [murz42@gmail.com] | ||
*/ | ||
class CustomMessageConverter implements HttpMessageConverter<Object> { | ||
private HttpMessageConverter<Object> inputConverter; | ||
private HttpMessageConverter<MultiValueMap<String, ?>> outputConverter; | ||
|
||
CustomMessageConverter() { | ||
inputConverter = new SimpleXmlHttpMessageConverter(); | ||
outputConverter = new FormHttpMessageConverter(); | ||
} | ||
|
||
@Override | ||
public boolean canRead(Class<?> clazz, MediaType mediaType) { | ||
return inputConverter.canRead(clazz, mediaType); | ||
} | ||
|
||
@Override | ||
public boolean canWrite(Class<?> clazz, MediaType mediaType) { | ||
return outputConverter.canWrite(clazz, mediaType); | ||
} | ||
|
||
@Override | ||
public List<MediaType> getSupportedMediaTypes() { | ||
List<MediaType> supportedMediaTypes = new ArrayList<>(inputConverter.getSupportedMediaTypes()); | ||
supportedMediaTypes.addAll(outputConverter.getSupportedMediaTypes()); | ||
return supportedMediaTypes; | ||
} | ||
|
||
@Override | ||
public Object read(Class<?> clazz, HttpInputMessage inputMessage) throws IOException, HttpMessageNotReadableException { | ||
return inputConverter.read(clazz, inputMessage); | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void write(Object o, MediaType contentType, HttpOutputMessage outputMessage) throws IOException, HttpMessageNotWritableException { | ||
if (!(o instanceof MultiValueMap)) { | ||
throw new HttpMessageNotWritableException("Can't write object " + o); | ||
} | ||
outputConverter.write((MultiValueMap<String, ?>) o, contentType, outputMessage); | ||
} | ||
} |
23 changes: 0 additions & 23 deletions
23
wail-app/src/main/java/com/artemzin/android/wail/api/lastfm/LFAuthApi.java
This file was deleted.
Oops, something went wrong.
157 changes: 157 additions & 0 deletions
157
wail-app/src/main/java/com/artemzin/android/wail/api/lastfm/LastFmClient.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,157 @@ | ||
package com.artemzin.android.wail.api.lastfm; | ||
|
||
import com.artemzin.android.wail.api.MD5Hash; | ||
import com.artemzin.android.wail.api.lastfm.model.LastFmSession; | ||
import com.artemzin.android.wail.events.InvalidUsernamePasswordEvent; | ||
import com.artemzin.android.wail.events.NetworkErrorEvent; | ||
import com.artemzin.android.wail.events.ServiceUnavailableEvent; | ||
import com.artemzin.android.wail.events.UnknownLoginErrorEvent; | ||
import com.artemzin.android.wail.storage.WAILSettings; | ||
|
||
import org.simpleframework.xml.Attribute; | ||
import org.simpleframework.xml.Element; | ||
import org.simpleframework.xml.Root; | ||
import org.springframework.http.HttpRequest; | ||
import org.springframework.http.HttpStatus; | ||
import org.springframework.http.MediaType; | ||
import org.springframework.http.client.ClientHttpRequestExecution; | ||
import org.springframework.http.client.ClientHttpRequestInterceptor; | ||
import org.springframework.http.client.ClientHttpResponse; | ||
import org.springframework.http.converter.HttpMessageConverter; | ||
import org.springframework.http.converter.xml.SimpleXmlHttpMessageConverter; | ||
import org.springframework.util.LinkedMultiValueMap; | ||
import org.springframework.util.MultiValueMap; | ||
import org.springframework.web.client.ResponseErrorHandler; | ||
import org.springframework.web.client.RestClientException; | ||
import org.springframework.web.client.RestTemplate; | ||
|
||
import java.io.IOException; | ||
import java.util.ArrayList; | ||
import java.util.Collections; | ||
import java.util.Comparator; | ||
import java.util.List; | ||
import java.util.Map; | ||
|
||
import de.greenrobot.event.EventBus; | ||
|
||
/** | ||
* @author Ilya Murzinov | ||
*/ | ||
public class LastFmClient { | ||
public static final String API_ROOT_URL = "https://ws.audioscrobbler.com/2.0/"; | ||
|
||
public static final String PARAM_API_KEY = "api_key"; | ||
public static final String PARAM_API_SIG = "api_sig"; | ||
public static final String PARAM_SK = "sk"; | ||
|
||
public static final int INVALID_USERNAME_PASSWORD = 4; | ||
public static final int SERVICE_UNAVAILABLE = 11; | ||
|
||
private final RestTemplate restTemplate; | ||
|
||
public LastFmClient() { | ||
restTemplate = new RestTemplate(); | ||
|
||
restTemplate.setMessageConverters( | ||
Collections.<HttpMessageConverter<?>>singletonList(new CustomMessageConverter()) | ||
); | ||
restTemplate.setInterceptors(Collections.<ClientHttpRequestInterceptor>singletonList(new ClientHttpRequestInterceptor() { | ||
@Override | ||
public ClientHttpResponse intercept(HttpRequest request, byte[] body, ClientHttpRequestExecution execution) throws IOException { | ||
request.getHeaders().setContentType(MediaType.APPLICATION_FORM_URLENCODED); | ||
return execution.execute(request, body); | ||
} | ||
})); | ||
restTemplate.setErrorHandler(new LastFmResponseErrorHandler()); | ||
} | ||
|
||
public LastFmSession authenticate(String username, String password) { | ||
MultiValueMap<String, String> request = new LinkedMultiValueMap<>(); | ||
request.add("method", "auth.getMobileSession"); | ||
request.add("username", username); | ||
request.add("password", password); | ||
try { | ||
return restTemplate.postForObject( | ||
API_ROOT_URL, | ||
generateSignedRequest(request), | ||
LastFmSession.class | ||
); | ||
} catch (RestClientException e) { | ||
EventBus.getDefault().post(new NetworkErrorEvent()); | ||
throw e; | ||
} | ||
} | ||
|
||
private MultiValueMap<String, String> generateSignedRequest(MultiValueMap<String, String> request) { | ||
MultiValueMap<String, String> result = new LinkedMultiValueMap<>(request); | ||
|
||
result.add(PARAM_API_KEY, WAILSettings.getLastfmApiKey()); | ||
|
||
StringBuilder stringBuilder = new StringBuilder(); | ||
List<Map.Entry<String, List<String>>> entrySet = new ArrayList<>(result.entrySet()); | ||
Collections.sort(entrySet, new Comparator<Map.Entry<String, List<String>>>() { | ||
@Override | ||
public int compare(Map.Entry<String, List<String>> lhs, Map.Entry<String, List<String>> rhs) { | ||
return String.CASE_INSENSITIVE_ORDER.compare(lhs.getKey(), rhs.getKey()); | ||
} | ||
}); | ||
for (Map.Entry<String, List<String>> entry : entrySet) { | ||
stringBuilder.append(entry.getKey()).append(entry.getValue().get(0)); | ||
} | ||
stringBuilder.append(WAILSettings.getLastfmSecret()); | ||
result.add(PARAM_API_SIG, MD5Hash.calculateMD5(stringBuilder.toString())); | ||
return result; | ||
} | ||
|
||
private static class LastFmResponseErrorHandler implements ResponseErrorHandler { | ||
private ErrorResponse errorResponse; | ||
|
||
@Override | ||
public boolean hasError(ClientHttpResponse response) throws IOException { | ||
if (response.getStatusCode() != HttpStatus.OK) { | ||
errorResponse = (ErrorResponse) new SimpleXmlHttpMessageConverter().read(ErrorResponse.class, response); | ||
return true; | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public void handleError(ClientHttpResponse response) throws IOException { | ||
if (errorResponse.getError().getCode() == INVALID_USERNAME_PASSWORD) { | ||
EventBus.getDefault().post(new InvalidUsernamePasswordEvent()); | ||
} else if (errorResponse.getError().getCode() == SERVICE_UNAVAILABLE) { | ||
EventBus.getDefault().post(new ServiceUnavailableEvent()); | ||
} else { | ||
EventBus.getDefault().post(new UnknownLoginErrorEvent()); | ||
} | ||
throw new RuntimeException(); | ||
} | ||
} | ||
|
||
@Root(name = "lfm", strict = false) | ||
private static class ErrorResponse { | ||
@Element(name = "error") | ||
private Error error; | ||
|
||
public Error getError() { | ||
return error; | ||
} | ||
|
||
public void setError(Error error) { | ||
this.error = error; | ||
} | ||
|
||
public static class Error { | ||
@Attribute(name = "code") | ||
private int code; | ||
|
||
public int getCode() { | ||
return code; | ||
} | ||
|
||
public void setCode(int code) { | ||
this.code = code; | ||
} | ||
} | ||
} | ||
} |
39 changes: 39 additions & 0 deletions
39
wail-app/src/main/java/com/artemzin/android/wail/api/lastfm/model/LastFmSession.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package com.artemzin.android.wail.api.lastfm.model; | ||
|
||
import org.simpleframework.xml.Element; | ||
import org.simpleframework.xml.Root; | ||
|
||
/** | ||
* @author Ilya Murzinov [murz42@gmail.com] | ||
*/ | ||
@Root(name = "lfm", strict = false) | ||
public class LastFmSession { | ||
@Element(name = "session", required = true) | ||
private Session session; | ||
|
||
public Session getSession() { | ||
return session; | ||
} | ||
|
||
public void setSession(Session session) { | ||
this.session = session; | ||
} | ||
|
||
public static class Session { | ||
private String name; | ||
private String key; | ||
private String subscriber; | ||
|
||
public String getName() { | ||
return name; | ||
} | ||
|
||
public String getKey() { | ||
return key; | ||
} | ||
|
||
public String getSubscriber() { | ||
return subscriber; | ||
} | ||
} | ||
} |
Oops, something went wrong.