Author: nthaker
Date: Mon Dec 10 08:33:02 2007
New Revision: 602962

URL: http://svn.apache.org/viewvc?rev=602962&view=rev
Log:
Commiting changes for JIRA - WSCOMMONS-282

Added:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/DynamicPart.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnByteArray.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnFile.java
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOutputStream.java
Modified:
    
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java

Modified: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java?rev=602962&r1=602961&r2=602962&view=diff
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java
 (original)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/Attachments.java
 Mon Dec 10 08:33:02 2007
@@ -19,6 +19,7 @@
 
 package org.apache.axiom.attachments;
 
+import org.apache.axiom.attachments.part.DynamicPart;
 import org.apache.axiom.om.OMException;
 import org.apache.axiom.om.impl.MTOMConstants;
 import org.apache.axiom.om.util.UUIDGenerator;
@@ -553,41 +554,22 @@
         try {
             if (fileCacheEnable) {
                 try {
-                    // The soapPart is normally the first part, 
-                    // so always keep the first part in memory
-                    if ((contentLength != 0 && contentLength <= 
fileStorageThreshold) ||
-                         partIndex == 0) {  
-                        // Since the content-length is less than the 
threshold, we can safely 
-                        // store it in memory.
+                    //If Content-Length defined.
+                    if(contentLength!=0){
                         if (log.isDebugEnabled()) {
-                            log.debug("Creating an Attachment Part (in 
memory)");
+                            log.debug("Fixed Content-Length Data");
                         }
                         MIMEBodyPartInputStream partStream =
                             new MIMEBodyPartInputStream(pushbackInStream,
                                                         boundary,
                                                         this,
                                                         PUSHBACK_SIZE);
-                        part = new PartOnMemory(partStream);
-                    } else if (contentLength != 0 && 
-                            contentLength > fileStorageThreshold * 2) {  
-                        if (log.isDebugEnabled()) {
-                            log.debug("Creating an Attachment Part (in a 
temporary file in " + attachmentRepoDir + ")");
-                        }
-                        // The content-length is much bigger than the 
threshold, then always
-                        // store the attachments in a file.  This prevents 
unnecessary buffering.
-                        // REVIEW Arbitrary heuristic.  Does this need to be 
fine-tuned.
-                        MIMEBodyPartInputStream partStream =
-                            new MIMEBodyPartInputStream(pushbackInStream,
-                                                        boundary,
-                                                        this,
-                                                        PUSHBACK_SIZE);
-                        PushbackFilePartInputStream filePartStream =
-                            new PushbackFilePartInputStream(partStream, new 
byte[0]);
-                        part = new PartOnFile(filePartStream, 
attachmentRepoDir);
                         
+                        part = DynamicPart.createPart(contentLength, 
partIndex, partStream, attachmentRepoDir, fileStorageThreshold);
+                 
                     } else {
                         if (log.isDebugEnabled()) {
-                            log.debug("Buffering attachment part to determine 
if it should be stored in memory");
+                            log.debug("Chunked Data");
                         }
                         // Read chunks of data to determine the size
                         // of the attachment.  This can wasteful because we 
need to gc the buffers.
@@ -595,29 +577,14 @@
                         // this is seldom provided.
                         
                         MIMEBodyPartInputStream partStream;
-                        byte[] buffer = new byte[fileStorageThreshold];
+                 
                         partStream =
                             new MIMEBodyPartInputStream(pushbackInStream,
                                                         boundary,
                                                         this,
                                                         PUSHBACK_SIZE);
-                        int count = readToBuffer(partStream, buffer);
-                  
-                        if (count == fileStorageThreshold) {
-                            if (log.isDebugEnabled()) {
-                                log.debug("The calculated attachment size is " 
+ count + ". Storing Part in file.");
-                            }
-                            PushbackFilePartInputStream filePartStream =
-                                new PushbackFilePartInputStream(partStream, 
buffer);
-                            part = new PartOnFile(filePartStream, 
attachmentRepoDir);
-                        } else {
-                            if (log.isDebugEnabled()) {
-                                log.debug("The calculated attachment size is " 
+ count + ". Storing Part in memory.");
-                            }
-                            ByteArrayInputStream byteArrayInStream =
-                                new ByteArrayInputStream(buffer, 0, count);
-                            part = new PartOnMemory(byteArrayInStream);
-                        }
+                        part = DynamicPart.createPart(partStream, 
attachmentRepoDir, fileStorageThreshold);
+                        
                     } 
                 } catch (Exception e) {
                     throw new OMException("Error creating temporary File.", e);

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/DynamicPart.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/DynamicPart.java?rev=602962&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/DynamicPart.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/DynamicPart.java
 Mon Dec 10 08:33:02 2007
@@ -0,0 +1,413 @@
+/*
+ * 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.axiom.attachments.part;
+
+import org.apache.axiom.attachments.MIMEBodyPartInputStream;
+import org.apache.axiom.attachments.Part;
+import org.apache.axiom.attachments.PushbackFilePartInputStream;
+import org.apache.axiom.om.OMException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+
+import javax.activation.DataHandler;
+import javax.mail.Header;
+import javax.mail.MessagingException;
+import javax.mail.internet.HeaderTokenizer;
+
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Enumeration;
+import java.util.Hashtable;
+/*
+ * Dynamic Part represents attachment Part, Inbound Attachment Data can be
+ * be represend by a http strem of fixed length or as chunks. Dynamic Part
+ * Supports both type of attachment part, it stores part content as a byte 
+ * array or in a file based on the file threshold and attachment part size.  
+ */
+public abstract class DynamicPart implements Part {
+    protected static Log log = LogFactory.getLog(DynamicPart.class);
+    private static final int bufferSize = 8192;
+    Hashtable headers = new Hashtable();
+
+    /** Create Part for stream with chunked data.
+     * @param in
+     * @param repoDir
+     * @param fileStorageThreshold
+     * @return
+     */
+    public static DynamicPart createPart(MIMEBodyPartInputStream in, String 
repoDir, int fileStorageThreshold){
+        if(log.isDebugEnabled()){
+            log.debug("Start createPart()");
+        }
+        try{
+            if (log.isDebugEnabled()) {
+                log.debug("Buffering attachment part to determine if it should 
be stored in File");
+            }
+
+            byte[] buffer = toBuffer(in, fileStorageThreshold);             
+            int count = (buffer!=null)?buffer.length:0;  
+            if(log.isDebugEnabled()){
+                log.debug("Total Byte read ="+count+" FileThresholdValue = 
"+fileStorageThreshold);
+            }
+            if(count < fileStorageThreshold){
+                if(log.isDebugEnabled()){
+                    log.debug("Storing Part On Byte Array");
+                }
+                InputStream bis = new ByteArrayInputStream(buffer);
+                //Create PartOnByteArray
+                return new PartOnByteArray(bis);
+            }
+            else{
+                //Create PartOnFile
+                if(log.isDebugEnabled()){
+                    log.debug("Storing Part On File");
+                }
+                PushbackFilePartInputStream filePartStream =
+                    new PushbackFilePartInputStream(in, buffer);
+                return new PartOnFile(filePartStream, repoDir);
+            }
+
+        }catch (Exception e) {
+            if(log.isDebugEnabled()){
+                log.debug("Error in createPart(), throwing OMException");
+            }
+            throw new OMException("Error Creating DynamicPart", e);
+        }
+    }
+
+    /**
+     * Create Part for stream with fixed contentLength.
+     * @param contentLength
+     * @param partIndex
+     * @param in
+     * @param repoDir
+     * @param fileStorageThreshold
+     * @return
+     */
+    public static DynamicPart createPart(int contentLength, int partIndex, 
MIMEBodyPartInputStream in, String repoDir, int fileStorageThreshold){
+        if(log.isDebugEnabled()){
+            log.debug("Start createPart()");
+        }
+        try{
+            if(log.isDebugEnabled()){
+                log.debug("Content-Length = "+contentLength+ " 
FileStorageThreshold = "+fileStorageThreshold);
+            }
+            if(contentLength <= fileStorageThreshold || partIndex == 0){
+                return new PartOnByteArray(in);
+            }else{
+                if(log.isDebugEnabled()){
+                    log.debug("Creating Part On File");
+                }
+                PushbackFilePartInputStream filePartStream =
+                    new PushbackFilePartInputStream(in, new byte[0]);
+                //Create PartOnFile
+                if(log.isDebugEnabled()){
+                    log.debug("Creating PartOnFile");
+                }
+                return new PartOnFile(filePartStream, repoDir);
+            }
+        }catch(Exception e){
+            if(log.isDebugEnabled()){
+                log.debug("Error in createPart(), throwing OMException");
+            }
+            throw new OMException("Error Creating DynamicPart", e);
+        }
+    }
+
+
+    public void addHeader(String arg0, String arg1) throws MessagingException {
+        if(log.isDebugEnabled()){
+            log.debug("addHeader");
+        }
+        Header headerObj = new Header(arg0, arg1);
+        headers.put(arg0, headerObj);
+    }
+
+    public Enumeration getAllHeaders() throws MessagingException {
+        if(log.isDebugEnabled()){
+            log.debug("getAllHeaders");
+        }
+        return headers.elements();
+    }
+
+    public String getContentID() throws MessagingException {   
+        if(log.isDebugEnabled()){
+            log.debug("getContentID");
+        }
+        Header cID = (Header) headers.get("Content-ID");
+        if (cID == null) {
+            cID = (Header) headers.get("Content-Id");
+            if (cID == null) {
+                cID = (Header) headers.get("Content-id");
+                if (cID == null) {
+                    cID = (Header) headers.get("content-id");
+                }
+            }
+        }
+        return cID.getValue();
+    }
+
+    public String getContentType() throws MessagingException {
+        if(log.isDebugEnabled()){
+            log.debug("getContentType");
+        }
+        Header cType = (Header) headers.get("Content-Type");
+        if (cType == null) {
+            cType = (Header) headers.get("Content-type");
+            if (cType == null) {
+                cType = (Header) headers.get("content-type");
+            }
+        }
+        return cType.getValue();
+    }
+
+
+    public String getHeader(String arg0) throws MessagingException {   
+        return ((Header) headers.get(arg0)).getValue();
+    }
+
+    public abstract InputStream getInputStream() throws IOException, 
MessagingException;
+
+    public abstract int getSize() throws MessagingException;
+
+    public abstract DataHandler getDataHandler() throws MessagingException;
+
+    public abstract String getFileName() throws MessagingException ;
+
+
+    /**
+     * Read bytes into buffer of 8k, expand by 8k if buffer is full.
+     * Keep filling buffer until threshold is reached.
+     * @param is
+     * @param buffer
+     * @return number of bytes read
+     * @throws IOException
+     * 
+     */
+ 
+    private static int readToBuffer(byte[] buffer, InputStream is, int 
fileStorageThreshold ) throws IOException {
+        int index = 0;
+        int remainder = fileStorageThreshold;
+        int len = buffer.length;
+        if(log.isDebugEnabled()){
+            log.debug("Start Buffering Stream Data");
+        }
+        do {
+            int bytesRead;
+            while ((bytesRead = is.read(buffer, index, len)) > 0) {
+                index += bytesRead;
+                remainder -= bytesRead;
+                if(remainder >0){
+                    //expand buffer by twice the current size or to 
fileStorageThreshold value.
+                    int newBufSize = ((buffer.length * 2) > 
fileStorageThreshold)?fileStorageThreshold:(buffer.length * 2);
+                    buffer = expandBuffer(buffer, newBufSize);
+                }
+                len = buffer.length-index;
+            }
+        } while (remainder > 0 && is.available() > 0);  // repeat if more 
bytes are now available
+
+        if(log.isDebugEnabled()){
+            log.debug("Done Creating Buffer, total bytes read " +index);
+        }
+        return index;
+    }
+
+
+    /**
+     * create byte buffer from input stream.  
+     * @param is
+     * @param buffer
+     * @return number of bytes read
+     * @throws IOException
+     */
+    private static byte[] toBuffer(InputStream is, int fileStorageThreshold ) 
throws IOException {
+        int index = 0, bytesRead=0;
+        int remainder = fileStorageThreshold;
+
+        if(log.isDebugEnabled()){
+            log.debug("Start Buffering Stream Data");
+        }
+
+        int len =0;
+        len = is.available();
+        //if buffer size (8k) is greater than available byte size, user 
availabe size
+        // or lets just read 8k first.
+        len = (len >0 && bufferSize > len)?len:bufferSize;        
+        //if available or buffersize is greater than fileStorageThreshold lets 
just use
+        //fileStorageThreshold as we should not be reading bytes more than 
threshold value.
+        len = (len>fileStorageThreshold)?fileStorageThreshold:len;
+        if(log.isDebugEnabled()){
+            log.debug("Initial Length "+len);
+        }
+        byte[] b = new byte[len];
+
+        PartOutputStream baos = null;
+        do {
+            while(remainder>0 &&((bytesRead = is.read(b))>0)){
+                index += bytesRead;
+                remainder -= bytesRead;
+                //Lets initialize Outputstream with initial buf size as 
bytesRead.
+                if(baos == null){
+                    baos = new PartOutputStream(bytesRead);
+                }
+                //Write read bytes to outputstream
+                baos.write(b, 0, bytesRead);
+                //Compute new length, so if there are still bytes available to 
be read
+                //and we have not hit the file threshold limit. Lets just try 
to expand
+                //buffer to twice its current size. 
+                if(remainder >0){
+                    len = ((len * 2) > 
fileStorageThreshold)?fileStorageThreshold:(len * 2);
+                    //Twice buffer size greater than remainder, just 
+                    //read remainder.
+                    b = null; //==> Garbage Collect.
+                    len = (len>remainder)?remainder:len;
+                    if(log.isDebugEnabled()){
+                        log.debug("New Length "+len);
+                    }
+                    b = new byte[len];
+                }
+            }
+        } while (remainder > 0 && is.available() > 0);  // repeat if more 
bytes are now available
+
+        b = null; 
+        
+
+        if(log.isDebugEnabled()){
+            log.debug("Total Bytes Read ="+index);
+        }
+        //return outputStream buffer;
+        return  (baos!=null)?baos.getBytes():new byte[0];
+    }
+
+    private static byte[] expandBuffer(byte[] buffer, int newBufSize){
+        if(buffer == null){
+            if(log.isDebugEnabled()){
+                log.debug("Initializing Buffer to be 8K in size");
+            }
+            return new byte[bufferSize];
+        }
+        if(log.isDebugEnabled()){
+            log.debug("Expanding Buffer to be of size ="+newBufSize);
+        }
+        byte[] newBuffer =new byte[newBufSize];
+        //copy old buffer content to new expanded buffer
+        System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
+        //return new buffer;
+        return newBuffer;
+    }
+
+    int  parseTheHeaders(InputStream inStream) throws IOException {
+        int value;
+        if(log.isDebugEnabled()){
+            log.debug("Start Parsing Header");
+        }
+        boolean readingHeaders = true;
+        StringBuffer header = new StringBuffer();
+        while (readingHeaders & (value = inStream.read()) != -1) {
+            if (value == 13) {
+                if ((value = inStream.read()) == 10) {
+                    if ((value = inStream.read()) == 13) {
+                        if ((value = inStream.read()) == 10) {
+                            putToMap(header);
+                            readingHeaders = false;
+                        }
+                    } else {
+                        putToMap(header);
+                        header = new StringBuffer();
+                        header.append((char) value);
+                    }
+                } else {
+                    header.append(13);
+                    header.append(value);
+                }
+            } else {
+                header.append((char) value);
+            }
+        }
+        if(log.isDebugEnabled()){
+            log.debug("End Parsing Header");
+        }
+        return value;
+    }
+
+    private void putToMap(StringBuffer header) {
+        String headerString = header.toString();
+        int delimiter = headerString.indexOf(":");
+        String name = headerString.substring(0, delimiter).trim();
+        String value = headerString.substring(delimiter + 1, 
headerString.length()).trim();
+        if(log.isDebugEnabled()){
+            log.debug("Header name ="+name+ " value = "+value);
+        }
+        Header headerObj = new Header(name, value);
+        headers.put(name, headerObj);
+    }
+
+    public String getEncoding() throws MessagingException{
+        if(log.isDebugEnabled()){
+            log.debug("getEncoding()");
+        }
+        String encoding = null;
+        Header cType = (Header) headers.get("Content-Transfer-Encoding");
+        if (cType == null) {
+            cType = (Header) headers.get("content-transfer-encoding");         
   
+        }
+
+        if(cType!=null){
+            encoding = cType.getValue();
+        }
+        if(encoding!=null){
+            encoding = encoding.trim();
+            if(log.isDebugEnabled()){
+                log.debug("Encoding =" + encoding);
+            }
+            if(encoding.equalsIgnoreCase("7bit") || 
+                encoding.equalsIgnoreCase("8bit") ||
+                encoding.equalsIgnoreCase("qyited-printable") ||
+                encoding.equalsIgnoreCase("base64")){
+
+                return encoding;
+            }
+            
+            HeaderTokenizer ht = new HeaderTokenizer(encoding, 
HeaderTokenizer.MIME);
+            boolean done = false;
+            while(!done){
+                HeaderTokenizer.Token token = ht.next();
+                switch(token.getType()){
+                case HeaderTokenizer.Token.EOF:
+                    if(log.isDebugEnabled()){
+                        log.debug("HeaderTokenizer EOF");
+                    }
+                    done = true;
+                    break;
+                case HeaderTokenizer.Token.ATOM:                    
+                    return token.getValue();
+                }
+            }
+            return encoding;
+        }
+        return null;
+
+
+    }
+
+
+
+}

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnByteArray.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnByteArray.java?rev=602962&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnByteArray.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnByteArray.java
 Mon Dec 10 08:33:02 2007
@@ -0,0 +1,221 @@
+/*
+ * 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.axiom.attachments.part;
+
+import java.io.BufferedInputStream;
+import java.io.ByteArrayInputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.Hashtable;
+import com.ibm.jvm.util.ByteArrayOutputStream;
+import javax.activation.DataHandler;
+import javax.activation.DataSource;
+import javax.mail.MessagingException;
+import javax.mail.internet.MimeUtility;
+import org.apache.axiom.om.OMException;
+import org.apache.commons.logging.Log;
+import org.apache.commons.logging.LogFactory;
+/*
+ * PartOnByteArray stores Attachment part data in to a byteArray
+ * DataSource and provides a DataHandler to access it. 
+ */
+public class PartOnByteArray extends DynamicPart {
+    protected static Log log = LogFactory.getLog(PartOnByteArray.class);
+    private static final int bufferSize = 8192;
+    ByteArrayDataSource ds;
+
+    public PartOnByteArray(InputStream in){
+        if(log.isDebugEnabled()){
+            log.debug("Creating Part On Byte Array");
+        }
+        headers = new Hashtable();  
+        int value;
+        try{
+            value = parseTheHeaders(in);
+        }catch(IOException e){
+            throw new OMException("Error Parsing Header "+e);
+        }   
+        try{
+            if(log.isDebugEnabled()){
+                log.debug("Reading byteArray from stream");
+            }
+            //Read all bytes minus header to the byte Array;                
+            byte[] byteArray= getBytes(in, value);
+
+            ds = new ByteArrayDataSource(byteArray, getEncoding());
+            if(log.isDebugEnabled()){
+                log.debug("Length of bytes read 
="+((byteArray!=null)?byteArray.length:0));               
+            }
+        }catch(Exception e){
+            throw new OMException("Error creating data source "+e);
+        }       
+        if(log.isDebugEnabled()){
+            log.debug("Part On Byte Array Created");
+        }
+    }
+
+    /** Get Bytes from InputStream for creating DataSource. The return value 
from
+     * parsed header is inserted first in the byte array and then read
+     * rest of the stream to create complete byte buffer for a input part. 
+     * @param in
+     * @param value
+     * @return
+     * @throws IOException
+     */
+    private static byte[] getBytes(InputStream in, int value) throws 
IOException{
+        int bytesRead = 0, totalBytes = 0;
+        if(!(in instanceof ByteArrayInputStream) && 
+            !(in instanceof BufferedInputStream)){
+            if(log.isDebugEnabled()){
+                log.debug("InputStream is not BufferedInputStream");
+            }
+            in = new BufferedInputStream(in);
+        }
+
+        if(in instanceof ByteArrayInputStream){
+            if(log.isDebugEnabled()){
+                log.debug("input stream a ByteArrayInputStream");
+            }
+            int len = in.available();
+            byte[] byteArray = new byte[len + 1];
+            byteArray[0] = new Integer(value).byteValue();
+            //create byteArray here.
+            in.read(byteArray, 1, len);
+
+            if(log.isDebugEnabled()){
+                log.debug("Total Bytes Read ="+len);
+            }
+
+            return byteArray;
+        }
+        else{
+            if(log.isDebugEnabled()){
+                log.debug("input stream not a ByteArrayInputStream");
+            }
+            byte[] b = new byte[bufferSize];
+            //TODO Should I create my own bufferStream to return the original 
buf
+            ByteArrayOutputStream baos = new ByteArrayOutputStream(bufferSize);
+            baos.write(value);
+            while((bytesRead = in.read(b))>0){
+                totalBytes+=bytesRead;
+                baos.write(b, 0, bytesRead);
+            }
+            if(log.isDebugEnabled()){
+                log.debug("Total Bytes Read ="+totalBytes);
+            }
+            return baos.toByteArray();
+        }
+
+    }
+
+    public DataHandler getDataHandler() throws MessagingException {
+        if(log.isDebugEnabled()){
+            log.debug("getDataHandler()");
+        }
+        ds.setType(getContentType());
+        return new DataHandler(ds);
+    }
+
+    public String getFileName() throws MessagingException {
+        if(log.isDebugEnabled()){
+            log.debug("getFileName()");
+        }
+        throw new UnsupportedOperationException("Not Supported");
+    }
+
+    public InputStream getInputStream() throws IOException, MessagingException 
{
+        if(log.isDebugEnabled()){
+            log.debug("getInputStream()");
+        }
+        return ds.getInputStream();
+    }
+
+    public int getSize() throws MessagingException {
+        if(log.isDebugEnabled()){
+            log.debug("getSize()");
+        }
+        return ds.getSize();
+    }
+
+    class ByteArrayDataSource implements DataSource {
+
+        private byte[] data;
+        private String type;
+        private String encoding;
+        public ByteArrayDataSource(byte[] data,  String encoding) {
+            super();
+            this.data = data;
+            this.encoding = encoding;
+        }
+
+        public ByteArrayDataSource(byte[] data) {
+            super();
+            this.data = data;
+        }
+
+        public void setType(String type) {
+            this.type = type;
+        }
+
+        public String getContentType() {
+            if (type == null)
+                return "application/octet-stream";
+            else
+                return type;
+        }
+
+        public InputStream getInputStream() throws IOException {
+            if(log.isDebugEnabled()){
+                log.debug("ByteArrayDataSource getInputStream()");
+            }
+            InputStream in = new ByteArrayInputStream(data == null ? new 
byte[0] : data);
+            if(log.isDebugEnabled()){
+                log.debug("Stream Encoding = "+encoding);
+            }
+            if(encoding !=null){
+                try{
+                    if(log.isDebugEnabled()){
+                        log.debug("Start Decoding stream");
+                    }
+                    return MimeUtility.decode(in, encoding);
+                }catch(Exception e){
+                    if(log.isDebugEnabled()){
+                        log.debug("Stream Failed decoding for encoding 
="+encoding);
+                    }
+                }
+            }
+            return in;
+        }
+
+        public String getName() {
+
+            return "ByteArrayDataSource";
+        }
+
+        public OutputStream getOutputStream() throws IOException {
+            throw new IOException("Not Supported");
+        }
+
+        public int getSize(){
+            return (data!=null)?data.length:0;
+        }
+    }
+}

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnFile.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnFile.java?rev=602962&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnFile.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOnFile.java
 Mon Dec 10 08:33:02 2007
@@ -0,0 +1,105 @@
+/*
+ * 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.axiom.attachments.part;
+
+import java.io.File;
+import java.io.FileInputStream;
+import java.io.FileOutputStream;
+import java.io.IOException;
+import java.io.InputStream;
+import java.util.Hashtable;
+
+import javax.activation.DataHandler;
+import javax.mail.MessagingException;
+
+import org.apache.axiom.attachments.CachedFileDataSource;
+import org.apache.axiom.attachments.Part;
+import org.apache.axiom.attachments.PushbackFilePartInputStream;
+import org.apache.axiom.om.OMException;
+/*
+ * PartOnFile stores Attachment part data in to a File
+ * and provides a DataHandler to access it. 
+ */
+public class PartOnFile extends DynamicPart {
+    File cacheFile;
+
+    Part bodyPart;
+
+    String contentType;
+
+    String contentID;
+
+    public PartOnFile(PushbackFilePartInputStream inStream, String repoDir) {
+        super();
+
+        headers = new Hashtable();
+        if (repoDir == null) {
+            repoDir = ".";
+        }
+        try {
+            File repoDirFile = null;
+            if (repoDir != null) {
+                repoDirFile = new File(repoDir);
+                if (!repoDirFile.exists()) {
+                    repoDirFile.mkdirs();
+                }
+            }
+            if (!repoDirFile.isDirectory()) {
+                throw new IllegalArgumentException("Given Axis2 Attachment 
File Cache Location "
+                    + repoDir + "  should be a directory.");
+            }
+            cacheFile = File.createTempFile("Axis2", ".att", repoDirFile);
+
+            FileOutputStream fileOutStream = new FileOutputStream(cacheFile);
+            int value;
+            value = parseTheHeaders(inStream);
+            fileOutStream.write(value);
+            do {
+                byte[] buffer = new byte[4000];
+                int len;
+                while ((len = inStream.read(buffer)) > 0) {
+                    fileOutStream.write(buffer, 0, len);
+                }
+            } while (inStream.available() > 0);
+            fileOutStream.flush();
+            fileOutStream.close();
+        } catch (IOException e) {
+            throw new OMException("Error creating temporary File.", e);
+        }
+    }
+
+    public DataHandler getDataHandler() throws MessagingException {
+        CachedFileDataSource dataSource = new CachedFileDataSource(cacheFile);
+        dataSource.setContentType(getContentType());
+        return new DataHandler(dataSource);
+    }
+
+    public String getFileName() throws MessagingException {
+        return cacheFile.getAbsolutePath();
+    }
+
+    public InputStream getInputStream() throws IOException, MessagingException 
{
+        return new FileInputStream(cacheFile);
+    }
+
+    public int getSize() throws MessagingException {
+        return (int) cacheFile.length();
+    }
+}

Added: 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOutputStream.java
URL: 
http://svn.apache.org/viewvc/webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOutputStream.java?rev=602962&view=auto
==============================================================================
--- 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOutputStream.java
 (added)
+++ 
webservices/commons/trunk/modules/axiom/modules/axiom-api/src/main/java/org/apache/axiom/attachments/part/PartOutputStream.java
 Mon Dec 10 08:33:02 2007
@@ -0,0 +1,43 @@
+/*
+ * 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.axiom.attachments.part;
+
+import java.io.ByteArrayOutputStream;
+/*
+ * Custom outputstream to return ByteArrayBuffer's byte[] buf.
+ */
+public class PartOutputStream extends ByteArrayOutputStream {
+
+
+    public PartOutputStream() {
+        super();
+
+    }
+
+    public PartOutputStream(int size) {
+        super(size);
+
+    }
+
+    public byte[] getBytes(){
+        return buf;
+    }
+
+}



---------------------------------------------------------------------
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to