All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.hfg.util.mime.MimeEntity Maven / Gradle / Ivy

There is a newer version: 20240423
Show newest version
package com.hfg.util.mime;

import com.hfg.exception.ProgrammingException;
import com.hfg.util.FileUtil;
import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;
import com.hfg.util.io.FileBytes;

import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.util.Base64;

//------------------------------------------------------------------------------
/**
 MIME Entity.
 @author J. Alex Taylor, hairyfatguy.com
 */
//------------------------------------------------------------------------------
// com.hfg XML/HTML Coding Library
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
//
// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com
// [email protected]
//------------------------------------------------------------------------------

public class MimeEntity
{
   //##########################################################################
   // PRIVATE FIELDS
   //##########################################################################

   private MimeContentDisposition mContentDisposition = new MimeContentDisposition().setType("form-data");
   private MimeType  mContentType;
   private String    mContentTransferEncoding;
   private String    mContent;

   // File data could take several forms. We won't convert them until actually generating the output.
   private File      mFile;
   private FileBytes mFileBytes;

   private File      mCachedContentFile;

   private static final int MAX_ENCODING_LINE_LENGTH = 76;
   private static final String CRLF = "\r\n";

   private static final String CONTENT_TYPE = "Content-Type";
   private static final String CONTENT_TRANSFER_ENCODING = "Content-Transfer-Encoding";

   private static final MimeType DEFAULT_FILE_CONTENT_TYPE = MimeType.APPLICATION_OCTET_STREAM;

   //##########################################################################
   // CONSTRUCTORS
   //##########################################################################

   //--------------------------------------------------------------------------
   public MimeEntity()
   {

   }

   //--------------------------------------------------------------------------
   public MimeEntity(String inName, File inValue)
   {
      getContentDisposition()
            .setName(inName)
            .setFilename(inValue.getName());

      mFile = inValue;
   }

   //--------------------------------------------------------------------------
   public MimeEntity(String inName, FileBytes inValue)
   {
      getContentDisposition()
            .setName(inName)
            .setFilename(inValue.name());

      mFileBytes = inValue;
   }

   //--------------------------------------------------------------------------
   public MimeEntity(String inName, Object inValue)
   {
      getContentDisposition()
            .setName(inName);

      if (inValue != null)
      {
         if (inValue instanceof File)
         {
            mContentDisposition.setFilename(((File)inValue).getName());
            mFile = (File) inValue;
         }
         else if (inValue instanceof FileBytes)
         {
            mContentDisposition.setFilename(((FileBytes)inValue).name());
            mFileBytes = (FileBytes) inValue;
         }
         else
         {
            mContent = inValue.toString();
         }
      }
   }

   //##########################################################################
   // PUBLIC METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   public MimeContentDisposition getContentDisposition()
   {
      return mContentDisposition;
   }

   //--------------------------------------------------------------------------
   public void setContentDisposition(MimeContentDisposition inValue)
   {
      mContentDisposition = inValue;
   }


   //--------------------------------------------------------------------------
   public MimeType getContentType()
   {
      if (null == mContentType
          && (mFile != null || mFileBytes != null))
      {
         setContentType(DEFAULT_FILE_CONTENT_TYPE);
      }

      return mContentType;
   }

   //--------------------------------------------------------------------------
   public void setContentType(MimeType inValue)
   {
      mContentType = inValue;
   }


   //--------------------------------------------------------------------------
   public String getContentTransferEncoding()
   {
      return mContentTransferEncoding;
   }

   //--------------------------------------------------------------------------
   public void setContentTransferEncoding(String inValue)
   {
      mContentTransferEncoding = inValue;
   }


   //--------------------------------------------------------------------------
   public void setContent(String inValue)
         throws IOException
   {
      if (mCachedContentFile != null)
      {
         FileUtil.write(mCachedContentFile, inValue);
         mContent = null;
      }
      else
      {
         mContent = inValue;
      }
   }

   //--------------------------------------------------------------------------
   public String getContent()
   {
      return mContent;
   }

   //--------------------------------------------------------------------------
   public File getCachedContentFile()
   {
      return mCachedContentFile;
   }

   //--------------------------------------------------------------------------
   /**
    Sets the file in which the entity's content will be cached.
    */
   public void setCachedContentFile(File inValue)
         throws IOException
   {
      mCachedContentFile = inValue;
      if (mCachedContentFile != null)
      {
/*
         if (mCachedContentFile.exists())
         {
            if (!mCachedContentFile.delete())
            {
               throw new IOException("The preexisting cache file '" + mCachedContentFile + "' couldn't be deleted!");
            }
         }
*/
         if (!mCachedContentFile.exists()
             && !mCachedContentFile.createNewFile())
         {
            throw new IOException("The cache file '" + mCachedContentFile + "' couldn't be created!");
         }
      }
   }

   //--------------------------------------------------------------------------
   public void write(OutputStream inStream)
      throws IOException
   {
      inStream.write(getContentDisposition().toString().getBytes());


      if (getContentType() != null)
      {
         inStream.write(CRLF.getBytes());
         inStream.write(CONTENT_TYPE.getBytes());
         inStream.write(": ".getBytes());
         inStream.write(getContentType().toString().getBytes());
      }

      if (getContentTransferEncoding() != null)
      {
         inStream.write(CRLF.getBytes());
         inStream.write(CONTENT_TRANSFER_ENCODING.getBytes());
         inStream.write(": ".getBytes());
         inStream.write(getContentTransferEncoding().getBytes());
      }

      inStream.write(CRLF.getBytes());
      inStream.write(CRLF.getBytes());

      byte[] content = generateContent();
      if (content != null)
      {
         inStream.write(content);
      }

      inStream.write(CRLF.getBytes());
   }

   //##########################################################################
   // PRIVATE METHODS
   //##########################################################################

   //--------------------------------------------------------------------------
   private byte[] generateContent()
      throws IOException
   {
      byte[] content = null;

      if (mFile != null)
      {
         content = getContentFromFile(mFile);
      }
      else if (mFileBytes != null)
      {
         content = getContentFromFileBytes(mFileBytes);
      }
      else if (getContent() != null)
      {
         content = getContent().getBytes();
      }

      return content;
   }


   //--------------------------------------------------------------------------
   private byte[] getContentFromFileBytes(FileBytes inValue)
         throws IOException
   {
      byte[] content = null;
      if (inValue != null)
      {
         content = getContentFromBytes(inValue.getBytes());
      }

      return content;
   }

   //--------------------------------------------------------------------------
   private byte[] getContentFromFile(File inValue)
         throws IOException
   {
      byte[] content = null;
      if (inValue != null)
      {
         content = getContentFromBytes(FileUtil.read(inValue).toString().getBytes());
      }

      return content;
   }

   //--------------------------------------------------------------------------
   private byte[] getContentFromBytes(byte[] inValue)
   {
      byte[] bytes = inValue;
      if (getContentTransferEncoding() != null)
      {
         if (getContentTransferEncoding().equalsIgnoreCase("base64"))
         {
            String encodedString = Base64.getEncoder().encodeToString(inValue);
            StringBuilderPlus buffer = new StringBuilderPlus().setDelimiter(CRLF);
            int i = 0;
            while (i < encodedString.length())
            {
               int limit = i + MAX_ENCODING_LINE_LENGTH;
               if (limit > encodedString.length())
               {
                  limit = encodedString.length();
               }

               buffer.delimitedAppend(encodedString.substring(i, limit));

               i = limit;
            }

            bytes = buffer.toString().getBytes();
         }
         else
         {
            throw new ProgrammingException(StringUtil.singleQuote(getContentTransferEncoding())
                                           + " is not a currently supported transfer encoding!");
         }
      }

      return bytes;
   }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy