Search in sources :

Example 91 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project jackrabbit-oak by apache.

the class RepositoryUpgradeTest method verifyReplacedBuiltinNodeTypes.

@Test
public void verifyReplacedBuiltinNodeTypes() throws Exception {
    Session session = createAdminSession();
    try {
        NodeTypeManager manager = session.getWorkspace().getNodeTypeManager();
        NodeType nt = manager.getNodeType(UserConstants.NT_REP_GROUP);
        assertTrue("Migrated repository must have new nodetype definitions", nt.isNodeType(UserConstants.NT_REP_MEMBER_REFERENCES));
    } finally {
        session.logout();
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) Session(javax.jcr.Session) Test(org.junit.Test)

Example 92 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project sling by apache.

the class JCRSupportImpl method isFileNodeType.

public Boolean isFileNodeType(final ResourceResolver resolver, final String nodeType) {
    final Session session = resolver.adaptTo(Session.class);
    if (session != null) {
        try {
            final NodeTypeManager ntMgr = session.getWorkspace().getNodeTypeManager();
            final NodeType nt = ntMgr.getNodeType(nodeType);
            return nt.isNodeType(JcrConstants.NT_FILE);
        } catch (RepositoryException e) {
            // assuming type not valid.
            return null;
        }
    }
    return false;
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) NodeType(javax.jcr.nodetype.NodeType) RepositoryException(javax.jcr.RepositoryException) Session(javax.jcr.Session)

Example 93 with NodeTypeManager

use of javax.jcr.nodetype.NodeTypeManager in project sling by apache.

the class DownloadDefaultBinaryValueServlet method doGet.

@SuppressWarnings("deprecation")
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
    response.setContentType("application/octet-stream; charset=UTF-8");
    String requestURI = request.getRequestURI();
    String[] idFields = requestURI.substring(1).split("/");
    try {
        NodeTypeManager nodeTypeManager = request.getResourceResolver().getResource("/").adaptTo(Node.class).getSession().getWorkspace().getNodeTypeManager();
        NodeType nodeType = nodeTypeManager.getNodeType(idFields[0]);
        PropertyDefinition[] propertyDefinitions = nodeType.getPropertyDefinitions();
        List<PropertyDefinition> propertyDefinitionList = Arrays.asList(propertyDefinitions);
        if (propertyDefinitionList != null) {
            // Every matcher represents a path element in the URL and is initialized with its value.
            // It will try to match the value of a path element with the corresponding property 
            // element of all property definitions from the node type in findMatchingPropertyDef().
            PropertyMatcher[] propertyMatcher = new PropertyMatcher[] { new PropertyNameMatcher(idFields, 1), new RequiredPropertyTypeMatcher(idFields, 2), new AutoCreatedMatcher(idFields, 3), new MandatoryMatcher(idFields, 4), new ProtectedMatcher(idFields, 5), new MultipleMatcher(idFields, 6), new OnParentVersionMatcher(idFields, 7) };
            PropertyDefinition propDef = findMatchingPropertyDef(propertyDefinitionList, new LinkedList<PropertyMatcher>(Arrays.asList(propertyMatcher)));
            if (propDef != null) {
                Value[] defaultValues = propDef.getDefaultValues();
                if (defaultValues != null && defaultValues.length > 0) {
                    int startIndex = requestURI.lastIndexOf('/') + 1;
                    int endIndex = requestURI.indexOf("default_binary_value.bin") - 1;
                    int defaultValueIndex = 0;
                    if (endIndex - startIndex == 1) {
                        String indexString = requestURI.substring(startIndex, endIndex);
                        defaultValueIndex = Integer.parseInt(indexString);
                    }
                    try {
                        if (defaultValueIndex < defaultValues.length) {
                            Value defaultValue = defaultValues[defaultValueIndex];
                            InputStream stream = defaultValue.getStream();
                            BufferedReader bufferedReader = new BufferedReader(new InputStreamReader(stream));
                            PrintWriter writer = response.getWriter();
                            String line = null;
                            while ((line = bufferedReader.readLine()) != null) {
                                writer.write(line);
                            }
                            writer.flush();
                            writer.close();
                            response.setStatus(HttpServletResponse.SC_OK);
                        } else {
                            response.sendError(HttpServletResponse.SC_NOT_FOUND);
                        }
                    } catch (NumberFormatException nfe) {
                        response.sendError(HttpServletResponse.SC_NOT_FOUND);
                    }
                } else {
                    response.sendError(HttpServletResponse.SC_NOT_FOUND);
                }
            } else {
                response.sendError(HttpServletResponse.SC_NOT_FOUND);
            }
        }
    } catch (RepositoryException e) {
        log.error("Could not return the binary file.", e);
        throw new ServletException(e);
    }
}
Also used : NodeTypeManager(javax.jcr.nodetype.NodeTypeManager) Node(javax.jcr.Node) ServletException(javax.servlet.ServletException) PrintWriter(java.io.PrintWriter) InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) RepositoryException(javax.jcr.RepositoryException) PropertyDefinition(javax.jcr.nodetype.PropertyDefinition) NodeType(javax.jcr.nodetype.NodeType) Value(javax.jcr.Value) BufferedReader(java.io.BufferedReader)

Aggregations

NodeTypeManager (javax.jcr.nodetype.NodeTypeManager)93 NodeType (javax.jcr.nodetype.NodeType)41 Node (javax.jcr.Node)32 Session (javax.jcr.Session)30 NodeTypeTemplate (javax.jcr.nodetype.NodeTypeTemplate)29 NodeTypeIterator (javax.jcr.nodetype.NodeTypeIterator)27 Test (org.junit.Test)24 ConstraintViolationException (javax.jcr.nodetype.ConstraintViolationException)14 PropertyDefinition (javax.jcr.nodetype.PropertyDefinition)14 RepositoryException (javax.jcr.RepositoryException)13 ByteArrayInputStream (java.io.ByteArrayInputStream)12 NotExecutableException (org.apache.jackrabbit.test.NotExecutableException)11 ArrayList (java.util.ArrayList)8 Workspace (javax.jcr.Workspace)8 NoSuchNodeTypeException (javax.jcr.nodetype.NoSuchNodeTypeException)8 NodeDefinition (javax.jcr.nodetype.NodeDefinition)8 PropertyDefinitionTemplate (javax.jcr.nodetype.PropertyDefinitionTemplate)8 NodeDefinitionTemplate (javax.jcr.nodetype.NodeDefinitionTemplate)7 AbstractRepositoryTest (org.apache.jackrabbit.oak.jcr.AbstractRepositoryTest)7 InputStream (java.io.InputStream)6