Skip to content

Instantly share code, notes, and snippets.

View runeflobakk's full-sized avatar

Rune Flobakk runeflobakk

View GitHub Profile
@runeflobakk
runeflobakk / gist:962518
Created May 9, 2011 13:28
Add this to your Wicket application class to limit Wicket dev mode warning to one per JVM instead of per test. Reduces clutter in build log.
private static boolean warnedAboutDevMode = false;
@Override
protected void outputDevelopmentModeWarning() {
if (warnedAboutDevMode) {
return;
}
super.outputDevelopmentModeWarning();
warnedAboutDevMode = true;
}
@runeflobakk
runeflobakk / EqualsAndHashCodeTemplate.java
Created August 23, 2011 10:57
equals() and hashCode() template for Eclipse. It uses EqualsBuilder and HashCodeBuilder in Apache Commons Lang.
${e:import(org.apache.commons.lang3.builder.EqualsBuilder)}
${h:import(org.apache.commons.lang3.builder.HashCodeBuilder)}
@Override
public boolean equals(Object object) {
if (object instanceof ${enclosing_type}) {
${enclosing_type} another = (${enclosing_type}) object;
return new EqualsBuilder().append(${replaceWithFieldName}, another.${replaceWithFieldName}).isEquals();
}
private Person per = new Person("Per", new Address("A-gate 5", "0560", "Oslo"), 30);
private Person kari = new Person("Kari", new Address("B-gate 4", "0560", "Oslo"), 37);
@Test
public void knowsWhichRelativesLivingInTheSamePostalCode() {
per.relateTo(SIBLING, kari);
Collection<Relative> nearbyRelatives = per.getRelativesWithSamePostalCode();
assertThat(nearbyRelatives, hasSize(1));
public class SecurityLevelVoter implements AccessDecisionVoter {
private String securityLevelPrefix = "SECURITY_LEVEL_";
public int vote(Authentication authentication,
Object object,
Collection<ConfigAttribute> attributes) {
int result = ACCESS_ABSTAIN;
Object principal = authentication.getPrincipal();
@runeflobakk
runeflobakk / SupportedBy.java
Created October 18, 2011 22:06
Funksjon som sjekker om ConfigAttributes støttes av en AccessDecisionVoter.
public class SupportedBy implements Predicate<ConfigAttribute> {
private final AccessDecisionVoter voter;
public SupportedBy(AccessDecisionVoter voter) {
this.voter = voter;
}
public boolean evaluate(ConfigAttribute attribute) {
return voter.supports(attribute);
@runeflobakk
runeflobakk / gist:1296896
Created October 18, 2011 22:14
Snippet: Dersom det ikke finnes ConfigAttributes som støttes av SecurityLevelVoter, returner ACCESS_ABSTAIN
Collection<ConfigAttribute> supportedAttributes = select(attributes, new SupportedBy(this));
if (supportedAttributes.isEmpty()) {
return ACCESS_ABSTAIN;
}
@runeflobakk
runeflobakk / AcceptingSecurityLevelOf.java
Created October 30, 2011 20:24
Funksjon for å avgjøre om MyPrincipal sitt sikkerhetsnivå er høyt nok for det som kreves av ConfigAttributes
public class SecurityLevelVoter implements AccessDecisionVoter {
private String securityLevelPrefix = "SECURITY_LEVEL_";
//...snip...
private class AcceptingSecurityLevelOf implements Predicate<ConfigAttribute> {
private final int principalSecurityLevel;
public AcceptingSecurityLevelOf(MyPrincipal principal) {
principalSecurityLevel = toInt(principal.getSecurityLevel(), Integer.MIN_VALUE);
@runeflobakk
runeflobakk / gist:1326397
Created October 30, 2011 20:36
Vi har en eller flere støttede ConfigAttributes, og sjekker om det finnes en som aksepterer sikkerhetsnivået til principalen.
Object p = authentication.getPrincipal();
if (p instanceof MyPrincipal
&& exists(supportedAttributes, new AcceptingSecurityLevelOf((MyPrincipal) p))) {
return ACCESS_GRANTED;
} else {
return ACCESS_DENIED;
}
@runeflobakk
runeflobakk / SecurityLevelVoter#vote.java
Created October 30, 2011 20:48
Hele vote-metoden hvor implementasjonen reflekterer bedre slik vi ville forklart forrutsetningene for brukertilgang.
public int vote(Authentication authentication,
Object object,
Collection<ConfigAttribute> attributes) {
Collection<ConfigAttribute> supportedAttributes =
select(attributes, new SupportedBy(this));
if (supportedAttributes.isEmpty()) {
return ACCESS_ABSTAIN;
}
@runeflobakk
runeflobakk / gist:1326485
Created October 30, 2011 21:44
Snippet: Dersom det ikke finnes ConfigAttributes som støttes av SecurityLevelVoter, returner ACCESS_ABSTAIN. Java 8 Project Lambda-syntaks.
Collection<ConfigAttribute> supportedAttributes =
select(attributes, attr -> this.supports(attr));
if (supportedAttributes.isEmpty()) {
return ACCESS_ABSTAIN;
}