| 191 | | URL url = new URL("http://" + this.submithost + ":" + this.submitport + this.submiturl); |
|---|
| | 197 | String content = null; |
|---|
| | 198 | |
|---|
| | 199 | /* If a backup URL is specified then two submits will take place. A |
|---|
| | 200 | * backup URL can be used to send your information to another server. |
|---|
| | 201 | */ |
|---|
| | 202 | if ((backupUrl != null) && !backupUrl.equals("")) { |
|---|
| | 203 | content = fetchContent(backupUrl, querystring); |
|---|
| | 204 | logger.log(Level.FINE, "Received from server:\n" + content); |
|---|
| | 205 | } |
|---|
| | 206 | |
|---|
| | 207 | String urlString = "http://" + submithost + ":" + submitport + submiturl; |
|---|
| | 208 | content = fetchContent(urlString, querystring); |
|---|
| | 209 | logger.log(Level.FINE, "Received from server:\n" + content); |
|---|
| | 210 | |
|---|
| | 211 | if ((content == null) || (content.length() == 0)) { |
|---|
| | 212 | throw new RuntimeException("Invalid response received from AudioScrobbler"); |
|---|
| | 213 | } |
|---|
| | 214 | |
|---|
| | 215 | String[] lines = content.split("\n"); |
|---|
| | 216 | |
|---|
| | 217 | if ((lines[0].length() >= 6) && lines[0].substring(0, 6).equals("FAILED")) { |
|---|
| | 218 | throw new RuntimeException(lines[0].substring(7)); |
|---|
| | 219 | } |
|---|
| | 220 | |
|---|
| | 221 | if ((lines[0].length() >= 7) && lines[0].substring(0, 7).equals("BADAUTH")) { |
|---|
| | 222 | throw new FailedLoginException("Invalid username/password"); |
|---|
| | 223 | } |
|---|
| | 224 | |
|---|
| | 225 | if ((lines[0].length() >= 2) && !lines[0].substring(0, 2).equals("OK")) { |
|---|
| | 226 | throw new RuntimeException("Unknown error submitting tracks"); |
|---|
| | 227 | } |
|---|
| | 228 | |
|---|
| | 229 | this.logger.log(Level.INFO, "Tracks submitted"); |
|---|
| | 230 | this.logger.log(Level.INFO, |
|---|
| | 231 | "You must now sync your iPod with your music management software " |
|---|
| | 232 | + "or delete 'Play Counts' from the iTunes folder!"); |
|---|
| | 233 | } |
|---|
| | 234 | |
|---|
| | 235 | /** |
|---|
| | 236 | * Creates the histories and writes them to a file. |
|---|
| | 237 | * @param activeRecentPlayed The list of active recently played tracks. |
|---|
| | 238 | * @param inactiveRecentPlayed The list of inactive recently played tracks. |
|---|
| | 239 | */ |
|---|
| | 240 | public void addHistories(List activeRecentPlayed, List inactiveRecentPlayed) { |
|---|
| | 241 | for (int i = 0; i < activeRecentPlayed.size(); i++) { |
|---|
| | 242 | TrackItem track = (TrackItem) activeRecentPlayed.get(i); |
|---|
| | 243 | History.getInstance().addhistory(track.getLastplayed()); |
|---|
| | 244 | } |
|---|
| | 245 | |
|---|
| | 246 | for (int i = 0; i < inactiveRecentPlayed.size(); i++) { |
|---|
| | 247 | TrackItem track = (TrackItem) inactiveRecentPlayed.get(i); |
|---|
| | 248 | |
|---|
| | 249 | if (History.getInstance().isInHistory(track.getLastplayed())) { |
|---|
| | 250 | History.getInstance().addhistory(track.getLastplayed()); |
|---|
| | 251 | } |
|---|
| | 252 | } |
|---|
| | 253 | |
|---|
| | 254 | History.getInstance().write(); |
|---|
| | 255 | } |
|---|
| | 256 | |
|---|
| | 257 | /** |
|---|
| | 258 | * Fetches the HTTP content given a URL String and a query String. |
|---|
| | 259 | * @param urlString The URL to fetch from. |
|---|
| | 260 | * @param querystring The query String to submit. |
|---|
| | 261 | * @return The content returned from the request. |
|---|
| | 262 | * @throws MalformedURLException Thrown if exceptions occur. |
|---|
| | 263 | * @throws IOException Thrown if exceptions occur. |
|---|
| | 264 | * @throws ProtocolException Thrown if exceptions occur. |
|---|
| | 265 | */ |
|---|
| | 266 | private String fetchContent(String urlString, String querystring) |
|---|
| | 267 | throws MalformedURLException, IOException, ProtocolException { |
|---|
| | 268 | String content = null; |
|---|
| | 269 | URL url = new URL(urlString); |
|---|
| 206 | | OutputStreamWriter wr = new OutputStreamWriter(c.getOutputStream()); |
|---|
| 207 | | wr.write(querystring); |
|---|
| 208 | | wr.flush(); |
|---|
| 209 | | wr.close(); |
|---|
| 210 | | |
|---|
| 211 | | if (c.getResponseCode() != 200) { |
|---|
| 212 | | throw new RuntimeException("Invalid HTTP return code"); |
|---|
| 213 | | } |
|---|
| 214 | | |
|---|
| 215 | | BufferedReader breader = new BufferedReader(new InputStreamReader(c.getInputStream())); |
|---|
| 216 | | |
|---|
| 217 | | String content = null; |
|---|
| 218 | | String buffer = null; |
|---|
| 219 | | |
|---|
| 220 | | while ((buffer = breader.readLine()) != null) { |
|---|
| 221 | | if (content != null) { |
|---|
| 222 | | content += (buffer + "\n"); |
|---|
| 223 | | } else { |
|---|
| 224 | | content = buffer + "\n"; |
|---|
| 225 | | } |
|---|
| 226 | | } |
|---|
| 227 | | |
|---|
| 228 | | this.logger.log(Level.FINE, "Received from server:\n" + content); |
|---|
| 229 | | |
|---|
| 230 | | if ((content == null) || (content.length() == 0)) { |
|---|
| 231 | | throw new RuntimeException("Invalid response received from AudioScrobbler"); |
|---|
| 232 | | } |
|---|
| 233 | | |
|---|
| 234 | | String[] lines = content.split("\n"); |
|---|
| 235 | | |
|---|
| 236 | | if ((lines[0].length() >= 6) && lines[0].substring(0, 6).equals("FAILED")) { |
|---|
| 237 | | throw new RuntimeException(lines[0].substring(7)); |
|---|
| 238 | | } |
|---|
| 239 | | |
|---|
| 240 | | if ((lines[0].length() >= 7) && lines[0].substring(0, 7).equals("BADAUTH")) { |
|---|
| 241 | | throw new FailedLoginException("Invalid username/password"); |
|---|
| 242 | | } |
|---|
| 243 | | |
|---|
| 244 | | if ((lines[0].length() >= 2) && !lines[0].substring(0, 2).equals("OK")) { |
|---|
| 245 | | throw new RuntimeException("Unknown error submitting tracks"); |
|---|
| 246 | | } |
|---|
| 247 | | |
|---|
| 248 | | this.logger.log(Level.INFO, "Tracks submitted"); |
|---|
| 249 | | this.logger.log(Level.INFO, |
|---|
| 250 | | "You must now sync your iPod with your music management software " |
|---|
| 251 | | + "or delete 'Play Counts' from the iTunes folder!"); |
|---|
| 252 | | } |
|---|
| 253 | | |
|---|
| 254 | | /** |
|---|
| 255 | | * Creates the histories and writes them to a file. |
|---|
| 256 | | * @param activeRecentPlayed The list of active recently played tracks. |
|---|
| 257 | | * @param inactiveRecentPlayed The list of inactive recently played tracks. |
|---|
| 258 | | */ |
|---|
| 259 | | public void addHistories(List activeRecentPlayed, List inactiveRecentPlayed) { |
|---|
| 260 | | for (int i = 0; i < activeRecentPlayed.size(); i++) { |
|---|
| 261 | | TrackItem track = (TrackItem) activeRecentPlayed.get(i); |
|---|
| 262 | | History.getInstance().addhistory(track.getLastplayed()); |
|---|
| 263 | | } |
|---|
| 264 | | |
|---|
| 265 | | for (int i = 0; i < inactiveRecentPlayed.size(); i++) { |
|---|
| 266 | | TrackItem track = (TrackItem) inactiveRecentPlayed.get(i); |
|---|
| 267 | | |
|---|
| 268 | | if (History.getInstance().isInHistory(track.getLastplayed())) { |
|---|
| 269 | | History.getInstance().addhistory(track.getLastplayed()); |
|---|
| 270 | | } |
|---|
| 271 | | } |
|---|
| 272 | | |
|---|
| 273 | | History.getInstance().write(); |
|---|
| | 284 | OutputStream out = null; |
|---|
| | 285 | OutputStreamWriter writer = null; |
|---|
| | 286 | InputStream in = null; |
|---|
| | 287 | Reader reader = null; |
|---|
| | 288 | BufferedReader bufferedReader = null; |
|---|
| | 289 | |
|---|
| | 290 | try { |
|---|
| | 291 | out = c.getOutputStream(); |
|---|
| | 292 | writer = new OutputStreamWriter(out); |
|---|
| | 293 | writer.write(querystring); |
|---|
| | 294 | writer.flush(); |
|---|
| | 295 | |
|---|
| | 296 | IoUtils.cleanup(null, writer); |
|---|
| | 297 | IoUtils.cleanup(null, out); |
|---|
| | 298 | |
|---|
| | 299 | if (c.getResponseCode() != 200) { |
|---|
| | 300 | throw new RuntimeException("Invalid HTTP return code"); |
|---|
| | 301 | } |
|---|
| | 302 | |
|---|
| | 303 | in = c.getInputStream(); |
|---|
| | 304 | reader = new InputStreamReader(in); |
|---|
| | 305 | bufferedReader = new BufferedReader(reader); |
|---|
| | 306 | |
|---|
| | 307 | String buffer = null; |
|---|
| | 308 | |
|---|
| | 309 | while ((buffer = bufferedReader.readLine()) != null) { |
|---|
| | 310 | if (content != null) { |
|---|
| | 311 | content += (buffer + "\n"); |
|---|
| | 312 | } else { |
|---|
| | 313 | content = buffer + "\n"; |
|---|
| | 314 | } |
|---|
| | 315 | } |
|---|
| | 316 | } finally { |
|---|
| | 317 | IoUtils.cleanup(null, writer); |
|---|
| | 318 | IoUtils.cleanup(null, out); |
|---|
| | 319 | IoUtils.cleanup(bufferedReader, null); |
|---|
| | 320 | IoUtils.cleanup(reader, null); |
|---|
| | 321 | IoUtils.cleanup(in, null); |
|---|
| | 322 | } |
|---|
| | 323 | |
|---|
| | 324 | return content; |
|---|