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;
028
029import org.apache.commons.logging.Log;
030import org.apache.commons.logging.LogFactory;
031import org.nuxeo.ecm.core.api.NuxeoPrincipal;
032import org.nuxeo.ecm.webengine.model.WebObject;
033import org.nuxeo.ecm.webengine.model.impl.ModuleRoot;
034import org.nuxeo.runtime.api.Framework;
035import org.nuxeo.segment.io.SegmentIO;
036import org.nuxeo.segment.io.SegmentIOUserFilter;
037
038import com.github.segmentio.models.Providers;
039
040@WebObject(type = "segmentIOScriptResource")
041@Path("/segmentIO")
042public class SegmentIOScriptResource extends ModuleRoot {
043
044    protected static final Log log = LogFactory.getLog(SegmentIOScriptResource.class);
045
046    @GET
047    public Object anonymous() {
048        // here the security context won't be initialized
049        // so there is no need to try to find the user
050        return buildScript(null);
051    }
052
053    @GET
054    @Path("user/{login}")
055    public Object signed(@PathParam("login")
056    String login) {
057        return buildScript(login);
058    }
059
060    protected String buildJsonProvidersOptions() {
061        SegmentIO segmentIO = Framework.getLocalService(SegmentIO.class);
062        Providers providers = segmentIO.getProviders();
063
064        StringBuffer p = new StringBuffer();
065        for (String pname : providers.keySet()) {
066            p.append("\"" + pname + "\"");
067            p.append(" : ");
068            p.append(providers.get(pname).toString());
069            p.append(" , ");
070        }
071
072        StringBuffer json = new StringBuffer("{");
073        json.append(p.toString());
074
075        json.append("\"providers\": {");
076        json.append(p.toString());
077        json.append("} }");
078
079        return json.toString();
080    }
081
082    protected String buildJsonBlackListedLogins() {
083        SegmentIO segmentIO = Framework.getLocalService(SegmentIO.class);
084
085        SegmentIOUserFilter filters = segmentIO.getUserFilters();
086        StringBuffer json = new StringBuffer("[");
087        if (filters != null) {
088            if (!filters.isEnableAnonymous()) {
089                String anonymous = filters.getAnonymousUserId();
090                if (anonymous != null) {
091                    json.append("'");
092                    json.append(anonymous);
093                    json.append("',");
094                }
095            }
096            for (String login : filters.getBlackListedUsers()) {
097                json.append("'");
098                json.append(login);
099                json.append("',");
100            }
101        }
102        json.append("]");
103        return json.toString();
104    }
105
106    protected Object buildScript(String login) {
107
108        SegmentIO segmentIO = Framework.getLocalService(SegmentIO.class);
109
110        Map<String, Object> ctx = new HashMap<String, Object>();
111        ctx.put("writeKey", segmentIO.getWriteKey());
112
113        NuxeoPrincipal principal = (NuxeoPrincipal) getContext().getPrincipal();
114
115        if (principal != null) {
116            if (login == null) {
117                ctx.put("principal", principal);
118            } else if (principal.getName().equals(login)) {
119                ctx.put("principal", principal);
120            }
121        }
122        ctx.put("providers", buildJsonProvidersOptions());
123        ctx.put("blackListedLogins", buildJsonBlackListedLogins());
124
125        return getView("script").args(ctx);
126    }
127
128    @GET
129    @Path("test")
130    public Object test() {
131        return getView("test");
132    }
133
134    @GET
135    @Path("marketo/{email}")
136    @Produces("text/plain")
137    public String getMarketo(@PathParam("email")
138    String email) {
139        if (email==null || email.isEmpty()) {
140            return "";
141        }
142        return MarketoHelper.getLeadHash(email);
143    }
144}