diff --git a/source/de/anomic/data/bookmarksDB.java b/source/de/anomic/data/bookmarksDB.java index f89dfd917..85c0dbf2f 100644 --- a/source/de/anomic/data/bookmarksDB.java +++ b/source/de/anomic/data/bookmarksDB.java @@ -44,6 +44,9 @@ package de.anomic.data; 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; @@ -433,58 +436,92 @@ public class bookmarksDB { 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=""; @@ -520,15 +557,19 @@ public class bookmarksDB { } bm.setPublic(importPublic); saveBookmark(bm); + + importCount++; } NodeList children=doc.getChildNodes(); if(children != null){ for (int i=0; i 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; diff --git a/source/de/anomic/soap/services/BookmarkService.java b/source/de/anomic/soap/services/BookmarkService.java index f826f2d21..8b6a8d2e0 100644 --- a/source/de/anomic/soap/services/BookmarkService.java +++ b/source/de/anomic/soap/services/BookmarkService.java @@ -46,14 +46,25 @@ package de.anomic.soap.services; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; import java.util.HashMap; import java.util.HashSet; +import javax.activation.DataHandler; +import javax.xml.soap.SOAPException; + import org.apache.axis.AxisFault; +import org.apache.axis.Message; +import org.apache.axis.MessageContext; +import org.apache.axis.attachments.AttachmentPart; +import org.apache.axis.attachments.Attachments; import org.w3c.dom.Document; import de.anomic.data.bookmarksDB; import de.anomic.index.indexURL; +import de.anomic.net.URL; import de.anomic.plasma.plasmaSwitchboard; import de.anomic.server.serverObjects; import de.anomic.soap.AbstractService; @@ -67,12 +78,58 @@ public class BookmarkService extends AbstractService { private static final String TEMPLATE_BOOKMARK_LIST_XML = "xml/bookmarks/posts/get.xml"; private static final String TEMPLATE_BOOKMARK_TAGS_XML = "xml/bookmarks/tags/get.xml"; + /** + * @return a handler to the YaCy Bookmark DB + */ + private bookmarksDB getBookmarkDB() { + assert (this.switchboard != null) : "Switchboard object is null"; + assert (this.switchboard instanceof plasmaSwitchboard) : "Incorrect switchboard object"; + assert (((plasmaSwitchboard)this.switchboard).bookmarksDB != null) : "Bookmark DB is null"; + + return ((plasmaSwitchboard)this.switchboard).bookmarksDB; + } + + /** + * @return returns the input stream of a soap attachment + * @throws AxisFault if no attachment was found or attachments are not supported + * @throws SOAPException if attachment decoding didn't work + * @throws IOException on attachment read errors + */ + private InputStream getAttachmentInputstream() throws AxisFault, SOAPException, IOException { + // get the current message context + MessageContext msgContext = MessageContext.getCurrentContext(); + + // getting the request message + Message reqMsg = msgContext.getRequestMessage(); + + // getting the attachment implementation + Attachments messageAttachments = reqMsg.getAttachmentsImpl(); + if (messageAttachments == null) { + throw new AxisFault("Attachments not supported"); + } + + int attachmentCount= messageAttachments.getAttachmentCount(); + if (attachmentCount == 0) + throw new AxisFault("No attachment found"); + else if (attachmentCount != 1) + throw new AxisFault("Too many attachments as expected."); + + // getting the attachments + AttachmentPart[] attachments = (AttachmentPart[])messageAttachments.getAttachments().toArray(new AttachmentPart[attachmentCount]); + + // getting the content of the attachment + DataHandler dh = attachments[0].getDataHandler(); + + // return the input stream + return dh.getInputStream(); + } + /** * Converts an array of tags into a HashSet * @param tagArray the array of tags * @return the HashSet */ - private HashSet stringArrayToHashSet(String[] tagArray) { + private HashSet tagArrayToHashSet(String[] tagArray) { HashSet tagSet = new HashSet(); if (tagArray == null) return tagSet; @@ -84,6 +141,25 @@ public class BookmarkService extends AbstractService { return tagSet; } + /** + * Converts the tag array into a space separated list + * @param tagArray the tag array + * @return space separated list of tags + */ + private String tagArrayToSepString(String[] tagArray, String sep) { + StringBuffer buffer = new StringBuffer(); + + for (int i=0; i < tagArray.length; i++) { + String nextTag = tagArray[i].trim(); + if (nextTag.length() > 0) { + if (i > 0) buffer.append(sep); + buffer.append(nextTag); + } + } + + return buffer.toString(); + } + /** * To publish a YaCy news message that a new bookmark was added. * This is only done for public bookmarks @@ -98,14 +174,14 @@ public class BookmarkService extends AbstractService { if (tags == null || tags.length == 0) tags = new String[]{"unsorted"}; // convert tag array into hashset - HashSet tagSet = stringArrayToHashSet(tags); + String tagString = tagArrayToSepString(tags," "); // create a news message HashMap map = new HashMap(); map.put("url", url.replace(',', '|')); map.put("title", title.replace(',', ' ')); map.put("description", description.replace(',', ' ')); - map.put("tags", tagSet.toString().replace(',', ' ')); + map.put("tags", tagString); yacyCore.newsPool.publishMyNews(new yacyNewsRecord("bkmrkadd", map)); } @@ -132,7 +208,7 @@ public class BookmarkService extends AbstractService { // convert tag array into hashset HashSet tagSet = null; - if (tags != null) tagSet = stringArrayToHashSet(tags); + if (tags != null) tagSet = tagArrayToHashSet(tags); // set properties if (url != null) bookmark.setProperty(bookmarksDB.Bookmark.BOOKMARK_URL, url); @@ -167,7 +243,7 @@ public class BookmarkService extends AbstractService { if (url == null || url.length()==0) throw new IllegalArgumentException("The url must not be null or empty"); // create new bookmark object - bookmarksDB.Bookmark bookmark = ((plasmaSwitchboard)this.switchboard).bookmarksDB.createBookmark(url); + bookmarksDB.Bookmark bookmark = getBookmarkDB().createBookmark(url); // set bookmark properties if(bookmark != null){ @@ -176,7 +252,7 @@ public class BookmarkService extends AbstractService { // create a news message publisNewBookmarkNews(url,title,description,tags); } - ((plasmaSwitchboard)this.switchboard).bookmarksDB.saveBookmark(bookmark); + getBookmarkDB().saveBookmark(bookmark); } else { throw new AxisFault("Unable to create bookmark. Unknown reason."); } @@ -197,7 +273,21 @@ public class BookmarkService extends AbstractService { if (urlHash == null || urlHash.length()==0) throw new IllegalArgumentException("The url hash must not be null or empty"); // delete bookmark - ((plasmaSwitchboard)this.switchboard).bookmarksDB.removeBookmark(urlHash); + getBookmarkDB().removeBookmark(urlHash); + } + + public void deleteBookmarksByHash(String[] urlHashs) throws AxisFault { + // extracting the message context + extractMessageContext(AUTHENTICATION_NEEDED); + if (urlHashs == null || urlHashs.length==0) throw new IllegalArgumentException("The url hash array must not be null or empty"); + + for (int i=0; i < urlHashs.length; i++) { + String urlHash = urlHashs[i]; + if (urlHash == null || urlHash.length()==0) throw new IllegalArgumentException("The url hash at position " + i + " is null or empty."); + + // delete bookmark + getBookmarkDB().removeBookmark(urlHash); + } } public void deleteBookmark(String url) throws AxisFault { @@ -210,6 +300,22 @@ public class BookmarkService extends AbstractService { this.deleteBookmarkByHash(hash); } + public void deleteBookmarks(String[] urls) throws AxisFault { + if (urls == null || urls.length==0) throw new IllegalArgumentException("The url array must not be null or empty"); + + String[] hashs = new String[urls.length]; + for (int i=0; i < urls.length; i++) { + String url = urls[i]; + if (url == null || url.length()==0) throw new IllegalArgumentException("The url at position " + i + " is null or empty"); + + // generating the url hash + hashs[i] = indexURL.urlHash(url); + } + + // delete url + this.deleteBookmarksByHash(hashs); + } + /** * Function to change the properties of a bookmark stored in the YaCy Bookmark DB * @@ -238,7 +344,7 @@ public class BookmarkService extends AbstractService { if (urlHash == null || urlHash.length()==0) throw new IllegalArgumentException("The url hash must not be null or empty"); // getting the bookmark - bookmarksDB.Bookmark bookmark = ((plasmaSwitchboard)this.switchboard).bookmarksDB.getBookmark(urlHash); + bookmarksDB.Bookmark bookmark = getBookmarkDB().getBookmark(urlHash); if (bookmark == null) throw new AxisFault("Bookmark with hash " + urlHash + " could not be found"); // edit values @@ -261,7 +367,7 @@ public class BookmarkService extends AbstractService { if (oldTagName == null || oldTagName.length()==0) throw new IllegalArgumentException("The old tag name not be null or empty"); if (newTagName == null || newTagName.length()==0) throw new IllegalArgumentException("The nwe tag name not be null or empty"); - boolean success = ((plasmaSwitchboard)this.switchboard).bookmarksDB.renameTag(oldTagName,newTagName); + boolean success = getBookmarkDB().renameTag(oldTagName,newTagName); if (!success) throw new AxisFault("Unable to rename tag. Unknown reason."); } @@ -324,4 +430,60 @@ public class BookmarkService extends AbstractService { // sending back the result to the client return this.convertContentToXML(result); } + + /** + * Function to import YaCy from XML (transfered via SOAP Attachment).
+ * This function expects a xml document in the same format as returned by + * function {@link #getBookmarkList(String, String)}. + * @param isPublic specifies if the imported bookmarks are public or local + * @return the amount of imported bookmarks + * + * @throws SOAPException if there is no data in the attachment + * @throws IOException if authentication failed or the attachment could not be read + */ + public int importBookmarkXML(boolean isPublic) throws SOAPException, IOException { + + // extracting the message context + extractMessageContext(AUTHENTICATION_NEEDED); + + // getting the attachment input stream + InputStream xmlIn = getAttachmentInputstream(); + + // import bookmarks + int importCount = getBookmarkDB().importFromXML(xmlIn, isPublic); + + // return amount of imported bookmarks + return importCount; + } + + /** + * Function to import YaCy from a html document (transfered via SOAP Attachment).
+ * This function expects a well formed html document. + * + * @param baseURL the base url. This is needed to generate absolut URLs from relative URLs + * @param tags a list of bookmarks tags that should be assigned to the new bookmarks + * @param isPublic specifies if the imported bookmarks are public or local + * @return the amount of imported bookmarks + * + * @throws SOAPException if there is no data in the attachment + * @throws IOException if authentication failed or the attachment could not be read + */ + public int importHtmlBookmarkFile(String baseURL, String[] tags, boolean isPublic) throws SOAPException, IOException { + if (tags == null || tags.length == 0) tags = new String[]{"unsorted"}; + + // extracting the message context + extractMessageContext(AUTHENTICATION_NEEDED); + + // getting the attachment input stream + InputStream htmlIn = getAttachmentInputstream(); + InputStreamReader htmlReader = new InputStreamReader(htmlIn,"UTF-8"); + + // import bookmarks + URL theBaseURL = new URL(baseURL); + String tagList = tagArrayToSepString(tags,","); + int importCount = getBookmarkDB().importFromBookmarks(theBaseURL,htmlReader, tagList,isPublic); + + // return amount of imported bookmarks + return importCount; + } } diff --git a/source/de/anomic/soap/services/bookmarks.wsdl b/source/de/anomic/soap/services/bookmarks.wsdl index 336abf2e9..fdd701b40 100644 --- a/source/de/anomic/soap/services/bookmarks.wsdl +++ b/source/de/anomic/soap/services/bookmarks.wsdl @@ -4,187 +4,272 @@ Built on Apr 22, 2006 (06:55:48 PDT)--> + + + + + + - + + + + + + + + + - + + + + + + + + + + + - + + + + - + - + + + + + - + + + + + + - + + + + + + + + + + - + - + - + + + + + + + + + + + + + + + + + + + + + - + - + + + + + + + + + + + + + + + + + + + + + - + diff --git a/test/de/anomic/soap/services/AdminServiceTest.java b/test/de/anomic/soap/services/AdminServiceTest.java index 52167542a..e98e58d76 100644 --- a/test/de/anomic/soap/services/AdminServiceTest.java +++ b/test/de/anomic/soap/services/AdminServiceTest.java @@ -27,9 +27,9 @@ public class AdminServiceTest extends AbstractServiceTest { private HashMap getMessageForwardingProperties(Document xml) throws DOMException, TransformerException { HashMap result = new HashMap(); - result.put("msgForwardingEnabled",Boolean.valueOf(XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingEnabled").getTextContent())); - result.put("msgForwardingCmd",XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingCmd").getTextContent()); - result.put("msgForwardingTo",XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingTo").getTextContent()); + result.put("msgForwardingEnabled",Boolean.valueOf(XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingEnabled").getFirstChild().getNodeValue())); + result.put("msgForwardingCmd",XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingCmd").getFirstChild().getNodeValue()); + result.put("msgForwardingTo",XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingTo").getFirstChild().getNodeValue()); return result; } @@ -48,9 +48,9 @@ public class AdminServiceTest extends AbstractServiceTest { Document xml = ((AdminService)service).getMessageForwarding(); // check if values are equal - assertEquals(msgEnabled,Boolean.valueOf(XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingEnabled").getTextContent())); - assertEquals(msgCmd,XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingCmd").getTextContent()); - assertEquals(msgTo,XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingTo").getTextContent()); + assertEquals(msgEnabled,Boolean.valueOf(XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingEnabled").getFirstChild().getNodeValue())); + assertEquals(msgCmd,XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingCmd").getFirstChild().getNodeValue()); + assertEquals(msgTo,XPathAPI.selectSingleNode(xml,"/msgForwarding/msgForwardingTo").getFirstChild().getNodeValue()); // print it out System.out.println(XMLUtils.DocumentToString(xml)); diff --git a/test/de/anomic/soap/services/BookmarkServiceTest.java b/test/de/anomic/soap/services/BookmarkServiceTest.java index 9badee2c8..0fdfce40e 100644 --- a/test/de/anomic/soap/services/BookmarkServiceTest.java +++ b/test/de/anomic/soap/services/BookmarkServiceTest.java @@ -1,15 +1,24 @@ package de.anomic.soap.services; +import java.net.MalformedURLException; +import java.rmi.RemoteException; import java.util.Date; +import javax.activation.DataHandler; +import javax.activation.DataSource; import javax.xml.rpc.ServiceException; +import org.apache.axis.attachments.AttachmentPart; +import org.apache.axis.attachments.PlainTextDataSource; +import org.apache.axis.client.Stub; import org.apache.axis.utils.XMLUtils; import org.w3c.dom.Document; import yacy.soap.bookmarks.BookmarkService; import yacy.soap.bookmarks.BookmarkServiceServiceLocator; import de.anomic.data.bookmarksDB; +import de.anomic.index.indexURL; +import de.anomic.net.URL; public class BookmarkServiceTest extends AbstractServiceTest { @@ -47,5 +56,98 @@ public class BookmarkServiceTest extends AbstractServiceTest { // delete tag bm.deleteBookmarkByHash(urlHash); } + + public void testImportHtmlBookmarklist() throws RemoteException { + BookmarkService bm = ((BookmarkService)service); + String[] hashs = new String[5]; + + // generate the html file + StringBuffer xmlStr = new StringBuffer(); + xmlStr.append(""); + for (int i=0; i < hashs.length; i++) { + String url = "/testxmlimport" + i; + String title = "YaCy Bookmark XML Import " + i; + String hash = indexURL.urlHash("http://www.yacy.de"+ url); + + xmlStr.append("\t").append(title).append("\r\n"); + + hashs[i] = hash; + } + xmlStr.append(""); + + // create datasource to hold the attachment content + DataSource data = new PlainTextDataSource("bookmarks.html",xmlStr.toString()); + DataHandler attachmentFile = new DataHandler(data); + + // creating attachment part + AttachmentPart part = new AttachmentPart(); + part.setDataHandler(attachmentFile); + + // setting the attachment format that should be used + ((Stub)service)._setProperty(org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT,org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT_MIME); + ((Stub)service).addAttachment(part); + + // import xml + int importCount = bm.importHtmlBookmarkFile("http://www.yacy.de/",new String[]{"yacy","bookmarks","htmlimport"},false); + assertEquals(hashs.length,importCount); + + // query imported documents + Document xml = bm.getBookmarkList("htmlimport",null); + System.out.println(XMLUtils.DocumentToString(xml)); + + // delete imported URLS + bm.deleteBookmarksByHash(hashs); + } + + public void testImportXML() throws MalformedURLException, RemoteException { + BookmarkService bm = ((BookmarkService)service); + + String dateString = bookmarksDB.dateToiso8601(new Date(System.currentTimeMillis())); + String[] hashs = new String[5]; + + // generate xml document to import + StringBuffer xmlStr = new StringBuffer(); + xmlStr.append("\r\n") + .append("\r\n"); + + for (int i=0; i < hashs.length; i++) { + URL url = new URL("http://www.yacy.de/testxmlimport" + i); + String title = "YaCy Bookmark XML Import " + i; + String description = "YaCy Bookmarkx XML Import junit test with url " + i; + String hash = indexURL.urlHash(url); + String tags = "yacy bookmarks xmlimport"; + + xmlStr.append("\t\r\n"); + + hashs[i] = hash; + } + + xmlStr.append(""); + + // create datasource to hold the attachment content + DataSource data = new PlainTextDataSource("bookmarks.xml",xmlStr.toString()); + DataHandler attachmentFile = new DataHandler(data); + + // creating attachment part + AttachmentPart part = new AttachmentPart(); + part.setDataHandler(attachmentFile); + + // setting the attachment format that should be used + ((Stub)service)._setProperty(org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT,org.apache.axis.client.Call.ATTACHMENT_ENCAPSULATION_FORMAT_MIME); + ((Stub)service).addAttachment(part); + + // import xml + int importCount = bm.importBookmarkXML(false); + assertEquals(hashs.length,importCount); + + // query imported documents + Document xml = bm.getBookmarkList("xmlimport",dateString); + System.out.println(XMLUtils.DocumentToString(xml)); + + // delete imported URLS + bm.deleteBookmarksByHash(hashs); + } } diff --git a/test/de/anomic/soap/services/ServiceTests.java b/test/de/anomic/soap/services/ServiceTests.java index 5b155267b..05a1f752a 100644 --- a/test/de/anomic/soap/services/ServiceTests.java +++ b/test/de/anomic/soap/services/ServiceTests.java @@ -12,6 +12,7 @@ public class ServiceTests { suite.addTestSuite(ShareServiceTest.class); suite.addTestSuite(StatusServiceTest.class); suite.addTestSuite(BlacklistServiceTest.class); + suite.addTestSuite(BookmarkServiceTest.class); //$JUnit-END$ return suite; }