001package org.nuxeo.segment.io.web; 002 003import java.util.HashMap; 004import java.util.Map; 005 006import javax.ws.rs.GET; 007import javax.ws.rs.Path; 008import javax.ws.rs.PathParam; 009import javax.ws.rs.Produces; 010 011import org.apache.commons.logging.Log; 012import org.apache.commons.logging.LogFactory; 013import org.nuxeo.ecm.core.api.NuxeoPrincipal; 014import org.nuxeo.ecm.webengine.model.WebObject; 015import org.nuxeo.ecm.webengine.model.impl.ModuleRoot; 016import org.nuxeo.runtime.api.Framework; 017import org.nuxeo.segment.io.SegmentIO; 018import org.nuxeo.segment.io.SegmentIOUserFilter; 019 020import com.github.segmentio.models.Providers; 021 022@WebObject(type = "segmentIOScriptResource") 023@Path("/segmentIO") 024public class SegmentIOScriptResource extends ModuleRoot { 025 026 protected static final Log log = LogFactory.getLog(SegmentIOScriptResource.class); 027 028 @GET 029 public Object anonymous() { 030 // here the security context won't be initialized 031 // so there is no need to try to find the user 032 return buildScript(null); 033 } 034 035 @GET 036 @Path("user/{login}") 037 public Object signed(@PathParam("login") 038 String login) { 039 return buildScript(login); 040 } 041 042 protected String buildJsonProvidersOptions() { 043 SegmentIO segmentIO = Framework.getLocalService(SegmentIO.class); 044 Providers providers = segmentIO.getProviders(); 045 046 StringBuffer p = new StringBuffer(); 047 for (String pname : providers.keySet()) { 048 p.append("\"" + pname + "\""); 049 p.append(" : "); 050 p.append(providers.get(pname).toString()); 051 p.append(" , "); 052 } 053 054 StringBuffer json = new StringBuffer("{"); 055 json.append(p.toString()); 056 057 json.append("\"providers\": {"); 058 json.append(p.toString()); 059 json.append("} }"); 060 061 return json.toString(); 062 } 063 064 protected String buildJsonBlackListedLogins() { 065 SegmentIO segmentIO = Framework.getLocalService(SegmentIO.class); 066 067 SegmentIOUserFilter filters = segmentIO.getUserFilters(); 068 StringBuffer json = new StringBuffer("["); 069 if (filters != null) { 070 if (!filters.isEnableAnonymous()) { 071 String anonymous = filters.getAnonymousUserId(); 072 if (anonymous != null) { 073 json.append("'"); 074 json.append(anonymous); 075 json.append("',"); 076 } 077 } 078 for (String login : filters.getBlackListedUsers()) { 079 json.append("'"); 080 json.append(login); 081 json.append("',"); 082 } 083 } 084 json.append("]"); 085 return json.toString(); 086 } 087 088 protected Object buildScript(String login) { 089 090 SegmentIO segmentIO = Framework.getLocalService(SegmentIO.class); 091 092 Map<String, Object> ctx = new HashMap<String, Object>(); 093 ctx.put("writeKey", segmentIO.getWriteKey()); 094 095 NuxeoPrincipal principal = (NuxeoPrincipal) getContext().getPrincipal(); 096 097 if (principal != null) { 098 if (login == null) { 099 ctx.put("principal", principal); 100 } else if (principal.getName().equals(login)) { 101 ctx.put("principal", principal); 102 } 103 } 104 ctx.put("providers", buildJsonProvidersOptions()); 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") 120 String email) { 121 if (email==null || email.isEmpty()) { 122 return ""; 123 } 124 return MarketoHelper.getLeadHash(email); 125 } 126}