forked from wttech/SecureAEM
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
wttech#31: Implementations for global and test configurations
- Loading branch information
Bartosz Wesolowski
committed
Mar 4, 2019
1 parent
c524eb7
commit 158a4eb
Showing
2 changed files
with
125 additions
and
0 deletions.
There are no files selected for viewing
96 changes: 96 additions & 0 deletions
96
src/main/java/com/cognifide/secureaem/sling/ResourceGlobalConfiguration.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,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; | ||
} | ||
} |
29 changes: 29 additions & 0 deletions
29
src/main/java/com/cognifide/secureaem/sling/ResourceTestConfiguration.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 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); | ||
} | ||
} |