Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package pl.matisoft.soy;
  
  import static java.util.Arrays.asList;
  import static java.util.Collections.list;
  import static java.util.Collections.unmodifiableList;
  import static java.util.regex.Pattern.quote;
  import static org.springframework.util.Assert.isInstanceOf;
  import static org.springframework.util.CollectionUtils.isEmpty;
  import static org.springframework.web.context.request.RequestContextHolder.getRequestAttributes;
 
 import java.util.List;
 
 
This class is responsible for determining whether or not it can support the requested content types.

Author(s):
Abhijit Sarkar
 
 public class DefaultContentNegotiator implements ContentNegotiator {
 
 	private static final Logger logger = LoggerFactory.getLogger(DefaultContentNegotiator.class);
 
 	public static final String DEFAULT_FAVORED_PARAMETER_NAME = "format";
 	public static final List<StringDEFAULT_SUPPORTED_CONTENT_TYPES = unmodifiableList(asList("text/html"));
 	public static final String ACCEPT_HEADER = "Accept";
 
     private static final String CONTENT_TYPE_DELIMITER = ",\\s*";
 
 	private boolean favorParameterOverAcceptHeader;
 
 	}
 
 	}
 
 	public void setSupportedContentTypes(final List<StringsupportedContentTypes) {
 		this. = supportedContentTypes;
 	}
 
 	public boolean isFavorParameterOverAcceptHeader() {
 	}
 
 	public void setFavorParameterOverAcceptHeader(final boolean favorParameterOverAcceptHeader) {
 		this. = favorParameterOverAcceptHeader;
 	}
 
 	}
 
 	public void setFavoredParameterName(final String parameterName) {
 		this. = parameterName;
 	}
 
 	}
 
 	public void setContentTypeComparator(final Comparator<List<String>> contentTypeComparator) {
 		this. = contentTypeComparator;
 	}

Returns true if able to support the requested content types; false otherwise.

Returns:
True if able to support the requested content types; false otherwise.
 
 	public boolean isSupportedContentTypes(final List<StringcontentTypes) {
 		return .compare(contentTypes) == 0;
 	}

Returns requested content types or default content type if none found.

Returns:
Requested content types or default content type if none found.
 
 	public List<StringcontentTypes() {
 		List<StringcontentTypes = null;
 		final HttpServletRequest request = getHttpRequest();
			contentTypes = getFavoredParameterValueAsList(request);
else {
			contentTypes = getAcceptHeaderValues(request);
		}
		if (isEmpty(contentTypes)) {
			.debug("Setting content types to default: {}.");
		}
		return unmodifiableList(contentTypes);
	}
	}
		.debug("Retrieving content type from favored parameter: {}.");
		final String favoredParameterValue = request.getParameter();
		return favoredParameterValue != null ? splitContentTypes(asList(favoredParameterValue)) : null;
	}
	@SuppressWarnings("unchecked")
		.debug("Retrieving content type from header: {}.");
	}
	private List<StringsplitContentTypes(final List<StringcontentTypes) {
		final List<StringsplitContentTypes = new ArrayList<String>();
		for (String aContentType : contentTypes) {
			splitContentTypes.addAll(asList(aContentType.split()));
		}
		.debug("Split content types {} into {}."contentTypessplitContentTypes);
		return splitContentTypes;
	}
	private static final class DefaultContentTypeComparator implements Comparator<List<String>> {
		private static final int NOT_EQUAL = 1;
		private static final int EQUAL = 0;
		private static final String ASTERISK = "*";
		private static final String PERIOD_PLUS_ASTERISK = ".*";
		public int compare(List<StringsupportedContentTypesList<StringcontentTypes) {
			.debug("Comparing supported content types with request content types: {}, {}.",
					supportedContentTypescontentTypes);
			if (isEmpty(contentTypes)) {
				return ;
			}
			for (String aContentType : contentTypes) {
				for (String aSupportedContentType : supportedContentTypes) {
					if (aSupportedContentType.matches(convertToRegex(aContentType))) {
						return ;
					}
				}
			}
			return ;
		}
		private String convertToRegex(final String aContentType) {
		}
	}
New to GrepCode? Check out our FAQ X