Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Adding shout() and getShouts() to userOperations

  • Loading branch information...
commit 640139a5ef9e6ad8e9211cbbcbff46f1763d4a87 1 parent 907ba31
Michael Lavelle authored
39 src/main/java/org/springframework/social/lastfm/api/Shout.java
View
@@ -0,0 +1,39 @@
+package org.springframework.social.lastfm.api;
+
+import java.util.Date;
+
+/**
+ * @author Michael Lavelle
+ */
+public class Shout {
+
+ private String message;
+
+ private Date date;
+
+ public String getAuthor() {
+ return author;
+ }
+ private String author;
+
+ public Shout(String message,Date date,String author)
+ {
+ this.message = message;
+ this.date = date;
+ this.author = author;
+ }
+
+ public String getMessage() {
+ return message;
+ }
+ public void setMessage(String message) {
+ this.message = message;
+ }
+ public Date getDate() {
+ return date;
+ }
+ public void setDate(Date date) {
+ this.date = date;
+ }
+
+}
4 src/main/java/org/springframework/social/lastfm/api/UserOperations.java
View
@@ -26,8 +26,12 @@
public LastFmProfile getUserProfile();
public LastFmProfile getUserProfile(String userName);
+
+ public void shout(String userName,String message);
public List<Track> getLovedTracks(String userName);
+
+ public List<Shout> getShouts(String userName);
public List<Track> getTopTracks(String userName);
5 src/main/java/org/springframework/social/lastfm/api/impl/LastFmTemplate.java
View
@@ -15,6 +15,7 @@
*/
package org.springframework.social.lastfm.api.impl;
+import java.text.SimpleDateFormat;
import java.util.List;
import org.codehaus.jackson.map.ObjectMapper;
@@ -104,7 +105,9 @@ private void initialize(LastFmAccessGrant lastFmAccessGrant, String apiKey,
private void registerLastFmJsonModule(RestTemplate restTemplate2) {
objectMapper = new ObjectMapper();
- objectMapper.registerModule(new LastFmModule());
+ LastFmModule lastFmModule = new LastFmModule();
+ objectMapper.setDateFormat(lastFmModule.getDateFormat());
+ objectMapper.registerModule(lastFmModule);
List<HttpMessageConverter<?>> converters = getRestTemplate()
.getMessageConverters();
for (HttpMessageConverter<?> converter : converters) {
33 src/main/java/org/springframework/social/lastfm/api/impl/UserTemplate.java
View
@@ -21,6 +21,7 @@
import java.util.Map;
import org.springframework.social.lastfm.api.LastFmProfile;
+import org.springframework.social.lastfm.api.Shout;
import org.springframework.social.lastfm.api.SimpleTrack;
import org.springframework.social.lastfm.api.Track;
import org.springframework.social.lastfm.api.TrackDescriptor;
@@ -28,6 +29,8 @@
import org.springframework.social.lastfm.api.impl.json.LastFmLovedTracksResponse;
import org.springframework.social.lastfm.api.impl.json.LastFmProfileResponse;
import org.springframework.social.lastfm.api.impl.json.LastFmRecentTracksResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmShoutListResponse;
+import org.springframework.social.lastfm.api.impl.json.LastFmShoutsResponse;
import org.springframework.social.lastfm.api.impl.json.LastFmTopTracksResponse;
import org.springframework.social.lastfm.auth.LastFmAccessGrant;
import org.springframework.web.client.RestTemplate;
@@ -153,4 +156,34 @@ public void updateNowPlaying(TrackDescriptor trackDescriptor) {
}
+ @Override
+ public void shout(String userName, String message) {
+ requireAuthorization();
+
+ Map<String, String> additionalParams = new HashMap<String, String>();
+ additionalParams.put("user", userName);
+ additionalParams.put("message", message);
+
+ LastFmApiMethodParameters methodParameters = new LastFmApiMethodParameters(
+ "user.shout", apiKey, lastFmAccessGrant.getToken(),
+ secret, lastFmAccessGrant.getSessionKey(), additionalParams);
+
+ restTemplate.postForObject(baseApiUrl, methodParameters, String.class);
+
+
+ }
+
+ @Override
+ public List<Shout> getShouts(String userName) {
+ Map<String, String> additionalParams = new HashMap<String, String>();
+ additionalParams.put("user", userName);
+
+ LastFmApiMethodParameters methodParameters = new LastFmApiMethodParameters(
+ "user.getshouts", apiKey, null, null, additionalParams);
+
+ return restTemplate
+ .getForObject(buildLastFmApiUrl(methodParameters),
+ LastFmShoutsResponse.class).getShoutListResponse().getShouts();
+ }
+
}
17 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmModule.java
View
@@ -15,11 +15,16 @@
*/
package org.springframework.social.lastfm.api.impl.json;
+import java.text.DateFormat;
+import java.text.SimpleDateFormat;
+import java.util.Date;
+
import org.codehaus.jackson.Version;
import org.codehaus.jackson.map.module.SimpleModule;
import org.springframework.social.lastfm.api.Artist;
import org.springframework.social.lastfm.api.Image;
import org.springframework.social.lastfm.api.LastFmProfile;
+import org.springframework.social.lastfm.api.Shout;
import org.springframework.social.lastfm.api.SimpleArtist;
import org.springframework.social.lastfm.api.SimpleTrack;
import org.springframework.social.lastfm.api.Track;
@@ -37,13 +42,22 @@
public LastFmModule() {
super("LastFmModule", new Version(1, 0, 0, null));
}
+
+ public DateFormat getDateFormat()
+ {
+ return new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss");
+ }
@Override
public void setupModule(SetupContext context) {
context.setMixInAnnotations(LastFmProfile.class,
LastFmProfileMixin.class);
context.setMixInAnnotations(TrackListContainer.class, TrackMixin.class);
+ context.setMixInAnnotations(ShoutListContainer.class, ShoutMixin.class);
+
context.setMixInAnnotations(Track.class, TrackMixin.class);
+ context.setMixInAnnotations(Shout.class, ShoutMixin.class);
+
context.setMixInAnnotations(SimpleTrack.class, SimpleTrackMixin.class);
context.setMixInAnnotations(TrackSearchResult.class,
TrackSearchResultMixin.class);
@@ -53,6 +67,7 @@ public void setupModule(SetupContext context) {
context.setMixInAnnotations(Artist.class, ArtistMixin.class);
context.setMixInAnnotations(Image.class, ImageMixin.class);
-
+
+
}
}
46 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmShoutListResponse.java
View
@@ -0,0 +1,46 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.social.lastfm.api.impl.json;
+
+import java.util.List;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.springframework.social.lastfm.api.Shout;
+import org.springframework.social.lastfm.api.SimpleTrack;
+import org.springframework.social.lastfm.api.Track;
+
+/**
+ * @author Michael Lavelle
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class LastFmShoutListResponse {
+
+ private List<Shout> shouts;
+
+ @JsonCreator
+ public LastFmShoutListResponse(
+ @JsonProperty("shout") ShoutListContainer shoutsContainer) {
+ this.shouts = shoutsContainer.getShouts();
+ }
+
+ public List<Shout> getShouts() {
+ return shouts;
+ }
+
+
+}
39 src/main/java/org/springframework/social/lastfm/api/impl/json/LastFmShoutsResponse.java
View
@@ -0,0 +1,39 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.social.lastfm.api.impl.json;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+
+/**
+ * @author Michael Lavelle
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+public class LastFmShoutsResponse {
+
+ private LastFmShoutListResponse shoutListResponse;
+
+ @JsonCreator
+ public LastFmShoutsResponse(
+ @JsonProperty("shouts") LastFmShoutListResponse shoutListResponse) {
+ this.shoutListResponse = shoutListResponse;
+ }
+
+ public LastFmShoutListResponse getShoutListResponse() {
+ return shoutListResponse;
+ }
+}
60 src/main/java/org/springframework/social/lastfm/api/impl/json/ShoutListContainer.java
View
@@ -0,0 +1,60 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.social.lastfm.api.impl.json;
+
+import java.util.Arrays;
+import java.util.Date;
+import java.util.List;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.springframework.social.lastfm.api.Artist;
+import org.springframework.social.lastfm.api.Shout;
+import org.springframework.social.lastfm.api.SimpleTrack;
+import org.springframework.social.lastfm.api.Track;
+
+/**
+ * @author Michael Lavelle
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+/**
+ * Container for a shout list - allows for automatic JSON binding from *either* a list of Shouts
+ * or a Map representation of a single shout, as LastFm responds with different Json structures
+ * depending on whether a single shout is returned
+ *
+ * @author Michael Lavelle
+ */
+public class ShoutListContainer {
+
+ private List<Shout> shouts;
+
+ @JsonCreator
+ public ShoutListContainer(List<Shout> shouts) {
+ this.shouts = shouts;
+ }
+
+ public ShoutListContainer(String message, Date date,String author) {
+ this.shouts = Arrays
+ .asList(new Shout(message,date,author));
+ }
+
+
+ public List<Shout> getShouts() {
+ return shouts;
+ }
+
+}
40 src/main/java/org/springframework/social/lastfm/api/impl/json/ShoutMixin.java
View
@@ -0,0 +1,40 @@
+/*
+ * Copyright 2011 the original author or authors.
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.springframework.social.lastfm.api.impl.json;
+
+import java.util.Date;
+
+import org.codehaus.jackson.annotate.JsonCreator;
+import org.codehaus.jackson.annotate.JsonIgnoreProperties;
+import org.codehaus.jackson.annotate.JsonProperty;
+import org.codehaus.jackson.map.annotate.JsonDeserialize;
+import org.codehaus.jackson.map.annotate.JsonSerialize;
+import org.springframework.social.lastfm.api.Artist;
+
+/**
+ * Annotated mixin to add Jackson annotations to Shout.
+ *
+ * @author Michael Lavelle
+ */
+@JsonIgnoreProperties(ignoreUnknown = true)
+abstract class ShoutMixin {
+
+ @JsonCreator
+ ShoutMixin(@JsonProperty("body") String message,
+ @JsonProperty("date") Date date,@JsonProperty("author") String author) {
+ }
+
+}
74 src/test/java/org/springframework/social/lastfm/api/UserTemplateTest.java
View
@@ -17,6 +17,8 @@
package org.springframework.social.lastfm.api;
import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertNotNull;
+
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.social.test.client.RequestMatchers.body;
@@ -119,6 +121,40 @@ public void getLovedTracks() {
assertTrackData(tracks.get(0));
}
+
+ @Test
+ public void getShouts() {
+
+ mockServer
+ .expect(requestTo("http://ws.audioscrobbler.com/2.0/?format=json&api_key=someApiKey&method=user.getshouts&user=mattslip"))
+ .andExpect(method(GET))
+ .andExpect(header("User-Agent", "someUserAgent"))
+ .andRespond(
+ withResponse(jsonResource("testdata/shouts"),
+ responseHeaders));
+
+ List<Shout> shouts = lastFm.userOperations().getShouts("mattslip");
+ assertShoutData(shouts.get(2));
+
+
+
+ }
+
+ @Test
+ public void getShoutsSingleShoutResponse() {
+
+ mockServer
+ .expect(requestTo("http://ws.audioscrobbler.com/2.0/?format=json&api_key=someApiKey&method=user.getshouts&user=mattslip"))
+ .andExpect(method(GET))
+ .andExpect(header("User-Agent", "someUserAgent"))
+ .andRespond(
+ withResponse(jsonResource("testdata/single-shout-response"),
+ responseHeaders));
+
+ List<Shout> shouts = lastFm.userOperations().getShouts("mattslip");
+ assertShoutData(shouts.get(0));
+
+ }
/**
* Tests for the case where the loved tracks response contains only a single
@@ -170,10 +206,36 @@ public void getLovedTracks_invalidUser() {
withResponse(jsonResource("testdata/invalid-user"),
responseHeaders));
- List<Track> tracks = lastFm.userOperations().getLovedTracks(
+ lastFm.userOperations().getLovedTracks(
"someOtherUser");
}
+
+
+ @Test
+ public void shout() {
+
+ mockServer
+ .expect(requestTo("http://ws.audioscrobbler.com/2.0/"))
+ .andExpect(method(POST))
+ .andExpect(header("User-Agent", "someUserAgent"))
+ .andExpect(
+ body("format=json&api_sig=0086bb835c43ac345624691862cc9fd4&api_key=someApiKey&sk=someSessionKey&method=user.shout&token=someToken&message=someMessage&user=someUserName"))
+ .andRespond(
+ withResponse(jsonResource("testdata/status-ok"),
+ responseHeaders));
+
+ lastFm.userOperations().shout("someUserName", "someMessage");
+
+ }
+
+
+ @Test(expected = NotAuthorizedException.class)
+ public void shout_unauthorized() {
+
+ unauthorizedLastFm.userOperations().shout("someUserName", "someMessage");
+
+ }
@Test
public void scrobble() {
@@ -185,7 +247,7 @@ public void scrobble() {
.andExpect(
body("format=json&api_sig=b4c8c73655abc90599cdfc0ed9c3b3e8&api_key=someApiKey&sk=someSessionKey&method=track.scrobble&token=someToken&timestamp=123456&track=My+track+name&artist=My+artist+name"))
.andRespond(
- withResponse(jsonResource("testdata/recent-tracks"),
+ withResponse(jsonResource("testdata/status-ok"),
responseHeaders));
lastFm.userOperations().scrobble(
@@ -246,5 +308,13 @@ private void assertTrackData(Track track) {
assertEquals("http://www.last.fm/music/Miami+Horror", track.getArtist()
.getUrl());
}
+
+ private void assertShoutData(Shout shout) {
+ assertEquals(shout.getAuthor(),"LAST.HQ");
+ assertEquals(shout.getMessage(),"Welcome aboard, mattslip! Happy listening.");
+ assertNotNull(shout.getDate());
+
+
+ }
}
8 src/test/resources/org/springframework/social/lastfm/api/testdata/shouts.json
View
@@ -0,0 +1,8 @@
+{"shouts":
+{"shout":[{"body":"hello from spring-social-lastfm","author":"michaellavelle","date":"Fri, 2 Dec 2011 12:28:24"},
+{"body":"hello from spring-social-lastfm","author":"michaellavelle","date":"Fri, 2 Dec 2011 12:25:48"},
+{"body":"Welcome aboard, mattslip! Happy listening.","author":"LAST.HQ","date":"Mon, 26 Oct 2009 10:38:36"}
+],
+"@attr":{"user":"mattslip","page":"1","perPage":"50","totalPages":"1","total":"3"}}
+}
+
1  src/test/resources/org/springframework/social/lastfm/api/testdata/single-shout-response.json
View
@@ -0,0 +1 @@
+{"shouts":{"shout":{"body":"Welcome aboard, mattslip! Happy listening.","author":"LAST.HQ","date":"Mon, 26 Oct 2009 10:38:36"},"@attr":{"user":"mattslip","page":"1","perPage":"50","totalPages":"1","total":"1"}}}
2  src/test/resources/org/springframework/social/lastfm/api/testdata/status-ok.json
View
@@ -0,0 +1,2 @@
+{"status":"ok"}
+
Please sign in to comment.
Something went wrong with that request. Please try again.