001/* 002 * (C) Copyright 2014-2018 Nuxeo (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.lang3.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 EMAIL_KEY = "email"; 042 043 public static final String GROUP_KEY_PREFIX = "group_"; 044 045 protected static final Log log = LogFactory.getLog(SegmentIODataWrapper.class); 046 047 protected String userId; 048 049 protected Map<String, Serializable> metadata; 050 051 public SegmentIODataWrapper(NuxeoPrincipal principal, Map<String, Serializable> metadata) { 052 if (metadata == null) { 053 metadata = new HashMap<>(); 054 } 055 056 if (metadata.containsKey(PRINCIPAL_KEY) && metadata.get(PRINCIPAL_KEY) != null) { 057 principal = (NuxeoPrincipal) metadata.get(PRINCIPAL_KEY); 058 } 059 060 userId = principal.getName(); 061 if (!metadata.containsKey(EMAIL_KEY)) { 062 metadata.put(EMAIL_KEY, principal.getEmail()); 063 } 064 065 // allow override 066 if (metadata.containsKey(LOGIN_KEY)) { 067 userId = (String) metadata.get(LOGIN_KEY); 068 } 069 070 this.metadata = metadata; 071 } 072 073 public String getUserId() { 074 return userId; 075 } 076 077 // code copied from com.github.segmentio.models.Props 078 private boolean isPrimitive(Object value) { 079 boolean primitive = false; 080 if (value != null) { 081 Class<?> clazz = value.getClass(); 082 // http://stackoverflow.com/questions/709961/determining-if-an-object-is-of-primitive-type 083 primitive = clazz.isPrimitive() || ClassUtils.wrapperToPrimitive(clazz) != null; 084 } 085 return primitive; 086 } 087 088 // code copied from com.github.segmentio.models.Props 089 protected boolean isAllowed(Object value) { 090 if (isPrimitive(value) || value instanceof String || value instanceof Date || value instanceof Props 091 || value instanceof BigDecimal || value instanceof Collection || value instanceof Map 092 || value instanceof Object[]) { 093 return true; 094 } else { 095 return false; 096 } 097 } 098 099 public Map<String, Serializable> getMetadata() { 100 Map<String, Serializable> map = new HashMap<>(); 101 for (String key : metadata.keySet()) { 102 if (!key.startsWith(GROUP_KEY_PREFIX)) { 103 Serializable value = metadata.get(key); 104 if (value != null) { 105 if (isAllowed(value)) { 106 map.put(key, value); 107 } else { 108 map.put(key, value.toString()); 109 } 110 } else { 111 log.debug("Skip null value for key " + key); 112 } 113 } 114 } 115 return map; 116 } 117 118 public Map<String, Serializable> getGroupMetadata() { 119 Map<String, Serializable> map = new HashMap<>(); 120 for (String key : metadata.keySet()) { 121 if (key.startsWith(GROUP_KEY_PREFIX)) { 122 String gKey = key.substring(GROUP_KEY_PREFIX.length()); 123 Serializable value = metadata.get(key); 124 if (value != null) { 125 if (isAllowed(value)) { 126 map.put(gKey, value); 127 } else { 128 map.put(gKey, value.toString()); 129 } 130 } else { 131 log.debug("Skip null value for key " + key); 132 } 133 } 134 } 135 return map; 136 } 137 138}