View Javadoc

1   /*
2    * Copyright 2003-2008 the original author or authors.
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    *     http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   *
16   * You are receiving this code free of charge, which represents many hours of
17   * effort from other individuals and corporations.  As a responsible member 
18   * of the community, you are asked (but not required) to donate any 
19   * enhancements or improvements back to the community under a similar open 
20   * source license.  Thank you. -TMN
21   */
22  package groovyx.net.http;
23  
24  import java.util.Iterator;
25  
26  import org.apache.commons.collections.iterators.ArrayIterator;
27  
28  /**
29   * Enumeration of common <a href="http://www.iana.org/assignments/media-types/">IANA</a>
30   * content-types.  This may be used to specify a request or response 
31   * content-type more easily than specifying the full string each time.  i.e.
32   * <pre>
33   * http.request( GET, JSON ) {...}</pre>
34   * 
35   * Is roughly equivalent to:
36   * <pre>
37   * http.request( GET, 'application/json' )</pre>
38   * 
39   * The only difference being, equivalent content-types (i.e. 
40   * <code>application/xml</code> and <code>text/xml</code> are all added to the 
41   * request's <code>Accept</code> header.  By default, all equivalent content-types
42   * are handled the same by the {@link EncoderRegistry} and {@link ParserRegistry}
43   * as well. 
44   * @author <a href='mailto:tnichols@enernoc.com'>Tom Nichols</a>
45   */
46  public enum ContentType {
47  	ANY("*/*"),
48  	TEXT("text/plain"), 
49  	JSON("application/json","application/javascript","text/javascript"),
50  	XML("application/xml","text/xml","application/xhtml+xml"),
51  	HTML("text/html"),
52  	URLENC("application/x-www-form-urlencoded"),
53  	BINARY("binary/octet-stream");
54  	
55  	private final String[] ctStrings;
56  	public String[] getContentTypeStrings() { return ctStrings; } 
57  	@Override public String toString() { return ctStrings[0]; }
58  	
59  	/**
60  	 * Builds a string to be used as an HTTP <code>Accept</code> header 
61  	 * value, i.e. "application/xml, text/xml"
62  	 * @return
63  	 */
64  	@SuppressWarnings("unchecked")
65  	public String getAcceptHeader() {
66  		Iterator<String> iter = new ArrayIterator(ctStrings);
67  		StringBuilder sb = new StringBuilder();
68  		while ( iter.hasNext() ) {
69  			sb.append( iter.next() );
70  			if ( iter.hasNext() ) sb.append( ", " );
71  		}
72  		return sb.toString();
73  	}
74  	
75  	private ContentType( String... contentTypes ) {
76  		this.ctStrings = contentTypes;
77  	}
78  }