| Classes in this File | Line Coverage | Branch Coverage | Complexity | ||||
| ContentType |
|
| 0.0;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 | 1 | public enum ContentType { |
| 47 | 1 | ANY("*/*"), |
| 48 | 1 | TEXT("text/plain"), |
| 49 | 1 | JSON("application/json","application/javascript","text/javascript"), |
| 50 | 1 | XML("application/xml","text/xml","application/xhtml+xml"), |
| 51 | 1 | HTML("text/html"), |
| 52 | 1 | URLENC("application/x-www-form-urlencoded"), |
| 53 | 1 | BINARY("binary/octet-stream"); |
| 54 | ||
| 55 | private final String[] ctStrings; | |
| 56 | 20 | public String[] getContentTypeStrings() { return ctStrings; } |
| 57 | 44 | @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 | 4 | Iterator<String> iter = new ArrayIterator(ctStrings); |
| 67 | 4 | StringBuilder sb = new StringBuilder(); |
| 68 | 10 | while ( iter.hasNext() ) { |
| 69 | 6 | sb.append( iter.next() ); |
| 70 | 6 | if ( iter.hasNext() ) sb.append( ", " ); |
| 71 | } | |
| 72 | 4 | return sb.toString(); |
| 73 | } | |
| 74 | ||
| 75 | 7 | private ContentType( String... contentTypes ) { |
| 76 | 7 | this.ctStrings = contentTypes; |
| 77 | 7 | } |
| 78 | } |