001    package com.khubla.pragmatach.plugin.jcr;
002    
003    import javax.jcr.Node;
004    import javax.jcr.Session;
005    
006    import com.khubla.pragmatach.framework.api.PragmatachException;
007    import com.khubla.pragmatach.framework.api.Response;
008    
009    /**
010     * @author tome
011     */
012    public class JCRJSONController extends JCRController {
013       private Node getNode(String[] nodeName) throws PragmatachException {
014          try {
015             /*
016              * session
017              */
018             final Session session = jcrSessionFactory.getSession();
019             /*
020              * check
021              */
022             if (null != session) {
023                final Node root = session.getRootNode();
024                if (null == nodeName) {
025                   return root;
026                } else {
027                   return root.getNode(buildWildcardResourceURI(nodeName));
028                }
029             } else {
030                throw new PragmatachException("Unable to get session");
031             }
032          } catch (final Exception e) {
033             throw new PragmatachException("Exception in getNode", e);
034          }
035       }
036    
037       /**
038        * render children
039        */
040       public Response renderChildren(String[] nodeName) throws PragmatachException {
041          try {
042             final Node node = getNode(nodeName);
043             if (null != node) {
044                return new JCRResponse(getCacheHeaders(), PragmatachJCRToJSON.renderSubnodeNames(node));
045             } else {
046                throw new Exception("Unable to get node '" + nodeName + "'");
047             }
048          } catch (final Exception e) {
049             throw new PragmatachException("Exception in render", e);
050          }
051       }
052    
053       /**
054        * render properties
055        */
056       public Response renderProperties(String[] nodeName) throws PragmatachException {
057          try {
058             final Node node = getNode(nodeName);
059             if (null != node) {
060                return new JCRResponse(getCacheHeaders(), PragmatachJCRToJSON.renderNodeProperties(node));
061             } else {
062                throw new Exception("Unable to get node '" + nodeName + "'");
063             }
064          } catch (final Exception e) {
065             throw new PragmatachException("Exception in render", e);
066          }
067       }
068    }