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 ANY("*/*"), 048 TEXT("text/plain"), 049 JSON("application/json","application/javascript","text/javascript"), 050 XML("application/xml","text/xml","application/xhtml+xml"), 051 HTML("text/html"), 052 URLENC("application/x-www-form-urlencoded"), 053 BINARY("binary/octet-stream"); 054 055 private final String[] ctStrings; 056 public String[] getContentTypeStrings() { return ctStrings; } 057 @Override public String toString() { return ctStrings[0]; } 058 059 /** 060 * Builds a string to be used as an HTTP <code>Accept</code> header 061 * value, i.e. "application/xml, text/xml" 062 * @return 063 */ 064 @SuppressWarnings("unchecked") 065 public String getAcceptHeader() { 066 Iterator<String> iter = new ArrayIterator(ctStrings); 067 StringBuilder sb = new StringBuilder(); 068 while ( iter.hasNext() ) { 069 sb.append( iter.next() ); 070 if ( iter.hasNext() ) sb.append( ", " ); 071 } 072 return sb.toString(); 073 } 074 075 private ContentType( String... contentTypes ) { 076 this.ctStrings = contentTypes; 077 } 078 }