public static ContentType doContentNegotiation()

in lib/server-core/src/main/java/org/apache/olingo/server/core/ContentNegotiator.java [91:179]


  public static ContentType doContentNegotiation(final FormatOption formatOption, final ODataRequest request,
      final CustomContentTypeSupport customContentTypeSupport, final RepresentationType representationType)
          throws ContentNegotiatorException {
    final List<ContentType> supportedContentTypes =
        getSupportedContentTypes(customContentTypeSupport, representationType);
    final List<String> acceptHeaderValueList = request.getHeaders(HttpHeader.ACCEPT);
    final String acceptHeaderValue = acceptHeaderValueList != null ? 
    		acceptHeaderValueList.stream().collect(Collectors.joining(", ")) : null;
    List<String> acceptCharsetValueList = request.getHeaders(HttpHeader.ACCEPT_CHARSET);
    String acceptCharset = acceptCharsetValueList != null ? 
    		acceptCharsetValueList.stream().collect(Collectors.joining(", ")) : null;
    List<AcceptCharset> charsets = null;
      
    ContentType result = null;

    if (formatOption != null && formatOption.getFormat() != null) {
      final String formatString = formatOption.getFormat().trim();
      final ContentType contentType = mapContentType(formatString, representationType);
      boolean isCharsetInFormat = false;
      List<AcceptType> formatTypes = null;
      try {
      formatTypes = AcceptType.fromContentType(contentType == null ?
          ContentType.create(formatOption.getFormat()) : contentType);
      } catch (final IllegalArgumentException e) {
        throw new AcceptHeaderContentNegotiatorException(
            "Unsupported $format=" + formatString, e,
            AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString);
      }
      Map<String, String> formatParameters = formatTypes.get(0).getParameters();
      if (!formatParameters.isEmpty() && null != formatParameters.get(ContentType.PARAMETER_CHARSET)) {
        isCharsetInFormat = true;
      } else {
        isCharsetInFormat = false;
        charsets = getAcceptCharset(acceptCharset);
      }
      try {
        if (isCharsetInFormat) {
          charsets = getAcceptCharset(formatParameters.get(ContentType.PARAMETER_CHARSET));
        }
        result = getAcceptedType(formatTypes, supportedContentTypes, charsets);
      } catch (final IllegalArgumentException e) {
        throw new AcceptHeaderContentNegotiatorException(
            "Unsupported $format=" + formatString, e,
            AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString);
      } catch (final AcceptHeaderContentNegotiatorException e) {
        throw new AcceptHeaderContentNegotiatorException (
            "Unsupported $format=" + formatString, e,
            AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString);
      } catch (final ContentNegotiatorException e) {
        throw new ContentNegotiatorException (
            "Unsupported $format=" + formatString, e, 
            ContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString);
      }
      if (result == null) {
        throw new ContentNegotiatorException("Unsupported $format = " + formatString,
            ContentNegotiatorException.MessageKeys.UNSUPPORTED_FORMAT_OPTION, formatString);
      }
    } else if (acceptHeaderValue != null) {
      charsets = getAcceptCharset(acceptCharset);
      try {
        result = getAcceptedType(AcceptType.create(acceptHeaderValue), 
            supportedContentTypes, charsets);
      } catch (final IllegalArgumentException e) {
        throw new AcceptHeaderContentNegotiatorException(e.getMessage(), e,
            AcceptHeaderContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_TYPES, 
            e.getMessage().substring(e.getMessage().lastIndexOf(COLON) + 1));
      } 
      if (result == null) {
        List<AcceptType> types = AcceptType.create(acceptHeaderValue);
        throw new ContentNegotiatorException(
            "The combination of type and subtype " + types.get(0) +
            " != " + supportedContentTypes,
            ContentNegotiatorException.MessageKeys.UNSUPPORTED_ACCEPT_TYPES, acceptHeaderValue);
      }
    } else {
      charsets = getAcceptCharset(acceptCharset);
      final ContentType requestedContentType = getDefaultSupportedContentTypes(representationType).get(0);
      result = getAcceptedType(AcceptType.fromContentType(requestedContentType), 
          supportedContentTypes, charsets);
      
      if (result == null) {
        throw new ContentNegotiatorException(
            "unsupported accept content type: " + requestedContentType + " != " + supportedContentTypes,
            ContentNegotiatorException.MessageKeys.UNSUPPORTED_CONTENT_TYPE,
            requestedContentType.toContentTypeString());
      }
    }
    return result;
  }