package ee.tlu.htk.waramu;
import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import org.apache.log4j.*;
import java.net.URL;
import java.rmi.RemoteException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import org.apache.axis.AxisFault;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.wookie.qti.Test;
import org.wookie.qti.util.DocumentBuilderHelper;
import org.wookie.qti.util.PropertiesHelper;
import ee.tlu.htk.waramu.wssoap.Exception;
import ee.tlu.htk.waramu.wssoap.WaramuPortBindingStub;
import ee.tlu.htk.waramu.wssoap.WaramuServiceLocator;
public class Waramu_Interface {
//TODO all the Waramu stuff
private WaramuPortBindingStub waramuStub;
private String waramuSessionId;
private static final String xmlDeclaration = "\n";
private Logger logger = Logger.getLogger(Waramu_Interface.class.getName());
public Waramu_Interface() {
try {
PropertiesHelper p = new PropertiesHelper();
String waramuLoc = p.getWaramuWsPath();
this.waramuStub = new WaramuPortBindingStub(
new URL(waramuLoc),
new WaramuServiceLocator());
} catch (AxisFault e) {
logger.fatal("Axis fault: "+e.getFaultReason());
} catch (MalformedURLException e) {
logger.fatal("Waramu Interface: URL Malformed");
}
}
/*
* Get WaramuPortBindingStub
*
* @return Instance of WaramuPortBindingStub
*/
private WaramuPortBindingStub getWaramuStub() {
return this.waramuStub;
}
/*
* Regex helper, to extract data from string
*
* @param String pattern, String input string
*
* @return String
*/
private String getDataWithRegex(String pattern, String inputStr) {
try {
Pattern pat = Pattern.compile(pattern, Pattern.DOTALL);
Matcher matcher = pat.matcher(inputStr);
matcher.reset(inputStr);
matcher.find();
return matcher.group(1);
} catch (IllegalStateException e) {
logger.error("Regex could not find match for pattern: \""+pattern+"\"");
}
return null;
}
/*
* Initialize new Waramu session
*
* @param String username, String password
*
* @return sessionID
*/
public String newSession(String uid, String pwd) {
String sessionId = "";
try {
// get new waramu session id
String sess_result = waramuStub.newSession(uid, pwd);
sessionId = this.getDataWithRegex("(.*?)", sess_result);
} catch (NullPointerException e) {
logger.fatal("Creating new Waramu session failed, check Waramu URL");
} catch (RemoteException e) {
logger.fatal("Error occured while communicating Waramu");
}
this.waramuSessionId = sessionId;
return sessionId;
}
/*
* Close Waramu session
*
* @param String session id
*
* @return true if success, false if failed
*/
public boolean closeSession() {
if(this.getSessionId() != null) {
try {
String result = this.getWaramuStub().closeSession(this.getSessionId());
int loggedOut = Integer.parseInt(this.getDataWithRegex("(.*?)", result));
if(loggedOut == 0) {
return true;
}
} catch (RemoteException e) {
logger.warn("Closing Waramu session failed");
} catch (NullPointerException e) {
logger.fatal("Closing Waramu session failed, check Waramu URL");
}
}
return false;
}
/*
* Get current session id
*
* @return session id
*/
private String getSessionId() {
return this.waramuSessionId;
}
/* Do query to Waramu
*
* @param String query
*
* @return HashMap = HashMap doQuery(String queryWord, String locale) {
HashMap queryMap = new HashMap();
TreeMap testItems = new TreeMap();
try {
PropertiesHelper propHelp = new PropertiesHelper();
String queryXmlns = propHelp.getWaramuQueryXmlns();
String[] queries = new String[3];
queries[0] = ""+new String(queryWord.getBytes("ISO-8859-1"), "UTF-8")+"\n";
queries[1] = "<_fulltext xmlns = \""+queryXmlns+"\">"+new String(queryWord.getBytes("ISO-8859-1"), "UTF-8")+"";
queries[2] = ""+new String(queryWord.getBytes("ISO-8859-1"), "UTF-8")+"";
//if namespace was loaded
if(queryXmlns.trim() != "") {
for(int j=0; j\n");
query.append(queries[j]);
query.append("\n");
String result = this.getWaramuStub().listIdentifiers("", this.getSessionId(), query.toString());
Document doc = new DocumentBuilderHelper().createDocument(result);
//iterate through available resourceIds
NodeList fstNmElmntLst = doc.getElementsByTagName("id");
for(int i = 0; i> a = queryMap.entrySet();
Iterator> queryIt = a.iterator();
while(queryIt.hasNext()) {
String resourceId = queryIt.next().getKey();
String response = this.getWaramuStub().getResource("", this.getSessionId(), resourceId);
logger.debug(response);
int statusCode = Integer.parseInt(this.getDataWithRegex("(.*?)", response));
if(statusCode == 0) {
String[] testData = this.parseQueryXML(response, locale);
//testData[0] = attachmentid, testData[1] = title, testData[2] = description
Test newTest = new Test(resourceId, testData[0], testData[1], testData[2]);
//add new item to TreeMap, title as key, this way tests are sorted by Title
testItems.put(testData[1], newTest);
}
}
}
} catch (RemoteException e) {
logger.error("Retriving identifiers list from Waramu failed");
} catch (UnsupportedEncodingException e) {
logger.error("Unsupported encoding occured, while building query string");
}
return testItems;
}
/* Response XML parser for doQuery method
*
* @param XML
*
* @return String attachment id
*/
private String[] parseQueryXML(String xml, String locale) {
String attachmentId = "";
String title = "";
String desc = "";
Document doc = new DocumentBuilderHelper().createDocument(xml);
String[] outArray = new String[3];
//TODO proper attachment id parsing, see WaramuXML
NodeList fstNmElmntLst = doc.getElementsByTagName("id");
NodeList titleNodes = doc.getElementsByTagName("title");
NodeList descNodes = doc.getElementsByTagName("desc");
try {
attachmentId = fstNmElmntLst.item(0).getTextContent();
//default title, is first node we find
title = titleNodes.item(0).getTextContent();
//if we find any matching lang attribute for input locale, then
//set title to that locale
for(int i=0; i 0) {
//default desc, is first one we find
desc = descNodes.item(0).getTextContent();
//if any desc node with lang attribute == locale, use that
for(int i=0; i