Пример #1
0
  /** {@inheritDoc} */
  public MediaType fromString(String header) {
    if (header == null) throw new IllegalArgumentException();

    try {
      int p = header.indexOf('/');
      int col = header.indexOf(';');

      String type = null;
      String subType = null;

      if (p < 0 && col < 0) // no '/' and ';'
      return new MediaType(header, null);
      else if (p > 0 && col < 0) // there is no ';' so no parameters
      return new MediaType(
            HeaderHelper.removeWhitespaces(header.substring(0, p)),
            HeaderHelper.removeWhitespaces(header.substring(p + 1)));
      else if (p < 0 && col > 0) { // there is no '/' but present ';'
        type = HeaderHelper.removeWhitespaces(header.substring(0, col));
        // sub-type is null
      } else { // presents '/' and ';'
        type = HeaderHelper.removeWhitespaces(header.substring(0, p));
        subType = header.substring(p + 1, col);
      }

      Map<String, String> m = new HeaderParameterParser().parse(header);
      return new MediaType(type, subType, m);

    } catch (ParseException e) {
      throw new IllegalArgumentException(e);
    }
  }