001 /**
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache License, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 * http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the License for the specific language governing permissions and
015 * limitations under the License.
016 */
017 package org.apache.camel.component.jcr;
018
019 import javax.jcr.Node;
020 import javax.jcr.RepositoryException;
021 import javax.jcr.Session;
022 import javax.jcr.Value;
023
024 import org.apache.camel.Exchange;
025 import org.apache.camel.TypeConverter;
026 import org.apache.camel.impl.DefaultProducer;
027 import org.apache.jackrabbit.util.Text;
028
029 public class JcrProducer extends DefaultProducer {
030
031 public JcrProducer(JcrEndpoint jcrEndpoint) throws RepositoryException {
032 super(jcrEndpoint);
033 }
034
035 public void process(Exchange exchange) throws Exception {
036 Session session = openSession();
037 try {
038 Node base = findOrCreateNode(session.getRootNode(),
039 getJcrEndpoint().getBase());
040 Node node = findOrCreateNode(base, getNodeName(exchange));
041 TypeConverter converter = exchange.getContext().getTypeConverter();
042 for (String key : exchange.getProperties().keySet()) {
043 Value value = converter.convertTo(Value.class, exchange,
044 exchange.getProperty(key));
045 node.setProperty(key, value);
046 }
047 node.addMixin("mix:referenceable");
048 session.save();
049 exchange.getOut().setBody(node.getUUID());
050 } finally {
051 if (session != null && session.isLive()) {
052 session.logout();
053 }
054 }
055 }
056
057 private String getNodeName(Exchange exchange) {
058 if (exchange.getProperty(JcrConstants.JCR_NODE_NAME) != null) {
059 return exchange.getProperty(JcrConstants.JCR_NODE_NAME,
060 String.class);
061 }
062 return exchange.getExchangeId();
063 }
064
065 private Node findOrCreateNode(Node parent, String path)
066 throws RepositoryException {
067 Node result = parent;
068 for (String component : path.split("/")) {
069 component = Text.escapeIllegalJcrChars(component);
070 if (component.length() > 0 && !result.hasNode(component)) {
071 result = result.addNode(component);
072 }
073 }
074 return result;
075 }
076
077 protected Session openSession() throws RepositoryException {
078 return getJcrEndpoint().getRepository().login(
079 getJcrEndpoint().getCredentials());
080 }
081
082 private JcrEndpoint getJcrEndpoint() {
083 JcrEndpoint endpoint = (JcrEndpoint) getEndpoint();
084 return endpoint;
085 }
086 }