Skip to content

Commit

Permalink
final cleaning and refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
leleuj committed Jan 17, 2016
1 parent ed3384a commit f95d062
Show file tree
Hide file tree
Showing 94 changed files with 581 additions and 1,113 deletions.
Expand Up @@ -30,23 +30,12 @@
*/
public class AttributesDefinition {

protected List<String> all = new ArrayList<>();

protected List<String> primaries = new ArrayList<>();

protected List<String> secondaries = new ArrayList<>();

protected Map<String, AttributeConverter<? extends Object>> converters = new HashMap<>();

/**
* Return all the attributes names.
*
* @return all the attributes names
*/
public List<String> getAllAttributes() {
return this.all;
}

/**
* Return the primary attributes names.
*
Expand All @@ -65,32 +54,15 @@ public List<String> getSecondaryAttributes() {
return this.secondaries;
}

/**
* Add an attribute as a primary one and no converter.
*
* @param name name of the attribute
*/
protected void primary(final String name) {
addAttribute(name, null, true);
}

/**
* Add an attribute as a primary one and its converter.
*
* @param name name of the attribute
* @param converter converter
*/
protected void primary(final String name, final AttributeConverter<? extends Object> converter) {
addAttribute(name, converter, true);
}

/**
* Add an attribute as a secondary one and no converter.
*
* @param name name of the attribute
*/
protected void secondary(final String name) {
addAttribute(name, null, false);
primaries.add(name);
converters.put(name, converter);
}

/**
Expand All @@ -100,27 +72,10 @@ protected void secondary(final String name) {
* @param converter converter
*/
protected void secondary(final String name, final AttributeConverter<? extends Object> converter) {
addAttribute(name, converter, false);
secondaries.add(name);
converters.put(name, converter);
}

/**
* Add an attribute, its primary aspect and its converter to this attributes definition.
*
* @param name name of the attribute
* @param converter converter
* @param principal whether the attribute is principal
*/
protected void addAttribute(final String name, final AttributeConverter<? extends Object> converter,
final boolean principal) {
this.all.add(name);
this.converters.put(name, converter);
if (principal) {
this.primaries.add(name);
} else {
this.secondaries.add(name);
}
}

/**
* Convert an attribute into the right type. If no converter exists for this attribute name, the attribute is returned.
*
Expand Down
10 changes: 0 additions & 10 deletions pac4j-core/src/main/java/org/pac4j/core/profile/ProfileHelper.java
Expand Up @@ -117,14 +117,4 @@ private static UserProfile buildUserProfileByClassCompleteName(final String type
logger.debug("userProfile built : {}", userProfile);
return userProfile;
}

/**
* Set whether the input data should be stored in object to be restored for CAS serialization when toString() is called.
* Save memory if <code>false</code>.
*
* @param keepRawData should we keep the raw data (for CAS)
*/
public static void setKeepRawData(final boolean keepRawData) {
RawDataObject.setKeepRawData(keepRawData);
}
}
46 changes: 0 additions & 46 deletions pac4j-core/src/main/java/org/pac4j/core/profile/RawDataObject.java

This file was deleted.

10 changes: 5 additions & 5 deletions pac4j-core/src/main/java/org/pac4j/core/profile/UserProfile.java
Expand Up @@ -43,15 +43,15 @@ public class UserProfile implements Serializable, Externalizable, Clearable {

private String id;

private Map<String, Object> attributes = new HashMap<String, Object>();
private Map<String, Object> attributes = new HashMap<>();

public transient static final String SEPARATOR = "#";

private boolean isRemembered = false;

private List<String> roles = new ArrayList<String>();
private List<String> roles = new ArrayList<>();

private List<String> permissions = new ArrayList<String>();
private List<String> permissions = new ArrayList<>();

/**
* Build a profile from user identifier and attributes.
Expand All @@ -65,11 +65,11 @@ public void build(final Object id, final Map<String, Object> attributes) {
}

/**
* Return the attributes definition for this user profile. Null for this (generic) user profile.
* Return the attributes definition for this user profile. <code>null</code> for a (generic) user profile.
*
* @return the attributes definition
*/
protected AttributesDefinition getAttributesDefinition() {
public AttributesDefinition getAttributesDefinition() {
return null;
}

Expand Down
Expand Up @@ -15,7 +15,6 @@
*/
package org.pac4j.core.profile.converter;


/**
* This class defines the default converters.
*
Expand All @@ -24,21 +23,22 @@
*/
public final class Converters {

public final static LocaleConverter localeConverter = new LocaleConverter();

public final static StringConverter stringConverter = new StringConverter();
public final static LocaleConverter LOCALE = new LocaleConverter();

public final static BooleanConverter booleanConverter = new BooleanConverter();
public final static StringConverter STRING = new StringConverter();

public final static IntegerConverter integerConverter = new IntegerConverter();
public final static BooleanConverter BOOLEAN = new BooleanConverter();

public final static LongConverter longConverter = new LongConverter();
public final static IntegerConverter INTEGER = new IntegerConverter();

public final static ColorConverter colorConverter = new ColorConverter();
public final static LongConverter LONG = new LongConverter();

public final static GenderConverter genderConverter = new GenderConverter("male", "female");
public final static ColorConverter COLOR = new ColorConverter();

public final static FormattedDateConverter dateConverter = new FormattedDateConverter("yyyy-MM-dd'T'HH:mm:ssz");
public final static GenderConverter GENDER = new GenderConverter("male", "female");

public final static StringReplaceConverter urlConverter = new StringReplaceConverter("\\/", "/");
public final static FormattedDateConverter DATE_TZ_GENERAL = new FormattedDateConverter("yyyy-MM-dd'T'HH:mm:ssz");
public final static FormattedDateConverter DATE_TZ_RFC822 = new FormattedDateConverter("yyyy-MM-dd'T'HH:mm:ss'Z'");

public final static StringReplaceConverter URL = new StringReplaceConverter("\\/", "/");
}
5 changes: 0 additions & 5 deletions pac4j-core/src/test/java/org/pac4j/core/client/ClientIT.java
Expand Up @@ -70,8 +70,6 @@ public void testMissingCallbackUrl() {
}

public void testAuthenticationAndUserProfileRetrieval() throws Exception {
ProfileHelper.setKeepRawData(true);
try {
final Client client = getClient();

final J2EContext context = getJ2EContext();
Expand Down Expand Up @@ -114,9 +112,6 @@ public void testAuthenticationAndUserProfileRetrieval() throws Exception {
bytes = kryoSerializationHelper.serializeToBytes(profile);
final UserProfile profile4 = (UserProfile) kryoSerializationHelper.unserializeFromBytes(bytes);
verifyProfile(profile4);
} finally {
ProfileHelper.setKeepRawData(false);
}
}

protected J2EContext getJ2EContext() {
Expand Down
Expand Up @@ -42,8 +42,16 @@ public void testNoConverter() {
}

@Test
public void testConverterNoEnforcement() {
public void testConverterPrimary() {
definition.primary(NAME, v -> { return FAKE_VALUE; });
assertEquals(NAME, definition.getPrimaryAttributes().get(0));
assertEquals(FAKE_VALUE, definition.convert(NAME, VALUE));
}

@Test
public void testConverterSecondary() {
definition.secondary(NAME, v -> { return FAKE_VALUE; });
assertEquals(NAME, definition.getSecondaryAttributes().get(0));
assertEquals(FAKE_VALUE, definition.convert(NAME, VALUE));
}
}
Expand Up @@ -32,7 +32,7 @@ public class GaeUserServiceAttributesDefinition extends AttributesDefinition {
public static final GaeUserServiceAttributesDefinition gaeUserServiceAttibuteDefinition = new GaeUserServiceAttributesDefinition();

public GaeUserServiceAttributesDefinition() {
primary(DISPLAYNAME, Converters.stringConverter);
primary(EMAIL, Converters.stringConverter);
primary(DISPLAYNAME, Converters.STRING);
primary(EMAIL, Converters.STRING);
}
}
Expand Up @@ -75,7 +75,7 @@ public class GaeUserServiceProfile extends CommonProfile {
private static final long serialVersionUID = 7866288887408897456L;

@Override
protected AttributesDefinition getAttributesDefinition() {
public AttributesDefinition getAttributesDefinition() {
return GaeUserServiceAttributesDefinition.gaeUserServiceAttibuteDefinition;
}
}
Expand Up @@ -28,6 +28,6 @@
public class HttpAttributesDefinition extends AttributesDefinition {

public HttpAttributesDefinition() {
primary(CommonProfile.USERNAME, Converters.stringConverter);
primary(CommonProfile.USERNAME, Converters.STRING);
}
}
Expand Up @@ -18,7 +18,6 @@
import com.fasterxml.jackson.databind.JsonNode;
import org.pac4j.core.context.WebContext;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.OAuthAttributesDefinitions;
import org.pac4j.oauth.profile.bitbucket.BitbucketProfile;
import org.scribe.builder.api.BitBucketApi;
import org.scribe.model.OAuthConfig;
Expand Down Expand Up @@ -64,10 +63,10 @@ protected BitbucketProfile extractUserProfile(String body) {
BitbucketProfile profile = new BitbucketProfile();
JsonNode json = JsonHelper.getFirstNode(body);
if (json != null) {
json = (JsonNode) JsonHelper.get(json, "user");
json = (JsonNode) JsonHelper.getElement(json, "user");
if (json != null) {
for (final String attribute : OAuthAttributesDefinitions.bitbucketDefinition.getPrimaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.get(json, attribute));
for (final String attribute : profile.getAttributesDefinition().getPrimaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.getElement(json, attribute));
}
}
}
Expand Down
Expand Up @@ -75,14 +75,14 @@ protected CasOAuthWrapperProfile extractUserProfile(final String body) {
final CasOAuthWrapperProfile userProfile = new CasOAuthWrapperProfile();
JsonNode json = JsonHelper.getFirstNode(body);
if (json != null) {
userProfile.setId(JsonHelper.get(json, "id"));
userProfile.setId(JsonHelper.getElement(json, "id"));
json = json.get("attributes");
if (json != null) {
final Iterator<JsonNode> nodes = json.iterator();
while (nodes.hasNext()) {
json = nodes.next();
final String attribute = json.fieldNames().next();
userProfile.addAttribute(attribute, JsonHelper.get(json, attribute));
userProfile.addAttribute(attribute, JsonHelper.getElement(json, attribute));
}
}
}
Expand Down
Expand Up @@ -16,9 +16,9 @@
package org.pac4j.oauth.client;

import org.pac4j.core.context.WebContext;
import org.pac4j.core.profile.AttributesDefinition;
import org.pac4j.oauth.credentials.OAuthCredentials;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.OAuthAttributesDefinitions;
import org.pac4j.oauth.profile.dropbox.DropBoxProfile;
import org.scribe.builder.api.DropBoxApi;
import org.scribe.model.OAuthConfig;
Expand Down Expand Up @@ -77,15 +77,16 @@ protected OAuthCredentials getOAuthCredentials(final WebContext context) {
protected DropBoxProfile extractUserProfile(final String body) {
final DropBoxProfile profile = new DropBoxProfile();
JsonNode json = JsonHelper.getFirstNode(body);
final AttributesDefinition definition = profile.getAttributesDefinition();
if (json != null) {
profile.setId(JsonHelper.get(json, "uid"));
for (final String attribute : OAuthAttributesDefinitions.dropBoxDefinition.getPrimaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.get(json, attribute));
profile.setId(JsonHelper.getElement(json, "uid"));
for (final String attribute : definition.getPrimaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.getElement(json, attribute));
}
json = (JsonNode) JsonHelper.get(json, "quota_info");
json = (JsonNode) JsonHelper.getElement(json, "quota_info");
if (json != null) {
for (final String attribute : OAuthAttributesDefinitions.dropBoxDefinition.getSecondaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.get(json, attribute));
for (final String attribute : definition.getSecondaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.getElement(json, attribute));
}
}
}
Expand Down
Expand Up @@ -26,7 +26,6 @@
import org.pac4j.core.util.CommonHelper;
import org.pac4j.oauth.exception.OAuthCredentialsException;
import org.pac4j.oauth.profile.JsonHelper;
import org.pac4j.oauth.profile.OAuthAttributesDefinitions;
import org.pac4j.oauth.profile.facebook.FacebookAttributesDefinition;
import org.pac4j.oauth.profile.facebook.FacebookProfile;
import org.scribe.builder.api.ExtendedFacebookApi;
Expand Down Expand Up @@ -166,9 +165,9 @@ protected FacebookProfile extractUserProfile(final String body) {
final FacebookProfile profile = new FacebookProfile();
final JsonNode json = JsonHelper.getFirstNode(body);
if (json != null) {
profile.setId(JsonHelper.get(json, "id"));
for (final String attribute : OAuthAttributesDefinitions.facebookDefinition.getPrimaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.get(json, attribute));
profile.setId(JsonHelper.getElement(json, "id"));
for (final String attribute : profile.getAttributesDefinition().getPrimaryAttributes()) {
profile.addAttribute(attribute, JsonHelper.getElement(json, attribute));
}
extractData(profile, json, FacebookAttributesDefinition.FRIENDS);
extractData(profile, json, FacebookAttributesDefinition.MOVIES);
Expand All @@ -185,9 +184,9 @@ protected FacebookProfile extractUserProfile(final String body) {
}

protected void extractData(final FacebookProfile profile, final JsonNode json, final String name) {
final JsonNode data = (JsonNode) JsonHelper.get(json, name);
final JsonNode data = (JsonNode) JsonHelper.getElement(json, name);
if (data != null) {
profile.addAttribute(name, JsonHelper.get(data, "data"));
profile.addAttribute(name, JsonHelper.getElement(data, "data"));
}
}

Expand Down

0 comments on commit f95d062

Please sign in to comment.