Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

StaticResourcesServlet.java 3.7KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. /*
  2. * SonarQube, open source software quality management tool.
  3. * Copyright (C) 2008-2014 SonarSource
  4. * mailto:contact AT sonarsource DOT com
  5. *
  6. * SonarQube is free software; you can redistribute it and/or
  7. * modify it under the terms of the GNU Lesser General Public
  8. * License as published by the Free Software Foundation; either
  9. * version 3 of the License, or (at your option) any later version.
  10. *
  11. * SonarQube is distributed in the hope that it will be useful,
  12. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  14. * Lesser General Public License for more details.
  15. *
  16. * You should have received a copy of the GNU Lesser General Public License
  17. * along with this program; if not, write to the Free Software Foundation,
  18. * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
  19. */
  20. package org.sonar.server.plugins;
  21. import com.google.common.annotations.VisibleForTesting;
  22. import org.apache.commons.io.IOUtils;
  23. import org.apache.commons.lang.StringUtils;
  24. import org.sonar.api.Plugin;
  25. import org.sonar.api.utils.log.Logger;
  26. import org.sonar.api.utils.log.Loggers;
  27. import org.sonar.core.platform.PluginRepository;
  28. import org.sonar.server.platform.Platform;
  29. import javax.servlet.ServletException;
  30. import javax.servlet.http.HttpServlet;
  31. import javax.servlet.http.HttpServletRequest;
  32. import javax.servlet.http.HttpServletResponse;
  33. import java.io.IOException;
  34. import java.io.InputStream;
  35. import java.io.OutputStream;
  36. public class StaticResourcesServlet extends HttpServlet {
  37. private static final Logger LOG = Loggers.get(StaticResourcesServlet.class);
  38. private static final long serialVersionUID = -2577454614650178426L;
  39. @Override
  40. public void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
  41. String pluginKey = getPluginKey(request);
  42. String resource = getResourcePath(request);
  43. PluginRepository pluginRepository = Platform.getInstance().getContainer().getComponentByType(PluginRepository.class);
  44. if (!pluginRepository.hasPlugin(pluginKey)) {
  45. response.sendError(HttpServletResponse.SC_NOT_FOUND);
  46. return;
  47. }
  48. InputStream in = null;
  49. OutputStream out = null;
  50. try {
  51. in = pluginRepository.getPluginInstance(pluginKey).getClass().getClassLoader().getResourceAsStream(resource);
  52. if (in != null) {
  53. // mime type must be set before writing response body
  54. completeContentType(response, resource);
  55. out = response.getOutputStream();
  56. IOUtils.copy(in, out);
  57. } else {
  58. response.sendError(HttpServletResponse.SC_NOT_FOUND);
  59. }
  60. } catch (Exception e) {
  61. LOG.error(String.format("Unable to load resource [%s] from plugin [%s]", resource, pluginKey), e);
  62. response.sendError(HttpServletResponse.SC_INTERNAL_SERVER_ERROR);
  63. } finally {
  64. IOUtils.closeQuietly(in);
  65. IOUtils.closeQuietly(out);
  66. }
  67. }
  68. /**
  69. * @return part of request URL after servlet path
  70. */
  71. protected String getPluginKeyAndResourcePath(HttpServletRequest request) {
  72. return StringUtils.substringAfter(request.getRequestURI(), request.getContextPath() + request.getServletPath() + "/");
  73. }
  74. protected String getPluginKey(HttpServletRequest request) {
  75. return StringUtils.substringBefore(getPluginKeyAndResourcePath(request), "/");
  76. }
  77. /**
  78. * Note that returned value should not have a leading "/" - see {@link Class#resolveName(String)}.
  79. */
  80. protected String getResourcePath(HttpServletRequest request) {
  81. return "static/" + StringUtils.substringAfter(getPluginKeyAndResourcePath(request), "/");
  82. }
  83. @VisibleForTesting
  84. void completeContentType(HttpServletResponse response, String filename) {
  85. response.setContentType(MimeTypes.getByFilename(filename));
  86. }
  87. }