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.file.remote;
018    
019    import java.io.ByteArrayOutputStream;
020    
021    import org.apache.camel.ExchangePattern;
022    import org.apache.camel.Message;
023    import org.apache.camel.impl.ScheduledPollEndpoint;
024    import org.apache.camel.util.UuidGenerator;
025    
026    public abstract class RemoteFileEndpoint<T extends RemoteFileExchange> extends ScheduledPollEndpoint<T> {
027        private RemoteFileBinding binding;
028        private RemoteFileConfiguration configuration;
029    
030        public RemoteFileEndpoint(String uri, RemoteFileComponent component, RemoteFileConfiguration configuration) {
031            super(uri, component);
032            this.configuration = configuration;
033        }
034    
035        protected RemoteFileEndpoint(String endpointUri, RemoteFileConfiguration configuration) {
036            super(endpointUri);
037            this.configuration = configuration;
038        }
039    
040        protected RemoteFileEndpoint(String endpointUri) {
041            this(endpointUri, new RemoteFileConfiguration());
042        }
043    
044        protected RemoteFileBinding createRemoteFileBinding() {
045            return new RemoteFileBinding();
046        }
047    
048        public T createExchange() {
049            return (T) new RemoteFileExchange(getCamelContext(), getExchangePattern(), getBinding());
050        }
051    
052        public T createExchange(ExchangePattern pattern) {
053            return (T) new RemoteFileExchange(getCamelContext(), pattern, getBinding());
054        }
055    
056        public T createExchange(String fullFileName, String fileName, long fileLength, ByteArrayOutputStream outputStream) {
057            return (T) new RemoteFileExchange(getCamelContext(), getExchangePattern(), getBinding(),
058                    getConfiguration().getHost(), fullFileName, fileName, fileLength, outputStream);
059        }
060    
061        public RemoteFileBinding getBinding() {
062            if (binding == null) {
063                binding = createRemoteFileBinding();
064            }
065            return binding;
066        }
067    
068        public void setBinding(RemoteFileBinding binding) {
069            this.binding = binding;
070        }
071    
072        public boolean isSingleton() {
073            return true;
074        }
075    
076        public RemoteFileConfiguration getConfiguration() {
077            return configuration;
078        }
079    
080        public void setConfiguration(RemoteFileConfiguration configuration) {
081            this.configuration = configuration;
082        }
083    
084        /**
085          * Return the file name that will be auto-generated for the given message if none is provided
086          */
087        public String getGeneratedFileName(Message message) {
088            return getFileFriendlyMessageId(message.getMessageId());
089        }
090    
091        protected String getFileFriendlyMessageId(String id) {
092            return UuidGenerator.generateSanitizedId(id);
093        }
094    }