Search in sources :

Example 11 with PropertyIterator

use of javax.jcr.PropertyIterator in project sling by apache.

the class RepositoryTestHelper method dump.

public static void dump(Node node) throws RepositoryException {
    if (node.getPath().equals("/jcr:system") || node.getPath().equals("/rep:policy")) {
        // ignore that one
        return;
    }
    PropertyIterator pi = node.getProperties();
    StringBuilder sb = new StringBuilder();
    while (pi.hasNext()) {
        Property p = pi.nextProperty();
        sb.append(" ");
        sb.append(p.getName());
        sb.append("=");
        if (p.getType() == PropertyType.BOOLEAN) {
            sb.append(p.getBoolean());
        } else if (p.getType() == PropertyType.STRING) {
            sb.append(p.getString());
        } else if (p.getType() == PropertyType.DATE) {
            sb.append(p.getDate().getTime());
        } else if (p.getType() == PropertyType.LONG) {
            sb.append(p.getLong());
        } else {
            sb.append("<unknown type=" + p.getType() + "/>");
        }
    }
    StringBuffer depth = new StringBuffer();
    for (int i = 0; i < node.getDepth(); i++) {
        depth.append(" ");
    }
    logger.info(depth + "/" + node.getName() + " -- " + sb);
    NodeIterator it = node.getNodes();
    while (it.hasNext()) {
        Node child = it.nextNode();
        dump(child);
    }
}
Also used : NodeIterator(javax.jcr.NodeIterator) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) Property(javax.jcr.Property)

Example 12 with PropertyIterator

use of javax.jcr.PropertyIterator in project camel by apache.

the class JcrProducer method process.

public void process(Exchange exchange) throws Exception {
    TypeConverter converter = exchange.getContext().getTypeConverter();
    Session session = openSession();
    Message message = exchange.getIn();
    String operation = determineOperation(message);
    try {
        if (JcrConstants.JCR_INSERT.equals(operation)) {
            Node base = findOrCreateNode(session.getRootNode(), getJcrEndpoint().getBase(), "");
            Node node = findOrCreateNode(base, getNodeName(message), getNodeType(message));
            Map<String, Object> headers = filterComponentHeaders(message.getHeaders());
            for (String key : headers.keySet()) {
                Object header = message.getHeader(key);
                if (header != null && Object[].class.isAssignableFrom(header.getClass())) {
                    Value[] value = converter.convertTo(Value[].class, exchange, header);
                    node.setProperty(key, value);
                } else {
                    Value value = converter.convertTo(Value.class, exchange, header);
                    node.setProperty(key, value);
                }
            }
            node.addMixin("mix:referenceable");
            exchange.getOut().setBody(node.getIdentifier());
            session.save();
        } else if (JcrConstants.JCR_GET_BY_ID.equals(operation)) {
            Node node = session.getNodeByIdentifier(exchange.getIn().getMandatoryBody(String.class));
            PropertyIterator properties = node.getProperties();
            while (properties.hasNext()) {
                Property property = properties.nextProperty();
                Class<?> aClass = classForJCRType(property);
                Object value;
                if (property.isMultiple()) {
                    value = converter.convertTo(aClass, exchange, property.getValues());
                } else {
                    value = converter.convertTo(aClass, exchange, property.getValue());
                }
                message.setHeader(property.getName(), value);
            }
        } else {
            throw new RuntimeException("Unsupported operation: " + operation);
        }
    } finally {
        if (session != null && session.isLive()) {
            session.logout();
        }
    }
}
Also used : Message(org.apache.camel.Message) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) TypeConverter(org.apache.camel.TypeConverter) Value(javax.jcr.Value) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 13 with PropertyIterator

use of javax.jcr.PropertyIterator in project kylo by Teradata.

the class KyloEsClient method storeDocument.

@Override
public boolean storeDocument(String name, String type, String id, EsRequest doc) throws IOException {
    log.debug("KyloEsClient - store document in ES: name={}, type={}, id={}, doc={}", name, type, id, doc.toString());
    if (name.contains("kylo-categories")) {
        // Categories start
        log.debug("Indexing a category's metadata now");
        KyloEsNodeDetails kyloEsNodeDetails = metadataAccess.read(() -> {
            Session session = JcrMetadataAccess.getActiveSession();
            KyloEsNodeDetails kyloEsNodeDetailsInternal = new KyloEsNodeDetails();
            if (session != null) {
                Node node = session.getNodeByIdentifier(id);
                if (node != null) {
                    kyloEsNodeDetailsInternal.setAllowIndexing(node.getProperty(ALLOW_INDEXING_PROPERTY).getValue().getString());
                    kyloEsNodeDetailsInternal.setExtendedId(id);
                    log.debug("KyloEsClient - (category metadata) - indexing={}, id={}", kyloEsNodeDetailsInternal.getAllowIndexing(), kyloEsNodeDetailsInternal.getExtendedId());
                } else {
                    log.warn("KyloEsClient - (category metadata) null node retrieved from metadata for id {}", id);
                }
            } else {
                log.warn("KyloEsClient - (category metadata) store document in ES: Could not get active session to metadata");
            }
            log.debug("KyloEsClient - (category metadata) node details: allow indexing = {}, extended id = {}, id = {}", kyloEsNodeDetailsInternal.getAllowIndexing(), kyloEsNodeDetailsInternal.getExtendedId(), kyloEsNodeDetailsInternal.getId());
            return kyloEsNodeDetailsInternal;
        }, MetadataAccess.SERVICE);
        if (kyloEsNodeDetails != null) {
            String indexIndicator = kyloEsNodeDetails.getAllowIndexing();
            // N indicates do-not-index, otherwise index
            if ((indexIndicator != null) && (indexIndicator.equals(INDEX_INDICATOR_FOR_NO))) {
                log.debug("KyloEsClient - Category metadata indexing not allowed for extended id= {}, id = {}", kyloEsNodeDetails.getExtendedId(), kyloEsNodeDetails.getId());
                CloseableHttpClient client = HttpClients.createDefault();
                HttpDelete method = new HttpDelete(String.format("http://%s:%d/%s/%s/%s", kyloHost, kyloPort, name, type, kyloEsNodeDetails.getExtendedId()));
                try {
                    log.debug("KyloEsClient - Doc id to be deleted from Elasticsearch: {}. (The modeshape identifier is {})", kyloEsNodeDetails.getExtendedId(), kyloEsNodeDetails.getId());
                    CloseableHttpResponse resp = client.execute(method);
                    int statusCode = resp.getStatusLine().getStatusCode();
                    log.debug("KyloEsClient - Deletion result (Expected 200/404): {}", statusCode);
                    return statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NOT_FOUND;
                } finally {
                    method.releaseConnection();
                }
            } else {
                log.debug("KyloEsClient - Category metadata indexing allowed for extended id= {}, id = {}", kyloEsNodeDetails.getExtendedId(), kyloEsNodeDetails.getId());
                CloseableHttpClient client = HttpClients.createDefault();
                HttpPost method = new HttpPost(String.format("http://%s:%d/%s/%s/%s", kyloHost, kyloPort, name, type, id));
                boolean hasIndexingBeenTurnedOn = checkIfIndexingHasBeenTurnedOnForCategories(doc);
                log.debug("Check if indexing has been turned on reveals (for category metadata): {}", hasIndexingBeenTurnedOn);
                JSONObject fullDocumentJsonObject = new JSONObject();
                if (hasIndexingBeenTurnedOn) {
                    fullDocumentJsonObject = metadataAccess.read(() -> {
                        Session session = JcrMetadataAccess.getActiveSession();
                        JSONObject fullDocumentJsonObjectInternal = new JSONObject();
                        if (session != null) {
                            Node node = session.getNodeByIdentifier(id);
                            if (node != null) {
                                PropertyIterator propertyIterator = node.getProperties();
                                while (propertyIterator.hasNext()) {
                                    Property property = (Property) propertyIterator.next();
                                    log.debug(property.toString());
                                    if ((property.toString().contains("jcr:title")) || (property.toString().contains("tba:allowIndexing")) || (property.toString().contains("tba:systemName")) || (property.toString().contains("jcr:description"))) {
                                        try {
                                            String propertyName = property.getName();
                                            String propertyValue = property.getString();
                                            fullDocumentJsonObjectInternal.put(propertyName, propertyValue);
                                            fullDocumentJsonObjectInternal.put("lowercase_" + propertyName, propertyValue.toLowerCase());
                                            fullDocumentJsonObjectInternal.put("uppercase_" + propertyName, propertyValue.toUpperCase());
                                            fullDocumentJsonObjectInternal.put("length_" + propertyName, propertyValue.length());
                                        } catch (JSONException e) {
                                            log.error("Error encountered when constructing JSON payload for category metadata to index, category id = {}: {}", id, e.getMessage());
                                            e.printStackTrace();
                                        } catch (RepositoryException e) {
                                            log.error("Error encountered related to repository when trying to index category metadata, category id = {}: {}", id, e.getMessage());
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            } else {
                                log.warn("KyloEsClient - (category metadata) null node retrieved from metadata for category id {}", id);
                            }
                        } else {
                            log.warn("KyloEsClient - (category metadata) store document in ES: Could not get active session to metadata");
                        }
                        return fullDocumentJsonObjectInternal;
                    }, MetadataAccess.SERVICE);
                    log.debug("Full document JSON for category metadata to index: {}", fullDocumentJsonObject);
                }
                try {
                    StringEntity requestEntity;
                    if (hasIndexingBeenTurnedOn) {
                        requestEntity = new StringEntity(fullDocumentJsonObject.toString(), ContentType.APPLICATION_JSON);
                    } else {
                        requestEntity = new StringEntity(doc.toString(), ContentType.APPLICATION_JSON);
                    }
                    method.setEntity(requestEntity);
                    CloseableHttpResponse resp = client.execute(method);
                    int statusCode = resp.getStatusLine().getStatusCode();
                    log.info("Indexing result for category id {} (Expected 200/201): {}", id, statusCode);
                    return statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_OK;
                } finally {
                    method.releaseConnection();
                }
            }
        }
    // Categories end
    } else if (name.contains("kylo-feeds")) {
        // Feeds start
        log.debug("Indexing a feed's metadata now");
        KyloEsNodeDetails kyloEsNodeDetails = metadataAccess.read(() -> {
            Session session = JcrMetadataAccess.getActiveSession();
            KyloEsNodeDetails kyloEsNodeDetailsInternal = new KyloEsNodeDetails();
            if (session != null) {
                Node node = session.getNodeByIdentifier(id);
                if (node != null) {
                    kyloEsNodeDetailsInternal.setAllowIndexing(node.getProperty(ALLOW_INDEXING_PROPERTY).getValue().getString());
                    kyloEsNodeDetailsInternal.setExtendedId(id);
                    log.debug("KyloEsClient - (feed metadata) - indexing={}, id={}", kyloEsNodeDetailsInternal.getAllowIndexing(), kyloEsNodeDetailsInternal.getExtendedId());
                } else {
                    log.warn("KyloEsClient - (feed metadata) null node retrieved from metadata for id {}", id);
                }
            } else {
                log.warn("KyloEsClient - (feed metadata) store document in ES: Could not get active session to metadata");
            }
            log.debug("KyloEsClient - (feed metadata) node details: allow indexing = {}, extended id = {}, id = {}", kyloEsNodeDetailsInternal.getAllowIndexing(), kyloEsNodeDetailsInternal.getExtendedId(), kyloEsNodeDetailsInternal.getId());
            return kyloEsNodeDetailsInternal;
        }, MetadataAccess.SERVICE);
        if (kyloEsNodeDetails != null) {
            String indexIndicator = kyloEsNodeDetails.getAllowIndexing();
            // N indicates do-not-index, otherwise index
            if ((indexIndicator != null) && (indexIndicator.equals(INDEX_INDICATOR_FOR_NO))) {
                log.debug("KyloEsClient - Feed metadata indexing not allowed for extended id= {}, id = {}", kyloEsNodeDetails.getExtendedId(), kyloEsNodeDetails.getId());
                CloseableHttpClient client = HttpClients.createDefault();
                HttpDelete method = new HttpDelete(String.format("http://%s:%d/%s/%s/%s", kyloHost, kyloPort, name, type, kyloEsNodeDetails.getExtendedId()));
                try {
                    log.debug("KyloEsClient - Doc id to be deleted from Elasticsearch: {}. (The modeshape identifier is {})", kyloEsNodeDetails.getExtendedId(), kyloEsNodeDetails.getId());
                    CloseableHttpResponse resp = client.execute(method);
                    int statusCode = resp.getStatusLine().getStatusCode();
                    log.debug("KyloEsClient - Deletion result (Expected 200/404): {}", statusCode);
                    return statusCode == HttpStatus.SC_OK || statusCode == HttpStatus.SC_NOT_FOUND;
                } finally {
                    method.releaseConnection();
                }
            } else {
                log.debug("KyloEsClient - Feed metadata indexing allowed for extended id= {}, id = {}", kyloEsNodeDetails.getExtendedId(), kyloEsNodeDetails.getId());
                CloseableHttpClient client = HttpClients.createDefault();
                HttpPost method = new HttpPost(String.format("http://%s:%d/%s/%s/%s", kyloHost, kyloPort, name, type, id));
                boolean hasIndexingBeenTurnedOn = checkIfIndexingHasBeenTurnedOnForFeeds(doc);
                log.debug("Check if indexing has been turned on reveals (for feed metadata): {}", hasIndexingBeenTurnedOn);
                JSONObject fullDocumentJsonObject = new JSONObject();
                if (hasIndexingBeenTurnedOn) {
                    fullDocumentJsonObject = metadataAccess.read(() -> {
                        Session session = JcrMetadataAccess.getActiveSession();
                        JSONObject fullDocumentJsonObjectInternal = new JSONObject();
                        if (session != null) {
                            Node node = session.getNodeByIdentifier(id);
                            if (node != null) {
                                PropertyIterator propertyIterator = node.getProperties();
                                while (propertyIterator.hasNext()) {
                                    Property property = (Property) propertyIterator.next();
                                    log.debug(property.toString());
                                    if ((property.toString().contains("jcr:title")) || (property.toString().contains("tba:allowIndexing")) || (property.toString().contains("tba:systemName")) || (property.toString().contains("jcr:description")) || (property.toString().contains("tba:category")) || (property.toString().contains("tba:tags"))) {
                                        try {
                                            String propertyName = property.getName();
                                            String propertyValue;
                                            if (property.isMultiple()) {
                                                propertyValue = StringUtils.join(property.getValues(), " ");
                                            } else {
                                                propertyValue = property.getString();
                                            }
                                            fullDocumentJsonObjectInternal.put(propertyName, propertyValue);
                                            fullDocumentJsonObjectInternal.put("lowercase_" + propertyName, propertyValue.toLowerCase());
                                            fullDocumentJsonObjectInternal.put("uppercase_" + propertyName, propertyValue.toUpperCase());
                                            fullDocumentJsonObjectInternal.put("length_" + propertyName, propertyValue.length());
                                        } catch (JSONException e) {
                                            log.error("Error encountered when constructing JSON payload for feed metadata to index, feed id = {}: {}", id, e.getMessage());
                                            e.printStackTrace();
                                        } catch (RepositoryException e) {
                                            log.error("Error encountered related to repository when trying to index feed metadata: feed id = {}: {}", id, e.getMessage());
                                            e.printStackTrace();
                                        }
                                    }
                                }
                            } else {
                                log.warn("KyloEsClient - (feed metadata) null node retrieved from metadata for feed id {}", id);
                            }
                        } else {
                            log.warn("KyloEsClient - store document in ES: Could not get active session to metadata");
                        }
                        return fullDocumentJsonObjectInternal;
                    }, MetadataAccess.SERVICE);
                    log.debug("Full document JSON for feed metadata to index: {}", fullDocumentJsonObject);
                }
                try {
                    StringEntity requestEntity;
                    if (hasIndexingBeenTurnedOn) {
                        requestEntity = new StringEntity(fullDocumentJsonObject.toString(), ContentType.APPLICATION_JSON);
                    } else {
                        requestEntity = new StringEntity(doc.toString(), ContentType.APPLICATION_JSON);
                    }
                    method.setEntity(requestEntity);
                    CloseableHttpResponse resp = client.execute(method);
                    int statusCode = resp.getStatusLine().getStatusCode();
                    log.info("Indexing result for feed id {} (Expected 200/201): {}", id, statusCode);
                    return statusCode == HttpStatus.SC_CREATED || statusCode == HttpStatus.SC_OK;
                } finally {
                    method.releaseConnection();
                }
            }
        }
    }
    return false;
}
Also used : CloseableHttpClient(org.apache.http.impl.client.CloseableHttpClient) HttpPost(org.apache.http.client.methods.HttpPost) HttpDelete(org.apache.http.client.methods.HttpDelete) Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) JSONException(org.codehaus.jettison.json.JSONException) RepositoryException(javax.jcr.RepositoryException) StringEntity(org.apache.http.entity.StringEntity) JSONObject(org.codehaus.jettison.json.JSONObject) CloseableHttpResponse(org.apache.http.client.methods.CloseableHttpResponse) Property(javax.jcr.Property) Session(javax.jcr.Session)

Example 14 with PropertyIterator

use of javax.jcr.PropertyIterator in project kylo by Teradata.

the class UserPropertiesUpgradeAction method moveUserProperties.

private void moveUserProperties(JcrFeed feed) {
    try {
        Node feedNode = feed.getNode();
        Node feedDetailsNode = feed.getFeedDetails().get().getNode();
        final PropertyIterator iterator = feedNode.getProperties();
        final String prefix = JcrMetadataAccess.USR_PREFIX + ":";
        while (iterator.hasNext()) {
            final Property property = iterator.nextProperty();
            if (property.getName().startsWith(prefix)) {
                moveProperty(property, feedDetailsNode);
            }
        }
    } catch (RepositoryException e) {
        throw new IllegalStateException(String.format("Failed to move user properties for feed %s", feed.getName()), e);
    }
}
Also used : Node(javax.jcr.Node) PropertyIterator(javax.jcr.PropertyIterator) RepositoryException(javax.jcr.RepositoryException) Property(javax.jcr.Property)

Example 15 with PropertyIterator

use of javax.jcr.PropertyIterator in project kylo by Teradata.

the class JcrPropertyUtil method setUserProperties.

/**
 * Sets the specified user-defined properties on the specified node.
 *
 * @param node       the target node
 * @param fields     the predefined user fields
 * @param properties the map of user-defined property names to values
 * @throws IllegalStateException       if a property name is encoded incorrectly
 * @throws MetadataRepositoryException if the metadata repository is unavailable
 */
public static void setUserProperties(@Nonnull final Node node, @Nonnull final Set<UserFieldDescriptor> fields, @Nonnull final Map<String, String> properties) {
    // Verify required properties are not empty
    for (final UserFieldDescriptor field : fields) {
        if (field.isRequired() && StringUtils.isEmpty(properties.get(field.getSystemName()))) {
            throw new MissingUserPropertyException("Missing required property: " + field.getSystemName());
        }
    }
    // Set properties on node
    final Set<String> newProperties = new HashSet<>(properties.size());
    final String prefix = JcrMetadataAccess.USR_PREFIX + ":";
    properties.forEach((key, value) -> {
        try {
            final String name = prefix + URLEncoder.encode(key, USER_PROPERTY_ENCODING);
            newProperties.add(name);
            node.setProperty(name, value);
        } catch (AccessDeniedException e) {
            log.debug("Access denied", e);
            throw new AccessControlException(e.getMessage());
        } catch (RepositoryException e) {
            throw new MetadataRepositoryException("Failed to set user property \"" + key + "\" on node: " + node, e);
        } catch (UnsupportedEncodingException e) {
            throw new IllegalStateException(e.toString(), e);
        }
    });
    // Get node properties
    final PropertyIterator iterator;
    try {
        iterator = node.getProperties();
    } catch (AccessDeniedException e) {
        log.debug("Access denied", e);
        throw new AccessControlException(e.getMessage());
    } catch (RepositoryException e) {
        throw new MetadataRepositoryException("Failed to get properties for node: " + node, e);
    }
    // Remove properties from node
    while (iterator.hasNext()) {
        final Property property = iterator.nextProperty();
        try {
            final String name = property.getName();
            if (name.startsWith(prefix) && !newProperties.contains(name)) {
                property.remove();
            }
        } catch (AccessDeniedException e) {
            log.debug("Access denied", e);
            throw new AccessControlException(e.getMessage());
        } catch (RepositoryException e) {
            throw new MetadataRepositoryException("Failed to remove property \"" + property + "\" on node: " + node, e);
        }
    }
}
Also used : MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) AccessDeniedException(javax.jcr.AccessDeniedException) UserFieldDescriptor(com.thinkbiganalytics.metadata.api.extension.UserFieldDescriptor) JcrUserFieldDescriptor(com.thinkbiganalytics.metadata.modeshape.extension.JcrUserFieldDescriptor) PropertyIterator(javax.jcr.PropertyIterator) AccessControlException(java.security.AccessControlException) UnsupportedEncodingException(java.io.UnsupportedEncodingException) MetadataRepositoryException(com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException) RepositoryException(javax.jcr.RepositoryException) MissingUserPropertyException(com.thinkbiganalytics.metadata.api.MissingUserPropertyException) Property(javax.jcr.Property) HashSet(java.util.HashSet)

Aggregations

PropertyIterator (javax.jcr.PropertyIterator)130 Property (javax.jcr.Property)97 Node (javax.jcr.Node)79 NodeIterator (javax.jcr.NodeIterator)31 RepositoryException (javax.jcr.RepositoryException)28 Value (javax.jcr.Value)28 Session (javax.jcr.Session)21 Test (org.junit.Test)21 ArrayList (java.util.ArrayList)19 HashSet (java.util.HashSet)14 PathNotFoundException (javax.jcr.PathNotFoundException)13 HashMap (java.util.HashMap)12 AccessDeniedException (javax.jcr.AccessDeniedException)7 Item (javax.jcr.Item)5 JackrabbitNode (org.apache.jackrabbit.api.JackrabbitNode)5 JSONObject (org.codehaus.jettison.json.JSONObject)5 MetadataRepositoryException (com.thinkbiganalytics.metadata.modeshape.MetadataRepositoryException)4 AccessControlException (java.security.AccessControlException)4 ArgumentMatchers.anyString (org.mockito.ArgumentMatchers.anyString)4 RepositoryFileDaoReferentialIntegrityException (org.pentaho.platform.repository2.unified.exception.RepositoryFileDaoReferentialIntegrityException)4