PageRenderTime 103ms CodeModel.GetById 29ms RepoModel.GetById 0ms app.codeStats 0ms

/web/src/main/java/jahspotify/web/api/HistoryController.java

http://github.com/johanlindquist/jahspotify
Java | 126 lines | 79 code | 15 blank | 32 comment | 3 complexity | 930cf759d4b823ca07c55c5c227fb51c MD5 | raw file
  1. package jahspotify.web.api;
  2. /*
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. */
  20. import java.io.*;
  21. import java.util.*;
  22. import javax.servlet.ServletOutputStream;
  23. import javax.servlet.http.*;
  24. import com.google.gson.Gson;
  25. import jahspotify.storage.statistics.*;
  26. import jahspotify.storage.statistics.TrackHistory;
  27. import org.springframework.beans.BeanUtils;
  28. import org.springframework.beans.factory.annotation.*;
  29. import org.springframework.stereotype.Controller;
  30. import org.springframework.web.bind.annotation.*;
  31. /**
  32. * @author Johan Lindquist
  33. */
  34. @Controller
  35. public class HistoryController extends BaseController
  36. {
  37. @Autowired
  38. @Qualifier(value = "mongodb")
  39. private HistoricalStorage _historicalStorage;
  40. @RequestMapping(value = "/history/count", method = RequestMethod.GET,produces = "application/json")
  41. public void getHistoryCount(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
  42. {
  43. final int count = _historicalStorage.getHistoryCount(null);
  44. }
  45. @RequestMapping(value = "/history", method = RequestMethod.GET)
  46. public void getHistory(final HttpServletResponse httpServletResponse, @RequestParam(value = "index", defaultValue = "0") int index, @RequestParam(value = "count",defaultValue = "${jahspotify.history.default-count}")int count)
  47. {
  48. final Collection<TrackHistory> history = _historicalStorage.getHistory(index, count, null);
  49. httpServletResponse.setContentType("application/json");
  50. serializeHistoryCursor(history, httpServletResponse);
  51. }
  52. public void serializeHistoryCursor(Collection<TrackHistory> historyCursor, HttpServletResponse httpServletResponse)
  53. {
  54. try
  55. {
  56. final ServletOutputStream httpOutputStream = httpServletResponse.getOutputStream();
  57. final BufferedWriter outputStream = new BufferedWriter(new OutputStreamWriter(httpOutputStream));
  58. outputStream.write("{");
  59. outputStream.write("\"count\":");
  60. outputStream.write("" + historyCursor.size());
  61. if (historyCursor.size() > 0)
  62. {
  63. Gson gson = new Gson();
  64. outputStream.write(",");
  65. outputStream.write("\"tracks\":[");
  66. for (Iterator<TrackHistory> iterator = historyCursor.iterator(); iterator.hasNext(); )
  67. {
  68. TrackHistory next = iterator.next();
  69. outputStream.write(gson.toJson(toWebTrack(next)));
  70. if (iterator.hasNext())
  71. {
  72. outputStream.write(",");
  73. }
  74. outputStream.flush();
  75. }
  76. /*
  77. while (historyCursor.hasNext())
  78. {
  79. outputStream.write(gson.toJson(toWebTrack(historyCursor.next())));
  80. if (historyCursor.hasNext())
  81. {
  82. outputStream.write(",");
  83. }
  84. outputStream.flush();
  85. }
  86. */
  87. outputStream.write("]");
  88. }
  89. outputStream.write("}");
  90. outputStream.flush();
  91. outputStream.close();
  92. httpOutputStream.close();
  93. }
  94. catch (IOException e)
  95. {
  96. e.printStackTrace();
  97. }
  98. }
  99. private jahspotify.web.media.TrackHistory toWebTrack(final TrackHistory next)
  100. {
  101. jahspotify.web.media.TrackHistory trackHistory = new jahspotify.web.media.TrackHistory();
  102. trackHistory.setTrackLink(toWebLink(next.getTrackLink()));
  103. trackHistory.setQueue(toWebLink(next.getQueue()));
  104. BeanUtils.copyProperties(next,trackHistory,new String[] { "trackLink", "queue"});
  105. return trackHistory;
  106. }
  107. @RequestMapping(value = "/history", method = RequestMethod.POST, produces = "application/json", headers="Accept=application/json")
  108. public void postHistory(final HttpServletRequest httpServletRequest, final HttpServletResponse httpServletResponse)
  109. {
  110. }
  111. }