You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@unomi.apache.org by am...@apache.org on 2017/11/02 16:36:25 UTC

incubator-unomi git commit: UNOMI-135 : Create a servlet to carry all operations requested by user

Repository: incubator-unomi
Updated Branches:
  refs/heads/master adbb62a54 -> 3e35842f4


UNOMI-135 : Create a servlet to carry all operations requested by user


Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo
Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/3e35842f
Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/3e35842f
Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/3e35842f

Branch: refs/heads/master
Commit: 3e35842f44b0982700f7770732bfb1c295b589fa
Parents: adbb62a
Author: Abdelkader Midani <am...@apache.org>
Authored: Thu Nov 2 17:36:23 2017 +0100
Committer: Abdelkader Midani <am...@apache.org>
Committed: Thu Nov 2 17:36:23 2017 +0100

----------------------------------------------------------------------
 .../org/apache/unomi/web/ClientServlet.java     | 130 +++++++++++++++++++
 .../resources/OSGI-INF/blueprint/blueprint.xml  |  13 ++
 2 files changed, 143 insertions(+)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/3e35842f/wab/src/main/java/org/apache/unomi/web/ClientServlet.java
----------------------------------------------------------------------
diff --git a/wab/src/main/java/org/apache/unomi/web/ClientServlet.java b/wab/src/main/java/org/apache/unomi/web/ClientServlet.java
new file mode 100644
index 0000000..c583226
--- /dev/null
+++ b/wab/src/main/java/org/apache/unomi/web/ClientServlet.java
@@ -0,0 +1,130 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.unomi.web;
+
+import com.fasterxml.jackson.core.JsonFactory;
+import com.fasterxml.jackson.databind.ObjectMapper;
+import org.apache.commons.io.IOUtils;
+import org.apache.commons.lang3.StringUtils;
+import org.apache.unomi.api.*;
+import org.apache.unomi.api.conditions.Condition;
+import org.apache.unomi.api.services.*;
+import org.apache.unomi.persistence.spi.CustomObjectMapper;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+
+import javax.servlet.ServletConfig;
+import javax.servlet.ServletException;
+import javax.servlet.ServletRequest;
+import javax.servlet.ServletResponse;
+import javax.servlet.http.Cookie;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+import java.io.IOException;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.io.Writer;
+import java.util.*;
+
+/**
+ * A servlet filter to serve a context-specific Javascript containing the current request context object.
+ */
+public class ClientServlet extends HttpServlet {
+
+    private static final Logger logger = LoggerFactory.getLogger(ClientServlet.class.getName());
+    private static final long serialVersionUID = 2928875960103325238L;
+    private ProfileService profileService;
+
+    private String profileIdCookieName = "context-profile-id";
+    private String profileIdCookieDomain;
+
+    @Override
+    public void init(ServletConfig config) throws ServletException {
+        super.init(config);
+        logger.info("ClientServlet initialized.");
+    }
+
+    @Override
+    public void destroy() {
+        super.destroy();
+        logger.info("Client servlet shutdown.");
+    }
+
+    @Override
+    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+        String operation = req.getParameter("op");
+        switch (operation){
+            case "downloadMyProfile" :
+                donwloadCurrentProfile(req, resp);
+                break;
+            default:
+                return;
+
+        }
+    }
+
+    @Override
+    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
+    }
+
+    public void donwloadCurrentProfile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
+        String cookieProfileId = null;
+        Cookie[] cookies = request.getCookies();
+        for (Cookie cookie : cookies) {
+            if (profileIdCookieName.equals(cookie.getName())) {
+                cookieProfileId = cookie.getValue();
+            }
+        }
+        if(cookieProfileId != null) {
+            Profile currentProfile = profileService.load(cookieProfileId);
+            if(currentProfile != null) {
+                response.setContentType("text/csv");
+                response.setHeader("Content-Disposition", "attachment; filename=\""+cookieProfileId+".csv\"");
+                try {
+                    OutputStream outputStream = response.getOutputStream();
+                    String outputResult = "";
+
+                    for (String prop : currentProfile.getProperties().keySet()) {
+                        outputResult += prop + "," + currentProfile.getProperties().get(prop) + "\n";
+                    }
+
+                    outputStream.write(outputResult.getBytes());
+                    outputStream.flush();
+                    outputStream.close();
+                }
+                catch(Exception e) {
+                    e.printStackTrace();
+                }
+            }
+        }
+    }
+
+    public void setProfileService(ProfileService profileService) {
+        this.profileService = profileService;
+    }
+
+    public void setProfileIdCookieDomain(String profileIdCookieDomain) {
+        this.profileIdCookieDomain = profileIdCookieDomain;
+    }
+
+    public void setProfileIdCookieName(String profileIdCookieName) {
+        this.profileIdCookieName = profileIdCookieName;
+    }
+
+}

http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/3e35842f/wab/src/main/resources/OSGI-INF/blueprint/blueprint.xml
----------------------------------------------------------------------
diff --git a/wab/src/main/resources/OSGI-INF/blueprint/blueprint.xml b/wab/src/main/resources/OSGI-INF/blueprint/blueprint.xml
index b38a97a..644fd31 100644
--- a/wab/src/main/resources/OSGI-INF/blueprint/blueprint.xml
+++ b/wab/src/main/resources/OSGI-INF/blueprint/blueprint.xml
@@ -70,4 +70,17 @@
             <entry key="urlPatterns" value="/eventcollector"/>
         </service-properties>
     </service>
+
+    <bean id="clientServlet" class="org.apache.unomi.web.ClientServlet">
+        <property name="profileService" ref="profileService"/>
+        <property name="profileIdCookieDomain" value="${web.contextserver.domain}" />
+        <property name="profileIdCookieName" value="${web.contextserver.profileIdCookieName}"/>
+    </bean>
+
+    <service id="clientServletService" auto-export="interfaces" ref="clientServlet">
+        <service-properties>
+            <entry key="urlPatterns" value="/client"/>
+        </service-properties>
+    </service>
+
 </blueprint>