private boolean in(MediaType mediaType, Set<MediaType> set) { return set.stream().anyMatch(s -> s.match(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(); }
private Object convertToUrlEncoded(Object content, MediaType contentType) { if (contentType.match(APPLICATION_OCTET_STREAM)) { return StandardConversions.convertOctetStreamToUrlEncoded(content, contentType); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.convertUrlEncodedToObject(content); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToUrlEncoded(content, contentType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return content; } throw log.unsupportedContent(content); }
default boolean supports(MediaType mediaType) { return getSupportedMediaTypes().stream().anyMatch(m -> m.match(mediaType)); }
public Object convertToOctetStream(Object content, MediaType contentType, MediaType destinationType) throws IOException, InterruptedException { if (contentType.match(APPLICATION_OCTET_STREAM) || contentType.match(APPLICATION_UNKNOWN)) { return StandardConversions.decodeOctetStream(content, contentType); } if (contentType.match(APPLICATION_OBJECT)) { return StandardConversions.convertJavaToOctetStream(content, contentType, javaMarshaller); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToOctetStream(content, destinationType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToOctetStream(content); } throw log.unsupportedContent(content); }
/** * @return true if the transcoder supports the conversion between supplied {@link MediaType}. */ default boolean supportsConversion(MediaType mediaType, MediaType other) { return !mediaType.match(other) && supports(mediaType) && supports(other); }
private Object convertToByteArray(Object content, MediaType contentType) { try { if (contentType.match(APPLICATION_OCTET_STREAM)) { return StandardConversions.decodeOctetStream(content, contentType); } if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) { return StandardConversions.convertUrlEncodedToOctetStream(content); } if (contentType.match(TEXT_PLAIN)) { return StandardConversions.convertTextToOctetStream(content, contentType); } return StandardConversions.convertJavaToOctetStream(content, contentType, jbossMarshaller); } catch (EncodingException | InterruptedException | IOException e) { throw log.unsupportedContent(content); } }
@Test public void testTranscoderSupportedMediaTypes() { List<MediaType> supportedMediaTypesList = new ArrayList<>(supportedMediaTypes); assertTrue(supportedMediaTypesList.size() >= 2, "Must be at least 2 supported MediaTypes"); assertFalse(supportedMediaTypesList.get(0).match(supportedMediaTypesList.get(1)), "Supported MediaTypes Must be different"); }
boolean isObjectStorage() { MediaType storageMediaType = cache.getAdvancedCache().getValueDataConversion().getStorageMediaType(); return APPLICATION_OBJECT.match(storageMediaType); }
@Override public Object transcode(Object content, MediaType contentType, MediaType destinationType) { if (destinationType.match(APPLICATION_OBJECT)) { try { return parser.parse((String) content); } catch (Exception e) { e.printStackTrace(); } } if (destinationType.match(APPLICATION_XML)) { try { return parser.serialize((Map<String, String>) content); } catch (Exception e) { e.printStackTrace(); } } return null; }
@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); }
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); }
@Test public void testMediaTypeMatchItself() { MediaType one = MediaType.APPLICATION_INFINISPAN_BINARY; assertTrue(one.match(one)); }
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); }