@Test
 public void testTokenizeToStringArrayWithNotTrimTokens() {
   String[] sa = StringUtils.tokenizeToStringArray("a,b ,c", ",", false, true);
   assertEquals(3, sa.length);
   assertTrue(
       "components are correct", sa[0].equals("a") && sa[1].equals("b ") && sa[2].equals("c"));
 }
예제 #2
0
 /**
  * Parse the given, comma-separated string into a list of {@code MimeType} objects.
  *
  * @param mimeTypes the string to parse
  * @return the list of mime types
  * @throws IllegalArgumentException if the string cannot be parsed
  */
 public static List<MimeType> parseMimeTypes(String mimeTypes) {
   if (!StringUtils.hasLength(mimeTypes)) {
     return Collections.emptyList();
   }
   String[] tokens = StringUtils.tokenizeToStringArray(mimeTypes, ",");
   List<MimeType> result = new ArrayList<>(tokens.length);
   for (String token : tokens) {
     result.add(parseMimeType(token));
   }
   return result;
 }
예제 #3
0
  /**
   * Parse the given String into a single {@code MimeType}.
   *
   * @param mimeType the string to parse
   * @return the mime type
   * @throws InvalidMimeTypeException if the string cannot be parsed
   */
  public static MimeType parseMimeType(String mimeType) {
    if (!StringUtils.hasLength(mimeType)) {
      throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
    }
    String[] parts = StringUtils.tokenizeToStringArray(mimeType, ";");
    if (parts.length == 0) {
      throw new InvalidMimeTypeException(mimeType, "'mimeType' must not be empty");
    }

    String fullType = parts[0].trim();
    // java.net.HttpURLConnection returns a *; q=.2 Accept header
    if (MimeType.WILDCARD_TYPE.equals(fullType)) {
      fullType = "*/*";
    }
    int subIndex = fullType.indexOf('/');
    if (subIndex == -1) {
      throw new InvalidMimeTypeException(mimeType, "does not contain '/'");
    }
    if (subIndex == fullType.length() - 1) {
      throw new InvalidMimeTypeException(mimeType, "does not contain subtype after '/'");
    }
    String type = fullType.substring(0, subIndex);
    String subtype = fullType.substring(subIndex + 1, fullType.length());
    if (MimeType.WILDCARD_TYPE.equals(type) && !MimeType.WILDCARD_TYPE.equals(subtype)) {
      throw new InvalidMimeTypeException(
          mimeType, "wildcard type is legal only in '*/*' (all mime types)");
    }

    Map<String, String> parameters = null;
    if (parts.length > 1) {
      parameters = new LinkedHashMap<>(parts.length - 1);
      for (int i = 1; i < parts.length; i++) {
        String parameter = parts[i];
        int eqIndex = parameter.indexOf('=');
        if (eqIndex != -1) {
          String attribute = parameter.substring(0, eqIndex);
          String value = parameter.substring(eqIndex + 1, parameter.length());
          parameters.put(attribute, value);
        }
      }
    }

    try {
      return new MimeType(type, subtype, parameters);
    } catch (UnsupportedCharsetException ex) {
      throw new InvalidMimeTypeException(
          mimeType, "unsupported charset '" + ex.getCharsetName() + "'");
    } catch (IllegalArgumentException ex) {
      throw new InvalidMimeTypeException(mimeType, ex.getMessage());
    }
  }