001/* 002 * (C) Copyright 2014-2017 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 * Yannis JULIENNE 019 */ 020package org.nuxeo.segment.io.web; 021 022import java.util.HashMap; 023import java.util.Map; 024 025import javax.ws.rs.GET; 026import javax.ws.rs.Path; 027import javax.ws.rs.PathParam; 028import javax.ws.rs.Produces; 029 030import org.apache.commons.lang3.StringUtils; 031import org.apache.commons.logging.Log; 032import org.apache.commons.logging.LogFactory; 033import org.nuxeo.ecm.core.api.NuxeoPrincipal; 034import org.nuxeo.ecm.webengine.model.WebObject; 035import org.nuxeo.ecm.webengine.model.impl.ModuleRoot; 036import org.nuxeo.runtime.api.Framework; 037import org.nuxeo.segment.io.SegmentIO; 038import org.nuxeo.segment.io.SegmentIOUserFilter; 039 040@WebObject(type = "segmentIOScriptResource") 041@Path("/segmentIO") 042@Produces("application/javascript") 043public class SegmentIOScriptResource extends ModuleRoot { 044 045 protected static final Log log = LogFactory.getLog(SegmentIOScriptResource.class); 046 047 public static final String OPTED_OUT_CONDITION_PARAM = "optedOutCondition"; 048 049 @GET 050 public Object anonymous() { 051 return buildScript(null); 052 } 053 054 @GET 055 @Path("user/{login}") 056 public Object signed(@PathParam("login") String login) { 057 return buildScript(login); 058 } 059 060 protected String buildJsonBlackListedLogins() { 061 SegmentIO segmentIO = Framework.getService(SegmentIO.class); 062 063 SegmentIOUserFilter filters = segmentIO.getUserFilters(); 064 StringBuffer json = new StringBuffer("["); 065 if (filters != null) { 066 if (!filters.isEnableAnonymous()) { 067 String anonymous = filters.getAnonymousUserId(); 068 if (anonymous != null) { 069 json.append("'"); 070 json.append(anonymous); 071 json.append("',"); 072 } 073 } 074 for (String login : filters.getBlackListedUsers()) { 075 json.append("'"); 076 json.append(login); 077 json.append("',"); 078 } 079 } 080 json.append("]"); 081 return json.toString(); 082 } 083 084 protected Object buildScript(String login) { 085 086 SegmentIO segmentIO = Framework.getService(SegmentIO.class); 087 088 Map<String, Object> ctx = new HashMap<String, Object>(); 089 ctx.put("writeKey", segmentIO.getWriteKey()); 090 ctx.put("debugMode", Boolean.toString(segmentIO.isDebugMode())); 091 // get opted out condition 092 String optedOutCondition = segmentIO.getGlobalParameters().get(OPTED_OUT_CONDITION_PARAM); 093 if(StringUtils.isBlank(optedOutCondition)){ 094 optedOutCondition = "false"; 095 } 096 ctx.put("optedOutCondition", optedOutCondition); 097 098 NuxeoPrincipal principal = getContext().getPrincipal(); 099 100 if (principal != null) { 101 if (login == null || principal.getName().equals(login)) { 102 ctx.put("principal", principal); 103 } 104 } 105 ctx.put("blackListedLogins", buildJsonBlackListedLogins()); 106 107 return getView("script").args(ctx); 108 } 109 110 @GET 111 @Path("test") 112 public Object test() { 113 return getView("test"); 114 } 115 116 @GET 117 @Path("marketo/{email}") 118 @Produces("text/plain") 119 public String getMarketo(@PathParam("email") String email) { 120 if (email == null || email.isEmpty()) { 121 return ""; 122 } 123 return MarketoHelper.getLeadHash(email); 124 } 125}