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 java.net.URI;
020    import java.net.URISyntaxException;
021    
022    import javax.jcr.Credentials;
023    import javax.jcr.Repository;
024    import javax.jcr.SimpleCredentials;
025    
026    import org.apache.camel.Consumer;
027    import org.apache.camel.Processor;
028    import org.apache.camel.Producer;
029    import org.apache.camel.RuntimeCamelException;
030    import org.apache.camel.impl.DefaultEndpoint;
031    
032    /**
033     * A JCR endpoint
034     */
035    public class JcrEndpoint extends DefaultEndpoint {
036    
037        private Credentials credentials;
038        private Repository repository;
039        private String base;
040    
041        protected JcrEndpoint(String endpointUri, JcrComponent component) {
042            super(endpointUri, component);
043            try {
044                URI uri = new URI(endpointUri);
045                if (uri.getUserInfo() != null) {
046                    String[] creds = uri.getUserInfo().split(":");
047                    if (creds != null) {
048                        String username = creds[0];
049                        String password = creds.length > 1 ? creds[1] : null;
050                        this.credentials = new SimpleCredentials(username, password
051                                .toCharArray());
052                    }
053                }
054                this.repository = component.getCamelContext().getRegistry().lookup(uri.getHost(), Repository.class);
055                if (repository == null) {
056                    throw new RuntimeCamelException("No JCR repository defined under '" + uri.getHost() + "'");
057                }
058                this.base = uri.getPath().replaceAll("^/", "");
059            } catch (URISyntaxException e) {
060                throw new IllegalArgumentException("Invalid URI: " + endpointUri, e);
061            }
062        }
063    
064        public JcrEndpoint(String endpointUri, String base, Credentials credentials, Repository repository) {
065            super(endpointUri);
066            this.base = base;
067            this.credentials = credentials;
068            this.repository = repository;
069        }
070    
071        /**
072         * Currently unsupported
073         * @throws RuntimeCamelException
074         */
075        public Consumer createConsumer(Processor processor) throws Exception {
076            throw new RuntimeCamelException("No consumer endpoint support for JCR available");
077        }
078    
079        public Producer createProducer() throws Exception {
080            return new JcrProducer(this);
081        }
082    
083        public boolean isSingleton() {
084            return false;
085        }
086    
087        /**
088         * Get the {@link Repository}
089         * 
090         * @return the repository
091         */
092        protected Repository getRepository() {
093            return repository;
094        }
095    
096        /**
097         * Get the {@link Credentials} for establishing the JCR repository connection
098         * 
099         * @return the credentials
100         */
101        protected Credentials getCredentials() {
102            return credentials;
103        }
104    
105        /**
106         * Get the base node when accessing the reposititory
107         * 
108         * @return the base node
109         */
110        protected String getBase() {
111            return base;
112        }
113    
114    }