Permalink
Browse files

Adding shout() and getShouts() to userOperations

  • Loading branch information...
1 parent 907ba31 commit 640139a5ef9e6ad8e9211cbbcbff46f1763d4a87 @michaellavelle committed Dec 2, 2011
@@ -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;
+ }
+
+}
@@ -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);
@@ -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) {
@@ -21,13 +21,16 @@
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;
import org.springframework.social.lastfm.api.UserOperations;
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();
+ }
+
}
@@ -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);
-
+
+
}
}
@@ -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;
+ }
+
+
+}
@@ -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;
+ }
+}
@@ -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;
+ }
+
+}
@@ -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) {
+ }
+
+}
Oops, something went wrong.

0 comments on commit 640139a

Please sign in to comment.