logger.debug("creating pipeadvertisement from " + out.toString());
Node pipeAdNode = list.item(0); try { ByteArrayOutputStream out = new ByteArrayOutputStream(); out.write(new String("<?xml version=\"1.0\"?><!DOCTYPE jxta:PipeAdvertisement>").getBytes()); XMLUtils.outputDOM(pipeAdNode, out);
    public void pipeMsgEvent(PipeMsgEvent event) {        final int WAIT_TIMEOUT = 10*1000;        Message msg;        try {            msg = event.getMessage();            if (msg == null)                return;        } catch (Exception e) {            e.printStackTrace();            return;        }        // Get message        String newMessage = msg.getString(DATA_TAG);        if (newMessage == null) {            logger.debug("null msg received");            return;        }        logger.debug("Received message: " + newMessage);        // verify message, then extract query and input pipe        org.w3c.dom.Document doc = null;        try {            doc = db.parse(new InputSource(new StringReader(newMessage)));        }        catch (Exception e) {            logger.warn("Could not parse message: " + e);            return;        }        SignatureManagerResponse response = null;        try {            response = signatureManager.verifyDocument(doc);        }        catch (SignatureManagerException e) {            logger.warn("Could not verify signature: " + e);            // for now we continue anyway        }        if (response != null) {            doc = response.document;        }        // extract inputpipe element        NodeList list = doc.getElementsByTagName(PIPE_TAG);        if (list == null || list.getLength() == 0) {            logger.warn("Did not find pipe element in document");            return;        }        // Unfortunately JXTA makes it very hard to create PipeAdvertisements from existing XML...        // the only way is to give it the actual XML for the pipe, which means we need to extract it        // and feed it in a very inefficient way        Node pipeAdNode = list.item(0);        try {            ByteArrayOutputStream out = new ByteArrayOutputStream();            out.write(new String("<?xml version=\"1.0\"?><!DOCTYPE jxta:PipeAdvertisement>").getBytes());            XMLUtils.outputDOM(pipeAdNode, out);            logger.debug("creating pipeadvertisement from " + out.toString());            PipeAdvertisement clientPipeAd = (PipeAdvertisement)                    AdvertisementFactory.newAdvertisement(                            xmlMimeMediaType, new ByteArrayInputStream(out.toByteArray()));            OutputPipe clientPipe = pipeService.createOutputPipe(clientPipeAd, WAIT_TIMEOUT);            Message outmsg = pipeService.createMessage();            outmsg.setString(DATA_TAG,"Hello there old chap!");            clientPipe.send(outmsg);            logger.debug("sent message");        } catch (Exception e) {            logger.error("Could not create client pipe");            e.printStackTrace();        }    }