Author: dkulp Date: Fri May 25 21:01:15 2007 New Revision: 541856 URL: http://svn.apache.org/viewvc?view=rev&rev=541856 Log: Fix more SWA issues Fix issue with Cached output stream that wasn't flushing the file before creating a reader for it.
Modified: incubator/cxf/trunk/api/src/main/java/org/apache/cxf/io/AbstractCachedOutputStream.java incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/Messages.properties incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java Modified: incubator/cxf/trunk/api/src/main/java/org/apache/cxf/io/AbstractCachedOutputStream.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/api/src/main/java/org/apache/cxf/io/AbstractCachedOutputStream.java?view=diff&rev=541856&r1=541855&r2=541856 ============================================================================== --- incubator/cxf/trunk/api/src/main/java/org/apache/cxf/io/AbstractCachedOutputStream.java (original) +++ incubator/cxf/trunk/api/src/main/java/org/apache/cxf/io/AbstractCachedOutputStream.java Fri May 25 21:01:15 2007 @@ -253,6 +253,7 @@ } public InputStream getInputStream() throws IOException { + flush(); if (inmem) { if (currentStream instanceof ByteArrayOutputStream) { return new ByteArrayInputStream(((ByteArrayOutputStream) currentStream).toByteArray()); Modified: incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java?view=diff&rev=541856&r1=541855&r2=541856 ============================================================================== --- incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java (original) +++ incubator/cxf/trunk/common/common/src/main/java/org/apache/cxf/helpers/IOUtils.java Fri May 25 21:01:15 2007 @@ -22,6 +22,8 @@ import java.io.IOException; import java.io.InputStream; import java.io.OutputStream; +import java.io.Reader; +import java.io.Writer; public final class IOUtils { @@ -31,30 +33,66 @@ } - public static void copy(final InputStream input, final OutputStream output) + public static int copy(final InputStream input, final OutputStream output) throws IOException { - copy(input, output, DEFAULT_BUFFER_SIZE); + return copy(input, output, DEFAULT_BUFFER_SIZE); } - public static void copy(final InputStream input, + public static int copy(final InputStream input, final OutputStream output, - final int bufferSize) + int bufferSize) throws IOException { + int avail = input.available(); + if (avail > 262144) { + avail = 262144; + } + if (avail > bufferSize) { + bufferSize = avail; + } final byte[] buffer = new byte[bufferSize]; int n = 0; n = input.read(buffer); + int total = 0; + while (-1 != n) { + output.write(buffer, 0, n); + total += n; + n = input.read(buffer); + } + return total; + } + public static void copy(final Reader input, + final Writer output, + final int bufferSize) + throws IOException { + final char[] buffer = new char[bufferSize]; + int n = 0; + n = input.read(buffer); while (-1 != n) { output.write(buffer, 0, n); n = input.read(buffer); } } + public static String toString(final InputStream input) throws IOException { StringBuffer buf = new StringBuffer(); final byte[] buffer = new byte[DEFAULT_BUFFER_SIZE]; + int n = 0; + n = input.read(buffer); + while (-1 != n) { + buf.append(new String(buffer, 0, n)); + n = input.read(buffer); + } + return buf.toString(); + } + public static String toString(final Reader input) + throws IOException { + + StringBuffer buf = new StringBuffer(); + final char[] buffer = new char[DEFAULT_BUFFER_SIZE]; int n = 0; n = input.read(buffer); while (-1 != n) { Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java?view=diff&rev=541856&r1=541855&r2=541856 ============================================================================== --- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java (original) +++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentDeserializer.java Fri May 25 21:01:15 2007 @@ -26,6 +26,8 @@ import java.io.PushbackInputStream; import java.net.URLDecoder; import java.util.Enumeration; +import java.util.HashSet; +import java.util.Set; import java.util.regex.Matcher; import java.util.regex.Pattern; @@ -67,6 +69,8 @@ private Message message; private InputStream body; + + private Set<DelegatingInputStream> loaded = new HashSet<DelegatingInputStream>(); public AttachmentDeserializer(Message message) { this.message = message; @@ -91,12 +95,9 @@ } if (contentType.toLowerCase().indexOf("multipart/related") != -1) { - // First try to find the boundary from InputStream - boundary = findBoundaryFromInputStream(); - // If a boundary wasn't found, try the ContentType - if (null == boundary) { - - boundary = findBoundaryFromContentType(contentType); + boundary = findBoundaryFromContentType(contentType); + if (null == boundary) { + boundary = findBoundaryFromInputStream(); } // If a boundary still wasn't found, throw an exception if (null == boundary) { @@ -104,7 +105,7 @@ } stream = new PushbackInputStream(message.getContent(InputStream.class), - boundary.getBytes().length); + boundary.getBytes().length * 2); if (!readTillFirstBoundary(stream, boundary.getBytes())) { throw new IOException("Couldn't find MIME boundary: " + boundary); } @@ -194,6 +195,10 @@ } private void cache(DelegatingInputStream input, boolean deleteOnClose) throws IOException { + if (loaded.contains(input)) { + return; + } + loaded.add(input); CachedOutputStream out = new CachedOutputStream(); IOUtils.copy(input, out); input.setInputStream(out.getInputStream()); Modified: incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java?view=diff&rev=541856&r1=541855&r2=541856 ============================================================================== --- incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java (original) +++ incubator/cxf/trunk/rt/core/src/main/java/org/apache/cxf/attachment/AttachmentSerializer.java Fri May 25 21:01:15 2007 @@ -21,7 +21,8 @@ import java.io.IOException; import java.io.OutputStream; -import java.io.OutputStreamWriter; +import java.io.StringWriter; +import java.io.Writer; import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.message.Attachment; @@ -33,8 +34,8 @@ private static final String BODY_ATTACHMENT_ID = "[EMAIL PROTECTED]"; private Message message; private String bodyBoundary; - private OutputStreamWriter writer; private OutputStream out; + private String encoding; private boolean xop = true; public AttachmentSerializer(Message messageParam) { @@ -78,11 +79,11 @@ // 2. write headers out = message.getContent(OutputStream.class); - String encoding = (String) message.get(Message.ENCODING); + encoding = (String) message.get(Message.ENCODING); if (encoding == null) { encoding = "UTF-8"; } - writer = new OutputStreamWriter(out, encoding); + StringWriter writer = new StringWriter(); writer.write("\r\n"); writer.write("--"); writer.write(bodyBoundary); @@ -96,10 +97,11 @@ .append(enc) .append("\""); - writeHeaders(mimeBodyCt.toString(), BODY_ATTACHMENT_ID); + writeHeaders(mimeBodyCt.toString(), BODY_ATTACHMENT_ID, writer); + out.write(writer.getBuffer().toString().getBytes(encoding)); } - private void writeHeaders(String contentType, String attachmentId) throws IOException { + private void writeHeaders(String contentType, String attachmentId, Writer writer) throws IOException { writer.write("\r\n"); writer.write("Content-Type: "); writer.write(contentType); @@ -110,7 +112,6 @@ writer.write("Content-ID: <"); writer.write(attachmentId); writer.write(">\r\n\r\n"); - writer.flush(); } /** @@ -120,22 +121,23 @@ public void writeAttachments() throws IOException { if (message.getAttachments() != null) { for (Attachment a : message.getAttachments()) { - + StringWriter writer = new StringWriter(); writer.write("\r\n"); writer.write("--"); writer.write(bodyBoundary); - - writeHeaders(a.getDataHandler().getContentType(), a.getId()); + writeHeaders(a.getDataHandler().getContentType(), a.getId(), writer); + out.write(writer.getBuffer().toString().getBytes(encoding)); IOUtils.copy(a.getDataHandler().getInputStream(), out); } } + StringWriter writer = new StringWriter(); writer.write("\r\n"); writer.write("--"); writer.write(bodyBoundary); writer.write("--"); - - writer.flush(); + out.write(writer.getBuffer().toString().getBytes(encoding)); + out.flush(); } public boolean isXop() { Modified: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/Messages.properties URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/Messages.properties?view=diff&rev=541856&r1=541855&r2=541856 ============================================================================== --- incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/Messages.properties (original) +++ incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/Messages.properties Fri May 25 21:01:15 2007 @@ -23,4 +23,4 @@ COULD_NOT_INVOKE = Could not invoke getFaultInfo method on Exception. DISPATCH_OBJECT_CANNOT_BE_NULL = Null object passed into Dispatch marshalling EXCEPTION_WRITING_OBJECT = Exception occured while marshalling Dispatch object to stream -ATACCHMENT_NOT_SUPPORTED = Attachments of type {0} are not supported. \ No newline at end of file +ATTACHMENT_NOT_SUPPORTED = Attachments of type {0} are not supported. \ No newline at end of file Modified: incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java URL: http://svn.apache.org/viewvc/incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java?view=diff&rev=541856&r1=541855&r2=541856 ============================================================================== --- incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java (original) +++ incubator/cxf/trunk/rt/frontend/jaxws/src/main/java/org/apache/cxf/jaxws/interceptors/SwAOutInterceptor.java Fri May 25 21:01:15 2007 @@ -24,6 +24,7 @@ import java.awt.image.BufferedImage; import java.io.ByteArrayOutputStream; import java.io.IOException; +import java.io.StringWriter; import java.util.ArrayList; import java.util.Collection; import java.util.Iterator; @@ -37,11 +38,11 @@ import javax.imageio.ImageWriter; import javax.mail.util.ByteArrayDataSource; import javax.xml.bind.JAXBContext; +import javax.xml.stream.XMLStreamException; +import javax.xml.stream.XMLStreamReader; +import javax.xml.stream.XMLStreamWriter; import javax.xml.transform.Source; -import javax.xml.transform.Transformer; -import javax.xml.transform.TransformerException; -import javax.xml.transform.TransformerFactory; -import javax.xml.transform.stream.StreamResult; +import javax.xml.transform.stream.StreamSource; import com.sun.xml.bind.v2.runtime.JAXBContextImpl; import com.sun.xml.bind.v2.util.DataSourceSource; @@ -50,8 +51,10 @@ import org.apache.cxf.binding.soap.SoapMessage; import org.apache.cxf.binding.soap.interceptor.AbstractSoapInterceptor; import org.apache.cxf.binding.soap.model.SoapBodyInfo; +import org.apache.cxf.common.logging.LogUtils; import org.apache.cxf.databinding.DataBinding; import org.apache.cxf.helpers.CastUtils; +import org.apache.cxf.helpers.IOUtils; import org.apache.cxf.interceptor.AttachmentOutInterceptor; import org.apache.cxf.interceptor.Fault; import org.apache.cxf.jaxb.JAXBDataBinding; @@ -62,9 +65,10 @@ import org.apache.cxf.service.model.BindingMessageInfo; import org.apache.cxf.service.model.BindingOperationInfo; import org.apache.cxf.service.model.MessagePartInfo; +import org.apache.cxf.staxutils.StaxUtils; public class SwAOutInterceptor extends AbstractSoapInterceptor { - private static final Logger LOG = Logger.getLogger(SwAOutInterceptor.class.getName()); + private static final Logger LOG = LogUtils.getL7dLogger(SwAOutInterceptor.class); public SwAOutInterceptor() { super(); @@ -114,7 +118,7 @@ int bodyParts = sbi.getParts().size(); for (MessagePartInfo mpi : sbi.getAttachments()) { String partName = mpi.getConcreteName().getLocalPart(); - final String ct = (String) mpi.getProperty(Message.CONTENT_TYPE); + String ct = (String) mpi.getProperty(Message.CONTENT_TYPE); String id = new StringBuilder().append(partName) .append("=") @@ -129,24 +133,8 @@ DataHandler dh = null; if (o instanceof Source) { - DataSource ds = null; - if (o instanceof DataSourceSource) { - ds = (DataSource) o; - } else { - TransformerFactory tf = TransformerFactory.newInstance(); - try { - Transformer transformer = tf.newTransformer(); - - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - transformer.transform((Source) o, new StreamResult(bos)); - ds = new ByteArrayDataSource(bos.toByteArray(), ct); - } catch (TransformerException e) { - throw new Fault(e); - } - } - - dh = new DataHandler(ds); + dh = new DataHandler(createDataSource((Source)o, ct)); } else if (o instanceof Image) { // TODO: make this streamable. This is one of my pet @@ -166,11 +154,15 @@ } catch (IOException e) { throw new Fault(e); } + } else { + throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", + LOG, ct)); } dh = new DataHandler(new ByteArrayDataSource(bos.toByteArray(), ct)); } else if (o instanceof DataHandler) { dh = (DataHandler) o; + ct = dh.getContentType(); } else { throw new Fault(new org.apache.cxf.common.i18n.Message("ATTACHMENT_NOT_SUPPORTED", LOG, o.getClass())); @@ -178,11 +170,47 @@ AttachmentImpl att = new AttachmentImpl(id); att.setDataHandler(dh); - att.setHeader("Content-Type", (String)mpi.getProperty(Message.CONTENT_TYPE)); + att.setHeader("Content-Type", ct); atts.add(att); } } + private DataSource createDataSource(Source o, String ct) { + DataSource ds = null; + + if (o instanceof DataSourceSource) { + ds = (DataSource) o; + } else if (o instanceof StreamSource) { + StreamSource src = (StreamSource)o; + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + try { + if (src.getInputStream() != null) { + IOUtils.copy(src.getInputStream(), bos, 1024); + ds = new ByteArrayDataSource(bos.toByteArray(), ct); + } else { + ds = new ByteArrayDataSource(IOUtils.toString(src.getReader()), + ct); + } + } catch (IOException e) { + throw new Fault(e); + } + } else { + XMLStreamReader reader = StaxUtils.createXMLStreamReader((Source)o); + StringWriter stringWriter = new StringWriter(); + XMLStreamWriter writer = StaxUtils.createXMLStreamWriter(stringWriter); + try { + StaxUtils.copy(reader, writer); + writer.flush(); + ds = new ByteArrayDataSource(stringWriter.toString(), ct); + } catch (XMLStreamException e1) { + throw new Fault(e1); + } catch (IOException e) { + throw new Fault(e); + } + } + return ds; + } + private BufferedImage convertToBufferedImage(Image image) throws IOException { if (image instanceof BufferedImage) { return (BufferedImage)image;