Skip to content

Commit

Permalink
ISPN-8427 Support for non-String keys in the rest server
Browse files Browse the repository at this point in the history
  • Loading branch information
Gustavo Fernandes authored and tristantarrant committed Jan 30, 2018
1 parent dbced19 commit ee0a83f
Show file tree
Hide file tree
Showing 59 changed files with 1,758 additions and 602 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String getName() {

@Override
public Object encodeKey(Object key) {
return key;
return key.toString().getBytes();
}

@Override
Expand All @@ -53,7 +53,7 @@ public Object decodeKey(Object key) {

@Override
public Object decodeValue(Object value) {
if (value == null) return null;
if (value == null) return null;
return value instanceof byte[] ? new String((byte[]) value, UTF_8) : value.toString();
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,138 @@
package org.infinispan.commons.dataconversion;

import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OBJECT;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OCTET_STREAM;
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_WWW_FORM_URLENCODED;
import static org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN;

import java.io.IOException;
import java.util.HashSet;
import java.util.Set;

import org.infinispan.commons.logging.Log;
import org.infinispan.commons.logging.LogFactory;

/**
* Handle conversions between text/plain, url-encoded, java objects, and octet-stream contents.
*
* @since 9.2
*/
public final class DefaultTranscoder implements Transcoder {

private static final Log log = LogFactory.getLog(DefaultTranscoder.class);

private static final Set<MediaType> supportedTypes = new HashSet<>();

public static final DefaultTranscoder INSTANCE = new DefaultTranscoder();

private DefaultTranscoder() {
}

static {
supportedTypes.add(APPLICATION_OBJECT);
supportedTypes.add(APPLICATION_OCTET_STREAM);
supportedTypes.add(APPLICATION_WWW_FORM_URLENCODED);
supportedTypes.add(TEXT_PLAIN);
}

@Override
public Object transcode(Object content, MediaType contentType, MediaType destinationType) {
try {
if (destinationType.match(APPLICATION_OCTET_STREAM)) {
return convertToOctetStream(content, contentType, destinationType);
}
if (destinationType.match(APPLICATION_OBJECT)) {
return convertToObject(content, contentType, destinationType);
}
if (destinationType.match(TEXT_PLAIN)) {
return convertToTextPlain(content, contentType, destinationType);
}
if (destinationType.match(APPLICATION_WWW_FORM_URLENCODED)) {
return convertToUrlEncoded(content, contentType);
}
throw log.unsupportedContent(content);
} catch (EncodingException | InterruptedException | IOException e) {
throw log.unsupportedContent(content);
}
}

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);
}

private Object convertToTextPlain(Object content, MediaType contentType, MediaType destinationType) {
if (contentType.match(APPLICATION_OCTET_STREAM)) {
byte[] decoded = StandardConversions.decodeOctetStream(content, destinationType);
return StandardConversions.convertOctetStreamToText(decoded, destinationType);
}
if (contentType.match(APPLICATION_OBJECT)) {
return StandardConversions.convertJavaToText(content, contentType, destinationType);
}
if (contentType.match(TEXT_PLAIN)) {
return StandardConversions.convertTextToText(content, contentType, destinationType);
}
if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) {
return StandardConversions.convertUrlEncodedToText(content, destinationType);
}
throw log.unsupportedContent(content);
}

private Object convertToObject(Object content, MediaType contentType, MediaType destinationType) {
if (contentType.match(APPLICATION_OCTET_STREAM)) {
byte[] decoded = StandardConversions.decodeOctetStream(content, destinationType);
return StandardConversions.convertOctetStreamToJava(decoded, destinationType);
}
if (contentType.match(APPLICATION_OBJECT)) {
return content;
}
if (contentType.match(TEXT_PLAIN)) {
return StandardConversions.convertTextToObject(content, contentType);
}
if (contentType.match(APPLICATION_WWW_FORM_URLENCODED)) {
return StandardConversions.convertUrlEncodedToObject(content);
}
throw log.unsupportedContent(content);
}

public Object convertToOctetStream(Object content, MediaType contentType, MediaType destinationType) throws IOException, InterruptedException {
if (contentType.match(APPLICATION_OCTET_STREAM)) {
return StandardConversions.decodeOctetStream(content, contentType);
}
if (contentType.match(APPLICATION_OBJECT)) {
return StandardConversions.convertJavaToOctetStream(content, contentType);
}
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);
}

@Override
public Set<MediaType> getSupportedMediaTypes() {
return supportedTypes;
}

private boolean in(MediaType mediaType, Set<MediaType> set) {
return set.stream().anyMatch(s -> s.match(mediaType));
}

@Override
public boolean supportsConversion(MediaType mediaType, MediaType other) {
return in(mediaType, supportedTypes) && in(other, supportedTypes);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ public GenericJbossMarshallerEncoder(ClassLoader classLoader) {

@Override
public MediaType getStorageFormat() {
return MediaType.APPLICATION_JBOSS_MARSHALLED;
return MediaType.APPLICATION_JBOSS_MARSHALLING;
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public final class MediaType {
public static MediaType APPLICATION_SERIALIZED_OBJECT = fromString(APPLICATION_SERIALIZED_OBJECT_TYPE);
public static MediaType APPLICATION_XML = fromString(APPLICATION_XML_TYPE);
public static MediaType APPLICATION_PROTOSTREAM = fromString(APPLICATION_PROTOSTREAM_TYPE);
public static MediaType APPLICATION_JBOSS_MARSHALLED = fromString(APPLICATION_JBOSS_MARSHALLING_TYPE);
public static MediaType APPLICATION_JBOSS_MARSHALLING = fromString(APPLICATION_JBOSS_MARSHALLING_TYPE);
public static MediaType APPLICATION_INFINISPAN_MARSHALLED = fromString(APPLICATION_INFINISPAN_MARSHALLING_TYPE);
public static MediaType APPLICATION_WWW_FORM_URLENCODED = fromString(WWW_FORM_URLENCODED_TYPE);
public static MediaType IMAGE_PNG = fromString(IMAGE_PNG_TYPE);
Expand Down Expand Up @@ -222,7 +222,7 @@ public Optional<String> getParameter(String name) {
}

private static String validate(String token) {
if(token == null) throw new NullPointerException("type and subtype cannot be null");
if (token == null) throw new NullPointerException("type and subtype cannot be null");
for (char c : token.toCharArray()) {
if (c < 0x20 || c > 0x7F || INVALID_TOKENS.indexOf(c) > 0) {
throw log.invalidCharMediaType(c, token);
Expand All @@ -231,6 +231,16 @@ private static String validate(String token) {
return token;
}

public MediaType withCharset(Charset charset) {
return withParameter(CHARSET_PARAM_NAME, charset.toString());
}

public MediaType withParameter(String name, String value) {
Map<String, String> newParams = new HashMap<>(params);
newParams.put(name, value);
return new MediaType(type, subType, newParams);
}

public String toStringExcludingParam(String... params) {
if (!hasParameters()) return typeSubtype;
StringBuilder builder = new StringBuilder().append(typeSubtype);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package org.infinispan.commons.dataconversion;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

/**
* Base class for {@link Transcoder} that converts between a single format and multiple other formats and back.
*/
public abstract class OneToManyTranscoder implements Transcoder {

private MediaType mainType;

protected final Set<MediaType> supportedTypes = new HashSet<>();

public OneToManyTranscoder(MediaType mainType, MediaType... supportedConversions) {
this.mainType = mainType;
this.supportedTypes.add(mainType);
this.supportedTypes.addAll(Arrays.asList(supportedConversions));
}

private boolean in(MediaType mediaType, Set<MediaType> set) {
return set.stream().anyMatch(s -> s.match(mediaType));
}

@Override
public Set<MediaType> getSupportedMediaTypes() {
return supportedTypes;
}

@Override
public boolean supportsConversion(MediaType mediaType, MediaType other) {
return mediaType.match(mainType) && in(other, supportedTypes) ||
other.match(mainType) && in(mediaType, supportedTypes);
}

}
Loading

0 comments on commit ee0a83f

Please sign in to comment.