Skip to content

Commit

Permalink
#47 Moved code for storing the resource ID as a custom token up to Ab…
Browse files Browse the repository at this point in the history
…stractResourceCommand
  • Loading branch information
rjrudin committed Aug 13, 2015
1 parent 98c4d6c commit aba8489
Show file tree
Hide file tree
Showing 3 changed files with 50 additions and 34 deletions.
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package com.rjrudin.marklogic.appdeployer.command;

import java.io.File;
import java.net.URI;
import java.util.Arrays;

import com.rjrudin.marklogic.mgmt.ResourceManager;
Expand All @@ -15,6 +16,7 @@ public abstract class AbstractResourceCommand extends AbstractCommand implements

private boolean deleteResourcesOnUndo = true;
private boolean restartAfterDelete = false;
private boolean storeResourceIdsAsCustomTokens = false;

protected abstract File getResourcesDir(CommandContext context);

Expand All @@ -33,6 +35,7 @@ public void execute(CommandContext context) {
for (File f : listFilesInDirectory(resourceDir)) {
if (isResourceFile(f)) {
SaveReceipt receipt = saveResource(mgr, context, f);

afterResourceSaved(mgr, context, f, receipt);
}
}
Expand All @@ -56,7 +59,11 @@ protected File[] listFilesInDirectory(File dir) {
protected SaveReceipt saveResource(ResourceManager mgr, CommandContext context, File f) {
String payload = copyFileToString(f);
payload = tokenReplacer.replaceTokens(payload, context.getAppConfig(), false);
return mgr.save(payload);
SaveReceipt receipt = mgr.save(payload);
if (storeResourceIdsAsCustomTokens) {
storeTokenForResourceId(receipt, context);
}
return receipt;
}

/**
Expand All @@ -72,6 +79,39 @@ protected void afterResourceSaved(ResourceManager mgr, CommandContext context, F

}

/**
* Any resource that may be referenced by its ID by another resource will most likely need its ID stored as a custom
* token so that it can be referenced by the other resource. To enable this, the subclass should set
* storeResourceIdAsCustomToken to true.
*
* @param receipt
* @param context
*/
protected void storeTokenForResourceId(SaveReceipt receipt, CommandContext context) {
URI location = receipt.getResponse().getHeaders().getLocation();

String idValue = null;
String resourceName = null;

if (location != null) {
String[] tokens = location.getPath().split("/");
idValue = tokens[tokens.length - 1];
resourceName = tokens[tokens.length - 2];
} else {
String[] tokens = receipt.getPath().split("/");
// Path is expected to end in /(resources-name)/(id)/properties
idValue = tokens[tokens.length - 2];
resourceName = tokens[tokens.length - 3];
}

String key = resourceName + "-id-" + receipt.getResourceId();
if (logger.isInfoEnabled()) {
logger.info(format("Storing token with key '%s' and value '%s'", key, idValue));
}

context.getAppConfig().getCustomTokens().put(key, idValue);
}

@Override
public void undo(CommandContext context) {
if (deleteResourcesOnUndo) {
Expand Down Expand Up @@ -108,4 +148,8 @@ public void setDeleteResourcesOnUndo(boolean deleteResourceOnUndo) {
public void setRestartAfterDelete(boolean restartAfterDelete) {
this.restartAfterDelete = restartAfterDelete;
}

public void setStoreResourceIdsAsCustomTokens(boolean storeResourceIdsAsCustomTokens) {
this.storeResourceIdsAsCustomTokens = storeResourceIdsAsCustomTokens;
}
}
Original file line number Diff line number Diff line change
@@ -1,20 +1,20 @@
package com.rjrudin.marklogic.appdeployer.command.security;

import java.io.File;
import java.net.URI;
import java.util.Map;

import com.rjrudin.marklogic.appdeployer.command.AbstractResourceCommand;
import com.rjrudin.marklogic.appdeployer.command.CommandContext;
import com.rjrudin.marklogic.appdeployer.command.SortOrderConstants;
import com.rjrudin.marklogic.mgmt.ResourceManager;
import com.rjrudin.marklogic.mgmt.SaveReceipt;
import com.rjrudin.marklogic.mgmt.security.CertificateTemplateManager;

public class CreateCertificateTemplatesCommand extends AbstractResourceCommand {

public CreateCertificateTemplatesCommand() {
setExecuteSortOrder(SortOrderConstants.CREATE_CERTIFICATE_TEMPLATES);

// Since an HTTP server file needs to refer to a certificate template by its ID, this is set to true
setStoreResourceIdsAsCustomTokens(true);
}

@Override
Expand All @@ -27,32 +27,4 @@ protected ResourceManager getResourceManager(CommandContext context) {
return new CertificateTemplateManager(context.getManageClient());
}

@Override
protected void afterResourceSaved(ResourceManager mgr, CommandContext context, File resourceFile,
SaveReceipt receipt) {
URI location = receipt.getResponse().getHeaders().getLocation();
Map<String, String> customTokens = context.getAppConfig().getCustomTokens();
if (location != null) {
// Create
String[] tokens = location.getPath().split("/");
String id = tokens[tokens.length - 1];
String key = "certificate-template-" + receipt.getResourceId();
if (logger.isInfoEnabled()) {
logger.info(format("Storing token with key '%s' and value '%s'", key, id));
}
customTokens.put(key, id);
} else {
// Update
String path = receipt.getPath();
String[] tokens = path.split("/");
// Path is expected to be /manage/v2/certificate-templates/id/properties
String id = tokens[tokens.length - 2];
String key = "certificate-template-" + receipt.getResourceId();
if (logger.isInfoEnabled()) {
logger.info(format("Storing token with key '%s' and value '%s'", key, id));
}
customTokens.put(key, id);
}
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ protected String[] getResourceNames() {
@Override
protected void afterResourcesCreated() {
Map<String, String> customTokens = appConfig.getCustomTokens();
String key = "certificate-template-sample-app-template";
String key = "certificate-templates-id-sample-app-template";
assertNotNull(
"The cert template ID should have been stored in the tokens map so that it can be referenced in an HTTP server file",
customTokens.get(key));
Expand All @@ -45,7 +45,7 @@ protected void afterResourcesCreated() {
@Override
protected void afterResourcesCreatedAgain() {
Map<String, String> customTokens = appConfig.getCustomTokens();
String key = "certificate-template-sample-app-template";
String key = "certificate-templates-id-sample-app-template";
assertNotNull("Verifying that the cert template ID is stored on an update as well", customTokens.get(key));
}

Expand Down

0 comments on commit aba8489

Please sign in to comment.