/** * Specifies the resource media type string. * * @param mediaType The resource media type string. * @return This object (for method chaining). */ public Builder mediaType(String mediaType) { this.mediaType = MediaType.forString(mediaType); return this; }
/** * Compares two MediaRanges for equality. * * <p> * The values are first compared according to <code>qValue</code> values. * Should those values be equal, the <code>type</code> is then lexicographically compared (case-insensitive) in * ascending order, with the <js>"*"</js> type demoted last in that order. * <code>MediaRanges</code> with the same type but different sub-types are compared - a more specific subtype is * promoted over the 'wildcard' subtype. * <code>MediaRanges</code> with the same types but with extensions are promoted over those same types with no * extensions. * * @param o The range to compare to. Never <jk>null</jk>. */ @Override /* Comparable */ public int compareTo(MediaTypeRange o) { // Compare q-values. int qCompare = Float.compare(o.qValue, qValue); if (qCompare != 0) return qCompare; // Compare media-types. // Note that '*' comes alphabetically before letters, so just do a reverse-alphabetical comparison. int i = o.mediaType.toString().compareTo(mediaType.toString()); return i; } }
@Override public MediaType[] forMediaTypes() { return MediaType.forStrings("*/x-www-form-urlencoded"); } @Override
listener = getClassProperty(SERIALIZER_listener, SerializerListener.class, null); this.produces = MediaType.forString(produces); this.accept = accept == null ? MediaTypeRange.parse(produces) : MediaTypeRange.parse(accept); this.accepts = accept == null ? new MediaType[] {this.produces} : MediaType.forStrings(StringUtils.split(accept, ','));
@SuppressWarnings("rawtypes") private static Locale getLocale(MultivaluedMap headers) { if (headers.containsKey("Accept-Language") && headers.get("Accept-Language") != null) { String h = String.valueOf(headers.get("Accept-Language")); if (h != null) { MediaTypeRange[] mr = MediaTypeRange.parse(h); if (mr.length > 0) return toLocale(mr[0].getMediaType().getType()); } } return null; }
/** * Given a list of media types, returns the best match for this <code>Content-Type</code> header. * * <p> * Note that fuzzy matching is allowed on the media types where the <code>Content-Types</code> header may * contain additional subtype parts. * <br>For example, given a <code>Content-Type</code> value of <js>"text/json+activity"</js>, * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js> * isn't found. * <br>The purpose for this is to allow parsers to match when artifacts such as <code>id</code> properties are * present in the header. * * @param mediaTypes The media types to match against. * @return The index into the array of the best match, or <code>-1</code> if no suitable matches could be found. */ public int findMatch(MediaType[] mediaTypes) { int matchQuant = 0, matchIndex = -1; for (int i = 0; i < mediaTypes.length; i++) { MediaType mt = mediaTypes[i]; int matchQuant2 = mt.match(this, true); if (matchQuant2 > matchQuant) { matchQuant = matchQuant2; matchIndex = i; } } return matchIndex; } }
/** * Convenience method for searching through all of the subtypes of all the media ranges in this header for the * presence of a subtype fragment. * * <p> * For example, given the header <js>"text/json+activity"</js>, calling * <code>hasSubtypePart(<js>"activity"</js>)</code> returns <jk>true</jk>. * * @param part The media type subtype fragment. * @return <jk>true</jk> if subtype fragment exists. */ public boolean hasSubtypePart(String part) { for (MediaTypeRange mr : this.mediaRanges) if (mr.getQValue() > 0 && mr.getMediaType().getSubTypes().indexOf(part) >= 0) return true; return false; }
/** * Returns a hash based on this instance's <code>media-type</code>. * * @return A hash based on this instance's <code>media-type</code>. */ @Override /* Object */ public int hashCode() { return mediaType.hashCode(); }
/** * Same as {@link #toString()} but strips comments from the text before returning it. * * <p> * Supports stripping comments from the following media types: HTML, XHTML, XML, JSON, Javascript, CSS. * * @return The resource contents stripped of any comments. */ public String toCommentStrippedString() { String s = toString(); String subType = mediaType.getSubType(); if ("html".equals(subType) || "xhtml".equals(subType) || "xml".equals(subType)) s = s.replaceAll("(?s)<!--(.*?)-->\\s*", ""); else if ("json".equals(subType) || "javascript".equals(subType) || "css".equals(subType)) s = s.replaceAll("(?s)\\/\\*(.*?)\\*\\/\\s*", ""); return s; }
/** * Returns <jk>true</jk> if the specified object is also a <code>MediaType</code>, and has the same qValue, type, * parameters, and extensions. * * @return <jk>true</jk> if object is equivalent. */ @Override /* Object */ public boolean equals(Object o) { if (o == null || !(o instanceof MediaTypeRange)) return false; if (this == o) return true; MediaTypeRange o2 = (MediaTypeRange) o; return qValue.equals(o2.qValue) && mediaType.equals(o2.mediaType) && extensions.equals(o2.extensions); }
if (mt != null) return mt; mt = new MediaType(s); if (NOCACHE) return mt;
listener = getClassProperty(SERIALIZER_listener, SerializerListener.class, null); this.produces = MediaType.forString(produces); this.accept = accept == null ? MediaTypeRange.parse(produces) : MediaTypeRange.parse(accept); this.accepts = accept == null ? new MediaType[] {this.produces} : MediaType.forStrings(StringUtils.split(accept, ','));
@SuppressWarnings("rawtypes") private static Locale getLocale(MultivaluedMap headers) { if (headers.containsKey("Accept-Language") && headers.get("Accept-Language") != null) { String h = String.valueOf(headers.get("Accept-Language")); if (h != null) { MediaTypeRange[] mr = MediaTypeRange.parse(h); if (mr.length > 0) return toLocale(mr[0].getMediaType().getType()); } } return null; }
/** * Given a list of media types, returns the best match for this <code>Content-Type</code> header. * * <p> * Note that fuzzy matching is allowed on the media types where the <code>Content-Types</code> header may * contain additional subtype parts. * <br>For example, given a <code>Content-Type</code> value of <js>"text/json+activity"</js>, * the media type <js>"text/json"</js> will match if <js>"text/json+activity"</js> or <js>"text/activity+json"</js> * isn't found. * <br>The purpose for this is to allow parsers to match when artifacts such as <code>id</code> properties are * present in the header. * * @param mediaTypes The media types to match against. * @return The index into the array of the best match, or <code>-1</code> if no suitable matches could be found. */ public int findMatch(MediaType[] mediaTypes) { int matchQuant = 0, matchIndex = -1; for (int i = 0; i < mediaTypes.length; i++) { MediaType mt = mediaTypes[i]; int matchQuant2 = mt.match(this, true); if (matchQuant2 > matchQuant) { matchQuant = matchQuant2; matchIndex = i; } } return matchIndex; } }
/** * Convenience method for searching through all of the subtypes of all the media ranges in this header for the * presence of a subtype fragment. * * <p> * For example, given the header <js>"text/json+activity"</js>, calling * <code>hasSubtypePart(<js>"activity"</js>)</code> returns <jk>true</jk>. * * @param part The media type subtype fragment. * @return <jk>true</jk> if subtype fragment exists. */ public boolean hasSubtypePart(String part) { for (MediaTypeRange mr : this.mediaRanges) if (mr.getQValue() > 0 && mr.getMediaType().getSubTypes().indexOf(part) >= 0) return true; return false; }
/** * Returns a hash based on this instance's <code>media-type</code>. * * @return A hash based on this instance's <code>media-type</code>. */ @Override /* Object */ public int hashCode() { return mediaType.hashCode(); }
/** * Same as {@link #toString()} but strips comments from the text before returning it. * * <p> * Supports stripping comments from the following media types: HTML, XHTML, XML, JSON, Javascript, CSS. * * @return The resource contents stripped of any comments. */ public String toCommentStrippedString() { String s = toString(); String subType = mediaType.getSubType(); if ("html".equals(subType) || "xhtml".equals(subType) || "xml".equals(subType)) s = s.replaceAll("(?s)<!--(.*?)-->\\s*", ""); else if ("json".equals(subType) || "javascript".equals(subType) || "css".equals(subType)) s = s.replaceAll("(?s)\\/\\*(.*?)\\*\\/\\s*", ""); return s; }
/** * Returns <jk>true</jk> if the specified object is also a <code>MediaType</code>, and has the same qValue, type, * parameters, and extensions. * * @return <jk>true</jk> if object is equivalent. */ @Override /* Object */ public boolean equals(Object o) { if (o == null || !(o instanceof MediaTypeRange)) return false; if (this == o) return true; MediaTypeRange o2 = (MediaTypeRange) o; return qValue.equals(o2.qValue) && mediaType.equals(o2.mediaType) && extensions.equals(o2.extensions); }
if (mt != null) return mt; mt = new MediaType(s); if (NOCACHE) return mt;
/** * Specifies the resource media type string. * * @param mediaType The resource media type string. * @return This object (for method chaining). */ public Builder mediaType(String mediaType) { this.mediaType = MediaType.forString(mediaType); return this; }