Created
August 13, 2016 21:49
-
-
Save fjunior87/ae6ebd63b4aa224951be56c7071fff55 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
@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