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.List;
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=type.trim().toLowerCase();
025                    this.subtype=subtype.trim().toLowerCase();
026                    this.charset=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=type.trim().toLowerCase();
036                    this.subtype=subtype.trim().toLowerCase();
037            }
038    
039            public ContentTypeImpl(InputStream is) {
040                    String raw=IOUtil.getMymeType(is, null);
041                    String[] arr = List.listToStringArray(raw, '/');
042                    this.type=arr[0];
043                    this.subtype=arr[1];
044            }
045    
046            /**
047             * @see java.lang.Object#equals(java.lang.Object)
048             */
049            public boolean equals(Object other) {
050                    if((other instanceof ContentType)) return false;
051                    return toString().equals(other.toString());
052            }
053    
054            /**
055             * @see java.lang.Object#toString()
056             */
057            public String toString() {
058                    if(type==null)return APPLICATION_UNKNOW.toString();
059                    if(this.charset==null) return type+"/"+subtype;
060                    return type+"/"+subtype+" charset="+charset;
061            }
062            
063            // FUTURE add to interface
064            /**
065             * @return the mime type
066             */
067            public String getMimeType() {
068                    if(type==null)return APPLICATION_UNKNOW.toString();
069                    return type+"/"+subtype;
070            }
071    
072            // FUTURE add to interface
073            /**
074             * @return the charset
075             */
076            public String getCharset() {
077                    if(StringUtil.isEmpty(charset,true)) return null;
078                    return charset;
079            }
080            
081    }