Skip to content

Commit

Permalink
ISPN-4113 Introduce a rawValues configuration property for the RestSt…
Browse files Browse the repository at this point in the history
…ore to tell it not to marshall/unmarshall data (in the context of Rolling Upgrades)
  • Loading branch information
tristantarrant authored and pruivo committed Mar 20, 2014
1 parent fd413fd commit 6b42803
Show file tree
Hide file tree
Showing 7 changed files with 49 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ public class RestStore implements AdvancedLoadWriteStore {
private PoolingClientConnectionManager connectionManager;
private String path;
private MetadataHelper metadataHelper;
private URLCodec urlCodec = new URLCodec();
private final URLCodec urlCodec = new URLCodec();
private InitializationContext ctx;
private HttpHost httpHost;

Expand Down Expand Up @@ -138,20 +138,31 @@ private String keyToUri(Object key) {
}

private byte[] marshall(String contentType, MarshalledEntry entry) throws IOException, InterruptedException {
if (contentType.startsWith("text/")) {
if (configuration.rawValues()) {
return (byte[]) entry.getValue();
} else {
if (isTextContentType(contentType)) {
return (byte[]) entry.getValue();
}
return ctx.getMarshaller().objectToByteBuffer(entry.getValue());
}
return ctx.getMarshaller().objectToByteBuffer(entry.getValue());
}

private Object unmarshall(String contentType, byte[] b) throws IOException, ClassNotFoundException {
if (contentType.startsWith("text/")) {
return new String(b); // TODO: use response header Content Encoding
if (configuration.rawValues()) {
return b;
} else {
return ctx.getMarshaller().objectFromByteBuffer(b);
if (isTextContentType(contentType)) {
return new String(b); // TODO: use response header Content Encoding
} else {
return ctx.getMarshaller().objectFromByteBuffer(b);
}
}
}

private boolean isTextContentType(String contentType) {
return contentType.startsWith("text/") || "application/xml".equals(contentType) || "application/json".equals(contentType);
}

@Override
public void write(MarshalledEntry entry) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,4 +62,9 @@ public RestStoreConfigurationBuilder path(String path) {
public RestStoreConfigurationBuilder appendCacheNameToPath(boolean appendCacheNameToPath) {
return builder.appendCacheNameToPath(appendCacheNameToPath);
}

@Override
public RestStoreConfigurationBuilder rawValues(boolean rawValues) {
return builder.rawValues(rawValues);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,13 @@ public class RestStoreConfiguration extends AbstractStoreConfiguration {
private final int port;
private final String path;
private final boolean appendCacheNameToPath;
private final boolean rawValues;

public RestStoreConfiguration(boolean purgeOnStartup, boolean fetchPersistentState, boolean ignoreModifications,
AsyncStoreConfiguration async, SingletonStoreConfiguration singletonStore,
boolean preload, boolean shared, Properties properties,
ConnectionPoolConfiguration connectionPool, String key2StringMapper,
String metadataHelper, String host, int port, String path, boolean appendCacheNameToPath) {
String metadataHelper, String host, int port, String path, boolean appendCacheNameToPath, boolean rawValues) {
super(purgeOnStartup, fetchPersistentState, ignoreModifications, async, singletonStore, preload, shared, properties);
this.connectionPool = connectionPool;
this.key2StringMapper = key2StringMapper;
Expand All @@ -40,6 +41,7 @@ public RestStoreConfiguration(boolean purgeOnStartup, boolean fetchPersistentSta
this.port = port;
this.path = path;
this.appendCacheNameToPath = appendCacheNameToPath;
this.rawValues = rawValues;
}

public ConnectionPoolConfiguration connectionPool() {
Expand Down Expand Up @@ -70,9 +72,15 @@ public boolean appendCacheNameToPath() {
return appendCacheNameToPath;
}

public boolean rawValues() {
return rawValues;
}

@Override
public String toString() {
return "RestStoreConfiguration [connectionPool=" + connectionPool + ", key2StringMapper=" + key2StringMapper + ", metadataHelper=" + metadataHelper + ", host=" + host
+ ", port=" + port + ", path=" + path + ", appendCacheNameToPath=" + appendCacheNameToPath + ", " + super.toString() + "]";
return "RestStoreConfiguration [connectionPool=" + connectionPool + ", key2StringMapper=" + key2StringMapper
+ ", metadataHelper=" + metadataHelper + ", host=" + host + ", port=" + port + ", path=" + path
+ ", appendCacheNameToPath=" + appendCacheNameToPath + ", rawValues=" + rawValues + ", "
+ super.toString() + "]";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class RestStoreConfigurationBuilder extends AbstractStoreConfigurationBui
private String host;
private int port = 80;
private boolean appendCacheNameToPath = false;
private boolean rawValues = false;

public RestStoreConfigurationBuilder(PersistenceConfigurationBuilder builder) {
super(builder);
Expand Down Expand Up @@ -90,11 +91,17 @@ public RestStoreConfigurationBuilder appendCacheNameToPath(boolean appendCacheNa
return this;
}

@Override
public RestStoreConfigurationBuilder rawValues(boolean rawValues) {
this.rawValues = rawValues;
return this;
}

@Override
public RestStoreConfiguration create() {
return new RestStoreConfiguration(purgeOnStartup, fetchPersistentState, ignoreModifications, async.create(),
singletonStore.create(), preload, shared, properties, connectionPool.create(),
key2StringMapper, metadataHelper, host, port, path, appendCacheNameToPath);
key2StringMapper, metadataHelper, host, port, path, appendCacheNameToPath, rawValues);
}

@Override
Expand All @@ -108,6 +115,7 @@ public RestStoreConfigurationBuilder read(RestStoreConfiguration template) {
this.appendCacheNameToPath = template.appendCacheNameToPath();
this.key2StringMapper = template.key2StringMapper();
this.metadataHelper = template.metadataHelper();
this.rawValues = template.rawValues();

return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,4 +61,9 @@ public interface RestStoreConfigurationChildBuilder<S> extends StoreConfiguratio
* Determines whether to append the cache name to the path URI. Defaults to false.
*/
RestStoreConfigurationBuilder appendCacheNameToPath(boolean appendCacheNameToPath);

/**
* Reads/writes "raw" values to the REST server instead of marshalling (used by the rolling upgrades feature)
*/
RestStoreConfigurationBuilder rawValues(boolean rawValues);
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ public void setup() throws Exception {

ConfigurationBuilder targetConfigurationBuilder = TestCacheManagerFactory.getDefaultCacheConfiguration(false);
targetConfigurationBuilder.persistence().addStore(RestStoreConfigurationBuilder.class).host("localhost").port(sourceServer.getPort())
.path("/rest/" + BasicCacheContainer.DEFAULT_CACHE_NAME).metadataHelper(MimeMetadataHelper.class).locking().isolationLevel(IsolationLevel.NONE);
.path("/rest/" + BasicCacheContainer.DEFAULT_CACHE_NAME).metadataHelper(MimeMetadataHelper.class).rawValues(true).locking().isolationLevel(IsolationLevel.NONE);

targetContainer = TestCacheManagerFactory.createCacheManager(targetConfigurationBuilder);
targetServerCache = targetContainer.getCache();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -778,6 +778,7 @@ public void inject(OutboundSocketBinding value) {
if (store.hasDefined(ModelKeys.PATH)) {
builder.path(store.get(ModelKeys.PATH).asString());
}
builder.rawValues(true);
builder.metadataHelper(MimeMetadataHelper.class);

if (store.hasDefined(ModelKeys.CONNECTION_POOL)) {
Expand Down

0 comments on commit 6b42803

Please sign in to comment.