Skip to content

Commit

Permalink
wttech#31: Implementations for global and test configurations
Browse files Browse the repository at this point in the history
  • Loading branch information
Bartosz Wesolowski committed Mar 4, 2019
1 parent c524eb7 commit 158a4eb
Show file tree
Hide file tree
Showing 2 changed files with 125 additions and 0 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
package com.cognifide.secureaem.sling;

import com.adobe.granite.crypto.CryptoException;
import com.adobe.granite.crypto.CryptoSupport;
import com.cognifide.secureaem.GlobalConfiguration;
import com.cognifide.secureaem.cli.CliConfiguration;
import org.apache.commons.lang3.StringUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ValueMap;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ResourceGlobalConfiguration implements GlobalConfiguration {

private static final Logger LOG = LoggerFactory.getLogger(ResourceConfiguration.class);

private ValueMap globalConfig;

private CryptoSupport cryptoSupport;

public ResourceGlobalConfiguration(SlingHttpServletRequest request, CryptoSupport cryptoSupport) {
this.cryptoSupport = cryptoSupport;
Resource globalConfigRes = findGlobalConfig(request);
if (globalConfigRes != null) {
globalConfig = globalConfigRes.adaptTo(ValueMap.class);
}
}

@Override
public String getDispatcherUrl() {
return StringUtils.removeEnd(getGlobalConfigParam("dispatcher"), "/");
}

@Override
public String getAuthor() {
return StringUtils.removeEnd(getGlobalConfigParam("author"), "/");
}

@Override public String getAuthorLogin() {
return getGlobalConfigParam("authorLogin");
}

@Override public String getAuthorPassword() {
return getPassword("authorPassword");
}

@Override
public String getPublish() {
return StringUtils.removeEnd(getGlobalConfigParam("publish"), "/");
}

@Override public String getPublishLogin() {
return getGlobalConfigParam("publishLogin");
}

@Override public String getPublishPassword() {
return getPassword("publishPassword");
}

private String getPassword(String paramName) {
String password = StringUtils.defaultString(getGlobalConfigParam(paramName));
String decryptedPassword = password;
if (cryptoSupport.isProtected(password)) {
try {
decryptedPassword = cryptoSupport.unprotect(password);
} catch (CryptoException e) {
LOG.error("Failed to decrypt password {}", paramName, e);
}
}
return decryptedPassword;
}


private String getGlobalConfigParam(String name) {
if (globalConfig == null) {
return null;
}
return globalConfig.get(name, String.class);
}

private Resource findGlobalConfig(SlingHttpServletRequest request) {
Resource resource = request.getResource();
while (resource != null) {
if (resource.isResourceType("cq:Page")) {
Resource content = resource.getChild("jcr:content");
String resourceType = content.adaptTo(ValueMap.class).get("sling:resourceType", String.class);
if ("cognifide/secureaem/renderers/mainRenderer".equals(resourceType)) {
return content.getChild("globalConfig");
}
}
resource = resource.getParent();
}
return null;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package com.cognifide.secureaem.sling;

import com.cognifide.secureaem.TestConfiguration;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.resource.ValueMap;

public class ResourceTestConfiguration implements TestConfiguration {

private final ValueMap valueMap;

public ResourceTestConfiguration(SlingHttpServletRequest request) {
this.valueMap = request.getResource().adaptTo(ValueMap.class);
}

@Override
public String getStringValue(String name, String defaultValue) {
return getLocalConfig(name, defaultValue);
}

@Override
public String[] getStringList(String name) {
return getLocalConfig(name, ArrayUtils.EMPTY_STRING_ARRAY);
}

private <T> T getLocalConfig(String name, T defaultValue) {
return this.valueMap.get(name, defaultValue);
}
}

0 comments on commit 158a4eb

Please sign in to comment.