public RestRequest value(Object value, MediaType contentType) { this.value = value; this.contentType = contentType.toString(); return this; }
public static MediaType fromString(String mediaType) { return parse(mediaType); }
private boolean in(MediaType mediaType, Set<MediaType> set) { return set.stream().anyMatch(s -> s.match(mediaType)); }
private static boolean isJavaString(MediaType mediaType) { return mediaType.match(MediaType.APPLICATION_OBJECT) && mediaType.hasStringType(); }
public MediaType dataType() { if (textBasedMedia.contains(dataType.getTypeSubtype())) { return dataType.withClassType(String.class); } return dataType; }
public Object convertToRequestType(Object obj, MediaType objType, MediaType requestType) { if (obj == null) return null; if (requestType.equals(MediaType.MATCH_ALL)) return obj; // Older HR clients do not send request type and assume the script metadata type is the output type MediaType outputFormat = requestType.match(MediaType.APPLICATION_UNKNOWN) ? objType : requestType; OutputFormatter outputFormatter = formatterByMediaType.get(outputFormat.getTypeSubtype()); if (obj instanceof Collection) { if (outputFormatter != null) { return outputFormatter.formatCollection((Collection<?>) obj, objType, requestType); } } Transcoder transcoder = encoderRegistry.getTranscoder(objType, requestType); return transcoder.transcode(obj, objType, requestType); }
/** * Convert text content to a different encoding. * * @param source The source content. * @param sourceType MediaType for the source content. * @param destinationType the MediaType of the converted content. * @return content conforming to the destination MediaType. */ public static Object convertTextToText(Object source, MediaType sourceType, MediaType destinationType) { if (source == null) return null; if (sourceType == null) throw new NullPointerException("MediaType cannot be null!"); if (!sourceType.match(MediaType.TEXT_PLAIN)) throw log.invalidMediaType(TEXT_PLAIN_TYPE, sourceType.toString()); boolean asString = destinationType.hasStringType(); Charset sourceCharset = sourceType.getCharset(); Charset destinationCharset = destinationType.getCharset(); if (sourceCharset.equals(destinationCharset)) return convertTextClass(source, destinationType, asString); byte[] byteContent = source instanceof byte[] ? (byte[]) source : source.toString().getBytes(sourceCharset); return convertTextClass(convertCharset(byteContent, sourceCharset, destinationCharset), destinationType, asString); }
/** * Converts an octet stream to a Java object * * @param source The source to convert * @param destination The type of the converted object. * @return an instance of a java object compatible with the supplied destination type. */ public static Object convertOctetStreamToJava(byte[] source, MediaType destination, Marshaller marshaller) { if (source == null) return null; if (!destination.match(MediaType.APPLICATION_OBJECT)) { throw log.invalidMediaType(APPLICATION_OBJECT_TYPE, destination.toString()); } String classType = destination.getClassType(); if (classType == null) return source; if (classType.equals("ByteArray")) { return source; } if (destination.hasStringType()) { return new String(source, UTF_8); } try { return marshaller.objectFromByteBuffer(source); } catch (IOException | ClassNotFoundException e) { throw log.conversionNotSupported(source, MediaType.APPLICATION_OCTET_STREAM_TYPE, destination.toString()); } }
@Test public void testToString() { assertEquals("application/xml", new MediaType("application", "xml", createMap(new MapEntry("q", "0.9"))).toString()); assertEquals("text/csv", new MediaType("text", "csv").toString()); assertEquals("foo/bar; a=2", new MediaType("foo", "bar", createMap(new MapEntry("a", "2"))).toString()); assertEquals("foo/bar; a=2; b=1; c=2", new MediaType("foo", "bar", createMap(new MapEntry("a", "2"), new MapEntry("b", "1"), new MapEntry("c", "2"))).toString()); assertEquals("a/b; p=1", MediaType.fromString("a/b; p=1; q=2;").toStringExcludingParam("q")); }
MediaType filterMediaType = customFilter.format(); if (filterMediaType != null && filterMediaType.equals(storageMediaType)) { iterationCache = advancedCache.withEncoding(IdentityEncoder.class).withMediaType(filterMediaType.toString(), filterMediaType.toString()); if (filterMediaType != null && !storageMediaType.equals(requestValueType)) { resultTransformer = valueDataConversion::fromStorage;
private static KeyValuePair<String, String> getRequestMediaTypes(HotRodHeader header, Configuration configuration) { String keyRequestType = header == null ? APPLICATION_UNKNOWN_TYPE : header.getKeyMediaType().toString(); String valueRequestType = header == null ? APPLICATION_UNKNOWN_TYPE : header.getValueMediaType().toString(); if (header != null && HotRodVersion.HOTROD_28.isOlder(header.version)) { // Pre-2.8 clients always send protobuf payload to the metadata cache if (header.cacheName.equals(PROTOBUF_METADATA_CACHE_NAME)) { keyRequestType = APPLICATION_PROTOSTREAM_TYPE; valueRequestType = APPLICATION_PROTOSTREAM_TYPE; } else { // Pre-2.8 clients always sent query encoded as protobuf unless object store is used. if (header.op == HotRodOperation.QUERY) { boolean objectStorage = APPLICATION_OBJECT.match(configuration.encoding().valueDataType().mediaType()); keyRequestType = objectStorage ? APPLICATION_JBOSS_MARSHALLING_TYPE : APPLICATION_PROTOSTREAM_TYPE; valueRequestType = objectStorage ? APPLICATION_JBOSS_MARSHALLING_TYPE : APPLICATION_PROTOSTREAM_TYPE; } } } return new KeyValuePair<>(keyRequestType, valueRequestType); }
public FooBarTranscoder() { supportedTypes = new HashSet<>(asList(fromString("application/foo"), fromString("application/bar"))); }
@Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { try { if (destinationType.equals(MediaType.APPLICATION_UNKNOWN) || contentType.equals(MediaType.APPLICATION_UNKNOWN)) { return content; } if (destinationType.match(marshaller.mediaType())) { return marshaller.objectToByteBuffer(content); } if (destinationType.match(MediaType.APPLICATION_OBJECT)) { return marshaller.objectFromByteBuffer((byte[]) content); } } catch (InterruptedException | IOException | ClassNotFoundException e) { throw new CacheException(e); } throw logger.unsupportedContent(content); }
@Test public void testByteArrayKeysAndByteArrayValues() throws Exception { // Write via Hot Rod the byte[] content directly byte[] key = new byte[]{0x13, 0x26}; byte[] value = new byte[]{10, 20}; defaultRemoteCache.put(key, value); assertArrayEquals(defaultRemoteCache.get(key), value); // Read via Rest Object bytesFromRest = new RestRequest().cache(DEFAULT_CACHE_NAME) .key("0x1326", APPLICATION_OCTET_STREAM.withParameter("encoding", "hex").toString()) .accept(APPLICATION_OCTET_STREAM) .read(); assertEquals(bytesFromRest, value); // Write via rest byte[] newKey = new byte[]{0, 0, 0, 1}; // Write via rest new RestRequest().cache(DEFAULT_CACHE_NAME) .key("0x00000001", MediaType.APPLICATION_OCTET_STREAM.withParameter("encoding", "hex").toString()) .value(value) .write(); // Read via Hot Rod assertEquals(defaultRemoteCache.get(newKey), value); }
@Test public void testWildCard() { MediaType mediaType = MediaType.fromString("*/*"); assertEquals("*", mediaType.getType()); assertEquals("*", mediaType.getSubType()); assertTrue(mediaType.match(MediaType.TEXT_PLAIN)); assertTrue(mediaType.match(MediaType.APPLICATION_PROTOSTREAM)); }
private void writeMediaType(ByteBuf buf, MediaType mediaType) { if (mediaType == null) { buf.writeByte(0); } else { Short id = MediaTypeIds.getId(mediaType); if (id != null) { buf.writeByte(1); VInt.write(buf, id); } else { buf.writeByte(2); ExtendedByteBuf.writeString(mediaType.toString(), buf); } Map<String, String> parameters = mediaType.getParameters(); VInt.write(buf, parameters.size()); parameters.forEach((key, value) -> { ExtendedByteBuf.writeString(key, buf); ExtendedByteBuf.writeString(value, buf); }); } }
case 22: hr2x_mediaTypeDescription = MediaTypeIds.getMediaType((short) hr2x_mediaTypeId).withParameters(hr2x_mediaTypeParams); hr2x_mediaType = hr2x_mediaTypeDescription; hr2x_keyType = hr2x_mediaType; case 30: hr2x_mediaTypeDescription = MediaType.parse(hr2x_mediaTypeName).withParameters(hr2x_mediaTypeParams); hr2x_mediaType = hr2x_mediaTypeDescription; hr2x_keyType = hr2x_mediaType; case 41: hr2x_mediaTypeDescription = MediaTypeIds.getMediaType((short) hr2x_mediaTypeId).withParameters(hr2x_mediaTypeParams); hr2x_mediaType = hr2x_mediaTypeDescription; hr2x_valueType = hr2x_mediaType; case 49: hr2x_mediaTypeDescription = MediaType.parse(hr2x_mediaTypeName).withParameters(hr2x_mediaTypeParams); hr2x_mediaType = hr2x_mediaTypeDescription; hr2x_valueType = hr2x_mediaType;
@Test public void testParseWeight() { MediaType mediaType = MediaType.fromString("application/json ; q=0.8"); assertEquals(0.8, mediaType.getWeight(), 0.0); }
@Test public void testParseBrowserRequest() { Stream<MediaType> list = MediaType.parseList("text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8"); Iterator<MediaType> iterator = list.iterator(); assertEquals("text/html", iterator.next().getTypeSubtype()); assertEquals("application/xhtml+xml", iterator.next().getTypeSubtype()); assertEquals("application/xml", iterator.next().getTypeSubtype()); assertEquals("*/*", iterator.next().getTypeSubtype()); }
@Test public void testWildCard2() { MediaType mediaType = MediaType.fromString("*"); assertEquals("*", mediaType.getType()); assertEquals("*", mediaType.getSubType()); }