001/** 002 * 003 * Copyright (c) 2014, the Railo Company Ltd. All rights reserved. 004 * 005 * This library is free software; you can redistribute it and/or 006 * modify it under the terms of the GNU Lesser General Public 007 * License as published by the Free Software Foundation; either 008 * version 2.1 of the License, or (at your option) any later version. 009 * 010 * This library is distributed in the hope that it will be useful, 011 * but WITHOUT ANY WARRANTY; without even the implied warranty of 012 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU 013 * Lesser General Public License for more details. 014 * 015 * You should have received a copy of the GNU Lesser General Public 016 * License along with this library. If not, see <http://www.gnu.org/licenses/>. 017 * 018 **/ 019package lucee.commons.io.res; 020 021import java.io.InputStream; 022 023import lucee.commons.io.IOUtil; 024import lucee.commons.lang.StringUtil; 025import lucee.runtime.type.util.ListUtil; 026 027public final class ContentTypeImpl implements ContentType { 028 029 public final static ContentType APPLICATION_UNKNOW=new ContentTypeImpl("application","unknow"); 030 031 private final String type; 032 private final String subtype; 033 private String charset; 034 035 /** 036 * Constructor of the class 037 * @param type 038 * @param subtype 039 * @param charset 040 */ 041 public ContentTypeImpl(String type,String subtype, String charset) { 042 this.type=StringUtil.isEmpty(type,true)?null:type.trim().toLowerCase(); 043 this.subtype=StringUtil.isEmpty(subtype,true)?null:subtype.trim().toLowerCase(); 044 this.charset=StringUtil.isEmpty(charset,true)?null:charset.trim().toLowerCase(); 045 } 046 047 /** 048 * Constructor of the class 049 * @param type 050 * @param subtype 051 */ 052 public ContentTypeImpl(String type,String subtype) { 053 this(type,subtype,null); 054 } 055 056 public ContentTypeImpl(InputStream is) { 057 String raw=IOUtil.getMimeType(is, null); 058 String[] arr = ListUtil.listToStringArray(raw, '/'); 059 this.type=arr[0]; 060 this.subtype=arr[1]; 061 } 062 063 @Override 064 public boolean equals(Object other) { 065 if((other instanceof ContentType)) return false; 066 return toString().equals(other.toString()); 067 } 068 069 @Override 070 public String toString() { 071 if(type==null)return APPLICATION_UNKNOW.toString(); 072 if(this.charset==null) return type+"/"+subtype; 073 return type+"/"+subtype+" charset="+charset; 074 } 075 076 /** 077 * @return the mime type 078 */ 079 public String getMimeType() { 080 if(type==null)return APPLICATION_UNKNOW.toString(); 081 return type+"/"+subtype; 082 } 083 084 /** 085 * @return the charset 086 */ 087 public String getCharset() { 088 if(StringUtil.isEmpty(charset,true)) return null; 089 return charset; 090 } 091 092}