Repository: incubator-unomi Updated Branches: refs/heads/master c3c678184 -> 62494af7c
UNOMI-135 : implement downloadMyProfile operation Project: http://git-wip-us.apache.org/repos/asf/incubator-unomi/repo Commit: http://git-wip-us.apache.org/repos/asf/incubator-unomi/commit/62494af7 Tree: http://git-wip-us.apache.org/repos/asf/incubator-unomi/tree/62494af7 Diff: http://git-wip-us.apache.org/repos/asf/incubator-unomi/diff/62494af7 Branch: refs/heads/master Commit: 62494af7cac24114d01d032b425246061762ebcc Parents: c3c6781 Author: Abdelkader Midani <[email protected]> Authored: Fri Nov 3 16:07:10 2017 +0100 Committer: Abdelkader Midani <[email protected]> Committed: Fri Nov 3 16:07:10 2017 +0100 ---------------------------------------------------------------------- wab/pom.xml | 12 +++ .../org/apache/unomi/web/ClientServlet.java | 108 ++++++++++++------- .../resources/OSGI-INF/blueprint/blueprint.xml | 5 +- wab/src/main/resources/org.apache.unomi.web.cfg | 4 +- 4 files changed, 86 insertions(+), 43 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/62494af7/wab/pom.xml ---------------------------------------------------------------------- diff --git a/wab/pom.xml b/wab/pom.xml index a639fec..b71a67a 100644 --- a/wab/pom.xml +++ b/wab/pom.xml @@ -62,6 +62,18 @@ <artifactId>jackson-databind</artifactId> <scope>provided</scope> </dependency> + <dependency> + <groupId>com.fasterxml.jackson.dataformat</groupId> + <artifactId>jackson-dataformat-yaml</artifactId> + <version>${version.jackson.core}</version> + </dependency> + <dependency> + <groupId>org.yaml</groupId> + <artifactId>snakeyaml</artifactId> + <version>1.19</version> + </dependency> + + <!-- Builds on core streaming API; also needs core annotations --> <dependency> <groupId>com.fasterxml.jackson.core</groupId> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/62494af7/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 index c583226..9ce93c3 100644 --- a/wab/src/main/java/org/apache/unomi/web/ClientServlet.java +++ b/wab/src/main/java/org/apache/unomi/web/ClientServlet.java @@ -17,30 +17,21 @@ 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 com.fasterxml.jackson.dataformat.yaml.YAMLFactory; +import org.apache.unomi.api.Profile; +import org.apache.unomi.api.services.ProfileService; 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. @@ -52,7 +43,7 @@ public class ClientServlet extends HttpServlet { private ProfileService profileService; private String profileIdCookieName = "context-profile-id"; - private String profileIdCookieDomain; + private String allowedProfileDownloadFormats; @Override public void init(ServletConfig config) throws ServletException { @@ -68,22 +59,33 @@ public class ClientServlet extends HttpServlet { @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; + String[] pathInfo = req.getPathInfo().substring(1).split("/"); + if (pathInfo != null && pathInfo.length > 0) { + String operation = pathInfo[0]; + String param = pathInfo[1]; + switch (operation) { + case "downloadMyProfile": + if (allowedProfileDownloadFormats.contains(param)) { + donwloadCurrentProfile(req, resp, param); + } else { + resp.setStatus(HttpServletResponse.SC_BAD_REQUEST); + } + break; + default: + resp.setStatus(HttpServletResponse.SC_NOT_FOUND); + } + } else { + resp.setStatus(HttpServletResponse.SC_NOT_FOUND); } + } @Override protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException { } - public void donwloadCurrentProfile(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException { + public void donwloadCurrentProfile(HttpServletRequest request, HttpServletResponse response, String downloadFileType) throws ServletException, IOException { String cookieProfileId = null; Cookie[] cookies = request.getCookies(); for (Cookie cookie : cookies) { @@ -91,36 +93,62 @@ public class ClientServlet extends HttpServlet { cookieProfileId = cookie.getValue(); } } - if(cookieProfileId != null) { + 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"; - } + if (currentProfile != null) { + switch (downloadFileType) { + case "yaml": + prepareYamlFileToDownload(response, currentProfile); + break; + case "json": + prepareJsonFileToDownload(response, currentProfile); + break; + default: + return; - outputStream.write(outputResult.getBytes()); - outputStream.flush(); - outputStream.close(); - } - catch(Exception e) { - e.printStackTrace(); } + } } } + private void prepareJsonFileToDownload(HttpServletResponse response, Profile currentProfile) { + response.setContentType("text/json"); + response.setHeader("Content-Disposition", "attachment; filename=\"" + currentProfile.getItemId() + ".json\""); + try { + ObjectMapper mapper = new ObjectMapper(); + String jsonContent = mapper.writerWithDefaultPrettyPrinter().writeValueAsString(currentProfile.getProperties()); + OutputStream outputStream = response.getOutputStream(); + outputStream.write(jsonContent.getBytes()); + outputStream.flush(); + outputStream.close(); + } catch (Exception e) { + e.printStackTrace(); + } + } + + private void prepareYamlFileToDownload(HttpServletResponse response, Profile currentProfile) { + response.setContentType("text/yaml"); + response.setHeader("Content-Disposition", "attachment; filename=\"" + currentProfile.getItemId() + ".yml\""); + try { + YAMLFactory yf = new YAMLFactory(); + ObjectMapper mapper = new ObjectMapper(yf); + String yamlContent = mapper.writeValueAsString(currentProfile.getProperties()); + OutputStream outputStream = response.getOutputStream(); + outputStream.write(yamlContent.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 setAllowedProfileDownloadFormats(String allowedProfileDownloadFormats) { + this.allowedProfileDownloadFormats = allowedProfileDownloadFormats; } public void setProfileIdCookieName(String profileIdCookieName) { http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/62494af7/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 644fd31..6ae9977 100644 --- a/wab/src/main/resources/OSGI-INF/blueprint/blueprint.xml +++ b/wab/src/main/resources/OSGI-INF/blueprint/blueprint.xml @@ -34,6 +34,7 @@ <cm:property name="contextserver.domain" value=""/> <cm:property name="contextserver.profileIdCookieName" value="context-profile-id"/> <cm:property name="contextserver.profileIdCookieMaxAgeInSeconds" value="31536000"/> <!-- 1 year by default --> + <cm:property name="allowed.profile.download.formats" value="yaml"/> </cm:default-properties> </cm:property-placeholder> @@ -73,13 +74,13 @@ <bean id="clientServlet" class="org.apache.unomi.web.ClientServlet"> <property name="profileService" ref="profileService"/> - <property name="profileIdCookieDomain" value="${web.contextserver.domain}" /> + <property name="allowedProfileDownloadFormats" value="${web.allowed.profile.download.formats}" /> <property name="profileIdCookieName" value="${web.contextserver.profileIdCookieName}"/> </bean> <service id="clientServletService" auto-export="interfaces" ref="clientServlet"> <service-properties> - <entry key="urlPatterns" value="/client"/> + <entry key="urlPatterns" value="/client/*"/> </service-properties> </service> http://git-wip-us.apache.org/repos/asf/incubator-unomi/blob/62494af7/wab/src/main/resources/org.apache.unomi.web.cfg ---------------------------------------------------------------------- diff --git a/wab/src/main/resources/org.apache.unomi.web.cfg b/wab/src/main/resources/org.apache.unomi.web.cfg index 5918f84..aa1c637 100644 --- a/wab/src/main/resources/org.apache.unomi.web.cfg +++ b/wab/src/main/resources/org.apache.unomi.web.cfg @@ -21,4 +21,6 @@ # This setting controls the name of the cookie use to track profiles using Apache Unomi #contextserver.profileIdCookieName=context-profile-id # This setting controls the maximum age of the profile cookie. By default it is set to a year. -#contextserver.profileIdCookieMaxAgeInSeconds=31536000 \ No newline at end of file +#contextserver.profileIdCookieMaxAgeInSeconds=31536000 +#Allowed profile download formats, actually only yaml and json are allowed. +allowed.profile.download.formats=yaml,json \ No newline at end of file
