Coverage Report - groovyx.net.http.ContentType
 
Classes in this File Line Coverage Branch Coverage Complexity
ContentType
100%
19/19
100%
4/4
0
 
 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 IANA content-types.  This may be used to specify a 
 30  
  * request or response content-type more easily than specifying the full 
 31  
  * 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  3
 public enum ContentType {
 47  3
         ANY("*/*"),
 48  3
         TEXT("text/plain"), 
 49  3
         JSON("application/json","application/javascript","text/javascript"),
 50  3
         XML("application/xml","text/xml","application/xhtml+xml"),
 51  3
         HTML("text/html"),
 52  3
         URLENC("application/x-www-form-urlencoded"),
 53  3
         BINARY("binary/octet-stream");
 54  
         
 55  
         private final String[] ctStrings;
 56  60
         public String[] getContentTypeStrings() { return ctStrings; } 
 57  132
         @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  12
                 Iterator<String> iter = new ArrayIterator(ctStrings);
 67  12
                 StringBuilder sb = new StringBuilder();
 68  30
                 while ( iter.hasNext() ) {
 69  18
                         sb.append( iter.next() );
 70  18
                         if ( iter.hasNext() ) sb.append( ", " );
 71  
                 }
 72  12
                 return sb.toString();
 73  
         }
 74  
         
 75  21
         private ContentType( String... contentTypes ) {
 76  21
                 this.ctStrings = contentTypes;
 77  21
         }
 78  
 }