001    package railo.commons.io.res;
002    
003    import java.io.InputStream;
004    
005    import railo.commons.io.IOUtil;
006    import railo.commons.lang.StringUtil;
007    import railo.runtime.type.util.ListUtil;
008    
009    public final class ContentTypeImpl implements ContentType {
010    
011            public final static ContentType APPLICATION_UNKNOW=new ContentTypeImpl("application","unknow");
012            
013            private final String type;
014            private final String subtype;
015            private String charset;
016    
017            /**
018             * Constructor of the class
019             * @param type
020             * @param subtype
021             * @param charset
022             */
023            public ContentTypeImpl(String type,String subtype, String charset) {
024                    this.type=StringUtil.isEmpty(type,true)?null:type.trim().toLowerCase();
025                    this.subtype=StringUtil.isEmpty(subtype,true)?null:subtype.trim().toLowerCase();
026                    this.charset=StringUtil.isEmpty(charset,true)?null:charset.trim().toLowerCase();
027            }
028            
029            /**
030             * Constructor of the class
031             * @param type
032             * @param subtype
033             */
034            public ContentTypeImpl(String type,String subtype) {
035                    this(type,subtype,null);
036            }
037    
038            public ContentTypeImpl(InputStream is) {
039                    String raw=IOUtil.getMimeType(is, null);
040                    String[] arr = ListUtil.listToStringArray(raw, '/');
041                    this.type=arr[0];
042                    this.subtype=arr[1];
043            }
044    
045            @Override
046            public boolean equals(Object other) {
047                    if((other instanceof ContentType)) return false;
048                    return toString().equals(other.toString());
049            }
050    
051            @Override
052            public String toString() {
053                    if(type==null)return APPLICATION_UNKNOW.toString();
054                    if(this.charset==null) return type+"/"+subtype;
055                    return type+"/"+subtype+" charset="+charset;
056            }
057            
058            /**
059             * @return the mime type
060             */
061            public String getMimeType() {
062                    if(type==null)return APPLICATION_UNKNOW.toString();
063                    return type+"/"+subtype;
064            }
065    
066            /**
067             * @return the charset
068             */
069            public String getCharset() {
070                    if(StringUtil.isEmpty(charset,true)) return null;
071                    return charset;
072            }
073            
074    }