diff --git a/source/de/anomic/soap/services/BlacklistService.java b/source/de/anomic/soap/services/BlacklistService.java index 1d1a0aaea..741008813 100644 --- a/source/de/anomic/soap/services/BlacklistService.java +++ b/source/de/anomic/soap/services/BlacklistService.java @@ -1,14 +1,23 @@ package de.anomic.soap.services; +import java.io.BufferedReader; import java.io.File; import java.io.FileWriter; import java.io.IOException; +import java.io.InputStreamReader; import java.io.PrintWriter; import java.util.ArrayList; import java.util.Arrays; 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.listManager; @@ -62,7 +71,7 @@ public class BlacklistService extends AbstractService { // extracting the message context extractMessageContext(AUTHENTICATION_NEEDED); - // initialize the list manager + // initialize the list manager initBlacklistManager(); // check if the blacklist already exists @@ -243,10 +252,84 @@ public class BlacklistService extends AbstractService { removeBlacklistItemFromBlacklist(blacklistItem,blacklistName); } + public void importBlacklist(String blacklistName) throws IOException, SOAPException { + // Check for errors + if ((blacklistName == null)||(blacklistName.length() == 0)) + throw new IllegalArgumentException("Blacklist name must not be null or empty."); + + // extracting the message context + extractMessageContext(AUTHENTICATION_NEEDED); + + // initialize the list manager + initBlacklistManager(); + + // check if the blacklist file exists + if (!blacklistExists(blacklistName)) { + // create blacklist + createBlacklistFile(blacklistName); + } + + // get attachment + 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(); + + PrintWriter writer = null; + BufferedReader reader = null; + try { + // getting a reader + reader = new BufferedReader(new InputStreamReader(dh.getInputStream(),"UTF-8")); + + // getting blacklist file writer + writer = getBlacklistFileWriter(blacklistName); + + // read new item + String blacklistItem = null; + while ((blacklistItem = reader.readLine()) != null) { + // convert it into a proper format + blacklistItem = prepareBlacklistItem(blacklistItem); + + // TODO: check if the item already exits + + // write item to blacklist file + writer.println(blacklistItem); + writer.flush(); + + // inform blacklist engine about new item + addBlacklistItemToBlacklist(blacklistItem, blacklistName); + } + } finally { + if (reader != null) try { reader.close(); } catch (Exception e) {/* */} + if (writer != null) try { writer.close(); } catch (Exception e) {/* */} + } + } + public String[] getBlacklistTypes() throws AxisFault { // extracting the message context extractMessageContext(AUTHENTICATION_NEEDED); + // initialize the list manager + initBlacklistManager(); + // return supported types return getSupportedBlacklistTypeArray(); } @@ -271,8 +354,9 @@ public class BlacklistService extends AbstractService { private void addBlacklistItemToFile(String blacklistItem, String blacklistName) throws AxisFault { PrintWriter pw = null; try { - pw = new PrintWriter(new FileWriter(getBlacklistFile(blacklistName), true)); + pw = getBlacklistFileWriter(blacklistName); pw.println(blacklistItem); + pw.flush(); pw.close(); } catch (IOException e) { throw new AxisFault("Unable to append blacklist entry.",e); @@ -281,6 +365,14 @@ public class BlacklistService extends AbstractService { } } + private PrintWriter getBlacklistFileWriter(String blacklistName) throws AxisFault { + try { + return new PrintWriter(new FileWriter(getBlacklistFile(blacklistName), true)); + } catch (IOException e) { + throw new AxisFault("Unable to open blacklist file.",e); + } + } + private void removeBlacklistItemFromBlacklistFile(String blacklistItem, String blacklistName) { // load blacklist data from file ArrayList list = listManager.getListArray(getBlacklistFile(blacklistName)); diff --git a/source/de/anomic/soap/services/blacklist.wsdl b/source/de/anomic/soap/services/blacklist.wsdl index 2647b9b7d..cf8557af4 100644 --- a/source/de/anomic/soap/services/blacklist.wsdl +++ b/source/de/anomic/soap/services/blacklist.wsdl @@ -21,94 +21,104 @@ Built on Nov 16, 2004 (12:19:44 EST)--> + + + + - + - + - + + + - + - + - + - + - + + + + + @@ -213,6 +223,16 @@ Built on Nov 16, 2004 (12:19:44 EST)--> + + + + + + + + + + diff --git a/test/de/anomic/soap/services/BlacklistServiceTest.java b/test/de/anomic/soap/services/BlacklistServiceTest.java index 46c33ada8..e0e0434e9 100644 --- a/test/de/anomic/soap/services/BlacklistServiceTest.java +++ b/test/de/anomic/soap/services/BlacklistServiceTest.java @@ -1,9 +1,17 @@ package de.anomic.soap.services; +import java.io.IOException; import java.rmi.RemoteException; +import javax.activation.DataHandler; +import javax.activation.DataSource; import javax.xml.rpc.ServiceException; +import org.apache.axis.attachments.AttachmentPart; +import org.apache.axis.attachments.Attachments; +import org.apache.axis.attachments.PlainTextDataSource; +import org.apache.axis.client.Call; +import org.apache.axis.client.Stub; import org.apache.axis.utils.XMLUtils; import org.w3c.dom.Document; @@ -61,5 +69,30 @@ public class BlacklistServiceTest extends AbstractServiceTest { // delete blacklist bl.deleteBlacklist(blacklistName); } + + public void testBacklistImport() throws IOException { + BlacklistService bl = ((BlacklistService)service); + + // create datasource to hold the attachment content + DataSource data = new PlainTextDataSource("import.txt","www.yacy.net/.*\r\n" + + "www.yacy-websuche.de/.*"); + DataHandler attachmentFile = new DataHandler(data); + + // creating attachment part + AttachmentPart part = new AttachmentPart(); + part.setDataHandler(attachmentFile); + part.setContentType("text/plain"); + + // 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 it + String blacklistName = "junit_test_" + System.currentTimeMillis(); + bl.importBlacklist(blacklistName); + + // delete blacklist + bl.deleteBlacklist(blacklistName); + } }