Created
February 22, 2019 09:21
-
-
Save mkgl/ba8c6e9789b5c4f102930684c0dda810 to your computer and use it in GitHub Desktop.
A sample REST endpoint in Magnolia, returning some contacts.
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
class: info.magnolia.dev.rest.ContactsEndpointDefinition |
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
package info.magnolia.dev.rest; | |
import static javax.ws.rs.core.MediaType.APPLICATION_JSON; | |
import info.magnolia.context.Context; | |
import info.magnolia.rest.AbstractEndpoint; | |
import info.magnolia.rest.delivery.jcr.QueryBuilder; | |
import info.magnolia.rest.registry.ConfiguredEndpointDefinition; | |
import java.util.List; | |
import java.util.Map; | |
import javax.inject.Inject; | |
import javax.inject.Provider; | |
import javax.jcr.NodeIterator; | |
import javax.jcr.RepositoryException; | |
import javax.jcr.Session; | |
import javax.jcr.query.Query; | |
import javax.jcr.query.QueryResult; | |
import javax.ws.rs.GET; | |
import javax.ws.rs.Path; | |
import javax.ws.rs.Produces; | |
/** | |
* A sample endpoint returning some contacts. | |
*/ | |
public class ContactsEndpointDefinition extends ConfiguredEndpointDefinition { | |
public ContactsEndpointDefinition() { | |
setImplementationClass(ContactsEndpoint.class); | |
} | |
@Path("/contacts/v1") | |
public static class ContactsEndpoint extends AbstractEndpoint<ContactsEndpointDefinition> { | |
private final Provider<Context> contextProvider; | |
@Inject | |
public ContactsEndpoint(ContactsEndpointDefinition endpointDefinition, Provider<Context> contextProvider) { | |
super(endpointDefinition); | |
this.contextProvider = contextProvider; | |
} | |
@GET | |
@Path("/monsters") | |
@Produces(APPLICATION_JSON) | |
public NodeIterator getContacts() throws RepositoryException { | |
Session session = contextProvider.get().getJCRSession("contacts"); | |
Query query = QueryBuilder.inWorkspace(session.getWorkspace()) | |
.nodeTypes(List.of("mgnl:contact")) | |
.conditions(Map.of( | |
"organizationName", List.of("Monsters Inc."))) | |
.build(); | |
QueryResult result = query.execute(); | |
return result.getNodes(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment