package org.knoesis.apihut.servlet;
import java.io.IOException;
import java.io.InputStream;
import java.io.UnsupportedEncodingException;
import java.net.URLDecoder;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.apache.solr.common.SolrInputDocument;
import org.knoesis.apihut.util.IDGenerator;
import org.knoesis.apihut.util.JSONResponder;
import org.knoesis.apihut.util.RemoteCallManager;
import org.knoesis.apihut.util.XMLManager;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
/**
* Servlet implementation class UpdateManager This servlet receives an XML and
* points to
*/
public class UpdateManager extends HttpServlet {
private static final long serialVersionUID = 1L;
private DocumentBuilder builder;
private XPathExpression domainrelXpath;
private XPathExpression semrelXpath;
private XPathExpression semclassXpath;
/**
* Default constructor.
*/
public UpdateManager() {
}
@Override
public void init() throws ServletException {
try {
builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
domainrelXpath = xpath.compile("//*[@class='domain-rel']");
semrelXpath = xpath.compile("//*[@class='sem-rel']");
semclassXpath = xpath.compile("//*[@class='sem-class']");
} catch (Exception e) {
throw new ServletException(e);
}
}
/**
* @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
* response)
*/
protected void doGet(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.sendRedirect("use_post.jsp");
}
/**
* @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
* response)
*
* Format of the received XML is
* <data link="url">
* XML content
* </data>
*/
protected void doPost(HttpServletRequest request,
HttpServletResponse response) throws ServletException, IOException {
response.setContentType("application/json");
try {
InputStream xmlStream = request.getInputStream();
Document doc = builder.parse(xmlStream);
String link = doc.getDocumentElement().getAttribute("link");
List<Item> itemsFound = new ArrayList<Item>();
extractItems(doc, itemsFound, domainrelXpath, Item.Type.DOMAIN_REL);
extractItems(doc, itemsFound, semclassXpath, Item.Type.SEM_CLASS);
extractItems(doc, itemsFound, semrelXpath, Item.Type.SEM_REL);
SolrInputDocument solrIndexData = createSolrDocument(link,
itemsFound, doc);
RemoteCallManager.sendUpdate(solrIndexData);
response.getWriter().write(JSONResponder.getSuccessResponse());
} catch (Exception e) {
e.printStackTrace();
response.getWriter().write(JSONResponder.getErrorResponse());
}
// dump
}
/**
* Creates a document to submit to solr
*
* @param link
* @param itemsFound
* @return
* @throws UnsupportedEncodingException
*/
private SolrInputDocument createSolrDocument(String link,
List<Item> itemsFound, Document xmldoc) throws UnsupportedEncodingException {
SolrInputDocument doc = new SolrInputDocument();
doc.addField("id", IDGenerator.getUniqueIdentifier());
doc.addField("link", URLDecoder.decode(link,"UTF-8"));
doc.addField("date", new Date());
for (Item item : itemsFound) {
appendField(doc, item);
appendIndexField(doc, item);
}
doc.addField("content", XMLManager.serializeToString(xmldoc.getDocumentElement())); //change ?
return doc;
}
private void appendIndexField(SolrInputDocument ownerDoc, Item item) {
ownerDoc.addField(Item.TYPE_2_STRING_MAPPING.get(item.type), item.name);
}
private void appendField(SolrInputDocument ownerDoc, Item item) {
ownerDoc.addField(item.name, item.value);
}
/**
*
* @param doc
* @param itemsFound
* @throws XPathExpressionException
*/
private void extractItems(Document doc, List<Item> itemsFound,
XPathExpression xpath, Item.Type type)
throws XPathExpressionException {
// use xpath to get the values
NodeList result = (NodeList) xpath
.evaluate(doc, XPathConstants.NODESET);
for (int i = 0; i < result.getLength(); i++) {
Element element = (Element) result.item(i);
itemsFound.add(new Item(type, element.getAttribute("title"),
element.getTextContent()));
}
}
/**
*
* @author ajith
*
*/
public static class Item {
public enum Type {
DOMAIN_REL, SEM_REL, SEM_CLASS
}
public static Map<Type, String> TYPE_2_STRING_MAPPING;
static {
TYPE_2_STRING_MAPPING = new HashMap<Type, String>();
TYPE_2_STRING_MAPPING.put(Type.DOMAIN_REL, "domain-rel");
TYPE_2_STRING_MAPPING.put(Type.SEM_CLASS, "sem-class");
TYPE_2_STRING_MAPPING.put(Type.SEM_REL, "sem-rel");
}
private Type type;
private String name;
private String value;
public Item(Type type, String name, String value) {
super();
this.type = type;
this.name = name;
this.value = value;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getValue() {
return value;
}
public void setValue(String value) {
this.value = value;
}
@Override
public String toString() {
return "Item [name=" + name + ", value=" + value + "]";
}
}
}