001/*
002 * (C) Copyright 2015 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;
022
023import java.io.IOException;
024import java.io.InputStream;
025import java.util.HashMap;
026import java.util.Iterator;
027import java.util.Map;
028
029import org.json.JSONException;
030import org.json.JSONObject;
031import org.json.JSONTokener;
032import org.nuxeo.common.utils.FileUtils;
033
034import com.unboundid.scim.data.BaseResource;
035import com.unboundid.scim.data.ResourceFactory;
036import com.unboundid.scim.marshal.json.JsonUnmarshaller;
037import com.unboundid.scim.schema.ResourceDescriptor;
038import com.unboundid.scim.sdk.InvalidResourceException;
039
040/**
041 * Copy of the original scimsdk class just to change some org.json constructors
042 *
043 * scimsdk uses a custom version of org.json with a different artifactId and
044 * some code differences but with the same namespace
045 *
046 * @author tiry
047 * @since 7.4
048 *
049 */
050public class NXJsonUnmarshaller extends JsonUnmarshaller {
051
052    @Override
053    public <R extends BaseResource> R unmarshal(final InputStream inputStream,
054            final ResourceDescriptor resourceDescriptor,
055            final ResourceFactory<R> resourceFactory)
056            throws InvalidResourceException {
057        try {
058
059            String json = FileUtils.read(inputStream);
060            final JSONObject jsonObject = makeCaseInsensitive(new JSONObject(
061                    new JSONTokener(json)));
062
063            final NXJsonParser parser = new NXJsonParser();
064            return parser.doUnmarshal(jsonObject, resourceDescriptor,
065                    resourceFactory, null);
066        } catch (JSONException e) {
067            throw new InvalidResourceException("Error while reading JSON: "
068                    + e.getMessage(), e);
069        } catch (IOException e) {
070            throw new InvalidResourceException("Error while reading JSON: "
071                    + e.getMessage(), e);
072        }
073    }
074
075    protected JSONObject makeCaseInsensitive(final JSONObject jsonObject)
076            throws JSONException {
077        if (jsonObject == null) {
078            return null;
079        }
080
081        Iterator keys = jsonObject.keys();
082        Map lowerCaseMap = new HashMap(jsonObject.length());
083        while (keys.hasNext()) {
084            String key = keys.next().toString();
085            String lowerCaseKey = toLowerCase(key);
086            lowerCaseMap.put(lowerCaseKey, jsonObject.get(key));
087        }
088
089        return new JSONObject(lowerCaseMap);
090    }
091}