001package ca.uhn.fhir.rest.api;
002
003/*
004 * #%L
005 * HAPI FHIR - Core Library
006 * %%
007 * Copyright (C) 2014 - 2021 Smile CDR, Inc.
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 ca.uhn.fhir.context.FhirContext;
024import ca.uhn.fhir.parser.IParser;
025import org.apache.commons.lang3.ObjectUtils;
026
027import java.util.Collections;
028import java.util.HashMap;
029import java.util.Map;
030
031import static org.apache.commons.lang3.StringUtils.isBlank;
032
033public enum EncodingEnum {
034
035        JSON(Constants.CT_FHIR_JSON, Constants.CT_FHIR_JSON_NEW, Constants.FORMAT_JSON) {
036                @Override
037                public IParser newParser(FhirContext theContext) {
038                        return theContext.newJsonParser();
039                }
040        },
041
042        XML(Constants.CT_FHIR_XML, Constants.CT_FHIR_XML_NEW, Constants.FORMAT_XML) {
043                @Override
044                public IParser newParser(FhirContext theContext) {
045                        return theContext.newXmlParser();
046                }
047        },
048
049        RDF(Constants.CT_RDF_TURTLE_LEGACY, Constants.CT_RDF_TURTLE, Constants.FORMAT_TURTLE) {
050                @Override
051                public IParser newParser(FhirContext theContext) {
052                        return theContext.newRDFParser();
053                }
054        },
055
056        ;
057
058        /**
059         * "json"
060         */
061        public static final String JSON_PLAIN_STRING = "json";
062
063        /**
064         * "rdf"
065         */
066        public static final String RDF_PLAIN_STRING = "rdf";
067
068
069        /**
070         * "xml"
071         */
072        public static final String XML_PLAIN_STRING = "xml";
073
074        private static Map<String, EncodingEnum> ourContentTypeToEncoding;
075        private static Map<String, EncodingEnum> ourContentTypeToEncodingLegacy;
076        private static Map<String, EncodingEnum> ourContentTypeToEncodingStrict;
077
078        static {
079                ourContentTypeToEncoding = new HashMap<>();
080                ourContentTypeToEncodingLegacy = new HashMap<>();
081
082                for (EncodingEnum next : values()) {
083                        ourContentTypeToEncoding.put(next.myResourceContentTypeNonLegacy, next);
084                        ourContentTypeToEncoding.put(next.myResourceContentTypeLegacy, next);
085                        ourContentTypeToEncodingLegacy.put(next.myResourceContentTypeLegacy, next);
086
087                        /*
088                         * See #346
089                         */
090                        ourContentTypeToEncoding.put(next.myResourceContentTypeNonLegacy.replace('+', ' '), next);
091                        ourContentTypeToEncoding.put(next.myResourceContentTypeLegacy.replace('+', ' '), next);
092                        ourContentTypeToEncodingLegacy.put(next.myResourceContentTypeLegacy.replace('+', ' '), next);
093
094                }
095
096                // Add before we add the lenient ones
097                ourContentTypeToEncodingStrict = Collections.unmodifiableMap(new HashMap<>(ourContentTypeToEncoding));
098
099                /*
100                 * These are wrong, but we add them just to be tolerant of other
101                 * people's mistakes
102                 */
103                ourContentTypeToEncoding.put("application/json", JSON);
104                ourContentTypeToEncoding.put("application/xml", XML);
105                ourContentTypeToEncoding.put("application/fhir+turtle", RDF);
106                ourContentTypeToEncoding.put("application/x-turtle", RDF);
107                ourContentTypeToEncoding.put("text/json", JSON);
108                ourContentTypeToEncoding.put("text/xml", XML);
109                ourContentTypeToEncoding.put("text/turtle", RDF);
110
111                /*
112                 * Plain values, used for parameter values
113                 */
114                ourContentTypeToEncoding.put(JSON_PLAIN_STRING, JSON);
115                ourContentTypeToEncoding.put(XML_PLAIN_STRING, XML);
116                ourContentTypeToEncoding.put(RDF_PLAIN_STRING, RDF);
117                ourContentTypeToEncoding.put(Constants.FORMAT_TURTLE, RDF);
118
119                ourContentTypeToEncodingLegacy = Collections.unmodifiableMap(ourContentTypeToEncodingLegacy);
120
121        }
122
123        private String myFormatContentType;
124        private String myResourceContentTypeLegacy;
125        private String myResourceContentTypeNonLegacy;
126
127        EncodingEnum(String theResourceContentTypeLegacy, String theResourceContentType, String theFormatContentType) {
128                myResourceContentTypeLegacy = theResourceContentTypeLegacy;
129                myResourceContentTypeNonLegacy = theResourceContentType;
130                myFormatContentType = theFormatContentType;
131        }
132
133        /**
134         * Returns <code>xml</code> or <code>json</code> as used on the <code>_format</code> search parameter
135         */
136        public String getFormatContentType() {
137                return myFormatContentType;
138        }
139
140        /**
141         * Will return application/xml+fhir style
142         */
143        public String getResourceContentType() {
144                return myResourceContentTypeLegacy;
145        }
146
147        /**
148         * Will return application/fhir+xml style
149         */
150        public String getResourceContentTypeNonLegacy() {
151                return myResourceContentTypeNonLegacy;
152        }
153
154        public abstract IParser newParser(final FhirContext theContext);
155
156        public static EncodingEnum detectEncoding(final String theBody) {
157                EncodingEnum retVal = detectEncodingNoDefault(theBody);
158                retVal = ObjectUtils.defaultIfNull(retVal, EncodingEnum.XML);
159                return retVal;
160        }
161
162        public static EncodingEnum detectEncodingNoDefault(String theBody) {
163                EncodingEnum retVal = null;
164                for (int i = 0; i < theBody.length() && retVal == null; i++) {
165                        switch (theBody.charAt(i)) {
166                                case '<':
167                                        retVal = EncodingEnum.XML;
168                                        break;
169                                case '{':
170                                        retVal = EncodingEnum.JSON;
171                                        break;
172                        }
173                }
174                return retVal;
175        }
176
177        /**
178         * Returns the encoding for a given content type, or <code>null</code> if no encoding
179         * is found.
180         * <p>
181         * <b>This method is lenient!</b> Things like "application/xml" will return {@link EncodingEnum#XML}
182         * even if the "+fhir" part is missing from the expected content type. Also,
183         * spaces are treated as a plus (i.e. "application/fhir json" will be treated as
184         * "application/fhir+json" in order to account for unescaped spaces in URL
185         * parameters)
186         * </p>
187         */
188        public static EncodingEnum forContentType(final String theContentType) {
189                String contentTypeSplitted = getTypeWithoutCharset(theContentType);
190                if (contentTypeSplitted == null) {
191                        return null;
192                } else {
193                        return ourContentTypeToEncoding.get(contentTypeSplitted );
194                }
195        }
196
197
198        /**
199         * Returns the encoding for a given content type, or <code>null</code> if no encoding
200         * is found.
201         * <p>
202         * <b>This method is NOT lenient!</b> Things like "application/xml" will return <code>null</code>
203         * </p>
204         *
205         * @see #forContentType(String)
206         */
207        public static EncodingEnum forContentTypeStrict(final String theContentType) {
208                String contentTypeSplitted = getTypeWithoutCharset(theContentType);
209                if (contentTypeSplitted == null) {
210                        return null;
211                } else {
212                        return ourContentTypeToEncodingStrict.get(contentTypeSplitted);
213                }
214        }
215
216        static String getTypeWithoutCharset(final String theContentType) {
217                if (isBlank(theContentType)) {
218                        return null;
219                } else {
220
221                        int start = 0;
222                        for (; start < theContentType.length(); start++) {
223                                if (theContentType.charAt(start) != ' ') {
224                                        break;
225                                }
226                        }
227                        int end = start;
228                        for (; end < theContentType.length(); end++) {
229                                if (theContentType.charAt(end) == ';') {
230                                        break;
231                                }
232                        }
233                        for (; end > start; end--) {
234                                if (theContentType.charAt(end - 1) != ' ') {
235                                        break;
236                                }
237                        }
238
239                        String retVal = theContentType.substring(start, end);
240
241                        if (retVal.contains(" ")) {
242                                retVal = retVal.replace(' ', '+');
243                        }
244                        return retVal;
245                }
246        }
247
248        /**
249         * Is the given type a FHIR legacy (pre-DSTU3) content type?
250         */
251        public static boolean isLegacy(final String theContentType) {
252                String contentTypeSplitted = getTypeWithoutCharset(theContentType);
253                if (contentTypeSplitted == null) {
254                        return false;
255                } else {
256                        return ourContentTypeToEncodingLegacy.containsKey(contentTypeSplitted);
257                }
258        }
259
260
261}