1 package org.musicontroller.streaming;
2
3 import java.io.IOException;
4 import java.io.OutputStream;
5 import java.util.HashMap;
6 import java.util.Map;
7
8 import org.apache.log4j.Logger;
9 import org.apache.tapestry.IRequestCycle;
10 import org.apache.tapestry.engine.ExternalService;
11 import org.apache.tapestry.engine.ExternalServiceParameter;
12 import org.apache.tapestry.engine.IEngineService;
13 import org.apache.tapestry.engine.ILink;
14 import org.apache.tapestry.services.LinkFactory;
15 import org.apache.tapestry.services.ServiceConstants;
16 import org.apache.tapestry.util.ContentType;
17 import org.apache.tapestry.web.WebResponse;
18 import org.musicontroller.core.Playlist;
19 import org.musicontroller.core.Song;
20 import org.musicontroller.dao.Dao;
21 import org.musicontroller.rss.MetadataProvider;
22 import org.musicontroller.rss.RssDAO;
23 import org.musicontroller.security.IUser;
24
25
26
27
28
29
30 public class RssService extends ExternalService implements IEngineService {
31 private static final Logger log = Logger.getLogger(RssService.class);
32
33 public static final String SERVICE_NAME = "rss";
34 private LinkFactory _linkFactory;
35 private WebResponse _response;
36 private Dao _dao;
37
38
39
40
41
42 public ILink getLink(boolean post, Object parameter) {
43
44 Object[] params = (Object[]) parameter;
45
46 Long id = params[0]==null ? 0 : (Long) params[0];
47 Long userid = params[1]==null ? 0 : (Long) params[1];
48 String passhash = params[2].toString();
49
50 Map<String,String> parameters = new HashMap<String,String>();
51 parameters.put(ServiceConstants.SERVICE, getName());
52 parameters.put("userid",userid.toString());
53 parameters.put("passhash", passhash);
54 parameters.put("id", id.toString());
55
56 ILink link = _linkFactory.constructLink(this, false, parameters, true);
57 return link;
58 }
59
60
61
62
63
64 public void service(IRequestCycle cycle) {
65 final long userid = Long.parseLong(cycle.getParameter("userid"));
66 final String passhash = cycle.getParameter("passhash");
67 long id = Long.parseLong(cycle.getParameter("id"));
68
69 IUser user = _dao.getUserById(userid);
70 if (passhash!=null && passhash.equals(user.getPassword())) {
71 Playlist playlist = _dao.getPlaylistById(id, user);
72 log.debug("Transferring podcast for " + playlist.getName());
73 OutputStream out = null;
74 try {
75 out = _response.getOutputStream(new ContentType("application/rss+xml"));
76 RssDAO rssDao = new RssDAO();
77 rssDao.writePlaylistRSS(playlist, out,
78 new MetadataProvider() {
79
80
81
82
83
84 public String getCoverUrl(Playlist playlist) {
85 CoverArtService ca = new CoverArtService();
86 ca.setLinkFactory(_linkFactory);
87 ILink link = ca.getLink(false, new Object[]{playlist.getId(), Integer.valueOf(0)});
88 return link.getAbsoluteURL();
89 }
90
91
92
93
94
95 public String getDownloadUrl(Song song) {
96 Downloader dl = new Downloader();
97 dl.setLinkFactory(_linkFactory);
98 ILink link = dl.getLink(false, new Object[]{"S", song.getId(), userid, passhash});
99 return link.getAbsoluteURL();
100 }
101
102
103
104
105
106 public String getRssUrl(Playlist playlist) {
107 ILink link = getLink(false, new Object[]{playlist.getId(), userid, passhash});
108 return link.getAbsoluteURL();
109 }
110
111
112
113
114
115 public String getUrl(Playlist playlist) {
116 ExternalService es = new ExternalService();
117 es.setLinkFactory(_linkFactory);
118 ExternalServiceParameter param = new ExternalServiceParameter("PlaylistInfo", new Object[]{playlist.getId(), (Long) null});
119 ILink link = es.getLink(false, param);
120 return link.getAbsoluteURL();
121 }
122
123
124
125
126
127 public String getUrl(Song song) {
128 ExternalService es = new ExternalService();
129 es.setLinkFactory(_linkFactory);
130 ExternalServiceParameter param = new ExternalServiceParameter("SongInfo", new Object[]{song.getId()});
131 ILink link = es.getLink(false, param);
132 return link.getAbsoluteURL();
133 }
134
135 });
136 log.debug("Done transferring podcast.");
137 } catch (IOException e) {
138
139 } finally {
140 try {
141 if (out!=null) out.close();
142 } catch (IOException e) {
143 log.error("Error closing outputstream "+e);
144 }
145 }
146 }
147 }
148
149
150
151
152
153 public String getName() {
154 return SERVICE_NAME;
155 }
156
157
158
159
160
161 public void setLinkFactory(LinkFactory linkFactory) {
162 _linkFactory = linkFactory;
163 }
164
165 public void setResponse(WebResponse response) {
166 _response = response;
167 }
168
169
170
171
172
173 public void setDao(Dao dao) {
174 _dao = dao;
175 }
176
177 }