Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
package com.techrevel.dam.core.servlets;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import org.apache.sling.api.SlingHttpServletRequest;
import org.apache.sling.api.SlingHttpServletResponse;
import org.apache.sling.api.resource.Resource;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ValueMap;
import org.apache.sling.api.servlets.HttpConstants;
import org.apache.sling.api.servlets.SlingAllMethodsServlet;
import org.osgi.framework.Constants;
import org.osgi.service.component.annotations.Component;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.fmdita.api.maps.MapUtilities;
import com.day.cq.dam.api.DamConstants;
import com.google.gson.Gson;
@Component(service = Servlet.class, immediate = true, property = {
Constants.SERVICE_DESCRIPTION + "=Fetch all the referenced assets for a map",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=/bin/techrevel/fetchreferencedassets" })
public class FetchReferencedAssetsForMap2 extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1L;
/** Default LOG. */
private static final Logger LOG = LoggerFactory.getLogger(FetchReferencedAssetsForMap2.class);
private static final String REQUEST_PARAM_PATH = "path";
@Override
protected void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) {
String requestedPath = request.getParameter(REQUEST_PARAM_PATH);
ResourceResolver resolver = request.getResourceResolver();
try {
Map<String, Object> resultMap = new HashMap<>();
Gson gson = new Gson();
Session session = resolver.adaptTo(Session.class);
Node ditamapNode = session.getNode(requestedPath);
List<Node> mapDependents = MapUtilities.getAllDependencies(ditamapNode);
List<AssetObject> listOfDitaObject = new ArrayList<>();
mapDependents.forEach(associatedAsset -> {
listOfDitaObject.add(getAssetObject(resolver, associatedAsset));
});
resultMap.put("assets", listOfDitaObject);
String jsonFromMap = gson.toJson(resultMap);
response.getWriter().print(jsonFromMap);
} catch (IOException | RepositoryException e) {
LOG.error("Exception while fetching dependencies of DITA Map {} : {}", requestedPath, e);
} catch (Exception e) {
LOG.error("Exception while fetching dependencies of DITA Map {} : {}", requestedPath, e);
}
}
private AssetObject getAssetObject(ResourceResolver resolver, Node associatedAsset) {
AssetObject ditaObject = new AssetObject();
try {
Resource assetMetadataResource = resolver
.getResource(associatedAsset.getPath() + "/jcr:content/metadata");
if (null != assetMetadataResource) {
ValueMap valueMap = assetMetadataResource.getValueMap();
String title = valueMap.get(DamConstants.DC_TITLE, String.class);
if (title != null) {
ditaObject.setTitle(title);
} else {
// Fallback to filename if title is not set. Can happen with images...
ditaObject.setTitle(associatedAsset.getName());
}
}
ditaObject.setName(associatedAsset.getName());
ditaObject.setPath(associatedAsset.getPath());
} catch (RepositoryException e) {
LOG.error("Repository Exception while fetching asset details : {}", e.getMessage());
} catch (Exception e) {
LOG.error("Exception while fetching asset details : {}", e.getMessage());
}
return ditaObject;
}
}
class AssetObject {
String title;
String name;
String docState;
String path;
String checkedOutBy;
public String getCheckedOutBy() {
return checkedOutBy;
}
public void setCheckedOutBy(String checkedOutBy) {
this.checkedOutBy = checkedOutBy;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDocState() {
return docState;
}
public void setDocState(String docState) {
this.docState = docState;
}
public String getPath() {
return path;
}
public void setPath(String path) {
this.path = path;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.