001/*
002 * (C) Copyright 2014 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.segment.io;
020
021import java.io.Serializable;
022import java.math.BigDecimal;
023import java.util.Collection;
024import java.util.Date;
025import java.util.HashMap;
026import java.util.Map;
027import org.apache.commons.lang.ClassUtils;
028import org.apache.commons.logging.Log;
029import org.apache.commons.logging.LogFactory;
030
031import org.nuxeo.ecm.core.api.NuxeoPrincipal;
032
033import com.github.segmentio.models.Props;
034
035public class SegmentIODataWrapper {
036
037    public static final String LOGIN_KEY = "login";
038
039    public static final String PRINCIPAL_KEY = "principal";
040
041    public static final String GROUP_KEY_PREFIX = "group_";
042
043    protected static final Log log = LogFactory.getLog(SegmentIODataWrapper.class);
044
045    protected String userId;
046    protected Map<String, Serializable> metadata;
047
048    public SegmentIODataWrapper(NuxeoPrincipal principal, Map<String, Serializable> metadata) {
049        if (metadata == null) {
050            metadata = new HashMap<>();
051        }
052
053        if (metadata.containsKey(PRINCIPAL_KEY) && metadata.get(PRINCIPAL_KEY)!=null) {
054            principal = (NuxeoPrincipal) metadata.get(PRINCIPAL_KEY);
055        }
056
057        userId = principal.getName();
058        if (!metadata.containsKey("email")) {
059            metadata.put("email", principal.getEmail());
060        }
061        if (!metadata.containsKey("firstName")) {
062            metadata.put("firstName", principal.getFirstName());
063        }
064        if (!metadata.containsKey("lastName")) {
065            metadata.put("lastName", principal.getLastName());
066        }
067
068        // allow override
069        if (metadata.containsKey(LOGIN_KEY)) {
070            userId = (String) metadata.get(LOGIN_KEY);
071        }
072
073        this.metadata = metadata;
074    }
075
076    public String getUserId() {
077        return userId;
078    }
079
080    // code copied from com.github.segmentio.models.Props
081    private boolean isPrimitive (Object value) {
082        boolean primitive = false;
083        if (value != null) {
084            Class<?> clazz = value.getClass();
085            // http://stackoverflow.com/questions/709961/determining-if-an-object-is-of-primitive-type
086            primitive = clazz.isPrimitive() || ClassUtils.wrapperToPrimitive(clazz) != null;
087        }
088        return primitive;
089    }
090
091    // code copied from com.github.segmentio.models.Props
092    protected boolean isAllowed(Object value) {
093        if (isPrimitive(value) ||
094            value instanceof String ||
095            value instanceof Date ||
096            value instanceof Props ||
097            value instanceof BigDecimal ||
098            value instanceof Collection ||
099            value instanceof Map ||
100            value instanceof Object[]) {
101            return true;
102        } else {
103            return false;
104        }
105    }
106
107    public Map<String, Serializable> getMetadata() {
108        Map<String, Serializable> map = new HashMap<>();
109        for (String key : metadata.keySet()) {
110            if (!key.startsWith(GROUP_KEY_PREFIX)) {
111                Serializable value = metadata.get(key);
112                if (value!=null) {
113                    if ( isAllowed(value)) {
114                        map.put(key, value);
115                    } else {
116                        map.put(key, value.toString());
117                    }
118                } else {
119                    log.debug("Skip null value for key " + key);
120                }
121            }
122        }
123        return map;
124    }
125
126    public Map<String, Serializable> getGroupMetadata() {
127        Map<String, Serializable> map = new HashMap<>();
128        for (String key : metadata.keySet()) {
129            if (key.startsWith(GROUP_KEY_PREFIX)) {
130                String gKey = key.substring(GROUP_KEY_PREFIX.length());
131                Serializable value = metadata.get(key);
132                if (value!=null) {
133                    if ( isAllowed(value)) {
134                        map.put(gKey, value);
135                    } else {
136                        map.put(gKey, value.toString());
137                    }
138                } else {
139                    log.debug("Skip null value for key " + key);
140                }
141            }
142        }
143        return map;
144    }
145
146
147}