Skip to content

Instantly share code, notes, and snippets.

@mak18ah
Created June 25, 2015 14:48
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save mak18ah/b695beef449e0f29c802 to your computer and use it in GitHub Desktop.
Save mak18ah/b695beef449e0f29c802 to your computer and use it in GitHub Desktop.
package manager example
package org.test.utils.article;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;
import java.net.URLConnection;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;
import java.util.Map;
import javax.jcr.Node;
import javax.jcr.PathNotFoundException;
import javax.jcr.RepositoryException;
import javax.jcr.Session;
import org.test.content.article.ArticleVO;
import org.test.utils.FtpClientUtil;
import org.test.utils.TimeFormatter;
import org.test.utils.xml.ExternalFeedContentHandler;
import org.test.utils.xml.XMLParser;
import org.apache.commons.codec.binary.Base64;
import org.apache.jackrabbit.JcrConstants;
import org.apache.sling.api.resource.ResourceResolver;
import org.apache.sling.jcr.api.SlingRepository;
import org.apache.sling.jcr.resource.JcrResourceResolverFactory;
import org.osgi.util.tracker.ServiceTracker;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import com.day.cq.packaging.CQPackageManager;
import com.day.cq.replication.ReplicationActionType;
import com.day.cq.replication.ReplicationException;
import com.day.cq.replication.Replicator;
import com.day.cq.wcm.api.Page;
import com.day.cq.wcm.api.PageManager;
import com.day.jcr.vault.fs.api.PathFilterSet;
import com.day.jcr.vault.fs.config.DefaultWorkspaceFilter;
import com.day.jcr.vault.packaging.JcrPackage;
import com.day.jcr.vault.packaging.JcrPackageManager;
import com.day.jcr.vault.packaging.PackageException;
import com.day.jcr.vault.packaging.PackagingService;
public class ImportFeed {
private static Logger log = LoggerFactory.getLogger(ImportFeed.class);
private static final String MAIN_REPORT_PATH = "/etc/test/config";
/**
* Package the articles imported and activate the package.
* @param group
* @param name
* @param session
* @param packSvc
* @param replicator
* @throws Exception
*/
public static void replicateArticles(String group, String name, Session session,
CQPackageManager packSvc, Replicator replicator, List<String> articlePaths) {
log.info("start replicating the package: " + name + " under group: " + group);
if (articlePaths.size() > 0) {
JcrPackageManager pkgMgr = PackagingService.getPackageManager(session);
//create the package
try {
JcrPackage pkg = packSvc.createPackage(session, group, name);
packSvc.ensureVersion(pkg);
//add path filters
DefaultWorkspaceFilter filter = new DefaultWorkspaceFilter();
for (String path : articlePaths) {
filter.add(new PathFilterSet(path));
}
articlePaths.clear();
pkg.getDefinition().setFilter(filter, true);
pkgMgr.assemble(pkg, null);
try {
replicator.replicate(session, ReplicationActionType.ACTIVATE, pkg.getNode().getPath());
} catch (Exception e) {
log.error("[job:run] cannot replicate package", e);
}
} catch (PackageException e1) {
log.error("PackageException:", e1);
} catch (RepositoryException e) {
log.error("RepositoryException", e);
} catch (IOException e) {
log.error("IOException", e);
}
}
}
/**
* Get the XML from FTP
* @param resource FTP site
* @param username username
* @param password password
* @param source server file
* @return InputStream or null
*/
public static InputStream getXMLFromFTP(String resource, String username, String password, String source) {
FtpClientUtil ftpUtil = new FtpClientUtil();
try {
ftpUtil.connect(resource, username, password);
return ftpUtil.downloadFileAsStream(source);
} catch (Exception e) {
log.error("Cannot connect to FTP site.", e);
} finally {
try {
ftpUtil.disconnect();
} catch (IOException e) {
log.error("Problem disconnecting from FTP site.", e);
}
}
return null;
}
/**
* Get the XML from RSS feed. Supports Basic authentication only.
* @param resource
* @param username null if no authentication needed
* @param password null if no authentication needed
* @return
*/
public static InputStream getXMLFromRSS(String resource, String username, String password) {
log.info("get the XML from: " + resource);
try {
URL url = new URL(resource);
if (username != null && password != null) {
String authString = username + ":" + password;
byte[] authBytes = Base64.encodeBase64(authString.getBytes());
String authEnc = new String(authBytes);
URLConnection connection = url.openConnection();
connection.addRequestProperty("Authorization", "Basic " + authEnc);
return connection.getInputStream();
} else {
return url.openStream();
}
} catch (MalformedURLException e) {
log.error("Invalid url:", e);
} catch (Exception e) {
log.error("Unable to retrieve XML: ", e);
} finally {
}
return null;
}
public static void addReportEntry(Session session, Node reportNode, String articlePath, String message, long duration) {
try {
reportNode.setProperty("Path", articlePath);
reportNode.setProperty("Message", message);
reportNode.setProperty("duration", duration);
session.save();
} catch (Exception e) {
log.error("Exception: ", e);
}
}
public static Node getReportNode(Session session, String name) throws PathNotFoundException, RepositoryException {
Node reportNode = null;
Node parent = (Node) session.getItem(MAIN_REPORT_PATH);
if (!parent.hasNode(name)) {
parent.addNode(name, JcrConstants.NT_UNSTRUCTURED);
}
parent = parent.getNode(name);
String startTime = TimeFormatter.format19.format(System.currentTimeMillis());
String reportName = startTime.replace(" ", "/").replace("-", "/").replace(":", "-");
String[] labels = reportName.split("/");
Node currentNode = parent;
for (String label : labels) {
if (!currentNode.hasNode(label)) {
currentNode.addNode(label, JcrConstants.NT_UNSTRUCTURED);
}
currentNode = currentNode.getNode(label);
}
session.save();
reportNode = currentNode;
return reportNode;
}
public static Replicator getReplicator(ServiceTracker replTracker) {
replTracker.open();
Replicator replicator = (Replicator) replTracker.getService();
return replicator;
}
public static CQPackageManager getPackageManager(ServiceTracker packSvcTracker) {
packSvcTracker.open();
CQPackageManager packSvc = (CQPackageManager) packSvcTracker.getService();
return packSvc;
}
public static SlingRepository getSlingRepository(ServiceTracker repoTracker) {
repoTracker.open();
SlingRepository repo = (SlingRepository) repoTracker.getService();
return repo;
}
/**
*
* @param session
* @param articles
* @param max
* @param ROOT_PATH
* @param reportNode
* @return
*/
public static List<String> createArticles(Session session, List<ArticleVO> articles, int max, String ROOT_PATH, Node reportNode, boolean paginate) {
List<String> articlePaths = new ArrayList<String>();
int i = 0;
//build the articles
for (ArticleVO article : articles) {
long startTime = System.currentTimeMillis();
i++;
if (i > max) {
break;
}
String message = "";
try {
Node parent = (Node) session.getItem(ROOT_PATH + article.getChannelPath());
//news folder
String folder = article.getType().trim().toLowerCase() + "-" + getMonthYear();
Node folderNode = null;
if (parent.hasNode(folder)) {
folderNode = parent.getNode(folder);
} else {
folderNode = PageBuilder.buildTemplate(session, parent, folder, true).getParent();
}
//create the article
if (folderNode != null) {
//setup the article template
Node articleNode = PageBuilder.buildTemplate(session, folderNode, article.getPageTitle(), true);
//build the article
try {
PageBuilder.buildArticlePage(articleNode, article, paginate);
article.setPagePath(articleNode.getPath());
articlePaths.add(article.getPagePath());
message = "Added successfully to " + article.getChannelPath();
} catch (Exception e) {
log.error("Unable to create the page " + articleNode.getPath(), e);
message = "Unable to create the page " + articleNode.getPath() + ", Reason: " + e;
}
}
} catch (PathNotFoundException e) {
message = "PathNotFoundException: " + ROOT_PATH + article.getChannelPath() + ", Reason: " + e;
} catch (RepositoryException e) {
message = "RepositoryException: " + ROOT_PATH + article.getChannelPath() + ", Reason: " + e;
} catch (Exception e) {
message = "Exception, Reason: " + e;
}
//report logging
try {
Node entryNode = reportNode.addNode(System.currentTimeMillis() + "", JcrConstants.NT_UNSTRUCTURED);
addReportEntry(session, entryNode, article.getPagePath(), message, System.currentTimeMillis() - startTime);
} catch (Exception e) {
log.error("Error adding report.", e.getMessage());
}
}
log.info("processed articlePaths-->" + articlePaths.size());
return articlePaths;
}
public static String getMonthYear() {
Calendar cal = Calendar.getInstance();
cal.setTimeInMillis(System.currentTimeMillis());
SimpleDateFormat format = new SimpleDateFormat("MM-yyyy");
String monthYear = format.format(cal.getTime());
return monthYear;
}
/**
* Parse the XML
* @param xmlObj xml
* @param elements elements in the xml to check
* @param attributes attributes in the xml to check
* @param articleFlag vendor name i.e. cq:kiplinger, cq:mochila, cq:illumen
* @return
*/
public static List<ArticleVO> parseXML(InputStream xmlObj, List<String> elements, List<String> attributes, String articleFlag, String source, String action) {
List<ArticleVO> list = new ArrayList<ArticleVO>();
XMLParser parser = new XMLParser(elements, attributes, "article", xmlObj, new ExternalFeedContentHandler());
parser.runParser();
List<Map<String, Object>> result = parser.getResult();
//assign Map<String, Object> to ArticleVO
for (Map<String, Object> map : result) {
ArticleVO article = new ArticleVO();
article.setTitle(map.get("title") != null ? map.get("title").toString() : "");
article.setPageTitle(map.get("page_title") != null ? map.get("page_title").toString() : "");
article.setKicker(map.get("kicker") != null ? map.get("kicker").toString() : "");
article.setDeck(map.get("deck") != null ? map.get("deck").toString() : "");
article.setSeoTitle(map.get("seotitle") != null ? map.get("seotitle").toString() : "");
article.setDescription(map.get("description") != null ? map.get("description").toString() : "");
article.setAuthor(map.get("author") != null ? map.get("author").toString() : "");
article.setSource(map.get("source") != null ? map.get("source").toString() : "");
article.setPublishDate(map.get("publishDate") != null ? map.get("publishDate").toString() : "");
article.setContent(map.get("text") != null ? map.get("text").toString() : "");
article.setKeywords(map.get("keywords") != null ? map.get("keywords").toString() : "");
article.setChannelPath(map.get("channel") != null ? map.get("channel").toString() : "");
article.setDivision(map.get("division") != null ? map.get("division").toString() : "");
article.setType(map.get("type") != null ? map.get("type").toString() : "");
article.setDcSubjects(map.get("dcSubjects") != null ? (List<String[]>) map.get("dcSubjects") : null);
article.setActionPhrase(action);
article.setArticleFlag(articleFlag);
if (source != null) {
article.setUseSource(true);
article.setSource(source);
}
list.add(article);
}
log.info("list-->" + list.size());
return list;
}
public static List<String> getElements() {
List<String> elements = new ArrayList<String>();
elements.add("title");
elements.add("page_title");
elements.add("kicker");
elements.add("deck");
elements.add("seotitle");
elements.add("description");
elements.add("author");
elements.add("source");
elements.add("publishDate");
elements.add("text");
elements.add("keywords");
elements.add("channel");
elements.add("author");
elements.add("division");
elements.add("type");
elements.add("link");
return elements;
}
public static void deleteArticle(String path, JcrResourceResolverFactory resourceResolver, Replicator replicator, Session session, Node reportNode) {
long startTime = System.currentTimeMillis();
String message = "";
if (path != null && !path.equals("")) {
// remove from publish, DELETE works the same
try {
replicator.replicate(session, ReplicationActionType.DEACTIVATE, path);
} catch (ReplicationException e) {
message = "Unable to deactivate " + path + ". " + e;
log.error("during deactivation of [" + path + "]", e);
}
try {
ResourceResolver resolver = resourceResolver.getResourceResolver(session);
PageManager pageManager = resolver.adaptTo(PageManager.class);
Page page = null;
try {
page = pageManager.getPage(path);
} catch (Exception e) {
message = "Page does not exist." + e;
log.error("Page does not exist.", e);
}
log.info("Deleting [" + page.getPath() + "]");
pageManager.delete(page, false);
message = "Deleted " + path;
} catch (Exception e) {
message = "Unable to delete " + path + ". " + e;
log.error("during delete of [" + path + "] in author:", e);
}
//report logging
try {
Node entryNode = reportNode.addNode(System.currentTimeMillis() + "", JcrConstants.NT_UNSTRUCTURED);
addReportEntry(session, entryNode, path, message, System.currentTimeMillis() - startTime);
} catch (Exception e) {
log.error("Error adding report.", e.getMessage());
}
}
}
public static void main(String[] args) {
String startTime = TimeFormatter.format19.format(System.currentTimeMillis());
String reportName = startTime.replace(" ", "/").replace("-", "/").replace(":", "-");
String[] labels = reportName.split("/");
for (String label : labels) {
System.out.println(label);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment