use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class TokenTest method shouldStoreDate.
@Test
public void shouldStoreDate() {
// Given
Calendar now = Calendar.getInstance();
CoreTokenField key = CoreTokenField.EXPIRY_DATE;
Token token = new Token("", TokenType.SESSION);
// When
token.setAttribute(key, now);
// Then
Calendar result = token.getValue(key);
assertEquals(now.getTimeInMillis(), result.getTimeInMillis());
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class LdapTokenAttributeConversion method getEntry.
/**
* Generate an Entry based on the given Token.
*
* @param token Non null Token to base the Entry on.
*
* @return An Entry suitable for LDAP operations. Includes the Object Class.
*/
public Entry getEntry(Token token) {
Entry entry = new LinkedHashMapEntry(generateTokenDN(token));
addObjectClass(entry);
for (CoreTokenField field : token.getAttributeNames()) {
String key = field.toString();
// Token Type special case is an Enum
if (CoreTokenField.TOKEN_TYPE.equals(field)) {
TokenType type = token.getValue(field);
entry.addAttribute(key, type.name());
continue;
}
if (CoreTokenFieldTypes.isCalendar(field)) {
Calendar calendar = token.getValue(field);
String dateString = conversion.toLDAPDate(calendar);
entry.addAttribute(key, dateString);
} else if (CoreTokenFieldTypes.isByteArray(field)) {
byte[] array = token.getValue(field);
entry.addAttribute(key, array);
} else if (CoreTokenFieldTypes.isInteger(field)) {
Integer value = token.getValue(field);
entry.addAttribute(key, value);
} else if (CoreTokenFieldTypes.isString(field)) {
String value = token.getValue(field);
if (!value.isEmpty()) {
entry.addAttribute(key, value);
}
} else {
throw new IllegalStateException();
}
}
return entry;
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class LdapTokenAttributeConversion method tokenFromEntry.
/**
* Convert an Entry into a Token.
*
* The implication of function is that the Entry contains all the attributes of the
* Token. If any required attributes are missing, this operation will fail.
*
* @param entry A non null Entry.
*
* @return A non null Token initialised with the contents of the Entry.
*
* @see #mapFromEntry(org.forgerock.opendj.ldap.Entry)
*/
public Token tokenFromEntry(Entry entry) {
Map<CoreTokenField, Object> map = mapFromEntry(entry);
String tokenId = (String) map.get(CoreTokenField.TOKEN_ID);
TokenType type = (TokenType) map.get(CoreTokenField.TOKEN_TYPE);
Token token = new Token(tokenId, type);
for (Map.Entry<CoreTokenField, Object> e : map.entrySet()) {
CoreTokenField key = e.getKey();
Object value = e.getValue();
if (Token.isFieldReadOnly(key)) {
continue;
}
token.setAttribute(key, value);
}
return token;
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class LdapTokenAttributeConversion method mapFromEntry.
/**
* Convert an Entry into a more convenient Mapping of CoreTokenField to Object.
*
* This function is important because no every operation with LDAP needs to return a
* fully initialised Token. Instead users may be interested in only certain
* attributes of the Token, and choose to query just those as a performance enhancement.
*
* @param entry Non null entry to convert.
*
* @return A mapping of zero or more CoreTokenFields to Objects.
*/
public Map<CoreTokenField, Object> mapFromEntry(Entry entry) {
stripObjectClass(entry);
Map<CoreTokenField, Object> r = new LinkedHashMap<>();
for (Attribute a : entry.getAllAttributes()) {
AttributeDescription description = a.getAttributeDescription();
CoreTokenField field = CoreTokenField.fromLDAPAttribute(description.toString());
// Special case for Token Type
if (CoreTokenField.TOKEN_TYPE.equals(field)) {
String value = entry.parseAttribute(description).asString();
r.put(field, TokenType.valueOf(value));
continue;
}
if (CoreTokenFieldTypes.isCalendar(field)) {
String dateString = entry.parseAttribute(description).asString();
Calendar calendar = conversion.fromLDAPDate(dateString);
r.put(field, calendar);
} else if (CoreTokenFieldTypes.isString(field)) {
String value = entry.parseAttribute(description).asString();
if (EMPTY.equals(value)) {
value = "";
}
r.put(field, value);
} else if (CoreTokenFieldTypes.isInteger(field)) {
Integer value = entry.parseAttribute(description).asInteger();
r.put(field, value);
} else if (CoreTokenFieldTypes.isByteArray(field)) {
byte[] data = entry.parseAttribute(description).asByteString().toByteArray();
r.put(field, data);
} else {
throw new IllegalStateException();
}
}
return r;
}
use of org.forgerock.openam.tokens.CoreTokenField in project OpenAM by OpenRock.
the class Token method toString.
/**
* Returns a formatted version fo the Token which is intended to be human readable.
* Some of the field values have been formatted to ensure that their contents are more
* meaningful.
*
* Note: This is not a machine readable/parsable format. This function is only intended
* for debugging use only as its conversion logic will not be performant.
*
* @return Non null string which describes the Token.
*/
@Override
public String toString() {
String r = this.getClass().getName() + "\n";
String format = "\t{0} = {1}\n";
for (CoreTokenField field : getAttributeNames()) {
Object value;
if (field.equals(CoreTokenField.EXPIRY_DATE)) {
DateFormat dateFormat = DateFormat.getInstance();
Date date = getExpiryTimestamp().getTime();
value = dateFormat.format(date);
} else if (field.equals(CoreTokenField.BLOB)) {
value = Long.toString(getBlob().length) + " bytes";
} else {
value = getValue(field);
}
if (value == null) {
value = "[null]";
}
r += MessageFormat.format(format, field.toString(), value);
}
return r;
}
Aggregations