-
Notifications
You must be signed in to change notification settings - Fork 612
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
ffcfc39
commit e9c1484
Showing
11 changed files
with
273 additions
and
6 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
47 changes: 47 additions & 0 deletions
47
server/rest/src/main/java/org/infinispan/rest/operations/mediatypes/EntrySetFormatter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package org.infinispan.rest.operations.mediatypes; | ||
|
||
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_JSON_TYPE; | ||
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_OCTET_STREAM_TYPE; | ||
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_SERIALIZED_OBJECT_TYPE; | ||
import static org.infinispan.commons.dataconversion.MediaType.APPLICATION_XML_TYPE; | ||
import static org.infinispan.commons.dataconversion.MediaType.IMAGE_PNG_TYPE; | ||
import static org.infinispan.commons.dataconversion.MediaType.MATCH_ALL_TYPE; | ||
import static org.infinispan.commons.dataconversion.MediaType.TEXT_PLAIN_TYPE; | ||
|
||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
import org.infinispan.commons.dataconversion.MediaType; | ||
import org.infinispan.rest.operations.mediatypes.impl.BinaryOutputPrinter; | ||
import org.infinispan.rest.operations.mediatypes.impl.JSONOutputPrinter; | ||
import org.infinispan.rest.operations.mediatypes.impl.TextOutputPrinter; | ||
import org.infinispan.rest.operations.mediatypes.impl.XMLOutputPrinter; | ||
|
||
/** | ||
* Formats collections of entries based on the associated {@link MediaType}; | ||
* | ||
*/ | ||
public class EntrySetFormatter { | ||
|
||
private static Map<String, OutputPrinter> printerByMediaType = new HashMap<>(10); | ||
|
||
static { | ||
TextOutputPrinter textOutputPrinter = new TextOutputPrinter(); | ||
XMLOutputPrinter xmlOutputPrinter = new XMLOutputPrinter(); | ||
JSONOutputPrinter jsonOutputPrinter = new JSONOutputPrinter(); | ||
BinaryOutputPrinter binaryOutputPrinter = new BinaryOutputPrinter(); | ||
|
||
printerByMediaType.put(TEXT_PLAIN_TYPE, textOutputPrinter); | ||
printerByMediaType.put(APPLICATION_XML_TYPE, xmlOutputPrinter); | ||
printerByMediaType.put(APPLICATION_JSON_TYPE, jsonOutputPrinter); | ||
printerByMediaType.put(APPLICATION_OCTET_STREAM_TYPE, binaryOutputPrinter); | ||
printerByMediaType.put(IMAGE_PNG_TYPE, binaryOutputPrinter); | ||
printerByMediaType.put(APPLICATION_SERIALIZED_OBJECT_TYPE, binaryOutputPrinter); | ||
printerByMediaType.put(MATCH_ALL_TYPE, textOutputPrinter); | ||
} | ||
|
||
public static OutputPrinter forMediaType(MediaType mediaType) { | ||
return printerByMediaType.get(mediaType.getTypeSubtype()); | ||
} | ||
|
||
} |
42 changes: 42 additions & 0 deletions
42
server/rest/src/main/java/org/infinispan/rest/operations/mediatypes/OutputPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package org.infinispan.rest.operations.mediatypes; | ||
|
||
import java.io.UnsupportedEncodingException; | ||
|
||
import org.infinispan.CacheSet; | ||
import org.infinispan.commons.dataconversion.EncodingException; | ||
import org.infinispan.rest.operations.exceptions.ServerInternalException; | ||
|
||
/** | ||
* Converts binary array from {@link org.infinispan.Cache} into output format. | ||
* | ||
* <p> | ||
* In order to avoid unnecessary conversion steps, all methods need to return a byte array. This way | ||
* Netty doesn't need to do any conversion - it just wraps it into a {@link io.netty.buffer.ByteBuf}. | ||
* </p> | ||
* | ||
* @author Sebastian Łaskawiec | ||
*/ | ||
public interface OutputPrinter { | ||
|
||
/** | ||
* Converts all values in the cache to a desired output format. | ||
* | ||
* @param cacheName Cache name (sometimes might be used as xml or json key). | ||
* @param cacheSet Key Set. | ||
* @param charset Desired {@link Charset} | ||
* @return Byte array representation of converted values. | ||
* @throws ServerInternalException Thrown if conversion was not successful. | ||
*/ | ||
byte[] print(String cacheName, CacheSet<?> cacheSet, Charset charset) throws ServerInternalException; | ||
|
||
default String asString(Object k) { | ||
try { | ||
if (k instanceof byte[]) { | ||
return new String((byte[]) k, "UTF-8"); | ||
} | ||
} catch (UnsupportedEncodingException e) { | ||
throw new EncodingException("Cannot convert key to string", e); | ||
} | ||
return k.toString(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
...est/src/main/java/org/infinispan/rest/operations/mediatypes/impl/BinaryOutputPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
package org.infinispan.rest.operations.mediatypes.impl; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import org.infinispan.CacheSet; | ||
import org.infinispan.commons.dataconversion.StandardConversions; | ||
import org.infinispan.rest.operations.mediatypes.Charset; | ||
import org.infinispan.rest.operations.mediatypes.OutputPrinter; | ||
|
||
/** | ||
* {@link OutputPrinter} for binary values. | ||
* | ||
* @author Sebastian Łaskawiec | ||
*/ | ||
public class BinaryOutputPrinter implements OutputPrinter { | ||
|
||
@Override | ||
public byte[] print(String cacheName, CacheSet<?> keys, Charset charset) { | ||
return Arrays.stream(keys.toArray()) | ||
.map(k -> (byte[]) k) | ||
.map(StandardConversions::bytesToHex) | ||
.collect(Collectors.joining("\n", "", "")) | ||
.getBytes(charset.getJavaCharset()); | ||
} | ||
} |
44 changes: 44 additions & 0 deletions
44
server/rest/src/main/java/org/infinispan/rest/operations/mediatypes/impl/Escaper.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package org.infinispan.rest.operations.mediatypes.impl; | ||
|
||
public class Escaper { | ||
|
||
private Escaper() { | ||
|
||
} | ||
|
||
static String escapeHtml(String html) { | ||
return escapeXml(html); | ||
} | ||
|
||
static String escapeXml(String xml) { | ||
StringBuilder sb = new StringBuilder(); | ||
for (char c : xml.toCharArray()) { | ||
switch (c) { | ||
case '&': | ||
sb.append("&"); | ||
break; | ||
case '>': | ||
sb.append(">"); | ||
break; | ||
case '<': | ||
sb.append("<"); | ||
break; | ||
case '\"': | ||
sb.append("""); | ||
break; | ||
case '\'': | ||
sb.append("'"); | ||
break; | ||
default: | ||
sb.append(c); | ||
break; | ||
} | ||
} | ||
return sb.toString(); | ||
} | ||
|
||
static String escapeJson(String json) { | ||
return json.replaceAll("\"", "\\\\\""); | ||
} | ||
|
||
} |
29 changes: 29 additions & 0 deletions
29
.../rest/src/main/java/org/infinispan/rest/operations/mediatypes/impl/JSONOutputPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
package org.infinispan.rest.operations.mediatypes.impl; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import org.infinispan.CacheSet; | ||
import org.infinispan.rest.logging.Log; | ||
import org.infinispan.rest.operations.mediatypes.Charset; | ||
import org.infinispan.rest.operations.mediatypes.OutputPrinter; | ||
import org.infinispan.util.logging.LogFactory; | ||
|
||
/** | ||
* {@link OutputPrinter} for JSON values. | ||
* | ||
* @author Sebastian Łaskawiec | ||
*/ | ||
public class JSONOutputPrinter implements OutputPrinter { | ||
|
||
protected final static Log logger = LogFactory.getLog(JSONOutputPrinter.class, Log.class); | ||
|
||
@Override | ||
public byte[] print(String cacheName, CacheSet<?> keys, Charset charset) { | ||
return Arrays.stream(keys.toArray()) | ||
.map(this::asString) | ||
.collect(Collectors.joining(",", "keys=[", "]")) | ||
.getBytes(charset.getJavaCharset()); | ||
} | ||
|
||
} |
24 changes: 24 additions & 0 deletions
24
.../rest/src/main/java/org/infinispan/rest/operations/mediatypes/impl/TextOutputPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
package org.infinispan.rest.operations.mediatypes.impl; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import org.infinispan.CacheSet; | ||
import org.infinispan.rest.operations.mediatypes.Charset; | ||
import org.infinispan.rest.operations.mediatypes.OutputPrinter; | ||
|
||
/** | ||
* {@link OutputPrinter} for text values. | ||
* | ||
* @author Sebastian Łaskawiec | ||
*/ | ||
public class TextOutputPrinter implements OutputPrinter { | ||
|
||
@Override | ||
public byte[] print(String cacheName, CacheSet<?> keys, Charset charset) { | ||
return Arrays.stream(keys.toArray()).map(this::asString) | ||
.collect(Collectors.joining("\n", "", "")) | ||
.getBytes(charset.getJavaCharset()); | ||
} | ||
|
||
} |
31 changes: 31 additions & 0 deletions
31
...r/rest/src/main/java/org/infinispan/rest/operations/mediatypes/impl/XMLOutputPrinter.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
package org.infinispan.rest.operations.mediatypes.impl; | ||
|
||
import java.util.Arrays; | ||
import java.util.stream.Collectors; | ||
|
||
import org.infinispan.CacheSet; | ||
import org.infinispan.rest.logging.Log; | ||
import org.infinispan.rest.operations.mediatypes.Charset; | ||
import org.infinispan.rest.operations.mediatypes.OutputPrinter; | ||
import org.infinispan.util.logging.LogFactory; | ||
|
||
/** | ||
* {@link OutputPrinter} for xml values. | ||
* | ||
* @author Sebastian Łaskawiec | ||
*/ | ||
public class XMLOutputPrinter implements OutputPrinter { | ||
|
||
protected final static Log logger = LogFactory.getLog(JSONOutputPrinter.class, Log.class); | ||
|
||
@Override | ||
public byte[] print(String cacheName, CacheSet<?> keys, Charset charset) { | ||
return Arrays.stream(keys.toArray()) | ||
.map(this::asString) | ||
.map(Escaper::escapeXml) | ||
.map(s -> "<key>" + s + "</key>") | ||
.collect(Collectors.joining("", "<?xml version=\"1.0\" encoding=\"UTF-8\"?><keys>", "</keys>")) | ||
.getBytes(charset.getJavaCharset()); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters