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

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

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


import java.util.regex.Matcher;
import java.util.regex.Pattern;

import com.hfg.util.StringBuilderPlus;
import com.hfg.util.StringUtil;

//------------------------------------------------------------------------------
/**
 MIME Content Disposition.
 @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 MimeContentDisposition
{
   //##########################################################################
   // PRIVATE FIELDS
   //##########################################################################

   private String mType;
   private String mName;
   private String mFilename;

   private static final String CONTENT_DISPOSITION = "Content-Disposition";

   private static Pattern PARSE_PATTERN = Pattern.compile("^\\s*" + CONTENT_DISPOSITION + ": (\\S+); name=\"([^\"]+)\"(?:; filename=\"([^\"]+)\")?$", Pattern.CASE_INSENSITIVE);


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

   //--------------------------------------------------------------------------
   // Content-Disposition: form-data; name="action"
   // Content-Disposition: form-data; name="upload_file"; filename="rss.png"
   public static MimeContentDisposition parse(String inValue)
   {
      MimeContentDisposition contentDisposition = new MimeContentDisposition();

      Matcher m = PARSE_PATTERN.matcher(inValue);

      if (m.matches())
      {
         contentDisposition.setType(m.group(1));
         contentDisposition.setName(m.group(2));
         if (m.group(3) != null)
         {
            contentDisposition.setFilename(m.group(3));
         }
      }
      else
      {
         throw new MimeException("Failed to parse the entity content-disposition line '" + inValue + "'!");
      }

      return contentDisposition;
   }


   //--------------------------------------------------------------------------
   @Override
   public String toString()
   {
      StringBuilderPlus buffer =  new StringBuilderPlus(CONTENT_DISPOSITION + ": ")
            .append(getType())
            .append("; name=")
            .append(StringUtil.quote(getName()));

      if (getFilename() != null)
      {
         buffer.append("; filename=")
               .append(StringUtil.quote(getFilename()));
      }

      return buffer.toString();
   }


   //--------------------------------------------------------------------------
   public String getType()
   {
      return mType;
   }

   //--------------------------------------------------------------------------
   public MimeContentDisposition setType(String inValue)
   {
      mType = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   public String getName()
   {
      return mName;
   }

   //--------------------------------------------------------------------------
   public MimeContentDisposition setName(String inValue)
   {
      mName = inValue;
      return this;
   }


   //--------------------------------------------------------------------------
   public String getFilename()
   {
      return mFilename;
   }

   //--------------------------------------------------------------------------
   public MimeContentDisposition setFilename(String inValue)
   {
      mFilename = inValue;
      return this;
   }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy