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

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

Introduction

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

Prototype

public MediaType withoutParameters() 

Source Link

Document

Returns a new instance with the same type and subtype as this instance, but without any parameters.

Usage

From source file:brooklyn.util.net.Urls.java

/** 
 * Creates a "data:..." scheme URL for use with supported parsers, using Base64 encoding.
 * (But note, by default Java's URL is not one of them, although Brooklyn's ResourceUtils does support it.)
 * <p>//from   ww w  .j a v a 2s . c  om
 * It is not necessary (at least for Brookyn's routines) to base64 encode it, but recommended as that is likely more
 * portable and easier to work with if odd characters are included.
 * <p>
 * It is worth noting that Base64 uses '+' which can be replaced by ' ' in some URL parsing.  
 * But in practice it does not seem to cause issues.
 * An alternative is to use {@link BaseEncoding#base64Url()} but it is not clear how widely that is supported
 * (nor what parameter should be given to indicate that type of encoding, as the spec calls for 'base64'!)
 * <p>
 * null type means no type info will be included in the URL. */
public static String asDataUrlBase64(MediaType type, byte[] bytes) {
    return "data:" + (type != null ? type.withoutParameters().toString() : "") + ";base64,"
            + new String(BaseEncoding.base64().encode(bytes));
}

From source file:org.mule.module.apikit.RestContentTypeParser.java

public static boolean isMediaTypeAcceptable(List<MediaType> acceptContentTypes, MediaType mediaType) {
    for (MediaType accept : acceptContentTypes) {
        if (accept.withoutParameters().equals(mediaType)) {
            return true;
        }/*from w  w w.  jav  a2s . c  o m*/
    }
    return false;
}

From source file:jp.opencollector.guacamole.auth.delegated.DelegatedAuthenticationProvider.java

private static Optional<GuacamoleConfiguration> buildConfigurationFromRequest(HttpServletRequest req)
        throws GuacamoleException {
    try {/*from   w w w.  j  a  va2 s .  c  o m*/
        if (req.getClass().getName().equals("org.glyptodon.guacamole.net.basic.rest.APIRequest")) {
            final GuacamoleConfiguration config = new GuacamoleConfiguration();
            final String protocol = req.getParameter("protocol");
            if (protocol == null)
                throw new GuacamoleException("required parameter \"protocol\" is missing");
            config.setProtocol(protocol);
            for (Map.Entry<String, String[]> param : req.getParameterMap().entrySet()) {
                String[] values = param.getValue();
                if (values.length > 0)
                    config.setParameter(param.getKey(), values[0]);
            }
            return Optional.of(config);
        } else {
            final ServletInputStream is = req.getInputStream();
            if (!is.isReady()) {
                MediaType contentType = MediaType.parse(req.getContentType());
                boolean invalidContentType = true;
                if (contentType.type().equals("application")) {
                    if (contentType.subtype().equals("json")) {
                        invalidContentType = false;
                    } else if (contentType.subtype().equals("x-www-form-urlencoded")
                            && req.getParameter("token") != null) {
                        return Optional.<GuacamoleConfiguration>absent();
                    }
                }
                if (invalidContentType)
                    throw new GuacamoleException(String.format("expecting application/json, got %s",
                            contentType.withoutParameters()));
                final GuacamoleConfiguration config = new GuacamoleConfiguration();
                try {
                    final ObjectMapper mapper = new ObjectMapper();
                    JsonNode root = (JsonNode) mapper.readTree(
                            createJsonParser(req.getInputStream(), contentType.charset().or(UTF_8), mapper));
                    {
                        final JsonNode protocol = root.get("protocol");
                        if (protocol == null)
                            throw new GuacamoleException("required parameter \"protocol\" is missing");
                        final JsonNode parameters = root.get("parameters");
                        if (parameters == null)
                            throw new GuacamoleException("required parameter \"parameters\" is missing");
                        config.setProtocol(protocol.asText());
                        {
                            for (Iterator<Entry<String, JsonNode>> i = parameters.fields(); i.hasNext();) {
                                Entry<String, JsonNode> member = i.next();
                                config.setParameter(member.getKey(), member.getValue().asText());
                            }
                        }
                    }
                } catch (ClassCastException e) {
                    throw new GuacamoleException("error occurred during parsing configuration", e);
                }
                return Optional.of(config);
            } else {
                return Optional.<GuacamoleConfiguration>absent();
            }
        }
    } catch (IOException e) {
        throw new GuacamoleException("error occurred during retrieving configuration from the request body", e);
    }
}

From source file:io.rebolt.http.utils.HeaderParser.java

public boolean isFirst(MediaType mediaType) {
    return isEmpty() || mediaTypes.get(0).is(mediaType.withoutParameters());
}

From source file:io.rebolt.http.utils.HeaderParser.java

public boolean contains(MediaType mediaType) {
    return isEmpty() || mediaTypes.stream().anyMatch(entry -> entry.is(mediaType.withoutParameters()));
}

From source file:com.github.avarabyeu.restendpoint.serializer.xml.JaxbSerializer.java

@Override
public boolean canRead(MediaType mimeType, Type resultType) {
    return mimeType.withoutParameters().is(MediaType.APPLICATION_XML_UTF_8.withoutParameters());
}

From source file:com.github.avarabyeu.restendpoint.serializer.StringSerializer.java

/**
 * Checks whether mime types is supported by this serializer implementation
 *///w w w.  j  ava  2 s .  c  o  m
@Override
public boolean canRead(@Nonnull MediaType mimeType, Type resultType) {
    MediaType type = mimeType.withoutParameters();
    return (type.is(MediaType.ANY_TEXT_TYPE) || MediaType.APPLICATION_XML_UTF_8.withoutParameters().is(type)
            || MediaType.JSON_UTF_8.withoutParameters().is(type))
            && String.class.equals(TypeToken.of(resultType).getRawType());
}

From source file:com.github.avarabyeu.restendpoint.serializer.StringSerializer.java

/**
 * Checks whether mime types is supported by this serializer implementation
 *///from   ww  w. j a  v a  2  s  .co  m
@Override
public boolean canRead(@Nonnull MediaType mimeType, Class<?> resultType) {
    MediaType type = mimeType.withoutParameters();
    return (type.is(MediaType.ANY_TEXT_TYPE) || MediaType.APPLICATION_XML_UTF_8.withoutParameters().is(type)
            || MediaType.JSON_UTF_8.withoutParameters().is(type)) && String.class.equals(resultType);
}

From source file:org.sonatype.nexus.repository.storage.DefaultContentValidator.java

/**
 * Removes any parameter (like charset) for simpler matching.
 *///from   ww w . j  a  v  a 2 s.  com
@Nullable
private String mediaTypeWithoutParameters(final String declaredMediaType) {
    if (Strings.isNullOrEmpty(declaredMediaType)) {
        return null;
    }
    try {
        MediaType mediaType = MediaType.parse(declaredMediaType);
        return mediaType.withoutParameters().toString();
    } catch (IllegalArgumentException e) {
        throw new InvalidContentException("Invalid declared contentType: " + declaredMediaType, e);
    }
}

From source file:org.wisdom.content.engines.Engine.java

/**
 * Finds the 'best' content serializer for the given accept headers.
 *
 * @param mediaTypes the ordered set of {@link com.google.common.net.MediaType} from the {@code ACCEPT} header.
 * @return the best serializer from the list matching the {@code ACCEPT} header, {@code null} if none match
 *//*  ww  w  .j a  va 2 s. co m*/
@Override
public ContentSerializer getBestSerializer(Collection<MediaType> mediaTypes) {
    if (mediaTypes == null || mediaTypes.isEmpty()) {
        mediaTypes = ImmutableList.of(MediaType.HTML_UTF_8);
    }
    for (MediaType type : mediaTypes) {
        for (ContentSerializer ser : serializers) {
            MediaType mt = MediaType.parse(ser.getContentType());
            if (mt.is(type.withoutParameters())) {
                return ser;
            }
        }
    }
    return null;
}