You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

MediaTypes.java 2.9KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. /*
  2. * SonarQube
  3. * Copyright (C) 2009-2019 SonarSource SA
  4. * mailto:info AT sonarsource DOT com
  5. *
  6. * This program 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. * This program 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.sonarqube.ws;
  21. import java.util.HashMap;
  22. import java.util.Locale;
  23. import java.util.Map;
  24. import static org.sonarqube.ws.WsUtils.isNullOrEmpty;
  25. /**
  26. * @since 5.3
  27. */
  28. public final class MediaTypes {
  29. public static final String JSON = "application/json";
  30. public static final String XML = "application/xml";
  31. public static final String TXT = "text/plain";
  32. public static final String PROTOBUF = "application/x-protobuf";
  33. public static final String ZIP = "application/zip";
  34. public static final String JAVASCRIPT = "application/javascript";
  35. public static final String HTML = "text/html";
  36. public static final String DEFAULT = "application/octet-stream";
  37. public static final String SVG = "image/svg+xml";
  38. private static final Map<String, String> MAP = new HashMap<>();
  39. static {
  40. MAP.put("js", JAVASCRIPT);
  41. MAP.put("json", JSON);
  42. MAP.put("zip", ZIP);
  43. MAP.put("tgz", "application/tgz");
  44. MAP.put("ps", "application/postscript");
  45. MAP.put("jnlp", "application/jnlp");
  46. MAP.put("jar", "application/java-archive");
  47. MAP.put("xls", "application/vnd.ms-excel");
  48. MAP.put("ppt", "application/vnd.ms-powerpoint");
  49. MAP.put("tar", "application/x-tar");
  50. MAP.put("xml", XML);
  51. MAP.put("dtd", "application/xml-dtd");
  52. MAP.put("xslt", "application/xslt+xml");
  53. MAP.put("bmp", "image/bmp");
  54. MAP.put("gif", "image/gif");
  55. MAP.put("jpg", "image/jpeg");
  56. MAP.put("jpeg", "image/jpeg");
  57. MAP.put("tiff", "image/tiff");
  58. MAP.put("png", "image/png");
  59. MAP.put("svg", SVG);
  60. MAP.put("ico", "image/x-icon");
  61. MAP.put("txt", TXT);
  62. MAP.put("csv", "text/csv");
  63. MAP.put("properties", TXT);
  64. MAP.put("rtf", "text/rtf");
  65. MAP.put("html", HTML);
  66. MAP.put("css", "text/css");
  67. MAP.put("tsv", "text/tab-separated-values");
  68. }
  69. private MediaTypes() {
  70. // only static methods
  71. }
  72. public static String getByFilename(String filename) {
  73. String extension = FilenameUtils.getExtension(filename);
  74. String mime = null;
  75. if (!isNullOrEmpty(extension)) {
  76. mime = MAP.get(extension.toLowerCase(Locale.ENGLISH));
  77. }
  78. return mime != null ? mime : DEFAULT;
  79. }
  80. }