Java & UDDI Registries
by Paul Tremblett 

Listing One
private SOAPMessage findBusiness(String url, String queryString)
      throws Exception {
    MessageFactory msgFactory = MessageFactory.newInstance();
    SOAPMessage msg = msgFactory.createMessage();
    SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
    SOAPBody body = envelope.getBody();
    SOAPElement findBusiness = body.addBodyElement(
        envelope.createName("find_business",
        "", "urn:uddi-org:api"));
    findBusiness.addAttribute(
        envelope.createName("generic"), "1.0");
    findBusiness.addAttribute(
        envelope.createName("maxRows"), "100");
    SOAPElement businessName =
        findBusiness.addChildElement(
        envelope.createName("name"));
    businessName.addTextNode(queryString);
    msg.saveChanges();
            SOAPConnectionFactory scf = 
        SOAPConnectionFactory.newInstance();
    SOAPConnection connection = scf.createConnection();
    URLEndpoint endpoint = new URLEndpoint(url);
    SOAPMessage reply = connection.call(msg, endpoint);
    return reply;
}

Listing Two
private void transformMessage(SOAPMessage msg, String xslSource,
            HttpServletResponse response)
        throws Exception {
    response.setContentType("text/html");
    ServletOutputStream out = response.getOutputStream();
    ServletContext context = getServletConfig().
        getServletContext();
    String filePath = context.getRealPath(xslSource);
    PipedOutputStream pipeToThread = new PipedOutputStream();
    PipedInputStream pipeFromThread = new PipedInputStream();
    TransformerThread transformer =
    new TransformerThread(filePath, pipeToThread, pipeFromThread);
    new Thread(transformer).start();
    msg.writeTo(pipeToThread);
    pipeToThread.flush();
    pipeToThread.close();
    int b;
    while ((b = pipeFromThread.read()) >= 0) {
        out.print((char)b);
    }
    pipeFromThread.close();
    out.flush();
    out.close();
}

Listing Three
public void run() {
    try {
        factory = DocumentBuilderFactory.newInstance();
        builder = factory.newDocumentBuilder();
        document = builder.parse(pipeFromMain);
        DOMSource source = new DOMSource(document);
        TransformerFactory tFactory = 
            TransformerFactory.newInstance();
        Transformer transformer = 
            tFactory.newTransformer(xslSource);
        StreamResult result = new StreamResult(pipeToMain);
        transformer.transform(source, result);
        pipeToMain.flush();
        pipeToMain.close();
    }
    catch (Exception e) {
        e.printStackTrace();
    }
}

Listing Four
private SOAPMessage findBusiness(String url, String queryString)
        throws Exception {
    BulkResponse response = null;
    Connection connection = null;
    SOAPMessage msg = null;
    Properties props = new Properties();
    props.setProperty("javax.xml.registry.queryManagerURL",
        url);
    props.setProperty("javax.xml.registry.factoryClass",
        "com.sun.xml.registry.uddi.ConnectionFactoryImpl");
    try {
        ConnectionFactory factory =
        ConnectionFactory.newInstance();
        factory.setProperties(props);
        connection = factory.createConnection();
        RegistryService rs = connection.getRegistryService();
        BusinessQueryManager bqm =
        rs.getBusinessQueryManager();
        Collection findQualifiers = new ArrayList();
        findQualifiers.add(FindQualifier.SORT_BY_NAME_DESC);
        Collection namePatterns = new ArrayList();
        namePatterns.add(queryString + "%");
        
        response = bqm.findOrganizations(findQualifiers,
        namePatterns, null, null, null, null);
        msg = convertToSOAPMessage(response);
    }
    catch (Exception e) {
        e.printStackTrace();
    } finally  {
        if (connection != null) {
            try {
                connection.close();
            } catch (JAXRException je) {}
        }
        return msg;
    }
}

Listing Five
private SOAPMessage convertToSOAPMessage(BulkResponse response) 
    throws Exception {
  MessageFactory factory = MessageFactory.newInstance();
  SOAPMessage msg = factory.createMessage();
  SOAPEnvelope envelope = msg.getSOAPPart().getEnvelope();
  SOAPBody body = envelope.getBody();
  Collection orgs = response.getCollection();
  Iterator orgIter = orgs.iterator();
  while (orgIter.hasNext()) {
      Organization org = (Organization) orgIter.next();
      SOAPBodyElement orgElement = body.addBodyElement(
          envelope.createName("Organization"));
      SOAPElement orgName = orgElement.addChildElement(
          envelope.createName("name"));
      orgName.addTextNode(getName(org));
      SOAPElement orgKey = orgElement.addChildElement(
          envelope.createName("key"));
      orgKey.addTextNode(getKey(org));
      SOAPElement orgDesc = orgElement.addChildElement(
          envelope.createName("description"));
      orgDesc.addTextNode(getDescription(org));
  }
  msg.saveChanges();
  return msg;
}




1


