001package com.hfg.util.mime; 002 003 004import java.util.regex.Matcher; 005import java.util.regex.Pattern; 006 007import com.hfg.util.StringBuilderPlus; 008import com.hfg.util.StringUtil; 009 010//------------------------------------------------------------------------------ 011/** 012 MIME Content Disposition. 013 @author J. Alex Taylor, hairyfatguy.com 014 */ 015//------------------------------------------------------------------------------ 016// com.hfg XML/HTML Coding Library 017// 018// This library is free software; you can redistribute it and/or 019// modify it under the terms of the GNU Lesser General Public 020// License as published by the Free Software Foundation; either 021// version 2.1 of the License, or (at your option) any later version. 022// 023// This library is distributed in the hope that it will be useful, 024// but WITHOUT ANY WARRANTY; without even the implied warranty of 025// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 026// Lesser General Public License for more details. 027// 028// You should have received a copy of the GNU Lesser General Public 029// License along with this library; if not, write to the Free Software 030// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA 031// 032// J. Alex Taylor, President, Founder, CEO, COO, CFO, OOPS hairyfatguy.com 033// jataylor@hairyfatguy.com 034//------------------------------------------------------------------------------ 035 036public class MimeContentDisposition 037{ 038 //########################################################################## 039 // PRIVATE FIELDS 040 //########################################################################## 041 042 private String mType; 043 private String mName; 044 private String mFilename; 045 046 private static final String CONTENT_DISPOSITION = "Content-Disposition"; 047 048 private static Pattern PARSE_PATTERN = Pattern.compile("^\\s*" + CONTENT_DISPOSITION + ": (\\S+); name=\"([^\"]+)\"(?:; filename=\"([^\"]+)\")?$", Pattern.CASE_INSENSITIVE); 049 050 051 //########################################################################## 052 // PUBLIC METHODS 053 //########################################################################## 054 055 //-------------------------------------------------------------------------- 056 // Content-Disposition: form-data; name="action" 057 // Content-Disposition: form-data; name="upload_file"; filename="rss.png" 058 public static MimeContentDisposition parse(String inValue) 059 { 060 MimeContentDisposition contentDisposition = new MimeContentDisposition(); 061 062 Matcher m = PARSE_PATTERN.matcher(inValue); 063 064 if (m.matches()) 065 { 066 contentDisposition.setType(m.group(1)); 067 contentDisposition.setName(m.group(2)); 068 if (m.group(3) != null) 069 { 070 contentDisposition.setFilename(m.group(3)); 071 } 072 } 073 else 074 { 075 throw new MimeException("Failed to parse the entity content-disposition line '" + inValue + "'!"); 076 } 077 078 return contentDisposition; 079 } 080 081 082 //-------------------------------------------------------------------------- 083 @Override 084 public String toString() 085 { 086 StringBuilderPlus buffer = new StringBuilderPlus(CONTENT_DISPOSITION + ": ") 087 .append(getType()) 088 .append("; name=") 089 .append(StringUtil.quote(getName())); 090 091 if (getFilename() != null) 092 { 093 buffer.append("; filename=") 094 .append(StringUtil.quote(getFilename())); 095 } 096 097 return buffer.toString(); 098 } 099 100 101 //-------------------------------------------------------------------------- 102 public String getType() 103 { 104 return mType; 105 } 106 107 //-------------------------------------------------------------------------- 108 public MimeContentDisposition setType(String inValue) 109 { 110 mType = inValue; 111 return this; 112 } 113 114 115 //-------------------------------------------------------------------------- 116 public String getName() 117 { 118 return mName; 119 } 120 121 //-------------------------------------------------------------------------- 122 public MimeContentDisposition setName(String inValue) 123 { 124 mName = inValue; 125 return this; 126 } 127 128 129 //-------------------------------------------------------------------------- 130 public String getFilename() 131 { 132 return mFilename; 133 } 134 135 //-------------------------------------------------------------------------- 136 public MimeContentDisposition setFilename(String inValue) 137 { 138 mFilename = inValue; 139 return this; 140 } 141}