Copy link to clipboard
Copied
Could someone help with converting the below hard coded servlet for dynamic drop down. I want to remove the hard coding in this
import java.io.IOException;
import java.util.ArrayList;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import javax.jcr.NoSuchWorkspaceException;
import javax.jcr.Node;
import javax.jcr.NodeIterator;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import javax.servlet.Servlet;
import javax.servlet.ServletException;
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.ResourceMetadata;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.api.resource.ResourceResolverFactory;
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.osgi.service.component.annotations.Reference;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.adobe.cq.commerce.common.ValueMapDecorator;
import com.adobe.granite.ui.components.ds.DataSource;
import com.adobe.granite.ui.components.ds.SimpleDataSource;
import com.adobe.granite.ui.components.ds.ValueMapResource;
service = Servlet.class,
property = {
Constants.SERVICE_DESCRIPTION + "=Simple Demo Servlet",
"sling.servlet.methods=" + HttpConstants.METHOD_GET, "sling.servlet.paths=" + "/bin/AnswerPathDropdown",
"sling.servlet.extensions=" + "sample",
}
)
public class AnswerPathDropdown extends SlingAllMethodsServlet {
private static final long serialVersionUID = 1;
Logger logger = LoggerFactory.getLogger(this.getClass());
ResourceResolverFactory resolverFactory;
public void doGet(SlingHttpServletRequest request, SlingHttpServletResponse response) throws ServletException, IOException {
Map<String, Object> param = new HashMap<String, Object>();
param.put(ResourceResolverFactory.SUBSERVICE, "datawrite");
logger.info("Entered Dynamic dropdown servlet");
try {
String urlPath = null;
Enumeration<?> values = request.getHeaders("Referer");
logger.info("values is " + values);
if (values != null) {
while (values.hasMoreElements()) {
urlPath = (String) values.nextElement();
logger.info("urlPath is " + urlPath);
}
}
String currentpagepath = urlPath.substring(0, urlPath.length() - 5);
String currentPath = null;
if (currentpagepath.contains("http://localhost:4502/editor.html/")) {
currentPath = currentpagepath.replaceAll("http://localhost:4502/editor.html", "");
}
ResourceResolver resolver = resolverFactory.getServiceResourceResolver(param);
Session session = resolver.adaptTo(Session.class);
logger.info("session is " + session);
Node rootNode = session.getNode(currentPath + "/jcr:content/pathPar");
logger.info("rootNode is " + rootNode.getPath());
NodeIterator nodeItr = rootNode.getNodes();
List<Resource> fakeResourceList = new ArrayList<Resource>();
ValueMap vm = null;
logger.info("nodeItr is " + nodeItr);
ValueMap vm1 = new ValueMapDecorator(new HashMap<String, Object>());
vm1.put("value", "");
vm1.put("text", "");
fakeResourceList.add(new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), "nt:unstructured", vm1));
int i = 1;
String path = null;
while (nodeItr.hasNext()) {
Node cNode = nodeItr.nextNode();
vm = new ValueMapDecorator(new HashMap<String, Object>());
String path1 = cNode.getPath();
path = path1.substring(path1.lastIndexOf('/') + 1);
String Text = "Path" + i;
vm.put("value", path);
vm.put("text", Text);
logger.info("path is " + path);
i++;
fakeResourceList.add(new ValueMapResource(request.getResourceResolver(), new ResourceMetadata(), "nt:unstructured", vm));
}
Object ds = new SimpleDataSource(fakeResourceList.iterator());
request.setAttribute(DataSource.class.getName(), ds);
} catch (NoSuchWorkspaceException e) {
logger.info("NoSuchWorkspaceException Exception is " + e);
} catch (RepositoryException e) {
logger.info("RepositoryException Exception is " + e);
} catch (org.apache.sling.api.resource.LoginException e) {
logger.info("Login Exception is " + e);
}
}
}
This forum is about JavaScript in Adobe Acrobat. Your question has nothing to do with that.
Copy link to clipboard
Copied
This forum is about JavaScript in Adobe Acrobat. Your question has nothing to do with that.