001/*
002 * Copyright 2008-2011 Thomas Nichols.  http://blog.thomnichols.org
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * You are receiving this code free of charge, which represents many hours of
017 * effort from other individuals and corporations.  As a responsible member
018 * of the community, you are encouraged (but not required) to donate any
019 * enhancements or improvements back to the community under a similar open
020 * source license.  Thank you. -TMN
021 */
022package groovyx.net.http;
023
024import java.util.Iterator;
025
026import org.apache.commons.collections.iterators.ArrayIterator;
027
028/**
029 * Enumeration of common <a href="http://www.iana.org/assignments/media-types/">IANA</a>
030 * content-types.  This may be used to specify a request or response
031 * content-type more easily than specifying the full string each time.  i.e.
032 * <pre>
033 * http.request( GET, JSON ) {...}</pre>
034 *
035 * Is roughly equivalent to:
036 * <pre>
037 * http.request( GET, 'application/json' )</pre>
038 *
039 * The only difference being, equivalent content-types (i.e.
040 * <code>application/xml</code> and <code>text/xml</code> are all added to the
041 * request's <code>Accept</code> header.  By default, all equivalent content-types
042 * are handled the same by the {@link EncoderRegistry} and {@link ParserRegistry}
043 * as well.
044 * @author <a href='mailto:tomstrummer+httpbuilder@gmail.com'>Tom Nichols</a>
045 */
046public enum ContentType {
047    /** <code>&#42;/*</code> */
048    ANY("*/*"),
049    /** <code>text/plain</code> */
050    TEXT("text/plain"),
051    /**
052     * <ul>
053     *  <li><code>application/json</code></li>
054     *  <li><code>application/javascript</code></li>
055     *  <li><code>text/javascript</code></li>
056     * </ul>
057     */
058    JSON("application/json","application/javascript","text/javascript"),
059    /**
060     * <ul>
061     *  <li><code>application/xml</code></li>
062     *  <li><code>text/xml</code></li>
063     *  <li><code>application/xhtml+xml</code></li>
064     *  <li><code>application/atom+xml</code></li>
065     * </ul>
066     */
067    XML("application/xml","text/xml","application/xhtml+xml","application/atom+xml"),
068    /** <code>text/html</code> */
069    HTML("text/html"),
070    /** <code>application/x-www-form-urlencoded</code> */
071    URLENC("application/x-www-form-urlencoded"),
072    /** <code>application/octet-stream</code> */
073    BINARY("application/octet-stream");
074
075    private final String[] ctStrings;
076    public String[] getContentTypeStrings() { return ctStrings; }
077    @Override public String toString() { return ctStrings[0]; }
078
079    /**
080     * Builds a string to be used as an HTTP <code>Accept</code> header
081     * value, i.e. "application/xml, text/xml"
082     * @return
083     */
084    @SuppressWarnings("unchecked")
085    public String getAcceptHeader() {
086        Iterator<String> iter = new ArrayIterator(ctStrings);
087        StringBuilder sb = new StringBuilder();
088        while ( iter.hasNext() ) {
089            sb.append( iter.next() );
090            if ( iter.hasNext() ) sb.append( ", " );
091        }
092        return sb.toString();
093    }
094
095    private ContentType( String... contentTypes ) {
096        this.ctStrings = contentTypes;
097    }
098}