Skip to content

Commit

Permalink
*) adding soap function to import yacy bookmarks from xml or html (tr…
Browse files Browse the repository at this point in the history
…ansfered via soap attachments)

*) soapHandler: code cleanup for service deployment

git-svn-id: https://svn.berlios.de/svnroot/repos/yacy/trunk@2915 6c8d7289-2bf4-0310-a012-ef5d649a1542
  • Loading branch information
theli committed Nov 5, 2006
1 parent c3ac9aa commit f37e204
Show file tree
Hide file tree
Showing 7 changed files with 536 additions and 142 deletions.
135 changes: 88 additions & 47 deletions source/de/anomic/data/bookmarksDB.java
Expand Up @@ -44,6 +44,9 @@
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.UnsupportedEncodingException;
import java.io.Writer;
import java.text.ParseException;
import java.text.SimpleDateFormat;
Expand Down Expand Up @@ -433,58 +436,92 @@ public Iterator bookmarkIterator(boolean up){
public void addBookmark(String url, String title, ArrayList tags){

}
public void importFromBookmarks(URL baseURL, String input, String tag, boolean importPublic){
HashMap links=new HashMap();
Iterator it;
String url,title;
Bookmark bm;
HashSet tags=listManager.string2hashset(tag); //this allow multiple default tags
try {
//load the links
htmlFilterContentScraper scraper = new htmlFilterContentScraper(baseURL);
//OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
Writer writer= new htmlFilterWriter(null,null,scraper, null, false);
serverFileUtils.write(input,writer);
writer.close();
links = (HashMap) scraper.getAnchors();
} catch (IOException e) {}
it=links.keySet().iterator();
while(it.hasNext()){
url=(String) it.next();
title=(String) links.get(url);
if(title.equals("")){//cannot be displayed
title=url;
}
bm=new Bookmark(url);
bm.setProperty(Bookmark.BOOKMARK_TITLE, title);
bm.setTags(tags);
bm.setPublic(importPublic);
saveBookmark(bm);
}
flushBookmarkCache();
flushTagCache();

public int importFromBookmarks(URL baseURL, String input, String tag, boolean importPublic){
try {
// convert string to inputstream
ByteArrayInputStream byteIn = new ByteArrayInputStream(input.getBytes("UTF-8"));
InputStreamReader reader = new InputStreamReader(byteIn,"UTF-8");

// import stream
return this.importFromBookmarks(baseURL,reader,tag,importPublic);
} catch (UnsupportedEncodingException e) {
return 0;
}
}
public int importFromBookmarks(URL baseURL, InputStreamReader input, String tag, boolean importPublic){
int importCount = 0;

HashMap links=new HashMap();
Iterator it;
String url,title;
Bookmark bm;
HashSet tags=listManager.string2hashset(tag); //this allow multiple default tags
try {
//load the links
htmlFilterContentScraper scraper = new htmlFilterContentScraper(baseURL);
//OutputStream os = new htmlFilterOutputStream(null, scraper, null, false);
Writer writer= new htmlFilterWriter(null,null,scraper, null, false);
serverFileUtils.copy(input,writer);
writer.close();
links = (HashMap) scraper.getAnchors();
} catch (IOException e) {}
it=links.keySet().iterator();
while(it.hasNext()){
url=(String) it.next();
title=(String) links.get(url);
if(title.equals("")){//cannot be displayed
title=url;
}
bm=new Bookmark(url);
bm.setProperty(Bookmark.BOOKMARK_TITLE, title);
bm.setTags(tags);
bm.setPublic(importPublic);
saveBookmark(bm);

importCount++;
}
flushBookmarkCache();
flushTagCache();

return importCount;
}
public void importFromXML(String input, boolean importPublic){
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc=builder.parse(new ByteArrayInputStream(input.getBytes()));
parseXMLimport(doc, importPublic);
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}


public int importFromXML(String input, boolean importPublic){
try {
// convert string to inputstream
ByteArrayInputStream byteIn = new ByteArrayInputStream(input.getBytes("UTF-8"));

// import stream
return this.importFromXML(byteIn,importPublic);
} catch (UnsupportedEncodingException e) {
return 0;
}
}
public void parseXMLimport(Node doc, boolean importPublic){

public int importFromXML(InputStream input, boolean importPublic){
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
factory.setValidating(false);
factory.setNamespaceAware(false);
DocumentBuilder builder;
try {
builder = factory.newDocumentBuilder();
Document doc=builder.parse(input);
return parseXMLimport(doc, importPublic);
} catch (ParserConfigurationException e) {
} catch (SAXException e) {
} catch (IOException e) {
}
return 0;

}
public int parseXMLimport(Node doc, boolean importPublic){
int importCount = 0;
if(doc.getNodeName()=="post"){
NamedNodeMap attributes = doc.getAttributes();
String url=attributes.getNamedItem("href").getNodeValue();
if(url.equals("")){
return;
return 0;
}
Bookmark bm=new Bookmark(url);
String tagsString="";
Expand Down Expand Up @@ -520,15 +557,19 @@ public void parseXMLimport(Node doc, boolean importPublic){
}
bm.setPublic(importPublic);
saveBookmark(bm);

importCount++;
}
NodeList children=doc.getChildNodes();
if(children != null){
for (int i=0; i<children.getLength(); i++) {
parseXMLimport(children.item(i), importPublic);
importCount += parseXMLimport(children.item(i), importPublic);
}
}
flushBookmarkCache();
flushTagCache();

return importCount;
}
/**
* Subclass, which stores an Tag
Expand Down
137 changes: 70 additions & 67 deletions source/de/anomic/soap/httpdSoapHandler.java
Expand Up @@ -181,7 +181,9 @@ public final class httpdSoapHandler extends httpdAbstractHandler implements http

/* ===============================================================
* Other object fields
* =============================================================== */
* =============================================================== */
private static final Object initSync = new Object();

private serverClassLoader provider = null;
private HashMap templates;
private serverSwitch switchboard;
Expand All @@ -191,32 +193,7 @@ public final class httpdSoapHandler extends httpdAbstractHandler implements http
private File htTemplatePath;

private static Properties additionalServices = null;

/*
* Creating and configuring an apache Axis server.
* This is only needed once.
*/
static {
// create an Axis server
serverLog.logInfo("SOAP","Init soap engine ...");
engine = new AxisServer();

// setting some options ...
engine.setShouldSaveConfig(false);

serverLog.logInfo("SOAP","Deploying default services ...");
for (int i=0; i < defaultServices.length; i++) {
String[] nextService = defaultServices[i].split("=");
serverLog.logInfo("SOAP","Deploying service " + nextService[0] + ":" + nextService[1]);
String deploymentStr = serviceDeploymentString
.replaceAll("@serviceName@", nextService[0])
.replaceAll("@className@", nextService[1]);

// deploy the service
deployService(deploymentStr,engine);
}
}

/**
* Constructor of this class
* @param theSwitchboard
Expand All @@ -242,50 +219,76 @@ public httpdSoapHandler(serverSwitch theSwitchboard) {
this.provider = new serverClassLoader(/*this.getClass().getClassLoader()*/);
}

if (this.templates == null) this.templates = loadTemplates(this.htTemplatePath);

synchronized (engine) {
if (additionalServices == null) {
additionalServices = new Properties();

// getting the property filename containing the file list
String fileName = theSwitchboard.getConfig("soap.serviceDeploymentList","");
if (fileName.length() > 0) {
BufferedInputStream fileInput = null;
try {
File deploymentFile = new File(theSwitchboard.getRootPath(),fileName);
fileInput = new BufferedInputStream(new FileInputStream(deploymentFile));

// load property list
additionalServices.load(fileInput);
fileInput.close();

// loop through and deploy services
if (additionalServices.size() > 0) {
Enumeration serviceNameEnum = additionalServices.keys();
while (serviceNameEnum.hasMoreElements()) {
String serviceName = (String) serviceNameEnum.nextElement();
String className = additionalServices.getProperty(serviceName);

String deploymentStr = serviceDeploymentString
.replaceAll("@serviceName@", serviceName)
.replaceAll("@className@", className);

// deploy the service
deployService(deploymentStr,engine);
}
}
} catch (Exception e) {
this.theLogger.logSevere("Unable to deploy additional services: " + e.getMessage(), e);
} finally {
if (fileInput != null) try { fileInput.close(); } catch (Exception e){/* ignore this */}
}
}
}
if (this.templates == null) {
this.templates = loadTemplates(this.htTemplatePath);
}

}
// deploy default soap services
if (engine == null) synchronized (initSync) { deployDefaultServices(); }

// init additional soap services
if (additionalServices == null) synchronized (initSync) { deployAdditionalServices(); }
}

private void deployDefaultServices() {
// create an Axis server
this.theLogger.logInfo("Init soap engine ...");
engine = new AxisServer();

// setting some options ...
engine.setShouldSaveConfig(false);

this.theLogger.logInfo("Deploying default services ...");
for (int i=0; i < defaultServices.length; i++) {
String[] nextService = defaultServices[i].split("=");
this.theLogger.logInfo("Deploying service " + nextService[0] + ": " + nextService[1]);
String deploymentStr = serviceDeploymentString
.replaceAll("@serviceName@", nextService[0])
.replaceAll("@className@", nextService[1]);

// deploy the service
deployService(deploymentStr,engine);
}
}

private void deployAdditionalServices() {
additionalServices = new Properties();

// getting the property filename containing the file list
String fileName = this.switchboard.getConfig("soap.serviceDeploymentList","");
if (fileName.length() > 0) {
BufferedInputStream fileInput = null;
try {
File deploymentFile = new File(this.switchboard.getRootPath(),fileName);
fileInput = new BufferedInputStream(new FileInputStream(deploymentFile));

// load property list
additionalServices.load(fileInput);
fileInput.close();

// loop through and deploy services
if (additionalServices.size() > 0) {
Enumeration serviceNameEnum = additionalServices.keys();
while (serviceNameEnum.hasMoreElements()) {
String serviceName = (String) serviceNameEnum.nextElement();
String className = additionalServices.getProperty(serviceName);

String deploymentStr = serviceDeploymentString
.replaceAll("@serviceName@", serviceName)
.replaceAll("@className@", className);

// deploy the service
deployService(deploymentStr,engine);
}
}
} catch (Exception e) {
this.theLogger.logSevere("Unable to deploy additional services: " + e.getMessage(), e);
} finally {
if (fileInput != null) try { fileInput.close(); } catch (Exception e){/* ignore this */}
}
}
}

private InputStream getBodyInputStream(httpHeader requestHeader, PushbackInputStream body) throws SoapException{
InputStream input;

Expand Down

0 comments on commit f37e204

Please sign in to comment.