Search in sources :

Example 1 with JcrTool

use of com.thinkbiganalytics.metadata.modeshape.support.JcrTool in project kylo by Teradata.

the class BaseJcrProvider method findOrCreateEntityNode.

/**
 * Creates a new Entity Node object for a Parent Path, relative Path and node type
 */
public Node findOrCreateEntityNode(String parentPath, String relPath, Class<? extends JcrEntity> jcrEntityType) {
    Session session = getSession();
    try {
        Node typesNode = session.getNode(parentPath);
        JcrTools tools = new JcrTool();
        Node entNode = tools.findOrCreateChild(typesNode, relPath, getNodeType(jcrEntityType));
        return entNode;
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to create new entity of type: " + getEntityClass(), e);
    }
}
Also used : Node(javax.jcr.Node) JcrTool(com.thinkbiganalytics.metadata.modeshape.support.JcrTool) RepositoryException(javax.jcr.RepositoryException) JcrTools(org.modeshape.jcr.api.JcrTools) Session(javax.jcr.Session)

Example 2 with JcrTool

use of com.thinkbiganalytics.metadata.modeshape.support.JcrTool in project kylo by Teradata.

the class DebugController method printJcrTree.

/**
 * Prints the nodes of the JCR path given, for debugging.
 *
 * @param abspath the path in JCR
 * @return a printout of the JCR tree
 */
@GET
@Path("jcr/{abspath: .*}")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public String printJcrTree(@PathParam("abspath") final String abspath) {
    return metadata.read(() -> {
        this.accessController.checkPermission(AccessController.SERVICES, MetadataAccessControl.ACCESS_METADATA);
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        try {
            Session session = JcrMetadataAccess.getActiveSession();
            try {
                Node node = Strings.isNullOrEmpty(abspath) ? session.getRootNode() : session.getRootNode().getNode(abspath);
                JcrTools tools = new JcrTool(true, pw);
                tools.printSubgraph(node);
            } catch (PathNotFoundException pnf) {
                try {
                    java.nio.file.Path path = JcrPath.get(abspath);
                    Node node = session.getRootNode().getNode(path.getParent().toString());
                    Object value = JcrPropertyUtil.getProperty(node, path.getFileName().toString());
                    pw.println(" - " + path.getFileName().toString() + "=" + value);
                } catch (PathNotFoundException e) {
                    throw pnf;
                }
            }
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        pw.flush();
        return sw.toString();
    });
}
Also used : Path(javax.ws.rs.Path) JcrPath(com.thinkbiganalytics.metadata.modeshape.support.JcrPath) StringWriter(java.io.StringWriter) Node(javax.jcr.Node) JcrTool(com.thinkbiganalytics.metadata.modeshape.support.JcrTool) PathNotFoundException(javax.jcr.PathNotFoundException) JcrTools(org.modeshape.jcr.api.JcrTools) ParseException(java.text.ParseException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) PrintWriter(java.io.PrintWriter) Session(javax.jcr.Session) Path(javax.ws.rs.Path) JcrPath(com.thinkbiganalytics.metadata.modeshape.support.JcrPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 3 with JcrTool

use of com.thinkbiganalytics.metadata.modeshape.support.JcrTool in project kylo by Teradata.

the class DebugController method printJcrId.

/**
 * Prints the subgraph of the node in JCR with the specified ID.
 *
 * @param jcrId the id of the node in JCR
 * @return the subgraph print out
 */
@GET
@Path("jcr")
@Produces({ MediaType.TEXT_PLAIN, MediaType.APPLICATION_JSON })
public String printJcrId(@QueryParam("id") final String jcrId) {
    return metadata.read(() -> {
        this.accessController.checkPermission(AccessController.SERVICES, MetadataAccessControl.ACCESS_METADATA);
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        // If the ID is over 36 characters long then assume it is a cache key and
        // extract only the node ID portion of the key.
        String nodeId = jcrId.length() > 36 ? jcrId.substring(jcrId.length() - 36, jcrId.length()) : jcrId;
        try {
            Session session = JcrMetadataAccess.getActiveSession();
            Node node = session.getNodeByIdentifier(nodeId);
            pw.print("Path: ");
            pw.println(node.getPath());
            JcrTools tools = new JcrTool(true, pw);
            tools.printSubgraph(node);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        pw.flush();
        return sw.toString();
    });
}
Also used : StringWriter(java.io.StringWriter) Node(javax.jcr.Node) JcrTool(com.thinkbiganalytics.metadata.modeshape.support.JcrTool) JcrTools(org.modeshape.jcr.api.JcrTools) ParseException(java.text.ParseException) PathNotFoundException(javax.jcr.PathNotFoundException) RepositoryException(javax.jcr.RepositoryException) PrintWriter(java.io.PrintWriter) Session(javax.jcr.Session) Path(javax.ws.rs.Path) JcrPath(com.thinkbiganalytics.metadata.modeshape.support.JcrPath) Produces(javax.ws.rs.Produces) GET(javax.ws.rs.GET)

Example 4 with JcrTool

use of com.thinkbiganalytics.metadata.modeshape.support.JcrTool in project kylo by Teradata.

the class JcrDatasourceProvider method createImpl.

private <J extends JcrDatasource> J createImpl(String name, String descr, Class<? extends Datasource> type) {
    try {
        JcrTool tool = new JcrTool();
        Class<J> implType = deriveImplType(type);
        Field folderField = FieldUtils.getField(implType, "PATH_NAME", true);
        String subfolderName = (String) folderField.get(null);
        String dsPath = EntityUtil.pathForDataSource();
        Node dsNode = getSession().getNode(dsPath);
        Node subfolderNode = tool.findOrCreateChild(dsNode, subfolderName, "nt:folder");
        Map<String, Object> props = new HashMap<>();
        props.put(JcrDatasource.SYSTEM_NAME, name);
        String encodedName = org.modeshape.jcr.value.Path.DEFAULT_ENCODER.encode(name);
        final boolean isNew = !hasEntityNode(subfolderNode.getPath(), encodedName);
        @SuppressWarnings("unchecked") J datasource = (J) findOrCreateEntity(subfolderNode.getPath(), encodedName, implType, props);
        if (isNew && JcrUserDatasource.class.isAssignableFrom(type)) {
            if (this.accessController.isEntityAccessControlled()) {
                final List<SecurityRole> roles = roleProvider.getEntityRoles(SecurityRole.DATASOURCE);
                actionsProvider.getAvailableActions(AllowedActions.DATASOURCE).ifPresent(actions -> ((JcrUserDatasource) datasource).enableAccessControl((JcrAllowedActions) actions, JcrMetadataAccess.getActiveUser(), roles));
            } else {
                actionsProvider.getAvailableActions(AllowedActions.DATASOURCE).ifPresent(actions -> ((JcrUserDatasource) datasource).disableAccessControl((JcrAllowedActions) actions, JcrMetadataAccess.getActiveUser()));
            }
        }
        datasource.setTitle(name);
        datasource.setDescription(descr);
        return datasource;
    } catch (IllegalArgumentException | IllegalAccessException | RepositoryException e) {
        throw new MetadataException("Unable to create datasource: " + type, e);
    }
}
Also used : SecurityRole(com.thinkbiganalytics.security.role.SecurityRole) HashMap(java.util.HashMap) Node(javax.jcr.Node) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) MetadataException(com.thinkbiganalytics.metadata.api.MetadataException) Field(java.lang.reflect.Field) JcrAllowedActions(com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions) JcrTool(com.thinkbiganalytics.metadata.modeshape.support.JcrTool)

Aggregations

JcrTool (com.thinkbiganalytics.metadata.modeshape.support.JcrTool)4 Node (javax.jcr.Node)4 RepositoryException (javax.jcr.RepositoryException)4 Session (javax.jcr.Session)3 JcrTools (org.modeshape.jcr.api.JcrTools)3 JcrPath (com.thinkbiganalytics.metadata.modeshape.support.JcrPath)2 PrintWriter (java.io.PrintWriter)2 StringWriter (java.io.StringWriter)2 ParseException (java.text.ParseException)2 PathNotFoundException (javax.jcr.PathNotFoundException)2 GET (javax.ws.rs.GET)2 Path (javax.ws.rs.Path)2 Produces (javax.ws.rs.Produces)2 MetadataException (com.thinkbiganalytics.metadata.api.MetadataException)1 MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)1 JcrAllowedActions (com.thinkbiganalytics.metadata.modeshape.security.action.JcrAllowedActions)1 SecurityRole (com.thinkbiganalytics.security.role.SecurityRole)1 Field (java.lang.reflect.Field)1 HashMap (java.util.HashMap)1