package railo.commons.io.res;

import java.io.InputStream;

import railo.commons.io.IOUtil;
import railo.commons.lang.StringUtil;
import railo.runtime.type.List;

public final class ContentTypeImpl implements ContentType {

	public final static ContentType APPLICATION_UNKNOW=new ContentTypeImpl("application","unknow");
	
	private final String type;
	private final String subtype;
	private String charset;

	/**
	 * Constructor of the class
	 * @param type
	 * @param subtype
	 * @param charset
	 */
	public ContentTypeImpl(String type,String subtype, String charset) {
		this.type=type.trim().toLowerCase();
		this.subtype=subtype.trim().toLowerCase();
		this.charset=charset.trim().toLowerCase();
	}
	
	/**
	 * Constructor of the class
	 * @param type
	 * @param subtype
	 */
	public ContentTypeImpl(String type,String subtype) {
		this.type=type.trim().toLowerCase();
		this.subtype=subtype.trim().toLowerCase();
	}

	public ContentTypeImpl(InputStream is) {
		String raw=IOUtil.getMymeType(is, null);
		String[] arr = List.listToStringArray(raw, '/');
		this.type=arr[0];
		this.subtype=arr[1];
	}

	/**
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	public boolean equals(Object other) {
		if((other instanceof ContentType)) return false;
		return toString().equals(other.toString());
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	public String toString() {
		if(type==null)return APPLICATION_UNKNOW.toString();
		if(this.charset==null) return type+"/"+subtype;
		return type+"/"+subtype+" charset="+charset;
	}
	
	// FUTURE add to interface
	/**
	 * @return the mime type
	 */
	public String getMimeType() {
		if(type==null)return APPLICATION_UNKNOW.toString();
		return type+"/"+subtype;
	}

	// FUTURE add to interface
	/**
	 * @return the charset
	 */
	public String getCharset() {
		if(StringUtil.isEmpty(charset,true)) return null;
		return charset;
	}
	
}

@@ -568,32 +568,67 @@
 ) %7B%0A%09%09this.type=
+StringUtil.isEmpty(type,true)?null:
 type.trim().toLo
@@ -645,32 +645,70 @@
 %0A%09%09this.subtype=
+StringUtil.isEmpty(subtype,true)?null:
 subtype.trim().t
@@ -736,16 +736,54 @@
 charset=
+StringUtil.isEmpty(charset,true)?null:
 charset.
@@ -947,83 +947,26 @@
 this
-.
+(
 type
-=type.trim().toLowerCase();%0A%09%09this.subtype=subtype.trim().toLowerCase(
+,subtype,null
 );%0A%09
@@ -1587,36 +1587,8 @@
 %7D%0A%09%0A
-%09// FUTURE add to interface%0A
 %09/**
@@ -1739,36 +1739,8 @@
 %09%7D%0A%0A
-%09// FUTURE add to interface%0A
 %09/**

make class more flexible