View Javadoc
1   package ca.uhn.fhir.rest.server;
2   
3   /*
4    * #%L
5    * HAPI FHIR - Core Library
6    * %%
7    * Copyright (C) 2014 University Health Network
8    * %%
9    * Licensed under the Apache License, Version 2.0 (the "License");
10   * you may not use this file except in compliance with the License.
11   * You may obtain a copy of the License at
12   * 
13   *      http://www.apache.org/licenses/LICENSE-2.0
14   * 
15   * Unless required by applicable law or agreed to in writing, software
16   * distributed under the License is distributed on an "AS IS" BASIS,
17   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18   * See the License for the specific language governing permissions and
19   * limitations under the License.
20   * #L%
21   */
22  
23  import java.util.HashMap;
24  
25  import ca.uhn.fhir.context.FhirContext;
26  import ca.uhn.fhir.parser.IParser;
27  
28  public enum EncodingEnum {
29  
30  	XML(Constants.CT_FHIR_XML, Constants.CT_ATOM_XML, "application/xml", Constants.FORMAT_XML) {
31  		@Override
32  		public IParser newParser(FhirContext theContext) {
33  			return theContext.newXmlParser();
34  		}
35  	},
36  
37  	JSON(Constants.CT_FHIR_JSON, Constants.CT_FHIR_JSON, Constants.CT_JSON, Constants.FORMAT_JSON) {
38  		@Override
39  		public IParser newParser(FhirContext theContext) {
40  			return theContext.newJsonParser();
41  		}
42  	}
43  
44  	;
45  
46  	private static HashMap<String, EncodingEnum> ourContentTypeToEncoding;
47  
48  	static {
49  		ourContentTypeToEncoding = new HashMap<String, EncodingEnum>();
50  		for (EncodingEnum next : values()) {
51  			ourContentTypeToEncoding.put(next.getBundleContentType(), next);
52  			ourContentTypeToEncoding.put(next.getResourceContentType(), next);
53  			ourContentTypeToEncoding.put(next.getBrowserFriendlyBundleContentType(), next);
54  		}
55  
56  		/*
57  		 * These are wrong, but we add them just to be tolerant of other
58  		 * people's mistakes
59  		 */
60  		ourContentTypeToEncoding.put("application/json", JSON);
61  		ourContentTypeToEncoding.put("application/xml", XML);
62  		ourContentTypeToEncoding.put("text/json", JSON);
63  		ourContentTypeToEncoding.put("text/xml", XML);
64  
65  	}
66  
67  	private String myResourceContentType;
68  	private String myBundleContentType;
69  	private String myBrowserFriendlyContentType;
70  	private String myFormatContentType;
71  
72  	EncodingEnum(String theResourceContentType, String theBundleContentType, String theBrowserFriendlyContentType, String theFormatContentType) {
73  		myResourceContentType = theResourceContentType;
74  		myBundleContentType = theBundleContentType;
75  		myBrowserFriendlyContentType = theBrowserFriendlyContentType;
76  		myFormatContentType = theFormatContentType;
77  	}
78  
79  	public String getRequestContentType() {
80  		return myFormatContentType;
81  	}
82  
83  	public abstract IParser newParser(FhirContext theContext);
84  
85  	public String getBundleContentType() {
86  		return myBundleContentType;
87  	}
88  
89  	public String getResourceContentType() {
90  		return myResourceContentType;
91  	}
92  
93  	public String getBrowserFriendlyBundleContentType() {
94  		return myBrowserFriendlyContentType;
95  	}
96  
97  	public static EncodingEnum forContentType(String theContentType) {
98  		return ourContentTypeToEncoding.get(theContentType);
99  	}
100 
101 	public String getFormatContentType() {
102 		return myFormatContentType;
103 	}
104 
105 }