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