Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
Copyright 2013 SAP AG Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the License for the specific language governing permissions and limitations under the License. /
 
 package com.sap.core.odata.core;
 
 import java.util.List;
 import java.util.Set;
 
 
 public class ContentNegotiator {
   private static final String URI_INFO_FOMRAT_JSON = "json";
   private static final String URI_INFO_FOMRAT_ATOM = "atom";
   private static final String URI_INFO_FOMRAT_XML = "xml";
   static final String DEFAULT_CHARSET = "utf-8";
 
   public ContentNegotiator() {}
 
   public String doContentNegotiation(final UriInfoImpl uriInfofinal List<StringacceptHeaderContentTypesfinal List<StringsupportedContentTypesthrows ODataException {
     ContentType contentType;
     if (uriInfo.getFormat() == null) {
       contentType = doContentNegotiationForAcceptHeader(acceptHeaderContentTypes, ContentType.create(supportedContentTypes));
     } else {
       contentType = doContentNegotiationForFormat(uriInfo, ContentType.create(supportedContentTypes));
     }
 
     if (contentType.getODataFormat() == .) {
       return contentType.getType();
     }
     return contentType.toContentTypeString();
   }
 
   private ContentType doContentNegotiationForFormat(final UriInfoImpl uriInfofinal List<ContentTypesupportedContentTypesthrows ODataException {
     validateFormatQuery(uriInfo);
     ContentType formatContentType = mapFormat(uriInfo);
     formatContentType = formatContentType.receiveWithCharsetParameter();
 
     for (final ContentType contentType : supportedContentTypes) {
       if (contentType.equals(formatContentType)) {
         return formatContentType;
       }
     }
 
   }

  
Validates that dollar format query/syntax is correct for further processing. If some validation error occurs an exception is thrown.

 
   private void validateFormatQuery(final UriInfoImpl uriInfothrows ODataBadRequestException {
     if (uriInfo.isValue()) {
     }
   }
 
   private ContentType mapFormat(final UriInfoImpl uriInfo) {
     final String format = uriInfo.getFormat();
     if (.equals(format)) {
       return .;
     } else if (.equals(format)) {
       if (uriInfo.getUriType() == .) {
         // special handling for serviceDocument uris (UriType.URI0)
         return .;
       }
       return .;
     } else if (.equals(format)) {
       return .;
     }
 
     return ContentType.create(format);
   }
 
   private ContentType doContentNegotiationForAcceptHeader(final List<StringacceptHeaderContentTypesfinal List<ContentTypesupportedContentTypesthrows ODataException {
     return contentNegotiation(extractAcceptHeaders(acceptHeaderContentTypes), supportedContentTypes);
   }
  private List<ContentTypeextractAcceptHeaders(final List<StringacceptHeaderValuesthrows ODataBadRequestException {
    final List<ContentTypemediaTypes = new ArrayList<ContentType>();
    if (acceptHeaderValues != null) {
      for (final String mediaType : acceptHeaderValues) {
        try {
          mediaTypes.add(ContentType.create(mediaType.toString()));
        } catch (IllegalArgumentException e) {
              .addContent(mediaType.toString()), e);
        }
      }
    }
    return mediaTypes;
  }
  ContentType contentNegotiation(final List<ContentTypeacceptedContentTypesfinal List<ContentTypesupportedContentTypesthrows ODataException {
    final Set<ContentTypesetSupported = new HashSet<ContentType>(supportedContentTypes);
    if (acceptedContentTypes.isEmpty()) {
      if (!setSupported.isEmpty()) {
        return supportedContentTypes.get(0);
      }
    } else {
      for (ContentType contentType : acceptedContentTypes) {
        contentType = contentType.receiveWithCharsetParameter();
        final ContentType match = contentType.match(supportedContentTypes);
        if (match != null) {
          return match;
        }
      }
    }
  }
New to GrepCode? Check out our FAQ X