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