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.servicemix.truezip;
018
019 import java.io.BufferedOutputStream;
020 import java.io.IOException;
021 import java.io.OutputStream;
022
023 import javax.jbi.management.DeploymentException;
024 import javax.jbi.messaging.MessageExchange;
025 import javax.jbi.messaging.NormalizedMessage;
026 import javax.jbi.servicedesc.ServiceEndpoint;
027
028 import de.schlichtherle.io.File;
029 import de.schlichtherle.io.FileOutputStream;
030
031 import org.apache.servicemix.common.endpoints.ProviderEndpoint;
032 import org.apache.servicemix.components.util.DefaultFileMarshaler;
033 import org.apache.servicemix.components.util.FileMarshaler;
034 import org.apache.servicemix.id.IdGenerator;
035
036 /**
037 * An endpoint which receives a message and writes the content to a file.
038 *
039 * @org.apache.xbean.XBean element="sender"
040 *
041 * @version $Revision: 43330 $
042 */
043 public class TrueZipSenderEndpoint extends ProviderEndpoint implements TrueZipEndpointType {
044
045 private File directory;
046
047 private FileMarshaler marshaler = new DefaultFileMarshaler();
048
049 private String tempFilePrefix = "servicemix-";
050
051 private String tempFileSuffix = ".xml";
052
053 private boolean autoCreateDirectory = true;
054
055 private IdGenerator idGenerator;
056
057 public TrueZipSenderEndpoint() {
058 }
059
060 public TrueZipSenderEndpoint(TrueZipComponent component, ServiceEndpoint endpoint) {
061 super(component, endpoint);
062 }
063
064 public void validate() throws DeploymentException {
065 super.validate();
066 if (directory == null) {
067 throw new DeploymentException("You must specify the directory property");
068 }
069 if (isAutoCreateDirectory()) {
070 directory.mkdirs();
071 }
072 if (!directory.isDirectory()) {
073 throw new DeploymentException("The directory property must be a directory but was: " + directory);
074 }
075 }
076
077 protected void processInOnly(MessageExchange exchange, NormalizedMessage in) throws Exception {
078 OutputStream out = null;
079 try {
080 String name = marshaler.getOutputName(exchange, in);
081 File newFile = null;
082 if (name == null) {
083 newFile = new File(directory, getNewTempName());
084 } else {
085 newFile = new File(directory, name);
086 }
087 if (logger.isDebugEnabled()) {
088 logger.debug("Writing to file: " + newFile.getCanonicalPath());
089 }
090 out = new BufferedOutputStream(new FileOutputStream(newFile));
091 marshaler.writeMessage(exchange, in, out, name);
092 } finally {
093 if (out != null) {
094 try {
095 out.close();
096 } catch (IOException e) {
097 logger.error("Caught exception while closing stream on error: " + e, e);
098 }
099 }
100 }
101 }
102
103 protected String getNewTempName() {
104 if (idGenerator == null) {
105 idGenerator = new IdGenerator(tempFilePrefix);
106 }
107 return idGenerator.generateSanitizedId() + tempFileSuffix;
108 }
109
110 protected void processInOut(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws Exception {
111 /** TODO list the files? */
112 super.processInOut(exchange, in, out);
113 }
114
115 // Properties
116 // -------------------------------------------------------------------------
117 public java.io.File getDirectory() {
118 return directory;
119 }
120
121 public void setDirectory(java.io.File directory) {
122 this.directory = new File(directory);
123 }
124
125 public FileMarshaler getMarshaler() {
126 return marshaler;
127 }
128
129 public void setMarshaler(FileMarshaler marshaler) {
130 this.marshaler = marshaler;
131 }
132
133 public String getTempFilePrefix() {
134 return tempFilePrefix;
135 }
136
137 public void setTempFilePrefix(String tempFilePrefix) {
138 this.tempFilePrefix = tempFilePrefix;
139 }
140
141 public String getTempFileSuffix() {
142 return tempFileSuffix;
143 }
144
145 public void setTempFileSuffix(String tempFileSuffix) {
146 this.tempFileSuffix = tempFileSuffix;
147 }
148
149 public boolean isAutoCreateDirectory() {
150 return autoCreateDirectory;
151 }
152
153 public void setAutoCreateDirectory(boolean autoCreateDirectory) {
154 this.autoCreateDirectory = autoCreateDirectory;
155 }
156
157 }