001 /*
002 * Copyright 2003-2008 the original author or authors.
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 asked (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 */
022 package groovyx.net.http;
023
024 import java.util.Iterator;
025
026 import 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:tnichols@enernoc.com'>Tom Nichols</a>
045 */
046 public enum ContentType {
047 /** <code>*/*</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/json</code></li>
062 * <li><code>application/javascript</code></li>
063 * <li><code>text/javascript</code></li>
064 * </ul>
065 */
066 XML("application/xml","text/xml","application/xhtml+xml"),
067 /** <code>text/html</code> */
068 HTML("text/html"),
069 /** <code>application/x-www-form-urlencoded</code> */
070 URLENC("application/x-www-form-urlencoded"),
071 /** <code>application/octet-stream</code> */
072 BINARY("application/octet-stream");
073
074 private final String[] ctStrings;
075 public String[] getContentTypeStrings() { return ctStrings; }
076 @Override public String toString() { return ctStrings[0]; }
077
078 /**
079 * Builds a string to be used as an HTTP <code>Accept</code> header
080 * value, i.e. "application/xml, text/xml"
081 * @return
082 */
083 @SuppressWarnings("unchecked")
084 public String getAcceptHeader() {
085 Iterator<String> iter = new ArrayIterator(ctStrings);
086 StringBuilder sb = new StringBuilder();
087 while ( iter.hasNext() ) {
088 sb.append( iter.next() );
089 if ( iter.hasNext() ) sb.append( ", " );
090 }
091 return sb.toString();
092 }
093
094 private ContentType( String... contentTypes ) {
095 this.ctStrings = contentTypes;
096 }
097 }