Skip to content

Instantly share code, notes, and snippets.

@fjunior87
Created August 13, 2016 21:49
Show Gist options
  • Save fjunior87/ae6ebd63b4aa224951be56c7071fff55 to your computer and use it in GitHub Desktop.
Save fjunior87/ae6ebd63b4aa224951be56c7071fff55 to your computer and use it in GitHub Desktop.
@Component
@Service
public class ContactListAdapterFactory implements AdapterFactory{
@Property(name="adapters")
public static final String[] ADAPTER_CLASSES = {
ContactList.class.getName()
};
@Property(name="adaptables")
public static final String[] ADAPTABLE_CLASSES = {
SlingHttpServletRequest.class.getName()
};
public static String CONTACTS_NODE = "contacts";
@Override
@SuppressWarnings("unchecked")
public <AdapterType> AdapterType getAdapter(Object adaptable, Class<AdapterType> type) {
if(adaptable instanceof SlingHttpServletRequest
&& type.equals(ContactList.class)) {
ContactList contactList = new ContactList();
SlingHttpServletRequest request = (SlingHttpServletRequest)adaptable;
Resource appResource = request.getResource();
Resource contactsNode = appResource.getChild(CONTACTS_NODE);
Iterable<Resource> contacts = contactsNode.getChildren();
Iterator<Resource> contactsIterator = contacts.iterator();
while(contactsIterator.hasNext()) {
contactList.addContact(contactsIterator.next().adaptTo(Contact.class));
}
return (AdapterType)contactList;
}
return null;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment