/*=============================================================================*
* Copyright 2006 The Apache Software Foundation
*
* Licensed under 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.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*=============================================================================*/
package org.apache.muse.ws.resource.properties.impl;
import java.lang.reflect.Array;
import javax.xml.namespace.QName;
import org.w3c.dom.Element;
import org.w3c.dom.Node;
import org.apache.muse.core.serializer.Serializer;
import org.apache.muse.core.serializer.SerializerRegistry;
import org.apache.muse.util.messages.Messages;
import org.apache.muse.util.messages.MessagesFactory;
import org.apache.muse.util.xml.XmlUtils;
import org.apache.muse.ws.addressing.soap.SoapFault;
import org.apache.muse.ws.resource.basefaults.BaseFault;
import org.apache.muse.ws.resource.ext.faults.SerializationErrorFault;
import org.apache.muse.ws.resource.properties.WsrpConstants;
import org.apache.muse.ws.wsdl.WsdlUtils;
/**
*
* WsrpUtils is a set of convenience methods related to WS-RP v1.2.
*
* @author Dan Jemiolo (danj)
*
*/
public class WsrpUtils
{
//
// Used to lookup all exception messages
//
private static Messages _MESSAGES = MessagesFactory.get(WsrpUtils.class);
/**
*
* Uses Muse's registered Serializers to convert the given objects
* into XML elements.
*
* @param properties
* The property values to serialize.
*
* @param type
* The type whose registered Serializer will be used to parse
* the property values.
*
* @param qname
* The QName of the XML elements that will represent the
* property values.
*
* @return An array with the POJO representation of the given property
* values. The array will be the same length as the one given.
* The order of the objects will be the same as the order of the
* values.
*
* @throws SoapFault
* <ul>
* <li>If any of the values could not be serialized.</li>
* </ul>
*
*/
public static Element[] convertToElements(Object[] properties,
Class type,
QName qname)
throws SoapFault
{
SerializerRegistry registry = SerializerRegistry.getInstance();
Serializer ser = registry.getSerializer(type);
Element[] xml = new Element[properties.length];
for (int n = 0; n < properties.length; ++n)
xml[n] = ser.toXML(properties[n], qname);
return xml;
}
/**
*
* Uses Muse's registered Serializers to convert the given Elements
* into instances of the given type.
*
* @param properties
* The property values to deserialize.
*
* @param type
* The type whose registered Serializer will be used to parse
* the property values.
*
* @return An array with the POJO representation of the given property
* values. The array will be the same length as the one given.
* The order of the objects will be the same as the order of the
* values.
*
* @throws BaseFault
* <ul>
* <li>If any of the values could not be deserialized.</li>
* </ul>
*
*/
public static Object convertToObjects(Element[] properties, Class type)
throws BaseFault
{
SerializerRegistry registry = SerializerRegistry.getInstance();
Serializer deser = registry.getSerializer(type);
Object objects = Array.newInstance(type, properties.length);
try
{
for (int n = 0; n < properties.length; ++n)
Array.set(objects, n, deser.fromXML(properties[n]));
}
catch (SoapFault fault)
{
throw new SerializationErrorFault(fault);
}
return objects;
}
/**
*
* Searches a WSDL document for the schema element name of a resource's
* WS-RP document. The WS-RP document is defined in the WSDL's <em>types</em>
* section and is an aggregate properties defined in other schemas (all
* properties listed in the WS-RP definition use the <em>ref</em> attribute
* to refer to their complete type definitions).
*
* @param wsdl
* The WSDL document that contains the WS-RP definition.
*
* @param portType
* The WSDL portType that has the name of the WS-RP definition.
*
* @return The QName of the WS-RP document element in <types/>. This element
* is a sequence of other XSD elements that use the <em>ref</em>
* attribute to define their types.
*
* @see WsdlUtils#getTypeDeclaration(Node, QName)
*
*/
public static QName getPropertiesName(Node wsdl, QName portType)
{
if (wsdl == null)
throw new NullPointerException(_MESSAGES.get("NullWSDLDocument"));
//
// get the resource's portType from the WSDL
//
Element portTypeXML = WsdlUtils.getPortType(wsdl, portType);
if (portTypeXML == null)
throw new RuntimeException(_MESSAGES.get("NoPortTypesFound"));
//
// get the name of the WS-RP element
//
String definitionName =
portTypeXML.getAttributeNS(WsrpConstants.NAMESPACE_URI, WsrpConstants.RESOURCE_PROPERTIES);
if (definitionName == null || definitionName.length() == 0)
throw new RuntimeException(_MESSAGES.get("NoPropertiesAttribute"));
return XmlUtils.parseQName(definitionName, portTypeXML);
}
}