001package ca.uhn.fhir.rest.server;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2016 University Health Network
008 * %%
009 * Licensed under the Apache License, Version 2.0 (the "License");
010 * you may not use this file except in compliance with the License.
011 * You may obtain a copy of the License at
012 * 
013 *      http://www.apache.org/licenses/LICENSE-2.0
014 * 
015 * Unless required by applicable law or agreed to in writing, software
016 * distributed under the License is distributed on an "AS IS" BASIS,
017 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
018 * See the License for the specific language governing permissions and
019 * limitations under the License.
020 * #L%
021 */
022
023import java.util.HashMap;
024
025import ca.uhn.fhir.context.FhirContext;
026import ca.uhn.fhir.parser.IParser;
027
028public enum EncodingEnum {
029
030        XML(Constants.CT_FHIR_XML, Constants.CT_ATOM_XML, Constants.FORMAT_XML) {
031                @Override
032                public IParser newParser(FhirContext theContext) {
033                        return theContext.newXmlParser();
034                }
035        },
036
037        JSON(Constants.CT_FHIR_JSON, Constants.CT_FHIR_JSON, Constants.FORMAT_JSON) {
038                @Override
039                public IParser newParser(FhirContext theContext) {
040                        return theContext.newJsonParser();
041                }
042        }
043
044        ;
045
046        private static HashMap<String, EncodingEnum> ourContentTypeToEncoding;
047
048        static {
049                ourContentTypeToEncoding = new HashMap<String, EncodingEnum>();
050                for (EncodingEnum next : values()) {
051                        ourContentTypeToEncoding.put(next.getBundleContentType(), next);
052                        ourContentTypeToEncoding.put(next.getResourceContentType(), next);
053                }
054
055                /*
056                 * These are wrong, but we add them just to be tolerant of other
057                 * people's mistakes
058                 */
059                ourContentTypeToEncoding.put("application/json", JSON);
060                ourContentTypeToEncoding.put("application/xml", XML);
061                ourContentTypeToEncoding.put("application/fhir+xml", XML);
062                ourContentTypeToEncoding.put("text/json", JSON);
063                ourContentTypeToEncoding.put("text/xml", XML);
064
065        }
066
067        private String myResourceContentType;
068        private String myBundleContentType;
069        private String myFormatContentType;
070
071        EncodingEnum(String theResourceContentType, String theBundleContentType, String theFormatContentType) {
072                myResourceContentType = theResourceContentType;
073                myBundleContentType = theBundleContentType;
074                myFormatContentType = theFormatContentType;
075        }
076
077        public String getRequestContentType() {
078                return myFormatContentType;
079        }
080
081        public abstract IParser newParser(FhirContext theContext);
082
083        public String getBundleContentType() {
084                return myBundleContentType;
085        }
086
087        public String getResourceContentType() {
088                return myResourceContentType;
089        }
090
091        public static EncodingEnum forContentType(String theContentType) {
092                return ourContentTypeToEncoding.get(theContentType);
093        }
094
095        public String getFormatContentType() {
096                return myFormatContentType;
097        }
098
099}