/*
* (c ) Copyright 2010 Epimorphics Ltd.
* All rights reserved.
* [See end of file]
*/
package org.openjena.fuseki.conneg;
import java.util.Iterator ;
import java.util.LinkedHashMap ;
import java.util.Map ;
import static org.openjena.atlas.lib.Lib.* ;
import org.openjena.fuseki.HttpNames ;
import org.slf4j.Logger ;
import org.slf4j.LoggerFactory ;
/** A structure to represent a media type.
* Se also the Apache httpd documentation.
*/
public class MediaType
{
private static Logger log = LoggerFactory.getLogger(MediaType.class) ;
private String type = null ;
private String subType = null ;
private String charset = null ;
// Keys in insertion order.
private Map params = new LinkedHashMap() ;
public MediaType(MediaType other)
{
this.type = other.type ;
this.subType = other.subType ;
// Order preserving copy.
this.params = new LinkedHashMap(other.params) ;
}
public MediaType(String string)
{
parseOneEntry(string) ;
}
// /** Create a media type from type and subType */
// protected MediaType(String type, String subType)
// {
// this.type = type ;
// this.subType = subType ;
// }
public static MediaType create(String contentType, String charset)
{
MediaType mediaType = new MediaType(contentType) ;
mediaType.setParameter(HttpNames.charset, charset) ;
return mediaType ;
}
private void parseOneEntry(String s)
{
String[] x = ConNeg.split(s, ";") ;
parseAndSetType(x[0]) ;
for ( int i = 1 ; i < x.length ; i++ )
{
// Each a parameter
String z[] = ConNeg.split(x[i], "=") ;
if ( z.length == 2 )
this.params.put(z[0], z[1]) ;
else
log.warn("Duff parameter: "+x[i]+" in "+s) ;
}
}
private void parseAndSetType(String s)
{
String[] t = ConNeg.split(s, "/") ;
type = t[0] ;
if ( t.length > 1 )
subType = t[1] ;
}
/** Format for use in HTTP header */
public String toHeaderString()
{
StringBuilder b = new StringBuilder() ;
b.append(type) ;
if ( subType != null )
b.append("/").append(subType) ;
for ( Map.Entry entry: params.entrySet() )
{
b.append(";") ;
b.append(entry.getKey()) ;
b.append("=") ;
b.append(entry.getValue()) ;
}
return b.toString() ;
}
/** Format to show structure - intentionally different from header
* form so you can tell parsing happened correctly
*/
@Override
public String toString()
{
StringBuffer b = new StringBuffer() ;
b.append("[") ;
b.append(type) ;
if ( subType != null )
b.append("/").append(subType) ;
for ( Iterator iter = params.keySet().iterator() ; iter.hasNext() ; )
{
String k = iter.next() ;
String v = params.get(k) ;
b.append(" ") ;
b.append(k) ;
b.append("=") ;
b.append(v) ;
}
b.append("]") ;
return b.toString() ;
}
// private String type = null ;
// private String subType = null ;
// // Keys in insertion order.
// private Map params = new LinkedHashMap() ;
@Override
public int hashCode()
{
return hashCodeObject(type, 1)^hashCodeObject(subType, 2)^hashCodeObject(params, 3) ;
}
@Override
public boolean equals(Object object)
{
if (this == object) return true ;
if (!(object instanceof MediaType)) return false ;
MediaType mt = (MediaType)object ;
return equal(type, mt.type) && equal(subType, mt.subType) && equal(params, mt.params) ;
}
public String getParameter(String name) { return params.get(name) ; }
public void setParameter(String name, String value) { params.put(name, value) ; }
public String getContentType()
{
if ( subType == null )
return type ;
return type+"/"+subType ;
}
public String getCharset() { return getParameter(HttpNames.charset) ; }
public String getSubType() { return subType ; }
public void setSubType(String subType) { this.subType = subType ; }
public String getType() { return type ; }
public void setType(String type) { this.type = type; }
}
/*
* (c) Copyright 2010 Epimorphics Ltd.
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
* IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
* OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
* IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
* NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/