001/*
002 * (C) Copyright 2015-2016 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     Thierry Delprat
018 */
019package org.nuxeo.scim.server.jaxrs.marshalling;
020
021import static com.unboundid.scim.sdk.StaticUtils.toLowerCase;
022import static java.nio.charset.StandardCharsets.UTF_8;
023
024import java.io.IOException;
025import java.io.InputStream;
026import java.util.HashMap;
027import java.util.Iterator;
028import java.util.Map;
029
030import org.apache.commons.io.IOUtils;
031import org.json.JSONException;
032import org.json.JSONObject;
033import org.json.JSONTokener;
034
035import com.unboundid.scim.data.BaseResource;
036import com.unboundid.scim.data.ResourceFactory;
037import com.unboundid.scim.marshal.json.JsonUnmarshaller;
038import com.unboundid.scim.schema.ResourceDescriptor;
039import com.unboundid.scim.sdk.InvalidResourceException;
040
041/**
042 * Copy of the original scimsdk class just to change some org.json constructors scimsdk uses a custom version of
043 * org.json with a different artifactId and some code differences but with the same namespace
044 *
045 * @author tiry
046 * @since 7.4
047 */
048public class NXJsonUnmarshaller extends JsonUnmarshaller {
049
050    @Override
051    public <R extends BaseResource> R unmarshal(final InputStream inputStream,
052            final ResourceDescriptor resourceDescriptor, final ResourceFactory<R> resourceFactory)
053                    throws InvalidResourceException {
054        try {
055
056            String json = IOUtils.toString(inputStream, UTF_8);
057            final JSONObject jsonObject = makeCaseInsensitive(new JSONObject(new JSONTokener(json)));
058
059            final NXJsonParser parser = new NXJsonParser();
060            return parser.doUnmarshal(jsonObject, resourceDescriptor, resourceFactory, null);
061        } catch (JSONException | IOException  e) {
062            throw new InvalidResourceException("Error while reading JSON: " + e.getMessage(), e);
063        }
064    }
065
066    protected JSONObject makeCaseInsensitive(final JSONObject jsonObject) throws JSONException {
067        if (jsonObject == null) {
068            return null;
069        }
070
071        Iterator<String> keys = jsonObject.keys();
072        Map<String, Object> lowerCaseMap = new HashMap<>(jsonObject.length());
073        while (keys.hasNext()) {
074            String key = keys.next();
075            String lowerCaseKey = toLowerCase(key);
076            lowerCaseMap.put(lowerCaseKey, jsonObject.get(key));
077        }
078
079        return new JSONObject(lowerCaseMap);
080    }
081}