You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@servicemix.apache.org by ge...@apache.org on 2008/08/29 14:30:20 UTC
svn commit: r690206 - in /servicemix/utils/trunk: ./
src/main/java/org/apache/servicemix/jbi/helper/
src/main/java/org/apache/servicemix/jbi/transformer/
src/main/java/org/apache/servicemix/util/
src/main/java/org/apache/servicemix/util/jaf/ src/test/j...
Author: gertv
Date: Fri Aug 29 05:30:19 2008
New Revision: 690206
URL: http://svn.apache.org/viewvc?rev=690206&view=rev
Log:
SM-1455: Move shared code from servicemix-core to servicemix-utils
Added:
servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java
servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/
servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/FileUtil.java
servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/jaf/
servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/jaf/ByteArrayDataSource.java
servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/
servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/CopyTransformerTest.java
servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/MessageCopierTest.java
servicemix/utils/trunk/src/test/java/org/apache/servicemix/util/
servicemix/utils/trunk/src/test/java/org/apache/servicemix/util/FileUtilTest.java
Modified:
servicemix/utils/trunk/pom.xml
servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/CopyTransformer.java
servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/MessageTransformer.java
Modified: servicemix/utils/trunk/pom.xml
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/pom.xml?rev=690206&r1=690205&r2=690206&view=diff
==============================================================================
--- servicemix/utils/trunk/pom.xml (original)
+++ servicemix/utils/trunk/pom.xml Fri Aug 29 05:30:19 2008
@@ -82,6 +82,12 @@
<version>2.1.0</version>
<optional>true</optional>
</dependency>
+ <dependency>
+ <groupId>commons-io</groupId>
+ <artifactId>commons-io</artifactId>
+ <version>1.3.1</version>
+ <optional>true</optional>
+ </dependency>
<!-- test dependencies -->
<dependency>
<groupId>junit</groupId>
Added: servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java?rev=690206&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/helper/MessageUtil.java Fri Aug 29 05:30:19 2008
@@ -0,0 +1,260 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.helper;
+
+import java.io.ByteArrayOutputStream;
+import java.io.IOException;
+import java.io.Serializable;
+import java.util.HashMap;
+import java.util.Iterator;
+import java.util.Map;
+import java.util.Set;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.jbi.messaging.Fault;
+import javax.jbi.messaging.MessageExchange;
+import javax.jbi.messaging.MessagingException;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.security.auth.Subject;
+import javax.xml.parsers.ParserConfigurationException;
+import javax.xml.transform.Source;
+import javax.xml.transform.TransformerException;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.SAXException;
+
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.util.FileUtil;
+import org.apache.servicemix.util.jaf.ByteArrayDataSource;
+
+/**
+ * @author gnodet
+ * @version $Revision: 376451 $
+ */
+public final class MessageUtil {
+
+ private MessageUtil() {
+ }
+
+ public static void transfer(NormalizedMessage source, NormalizedMessage dest) throws MessagingException {
+ dest.setContent(source.getContent());
+ for (Iterator it = source.getPropertyNames().iterator(); it.hasNext();) {
+ String name = (String) it.next();
+ dest.setProperty(name, source.getProperty(name));
+ }
+ for (Iterator it = source.getAttachmentNames().iterator(); it.hasNext();) {
+ String name = (String) it.next();
+ dest.addAttachment(name, source.getAttachment(name));
+ }
+ dest.setSecuritySubject(source.getSecuritySubject());
+ }
+
+ public static NormalizedMessage copy(NormalizedMessage source) throws MessagingException {
+ if (source instanceof Fault) {
+ return new FaultImpl((Fault) source);
+ } else {
+ return new NormalizedMessageImpl(source);
+ }
+ }
+
+ public static NormalizedMessage copyIn(MessageExchange exchange) throws MessagingException {
+ return copy(exchange.getMessage("in"));
+ }
+
+ public static NormalizedMessage copyOut(MessageExchange exchange) throws MessagingException {
+ return copy(exchange.getMessage("out"));
+ }
+
+ public static Fault copyFault(MessageExchange exchange) throws MessagingException {
+ return (Fault) copy(exchange.getMessage("fault"));
+ }
+
+ public static void transferInToIn(MessageExchange source, MessageExchange dest) throws MessagingException {
+ transferToIn(source.getMessage("in"), dest);
+ }
+
+ public static void transferOutToIn(MessageExchange source, MessageExchange dest) throws MessagingException {
+ transferToIn(source.getMessage("out"), dest);
+ }
+
+ public static void transferToIn(NormalizedMessage sourceMsg, MessageExchange dest) throws MessagingException {
+ transferTo(sourceMsg, dest, "in");
+ }
+
+ public static void transferOutToOut(MessageExchange source, MessageExchange dest) throws MessagingException {
+ transferToOut(source.getMessage("out"), dest);
+ }
+
+ public static void transferInToOut(MessageExchange source, MessageExchange dest) throws MessagingException {
+ transferToOut(source.getMessage("in"), dest);
+ }
+
+ public static void transferToOut(NormalizedMessage sourceMsg, MessageExchange dest) throws MessagingException {
+ transferTo(sourceMsg, dest, "out");
+ }
+
+ public static void transferFaultToFault(MessageExchange source, MessageExchange dest) throws MessagingException {
+ transferToFault(source.getFault(), dest);
+ }
+
+ public static void transferToFault(Fault fault, MessageExchange dest) throws MessagingException {
+ transferTo(fault, dest, "fault");
+ }
+
+ public static void transferTo(NormalizedMessage sourceMsg, MessageExchange dest, String name) throws MessagingException {
+ NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage();
+ transfer(sourceMsg, destMsg);
+ dest.setMessage(destMsg, name);
+ }
+
+ public static void transferTo(MessageExchange source, MessageExchange dest, String name) throws MessagingException {
+ NormalizedMessage sourceMsg = source.getMessage(name);
+ NormalizedMessage destMsg = (sourceMsg instanceof Fault) ? dest.createFault() : dest.createMessage();
+ transfer(sourceMsg, destMsg);
+ dest.setMessage(destMsg, name);
+ }
+
+ /**
+ * Convert the given {@link NormalizedMessage} instance's content to a re-readable {@link Source} This allows the
+ * content to be read more than once (e.g. for XPath evaluation or auditing).
+ *
+ * @param message
+ * the {@link NormalizedMessage} to convert the content for
+ * @throws MessagingException
+ */
+ public static void enableContentRereadability(NormalizedMessage message) throws MessagingException {
+ if (message.getContent() instanceof StreamSource) {
+ try {
+ String content = new SourceTransformer().contentToString(message);
+ if (content != null) {
+ message.setContent(new StringSource(content));
+ }
+ } catch (TransformerException e) {
+ throw new MessagingException("Unable to convert message content into StringSource", e);
+ } catch (ParserConfigurationException e) {
+ throw new MessagingException("Unable to convert message content into StringSource", e);
+ } catch (IOException e) {
+ throw new MessagingException("Unable to convert message content into StringSource", e);
+ } catch (SAXException e) {
+ throw new MessagingException("Unable to convert message content into StringSource", e);
+ }
+ }
+ }
+
+ public static class NormalizedMessageImpl implements NormalizedMessage, Serializable {
+
+ private static final long serialVersionUID = -5813947566001096708L;
+
+ private Subject subject;
+ private Source content;
+ private Map properties = new HashMap();
+ private Map attachments = new HashMap();
+
+ public NormalizedMessageImpl() {
+ }
+
+ public NormalizedMessageImpl(NormalizedMessage message) throws MessagingException {
+ try {
+ String str = new SourceTransformer().contentToString(message);
+ if (str != null) {
+ this.content = new StringSource(str);
+ }
+ for (Iterator it = message.getPropertyNames().iterator(); it.hasNext();) {
+ String name = (String) it.next();
+ this.properties.put(name, message.getProperty(name));
+ }
+ for (Iterator it = message.getAttachmentNames().iterator(); it.hasNext();) {
+ String name = (String) it.next();
+ DataHandler dh = message.getAttachment(name);
+ DataSource ds = dh.getDataSource();
+ if (!(ds instanceof ByteArrayDataSource)) {
+ ByteArrayOutputStream baos = new ByteArrayOutputStream();
+ FileUtil.copyInputStream(ds.getInputStream(), baos);
+ ByteArrayDataSource bads = new ByteArrayDataSource(baos.toByteArray(), ds.getContentType());
+ bads.setName(ds.getName());
+ dh = new DataHandler(bads);
+ }
+ this.attachments.put(name, dh);
+ }
+ this.subject = message.getSecuritySubject();
+ } catch (MessagingException e) {
+ throw e;
+ } catch (Exception e) {
+ throw new MessagingException(e);
+ }
+ }
+
+ public void addAttachment(String id, DataHandler data) throws MessagingException {
+ this.attachments.put(id, data);
+ }
+
+ public Source getContent() {
+ return content;
+ }
+
+ public DataHandler getAttachment(String id) {
+ return (DataHandler) this.attachments.get(id);
+ }
+
+ public Set getAttachmentNames() {
+ return this.attachments.keySet();
+ }
+
+ public void removeAttachment(String id) throws MessagingException {
+ this.attachments.remove(id);
+ }
+
+ public void setContent(Source content) throws MessagingException {
+ this.content = content;
+ }
+
+ public void setProperty(String name, Object value) {
+ this.properties.put(name, value);
+ }
+
+ public void setSecuritySubject(Subject sub) {
+ this.subject = sub;
+ }
+
+ public Set getPropertyNames() {
+ return this.properties.keySet();
+ }
+
+ public Object getProperty(String name) {
+ return this.properties.get(name);
+ }
+
+ public Subject getSecuritySubject() {
+ return this.subject;
+ }
+
+ }
+
+ public static class FaultImpl extends NormalizedMessageImpl implements Fault {
+ private static final long serialVersionUID = -6076815664102825860L;
+
+ public FaultImpl() {
+ }
+
+ public FaultImpl(Fault fault) throws MessagingException {
+ super(fault);
+ }
+ }
+
+}
Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/CopyTransformer.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/CopyTransformer.java?rev=690206&r1=690205&r2=690206&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/CopyTransformer.java (original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/CopyTransformer.java Fri Aug 29 05:30:19 2008
@@ -31,6 +31,7 @@
import org.xml.sax.SAXException;
+import org.apache.servicemix.jbi.helper.MessageUtil;
import org.apache.servicemix.jbi.jaxp.BytesSource;
import org.apache.servicemix.jbi.jaxp.ResourceSource;
import org.apache.servicemix.jbi.jaxp.SourceTransformer;
@@ -48,11 +49,25 @@
private SourceTransformer sourceTransformer = new SourceTransformer();
- private boolean copyProperties = true;
+ private boolean copyProperties;
- private boolean copyAttachments = true;
+ private boolean copyAttachments;
- private boolean copySecuritySubject = true;
+ private boolean copySecuritySubject;
+
+ private boolean copyContent;
+
+ public CopyTransformer() {
+ this(true, true, true, true);
+ }
+
+ public CopyTransformer(boolean copySecuritySubject, boolean copyContent, boolean copyProperties, boolean copyAttachments) {
+ super();
+ this.copySecuritySubject = copySecuritySubject;
+ this.copyContent = copyContent;
+ this.copyProperties = copyProperties;
+ this.copyAttachments = copyAttachments;
+ }
/**
* @return the copyAttachments
@@ -111,6 +126,22 @@
copyProperties(from, to);
}
+ if (copyContent) {
+ copyContent(from, to);
+ }
+
+ if (copyAttachments) {
+ copyAttachments(from, to);
+ }
+
+ if (copySecuritySubject) {
+ copySecuritySubject(from, to);
+ }
+
+ return true;
+ }
+
+ private void copyContent(NormalizedMessage from, NormalizedMessage to) throws MessagingException {
Source content = from.getContent();
if ((content instanceof StreamSource || content instanceof SAXSource)
&& !(content instanceof StringSource)
@@ -130,16 +161,13 @@
}
}
to.setContent(content);
-
- if (copyAttachments) {
- copyAttachments(from, to);
- }
-
- if (copySecuritySubject) {
- copySecuritySubject(from, to);
- }
-
- return true;
+ }
+
+ public NormalizedMessage transform(MessageExchange exchange, NormalizedMessage in)
+ throws MessagingException {
+ NormalizedMessage out = new MessageUtil.NormalizedMessageImpl();
+ transform(exchange, in, out);
+ return out;
}
/**
Modified: servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/MessageTransformer.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/MessageTransformer.java?rev=690206&r1=690205&r2=690206&view=diff
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/MessageTransformer.java (original)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/jbi/transformer/MessageTransformer.java Fri Aug 29 05:30:19 2008
@@ -34,4 +34,14 @@
* @param out an empty out message ready to contain the result of the transformation
*/
boolean transform(MessageExchange exchange, NormalizedMessage in, NormalizedMessage out) throws MessagingException;
+
+ /**
+ * Transforms an input message into the returned output message
+ *
+ * @param exchange the exchange fon which the messages are flowing
+ * @param in the input message
+ * @return the output message or <code>null</code> if the output message co
+ * @throws MessagingException when an error occurs while transforming the message
+ */
+ NormalizedMessage transform(MessageExchange exchange, NormalizedMessage in) throws MessagingException;
}
Added: servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/FileUtil.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/FileUtil.java?rev=690206&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/FileUtil.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/FileUtil.java Fri Aug 29 05:30:19 2008
@@ -0,0 +1,317 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.util;
+
+import java.io.BufferedInputStream;
+import java.io.BufferedOutputStream;
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.net.URL;
+import java.util.Enumeration;
+import java.util.zip.ZipEntry;
+import java.util.zip.ZipFile;
+import java.util.zip.ZipOutputStream;
+
+/**
+ * File utilities
+ *
+ * @version $Revision: 658853 $
+ */
+public final class FileUtil {
+
+ /**
+ * Buffer size used when copying the content of an input stream to
+ * an output stream.
+ */
+ private static final int DEFAULT_BUFFER_SIZE = 4096;
+
+ private FileUtil() {
+ }
+
+ /**
+ * Move a File
+ *
+ * @param src
+ * @param targetDirectory
+ * @throws IOException
+ */
+ public static void moveFile(File src, File targetDirectory) throws IOException {
+ if (!src.renameTo(new File(targetDirectory, src.getName()))) {
+ throw new IOException("Failed to move " + src + " to " + targetDirectory);
+ }
+ }
+
+ /**
+ * Build a path- but do not create it
+ *
+ * @param parent
+ * @param subDirectory
+ * @return a File representing the path
+ */
+ public static File getDirectoryPath(File parent, String subDirectory) {
+ File result = null;
+ if (parent != null) {
+ result = new File(parent, subDirectory);
+ }
+ return result;
+ }
+
+ /**
+ * Build a directory path - creating directories if neccesary
+ *
+ * @param file
+ * @return true if the directory exists, or making it was successful
+ */
+ public static boolean buildDirectory(File file) {
+ return file.exists() || file.mkdirs();
+ }
+
+ /**
+ * Count files in a directory (including files in all subdirectories)
+ * @param directory the directory to start in
+ * @return the total number of files
+ */
+ public static int countFilesInDirectory(File directory) {
+ int count = 0;
+ for (File file : directory.listFiles()) {
+ if (file.isFile()) {
+ count++;
+ }
+ if (file.isDirectory()) {
+ count += countFilesInDirectory(file);
+ }
+ }
+ return count;
+ }
+
+ /**
+ * Copy in stream to an out stream
+ *
+ * @param in
+ * @param out
+ * @throws IOException
+ */
+ public static void copyInputStream(InputStream in, OutputStream out) throws IOException {
+ byte[] buffer = new byte[DEFAULT_BUFFER_SIZE];
+ int len = in.read(buffer);
+ while (len >= 0) {
+ out.write(buffer, 0, len);
+ len = in.read(buffer);
+ }
+ in.close();
+ out.close();
+ }
+
+ /**
+ * Unpack a zip file
+ *
+ * @param theFile
+ * @param targetDir
+ * @return the file
+ * @throws IOException
+ */
+ public static File unpackArchive(File theFile, File targetDir) throws IOException {
+ if (!theFile.exists()) {
+ throw new IOException(theFile.getAbsolutePath() + " does not exist");
+ }
+ if (!buildDirectory(targetDir)) {
+ throw new IOException("Could not create directory: " + targetDir);
+ }
+ ZipFile zipFile;
+ zipFile = new ZipFile(theFile);
+ for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
+ ZipEntry entry = (ZipEntry) entries.nextElement();
+ File file = new File(targetDir, File.separator + entry.getName());
+ // Take the sledgehammer approach to creating directories
+ // to work around ZIP's that incorrectly miss directories
+ if (!buildDirectory(file.getParentFile())) {
+ throw new IOException("Could not create directory: " + file.getParentFile());
+ }
+ if (!entry.isDirectory()) {
+ copyInputStream(zipFile.getInputStream(entry), new BufferedOutputStream(new FileOutputStream(file)));
+ } else {
+ if (!buildDirectory(file)) {
+ throw new IOException("Could not create directory: " + file);
+ }
+ }
+ }
+ zipFile.close();
+ return theFile;
+ }
+
+ /**
+ * Unpack an archive from a URL
+ *
+ * @param url
+ * @param targetDir
+ * @return the file to the url
+ * @throws IOException
+ */
+ public static File unpackArchive(URL url, File targetDir) throws IOException {
+ if (!targetDir.exists()) {
+ targetDir.mkdirs();
+ }
+ InputStream in = new BufferedInputStream(url.openStream(), DEFAULT_BUFFER_SIZE);
+ // make sure we get the actual file
+ File zip = File.createTempFile("arc", ".zip", targetDir);
+ OutputStream out = new BufferedOutputStream(new FileOutputStream(zip));
+ copyInputStream(in, out);
+ out.close();
+ return unpackArchive(zip, targetDir);
+ }
+
+ /**
+ * Validate that an archive contains a named entry
+ *
+ * @param theFile
+ * @param name
+ * @return true if the entry exists
+ * @throws IOException
+ */
+ public static boolean archiveContainsEntry(File theFile, String name) throws IOException {
+ boolean result = false;
+ ZipFile zipFile;
+ zipFile = new ZipFile(theFile);
+ for (Enumeration entries = zipFile.entries(); entries.hasMoreElements();) {
+ ZipEntry entry = (ZipEntry) entries.nextElement();
+ if (entry.getName().equals(name)) {
+ result = true;
+ break;
+ }
+ }
+ zipFile.close();
+ return result;
+ }
+
+ /**
+ * Create a unique directory within a directory 'root'
+ *
+ * @param rootDir
+ * @param seed
+ * @return unique directory
+ * @throws IOException
+ */
+ public static synchronized File createUniqueDirectory(File rootDir, String seed) throws IOException {
+ int index = seed.lastIndexOf('.');
+ if (index > 0) {
+ seed = seed.substring(0, index);
+ }
+ File result = null;
+ int count = 0;
+ while (result == null) {
+ String name = seed + "." + count + ".tmp";
+ File file = new File(rootDir, name);
+ if (!file.exists()) {
+ file.mkdirs();
+ result = file;
+ }
+ count++;
+ }
+ return result;
+ }
+
+ /**
+ * Delete a file
+ *
+ * @param fileToDelete
+ * @return true if the File is deleted
+ */
+ public static boolean deleteFile(File fileToDelete) {
+ if (fileToDelete == null || !fileToDelete.exists()) {
+ return true;
+ }
+ boolean result = true;
+ if (fileToDelete.isDirectory()) {
+ File[] files = fileToDelete.listFiles();
+ if (files == null) {
+ result = false;
+ } else {
+ for (int i = 0; i < files.length; i++) {
+ File file = files[i];
+ if (file.getName().equals(".") || file.getName().equals("..")) {
+ continue;
+ }
+ if (file.isDirectory()) {
+ result &= deleteFile(file);
+ } else {
+ result &= file.delete();
+ }
+ }
+ }
+ }
+ result &= fileToDelete.delete();
+ return result;
+ }
+
+ /**
+ * Zip up a directory
+ *
+ * @param directory
+ * @param zipName
+ * @throws IOException
+ */
+ public static void zipDir(String directory, String zipName) throws IOException {
+ // create a ZipOutputStream to zip the data to
+ ZipOutputStream zos = new ZipOutputStream(new FileOutputStream(zipName));
+ String path = "";
+ zipDir(directory, zos, path);
+ // close the stream
+ zos.close();
+ }
+
+ /**
+ * Zip up a directory path
+ * @param directory
+ * @param zos
+ * @param path
+ * @throws IOException
+ */
+ public static void zipDir(String directory, ZipOutputStream zos, String path) throws IOException {
+ File zipDir = new File(directory);
+ // get a listing of the directory content
+ String[] dirList = zipDir.list();
+ byte[] readBuffer = new byte[2156];
+ int bytesIn = 0;
+ // loop through dirList, and zip the files
+ for (int i = 0; i < dirList.length; i++) {
+ File f = new File(zipDir, dirList[i]);
+ if (f.isDirectory()) {
+ String filePath = f.getPath();
+ zipDir(filePath, zos, path + f.getName() + "/");
+ continue;
+ }
+ FileInputStream fis = new FileInputStream(f);
+ try {
+ ZipEntry anEntry = new ZipEntry(path + f.getName());
+ zos.putNextEntry(anEntry);
+ bytesIn = fis.read(readBuffer);
+ while (bytesIn != -1) {
+ zos.write(readBuffer, 0, bytesIn);
+ bytesIn = fis.read(readBuffer);
+ }
+ } finally {
+ fis.close();
+ }
+ }
+ }
+
+
+}
\ No newline at end of file
Added: servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/jaf/ByteArrayDataSource.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/jaf/ByteArrayDataSource.java?rev=690206&view=auto
==============================================================================
--- servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/jaf/ByteArrayDataSource.java (added)
+++ servicemix/utils/trunk/src/main/java/org/apache/servicemix/util/jaf/ByteArrayDataSource.java Fri Aug 29 05:30:19 2008
@@ -0,0 +1,68 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.util.jaf;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Serializable;
+
+import javax.activation.DataSource;
+
+/**
+ * Byte array DataSource for Mail and message attachments
+ *
+ * @author George Gastaldi
+ * @since 3.0
+ */
+public class ByteArrayDataSource implements DataSource, Serializable {
+
+ private static final long serialVersionUID = 1L;
+
+ private byte[] data;
+ private String contentType;
+ private String name = "unused";
+
+ public ByteArrayDataSource(byte[] data, String contentType) {
+ this.data = data;
+ this.contentType = contentType;
+ }
+
+ public InputStream getInputStream() throws IOException {
+ if (data == null) {
+ throw new IOException("no data");
+ }
+ return new ByteArrayInputStream(data);
+ }
+
+ public OutputStream getOutputStream() throws IOException {
+ throw new IOException("getOutputStream() not supported");
+ }
+
+ public String getContentType() {
+ return contentType;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+}
\ No newline at end of file
Added: servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/CopyTransformerTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/CopyTransformerTest.java?rev=690206&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/CopyTransformerTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/CopyTransformerTest.java Fri Aug 29 05:30:19 2008
@@ -0,0 +1,62 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.transformer;
+
+import java.io.Reader;
+import java.io.StringReader;
+
+import javax.jbi.messaging.NormalizedMessage;
+import javax.xml.transform.Source;
+import javax.xml.transform.sax.SAXSource;
+import javax.xml.transform.stream.StreamSource;
+
+import org.xml.sax.InputSource;
+
+import junit.framework.TestCase;
+
+import org.apache.servicemix.jbi.helper.MessageUtil;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+
+public class CopyTransformerTest extends TestCase {
+
+ private CopyTransformer transformer = CopyTransformer.getInstance();
+
+ public void testWithSAXSource() throws Exception {
+ Reader r = new StringReader("<hello>world</hello>");
+ Source src = new SAXSource(new InputSource(r));
+ NormalizedMessage msg = copyMessage(src);
+ r.close();
+ new SourceTransformer().contentToString(msg);
+ }
+
+ public void testWithStreamSource() throws Exception {
+ Reader r = new StringReader("<hello>world</hello>");
+ Source src = new StreamSource(r);
+ NormalizedMessage msg = copyMessage(src);
+ r.close();
+ new SourceTransformer().contentToString(msg);
+ }
+
+ protected NormalizedMessage copyMessage(Source src) throws Exception {
+ NormalizedMessage from = new MessageUtil.NormalizedMessageImpl();
+ NormalizedMessage to = new MessageUtil.NormalizedMessageImpl();
+ from.setContent(src);
+ transformer.transform(null, from, to);
+ return to;
+ }
+
+}
Added: servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/MessageCopierTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/MessageCopierTest.java?rev=690206&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/MessageCopierTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/jbi/transformer/MessageCopierTest.java Fri Aug 29 05:30:19 2008
@@ -0,0 +1,95 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.jbi.transformer;
+
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.jbi.messaging.NormalizedMessage;
+import javax.security.auth.Subject;
+
+import junit.framework.TestCase;
+
+import org.apache.commons.io.IOUtils;
+import org.apache.servicemix.jbi.helper.MessageUtil;
+import org.apache.servicemix.jbi.jaxp.SourceTransformer;
+import org.apache.servicemix.jbi.jaxp.StringSource;
+import org.apache.servicemix.util.jaf.ByteArrayDataSource;
+
+/**
+ * Test case to ensure {@link CopyTransformer} can perform all functionality previously in {{MessageCopier}}
+ */
+public class MessageCopierTest extends TestCase {
+
+ private NormalizedMessage message;
+
+ private Subject subject;
+
+ public void setUp() throws Exception {
+ subject = new Subject();
+ message = new MessageUtil.NormalizedMessageImpl();
+ message.setContent(new StringSource("<doc>s1<doc>"));
+ message.addAttachment("a", new DataHandler(createDataSource("s2")));
+ message.setProperty("p", "s3");
+ message.setSecuritySubject(subject);
+ }
+
+ public void tearDown() throws Exception {
+ }
+
+ public void testCopyContent() throws Exception {
+ MessageTransformer copier = new CopyTransformer(false, true, false, false);
+ NormalizedMessage copy = copier.transform(null, message);
+ String content = new SourceTransformer().toString(copy.getContent());
+ assertEquals("wrong content", "<doc>s1<doc>", content);
+ assertEquals("wrong attachment", null, copy.getAttachment("a"));
+ assertEquals("wrong property", null, copy.getProperty("p"));
+ assertEquals("wrong subject", null, copy.getSecuritySubject());
+ }
+
+ public void testCopyAttachment() throws Exception {
+ MessageTransformer copier = new CopyTransformer(false, false, false, true);
+ NormalizedMessage copy = copier.transform(null, message);
+ String attachment = IOUtils.toString(copy.getAttachment("a").getInputStream());
+ assertEquals("wrong content", null, copy.getContent());
+ assertEquals("wrong attachment", "s2", attachment);
+ assertEquals("wrong property", null, copy.getProperty("p"));
+ assertEquals("wrong subject", null, copy.getSecuritySubject());
+ }
+
+ public void testCopyProperties() throws Exception {
+ MessageTransformer copier = new CopyTransformer(false, false, true, false);
+ NormalizedMessage copy = copier.transform(null, message);
+ assertEquals("wrong content", null, copy.getContent());
+ assertEquals("wrong attachment", null, copy.getAttachment("a"));
+ assertEquals("wrong property", "s3", copy.getProperty("p"));
+ assertEquals("wrong subject", null, copy.getSecuritySubject());
+ }
+
+ public void testCopySubject() throws Exception {
+ MessageTransformer copier = new CopyTransformer(true, false, false, false);
+ NormalizedMessage copy = copier.transform(null, message);
+ assertEquals("wrong content", null, copy.getContent());
+ assertEquals("wrong attachment", null, copy.getAttachment("a"));
+ assertEquals("wrong property", null, copy.getProperty("p"));
+ assertEquals("wrong subject", subject, copy.getSecuritySubject());
+ }
+
+ private static DataSource createDataSource(String text) {
+ return new ByteArrayDataSource(text.getBytes(), "text/plain");
+ }
+
+}
Added: servicemix/utils/trunk/src/test/java/org/apache/servicemix/util/FileUtilTest.java
URL: http://svn.apache.org/viewvc/servicemix/utils/trunk/src/test/java/org/apache/servicemix/util/FileUtilTest.java?rev=690206&view=auto
==============================================================================
--- servicemix/utils/trunk/src/test/java/org/apache/servicemix/util/FileUtilTest.java (added)
+++ servicemix/utils/trunk/src/test/java/org/apache/servicemix/util/FileUtilTest.java Fri Aug 29 05:30:19 2008
@@ -0,0 +1,86 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements. See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.servicemix.util;
+
+import java.io.File;
+
+import junit.framework.TestCase;
+
+public class FileUtilTest extends TestCase {
+
+ private static final File WORKDIR = new File("target/servicemix-test");
+
+ protected void setUp() throws Exception {
+ FileUtil.deleteFile(WORKDIR);
+ WORKDIR.mkdirs();
+ }
+
+ public void testDeleteFile() throws Exception {
+ File f = new File(WORKDIR, "test.txt");
+ assertFalse(f.exists());
+ assertTrue(f.createNewFile());
+ assertTrue(f.exists());
+ assertFalse(f.isDirectory());
+ assertTrue(f.isFile());
+ assertTrue(FileUtil.deleteFile(f));
+ assertFalse(f.exists());
+ }
+
+ /*
+ * This test only works on windows, as writing to a file does not prevent
+ * its deletion on unix systems.
+ *
+ * public void testDeleteLockedFile() throws Exception { File f = new
+ * File(WORKDIR, "test.txt"); assertFalse(f.exists()); OutputStream os = new
+ * FileOutputStream(f); try { Writer w = new OutputStreamWriter(os);
+ * w.write("hello"); w.flush(); assertTrue(f.exists());
+ * assertFalse(FileUtil.deleteFile(f)); assertTrue(f.exists()); } finally {
+ * os.close(); } assertTrue(f.exists()); assertTrue(FileUtil.deleteFile(f));
+ * assertFalse(f.exists()); }
+ */
+
+ public void testDeleteDir() throws Exception {
+ File f = new File(WORKDIR, "testdir");
+ assertFalse(f.exists());
+ assertTrue(f.mkdir());
+ assertTrue(f.exists());
+ assertTrue(f.isDirectory());
+ assertFalse(f.isFile());
+ assertTrue(FileUtil.deleteFile(f));
+ assertFalse(f.exists());
+ }
+
+ /*
+ * This test only works on windows, as writing to a file does not prevent
+ * its deletion on unix systems.
+ *
+ * public void testDeleteDirWithLockedFile() throws Exception { File f = new
+ * File(WORKDIR, "testdir"); assertFalse(f.exists()); assertTrue(f.mkdir());
+ * assertTrue(f.exists()); assertTrue(f.isDirectory());
+ * assertFalse(f.isFile()); File f2 = new File(f, "test.txt");
+ * assertFalse(f2.exists()); File f3 = new File(f, "test2.txt");
+ * assertFalse(f3.exists()); assertTrue(f3.createNewFile());
+ * assertTrue(f3.exists()); OutputStream os = new FileOutputStream(f2); try {
+ * Writer w = new OutputStreamWriter(os); w.write("hello"); w.flush();
+ * assertTrue(f2.exists()); assertFalse(FileUtil.deleteFile(f));
+ * assertTrue(f.exists()); assertTrue(f2.exists()); } finally { os.close(); }
+ * assertFalse(f3.exists()); assertTrue(f2.exists());
+ * assertTrue(f.exists()); assertTrue(FileUtil.deleteFile(f));
+ * assertFalse(f.exists()); }
+ */
+
+}