
com.day.cq.dam.video.servlet.VideoProfileListServlet Maven / Gradle / Ivy
/*
* Copyright 1997-2008 Day Management AG
* Barfuesserplatz 6, 4001 Basel, Switzerland
* All Rights Reserved.
*
* This software is the confidential and proprietary information of
* Day Management AG, ("Confidential Information"). You shall not
* disclose such Confidential Information and shall use it only in
* accordance with the terms of the license agreement you entered into
* with Day.
*/
package com.day.cq.dam.video.servlet;
import java.io.IOException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.servlet.ServletException;
import org.apache.felix.scr.annotations.Component;
import org.apache.felix.scr.annotations.Properties;
import org.apache.felix.scr.annotations.Property;
import org.apache.felix.scr.annotations.Service;
import org.apache.sling.api.SlingConstants;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.apache.sling.commons.json.JSONException;
import org.apache.sling.commons.json.io.JSONWriter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.commons.jcr.JcrConstants;
import com.day.cq.dam.video.VideoProfile;
/**
* Servlet to list all existing Video profiles.
*/
@Component
@Service
@Properties({
@Property(name="sling.servlet.resourceTypes", value="dam/components/video/home"),
@Property(name="sling.servlet.extensions", value="json"),
@Property(name="sling.servlet.selectors", value="profiles.list"),
@Property(name="sling.servlet.methods", value="GET"),
@Property(name="service.description", value="Servlet to list all video profiles")
})
public class VideoProfileListServlet extends SlingAllMethodsServlet {
private static final long serialVersionUID = -780464494271946961L;
private static final String NN_REP_POLICY = "rep:policy";
private final Logger log = LoggerFactory.getLogger(VideoProfileListServlet.class);
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException,
IOException {
response.setContentType("application/json");
response.setCharacterEncoding("utf-8");
JSONWriter writer = new JSONWriter(response.getWriter());
try {
writer.array();
Node videoProfilesRoot = request.getResourceResolver().getResource(VideoProfile.PROFILE_BASE_PATH).adaptTo(Node.class);
loopVideoProfiles(writer, videoProfilesRoot);
writer.endArray();
} catch (RepositoryException e) {
throw new ServletException("Could not lookup video profiles.", e);
} catch (JSONException e) {
throw new ServletException("Error", e);
}
}
private void loopVideoProfiles(JSONWriter writer, Node videoProfilesRoot) throws RepositoryException, JSONException {
NodeIterator profiles = videoProfilesRoot.getNodes();
while (profiles.hasNext()) {
Node profile = profiles.nextNode();
//if node type sling:folder, dismiss (encoding.com folder). Profiles are type "cqPage"
if (profile.isNodeType("sling:Folder")) {
//if node name "analyticsreport" skip. there are not profiles here
if(profile.getName().equals("analyticsreport")) {
continue;
}
//if sling folder (AVS) get children
if(profile.hasNodes()) {
NodeIterator itr = profile.getNodes();
while(itr.hasNext()) {
Node node = (Node) itr.next();
writeAdaptiveVideoProfile(writer, node);
}
}
continue;
}
//if node name "configuration" (for encoding.com)
if(profile.getName().equals("configuration")) {
continue;
}
// if the node name is "rep:policy" (holding ACLs): skip it
if (NN_REP_POLICY.equals(profile.getName())) {
continue;
}
if (profile.isNodeType(JcrConstants.NT_FOLDER)) {
loopVideoProfiles(writer, profile);
}else {
writeVideoProfile(writer, profile);
}
}
}
private void writeVideoProfile(JSONWriter writer, Node profile) throws RepositoryException, JSONException {
writer.object();
writer.key("text").value(getProfileTitle(profile));
writer.key("description").value(getProfileDescription(profile));
writer.key("value").value(getProfileName(profile));
writer.endObject();
}
private void writeAdaptiveVideoProfile(JSONWriter writer, Node profile) throws RepositoryException, JSONException {
writer.object();
writer.key("name").value(profile.getProperty("name").getString());
if(profile.getProperty("description") != null) {
writer.key("description").value(profile.getProperty("description").getString());
}
writer.key("value").value(getAdaptiveVideoProfileDescription(profile));
writer.endObject();
}
private String getProfileTitle(Node profile) throws RepositoryException {
String title = profile.getPath();
try {
Node jcrContent = profile.getNode(JcrConstants.JCR_CONTENT);
title = jcrContent.getProperty(JcrConstants.JCR_TITLE).getString();
} catch (PathNotFoundException e) {
log.warn("trouble resovling jcr:title property from profile's jcr:content node. Fallback to profile path");
}
return title;
}
private Object getProfileDescription(Node profile) throws RepositoryException {
String description = "";
try {
Node jcrContent = profile.getNode(JcrConstants.JCR_CONTENT);
description = jcrContent.getProperty(JcrConstants.JCR_DESCRIPTION).getString();
} catch (PathNotFoundException e) {
log.warn("trouble resovling jcr:description property from profile's jcr:content node. Fallback to empty string");
}
return description;
}
private String getAdaptiveVideoProfileDescription(Node profile) throws RepositoryException {
String description = "";
if(profile.hasNodes()) {
NodeIterator itr = profile.getNodes();
while(itr.hasNext()) {
Node node = (Node) itr.next();
if(description.length()>0) {
description += " | ";
}
description += node.getName();
}
}
return description;
}
private String getProfileName(Node profileNode) throws RepositoryException {
String path = profileNode.getPath();
String resourceType = getResourceType(profileNode);
if (path.startsWith(VideoProfile.PROFILE_BASE_PATH + "/")) {
if (resourceType.equals(VideoProfile.PROFILE_TYPE_CQ)) {
return path.substring(VideoProfile.PROFILE_BASE_PATH.length() + 1);
} else if(resourceType.endsWith(VideoProfile.PROFILE_TYPE_S7)){
return path;
}
}
return profileNode.getPath();
}
private String getResourceType(Node profileNode) throws RepositoryException {
String resourceType = null;
if (profileNode.hasNode(JcrConstants.JCR_CONTENT)) {
Node contentNode = profileNode.getNode(JcrConstants.JCR_CONTENT);
if (contentNode.hasProperty("sling:resourceType")) {
return contentNode.getProperty("sling:resourceType").getString();
}
}
return resourceType;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy