Example usage for com.google.common.net MediaType hasWildcard

List of usage examples for com.google.common.net MediaType hasWildcard

Introduction

In this page you can find the example usage for com.google.common.net MediaType hasWildcard.

Prototype

public boolean hasWildcard() 

Source Link

Document

Returns true if either the type or subtype is the wildcard.

Usage

From source file:org.wisdom.api.router.Route.java

/**
 * Sets the set of media types produced by the route.
 *
 * @param types the set of type//from  w ww  .j  a va  2s  .c o m
 * @return the current route
 */
public Route produces(String... types) {
    Preconditions.checkNotNull(types);
    final ImmutableSet.Builder<MediaType> builder = new ImmutableSet.Builder<>();
    builder.addAll(this.producedMediaTypes);
    for (String s : types) {
        final MediaType mt = MediaType.parse(s);
        if (mt.hasWildcard()) {
            throw new RoutingException("A route cannot `produce` a mime type with a wildcard: " + mt);
        }
        builder.add(mt);
    }
    this.producedMediaTypes = builder.build();
    return this;
}

From source file:org.wisdom.api.router.Route.java

/**
 * Checks whether or not the current route can accept the given request. It checks the request content type
 * against the list of accepted mime types. It does not return a boolean but an integer indicating the level of
 * acceptation: 0 - not accepted, 1 - accepted using a wildcard, 2 - full accept. This distinction comes from the
 * possibility to have wildcard in the accepted mime types. For instance, if the request contains `text/plain`,
 * and the route accepts `text/*`, it returns 1. If the route would have accepted `text/plain`, 2 would have been
 * returned.//from   www . ja  v  a 2 s  . c  om
 *
 * @param request the incoming request
 * @return the acceptation level (0, 1 or 2).
 */
public int isCompliantWithRequestContentType(Request request) {
    if (acceptedMediaTypes == null || acceptedMediaTypes.isEmpty() || request == null) {
        return 2;
    } else {
        String content = request.contentMimeType();
        if (content == null) {
            return 2;
        } else {
            // For all consume, check whether we accept it
            MediaType contentMimeType = MediaType.parse(request.contentMimeType());
            for (MediaType type : acceptedMediaTypes) {
                if (contentMimeType.is(type)) {
                    if (type.hasWildcard()) {
                        return 1;
                    } else {
                        return 2;
                    }
                }
            }
            return 0;
        }
    }
}