Last active
January 11, 2019 14:10
-
-
Save rah003/5c4cc40aa6baff0afa35b37d813b265d to your computer and use it in GitHub Desktop.
Lists all virtual URI mapping defined in given instance of Magnolia that would fire for URI provided as parameter
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
import java.net.URI; | |
import info.magnolia.virtualuri.VirtualUriMapping; | |
import info.magnolia.virtualuri.VirtualUriRegistry; | |
import info.magnolia.objectfactory.Components; | |
import java.util.List; | |
import java.util.Optional; | |
import java.util.stream.Collectors; | |
//** Set your URI here **// | |
URI link = new URI("/feature/cokoliv.html"); | |
// get registry | |
VirtualUriRegistry registry = Components.getComponent(VirtualUriRegistry.class); | |
// get all vir uri definitions that match | |
List<VirtualUriItem> items = registry.getAllProviders().stream() | |
.map({ | |
provider -> mapProvider(provider, link); | |
}).filter({item -> item != null}).sorted().collect(Collectors.toList()); | |
// print the matches | |
items.each() { | |
print(it); | |
} | |
// extra message to know we are done | |
if (items.isEmpty()) { | |
return "no dice"; | |
} else { | |
return "done" | |
} | |
// inlined mapping check | |
def mapProvider(provider, link) { | |
VirtualUriMapping virtualMapping = provider.get(); | |
if (virtualMapping != null && virtualMapping.isValid()) { | |
Optional<VirtualUriMapping.Result> predicate = virtualMapping.mapUri(link); | |
if (predicate.isPresent()) { | |
VirtualUriMapping.Result mappingResult = predicate.get(); | |
return new VirtualUriItem(provider.getMetadata().getModule(), mappingResult.getToUri(), mappingResult.getWeight(), provider.getMetadata().getLocation()); | |
} | |
} | |
return null; | |
} | |
// pojo to list results | |
class VirtualUriItem implements Comparable<VirtualUriItem> { | |
private final String module; | |
private final String uri; | |
private final int weight; | |
private final String location; | |
public VirtualUriItem(String module, String uri, int weight, String location) { | |
this.module = module; | |
this.uri = uri; | |
this.weight = weight; | |
this.location = location; | |
} | |
public String getModule() { | |
return module; | |
} | |
public String getUri() { | |
return uri; | |
} | |
public int getWeight() { | |
return weight; | |
} | |
public String getLocation() { | |
return location; | |
} | |
@Override | |
public int compareTo(VirtualUriItem o) { | |
return Integer.valueOf(o.weight).compareTo(Integer.valueOf(weight)); | |
} | |
public String toString() { | |
return "Module: " + module + "\n" + | |
"from uri: " + uri + "\n" + | |
" to uri: " + uri + "\n" | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment