
java.fedora.client.bmech.xml.WSDLGenerator Maven / Gradle / Ivy
Show all versions of fcrepo-client Show documentation
/*
* -----------------------------------------------------------------------------
*
* License and Copyright: The contents of this file are subject to the
* Apache License, Version 2.0 (the "License"); you may not use
* this file except in compliance with the License. You may obtain a copy of
* the License at
* http://www.fedora-commons.org/licenses.
*
* Software distributed under the License is distributed on an "AS IS" basis,
* WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License for
* the specific language governing rights and limitations under the License.
*
* The entire file consists of original code.
* Copyright © 2008 Fedora Commons, Inc.
*
Copyright © 2002-2007 The Rector and Visitors of the University of
* Virginia and Cornell University
* All rights reserved.
*
* -----------------------------------------------------------------------------
*/
package fedora.client.bmech.xml;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.transform.dom.DOMResult;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.ParserConfigurationException;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Set;
import java.util.Iterator;
import java.util.Vector;
import fedora.client.bmech.data.*;
import fedora.client.bmech.BMechBuilderException;
/**
*
* Title: WSDLGenerator.java
* Description:
*
* @author [email protected]
* @version $Id: WSDLGenerator.java 5166 2006-10-25 11:05:45Z eddie $
*/
public class WSDLGenerator
{
private static final String THIS = "bmech";
private static final String WSDL = "http://schemas.xmlsoap.org/wsdl/";
private static final String SOAP = "http://schemas.xmlsoap.org/wsdl/soap";
private static final String SOAPENC = "http://schemas.xmlsoap.org/wsdl/soap/encoding";
private static final String HTTP = "http://schemas.xmlsoap.org/wsdl/http/";
private static final String MIME = "http://schemas.xmlsoap.org/wsdl/mime/";
private static final String XSD = "http://www.w3.org/2001/XMLSchema";
private static final String XMLNS = "http://www.w3.org/2000/xmlns/";
private Document document;
private Element root;
private Element types;
private Vector messageElements;
private Element portType;
private Element service;
private Element binding;
public WSDLGenerator(BMechTemplate newBMech) throws BMechBuilderException
{
initializeTree();
genWSDL(newBMech);
finalizeTree();
}
private void initializeTree() throws BMechBuilderException
{
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
try
{
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.newDocument();
}
catch (ParserConfigurationException pce)
{
// Parser with specified options can't be built
pce.printStackTrace();
throw new BMechBuilderException("WSDLGenerator: error configuring parser."
+ "Underlying exception: " + pce.getMessage());
}
root = (Element)document.createElementNS(WSDL, "wsdl:definitions");
types = (Element)document.createElementNS(WSDL, "wsdl:types");
messageElements = new Vector();
portType = (Element)document.createElementNS(WSDL, "wsdl:portType");
service = (Element)document.createElementNS(WSDL, "wsdl:service");
binding = (Element)document.createElementNS(WSDL, "wsdl:binding");
}
private void finalizeTree()
{
// put the tree together
document.appendChild(root);
root.appendChild(types);
Element[] messages = (Element[])messageElements.toArray(new Element[0]);
for (int i=0; i parmUnion = new HashMap();
for (int m=0; m oldVals = new HashSet();
for (int p1=0; p1 newVals = new HashSet();
for (int p2=0; p2 unionVals = new HashSet(oldVals);
unionVals.addAll(newVals);
existingParm.parmDomainValues =
(String[])unionVals.toArray(new String[0]);
parmUnion.put(parms[p].parmName, existingParm);
}
else
{
parmUnion.put(parms[p].parmName, parms[p]);
}
}
// add wsdl:message to the message vector
messageElements.add(message);
// create wsdl:portType with one or more wsdl:operation elements
portType.setAttribute("name", (bMechName + "PortType"));
Element operation = document.createElementNS(WSDL, "wsdl:operation");
operation.setAttribute("name", methods[m].methodName);
Element input = document.createElementNS(WSDL, "wsdl:input");
input.setAttribute("message", ("this:" + methods[m].methodName + "Request"));
Element output = document.createElementNS(WSDL, "wsdl:output");
output.setAttribute("message", "this:dissemResponse");
operation.appendChild(input);
operation.appendChild(output);
portType.appendChild(operation);
Element wsdlOperation = document.createElementNS(WSDL, "wsdl:operation");
wsdlOperation.setAttribute("name", methods[m].methodName);
Element httpOperation = document.createElementNS(HTTP, "http:operation");
if (hasBaseURL)
{
httpOperation.setAttribute("location",
methods[m].methodProperties.methodRelativeURL);
}
else
{
httpOperation.setAttribute("location",
methods[m].methodProperties.methodFullURL);
}
Element wsdlInput = document.createElementNS(WSDL, "wsdl:input");
wsdlInput.appendChild(document.createElementNS(HTTP, "http:urlReplacement"));
Element wsdlOutput = document.createElementNS(WSDL, "wsdl:output");
String[] MIMETypes = methods[m].methodProperties.returnMIMETypes;
for (int t = 0; t < MIMETypes.length; t++)
{
Element MIMEType = document.createElementNS(MIME, "mime:content");
MIMEType.setAttribute("type", MIMETypes[t]);
wsdlOutput.appendChild(MIMEType);
}
wsdlOperation.appendChild(httpOperation);
wsdlOperation.appendChild(wsdlInput);
wsdlOperation.appendChild(wsdlOutput);
binding.appendChild(wsdlOperation);
}
// end methods loop
// add wsdl:message for Fedora dissemination response to wsdl:definitions
Element responseMessage = document.createElementNS(WSDL, "wsdl:message");
responseMessage.setAttribute("name", "dissemResponse");
Element responseMessagePart = document.createElementNS(WSDL, "wsdl:part");
responseMessagePart.setAttribute("name", "dissem");
responseMessagePart.setAttribute("type", "xsd:base64Binary");
responseMessage.appendChild(responseMessagePart);
messageElements.add(responseMessage);
// create wsdl:types
Iterator iparm = parmUnion.values().iterator();
while (iparm.hasNext())
{
MethodParm parm = (MethodParm)iparm.next();
// XSD Schema Type element
Element typeDef = document.createElementNS(XSD, "xsd:simpleType");
typeDef.setAttribute("name", (parm.parmName + "Type"));
// XSD Schema Type Restriction element
Element restrict = document.createElementNS(XSD, "xsd:restriction");
restrict.setAttribute("base", "xsd:string");
String[] domainValues = parm.parmDomainValues;
for (int k=0; k