-
Notifications
You must be signed in to change notification settings - Fork 14
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
Showing
15 changed files
with
284 additions
and
40 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
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
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
126 changes: 126 additions & 0 deletions
126
core/src/test/java/io/wcm/testing/mock/aem/context/MockOverrideSlingPathRequestWrapper.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,126 @@ | ||
package io.wcm.testing.mock.aem.context; | ||
|
||
import java.util.Collection; | ||
import java.util.HashMap; | ||
import java.util.Iterator; | ||
import java.util.Map; | ||
|
||
import javax.script.ScriptEngine; | ||
import javax.script.ScriptEngineFactory; | ||
import javax.script.SimpleBindings; | ||
|
||
import org.apache.sling.api.SlingHttpServletRequest; | ||
import org.apache.sling.api.adapter.AdapterManager; | ||
import org.apache.sling.api.resource.Resource; | ||
import org.apache.sling.api.scripting.SlingBindings; | ||
import org.apache.sling.api.wrappers.SlingHttpServletRequestWrapper; | ||
import org.apache.sling.scripting.api.AbstractScriptEngineFactory; | ||
import org.apache.sling.scripting.api.BindingsValuesProvider; | ||
import org.apache.sling.scripting.api.BindingsValuesProvidersByContext; | ||
|
||
import com.day.cq.wcm.api.Page; | ||
import com.day.cq.wcm.api.PageManager; | ||
|
||
class MockOverrideSlingPathRequestWrapper extends SlingHttpServletRequestWrapper { | ||
|
||
private static final String ATTR_SLING_BINDINGS = SlingBindings.class.getName(); | ||
private final AdapterManager adapterManager; | ||
private final SlingBindings myBindings; | ||
private final Resource resource; | ||
private final Map<Class<?>, Object> adaptersCache; | ||
|
||
MockOverrideSlingPathRequestWrapper(AdapterManager adapterManager, SlingHttpServletRequest request, String path) { | ||
super(request); | ||
this.adapterManager = adapterManager; | ||
this.myBindings = new SlingBindings(); | ||
this.adaptersCache = new HashMap(); | ||
SlingBindings slingBindings = (SlingBindings)this.getSlingRequest().getAttribute(ATTR_SLING_BINDINGS); | ||
this.resource = this.getSlingRequest().getResourceResolver().resolve(this.getSlingRequest(), path); | ||
if (slingBindings != null) { | ||
this.myBindings.putAll(slingBindings); | ||
} | ||
|
||
this.myBindings.put("properties", this.resource.getValueMap()); | ||
this.myBindings.put("resource", this.resource); | ||
this.myBindings.put("request", this); | ||
this.myBindings.put("resolver", this.resource.getResourceResolver()); | ||
Page currentPage = null; | ||
PageManager pageManager = this.getSlingRequest().getResourceResolver().adaptTo(PageManager.class); | ||
if (pageManager != null) { | ||
currentPage = pageManager.getContainingPage(this.resource); | ||
} | ||
|
||
this.myBindings.put("currentPage", currentPage); | ||
} | ||
|
||
MockOverrideSlingPathRequestWrapper(AdapterManager adapterManager, SlingHttpServletRequest request, String path, | ||
BindingsValuesProvidersByContext bindingsValuesProvidersByContext) { | ||
this(adapterManager, request, path); | ||
SimpleBindings additionalBindings = new SimpleBindings(); | ||
additionalBindings.putAll(this.myBindings); | ||
Collection<BindingsValuesProvider> bindingsValuesProviders = bindingsValuesProvidersByContext | ||
.getBindingsValuesProviders(new SlingModelsScriptEngineFactory(), "request"); | ||
Iterator<BindingsValuesProvider> bindingsValuesProviderIterator = bindingsValuesProviders.iterator(); | ||
|
||
while (bindingsValuesProviderIterator.hasNext()) { | ||
BindingsValuesProvider provider = bindingsValuesProviderIterator.next(); | ||
provider.addBindings(additionalBindings); | ||
} | ||
|
||
this.myBindings.putAll(additionalBindings); | ||
} | ||
|
||
@Override | ||
public Object getAttribute(String name) { | ||
return ATTR_SLING_BINDINGS.equals(name) ? this.myBindings : super.getAttribute(name); | ||
} | ||
|
||
@Override | ||
public Resource getResource() { | ||
return this.resource; | ||
} | ||
|
||
/** | ||
* Overriding `adaptTo` to avoid using the original request as the adaptable. | ||
*/ | ||
@Override | ||
public <AdapterType> AdapterType adaptTo(Class<AdapterType> type) { | ||
AdapterType result = null; | ||
synchronized (this) { | ||
result = (AdapterType)this.adaptersCache.get(type); | ||
|
||
if (result == null) { | ||
result = adapterManager.getAdapter(this, type); | ||
|
||
if (result != null) { | ||
this.adaptersCache.put(type, result); | ||
} | ||
} | ||
|
||
return result; | ||
} | ||
} | ||
|
||
private static class SlingModelsScriptEngineFactory extends AbstractScriptEngineFactory implements ScriptEngineFactory { | ||
|
||
SlingModelsScriptEngineFactory() { | ||
this.setNames(new String[] { "sling-models-exporter", "sling-models" }); | ||
} | ||
|
||
@Override | ||
public String getLanguageName() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public String getLanguageVersion() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public ScriptEngine getScriptEngine() { | ||
return null; | ||
} | ||
} | ||
} | ||
|
Oops, something went wrong.