log.debug("getObjectValue [xmlType=" + xmlType + ",javaType=" + javaType + "]");
CommonMessageContext msgContext = MessageContextAssociation.peekMessageContext();
if (msgContext == null)
throw new WSException("MessageContext not available");
SerializationContext serContext = msgContext.getSerializationContext();
ParameterMetaData pmd = container.getParamMetaData();
OperationMetaData opMetaData = pmd.getOperationMetaData();
serContext.setProperty(ParameterMetaData.class.getName(), pmd);
serContext.setJavaType(javaType);
List<Class> registeredTypes = opMetaData.getEndpointMetaData().getRegisteredTypes();
serContext.setProperty(SerializationContextJAXWS.JAXB_CONTEXT_TYPES, registeredTypes.toArray(new Class[0]));
try
{
// Get the deserializer from the type mapping
TypeMappingImpl typeMapping = serContext.getTypeMapping();
AbstractDeserializerFactory deserializerFactory = getDeserializerFactory(typeMapping, javaType, xmlType);
DeserializerSupport des = (DeserializerSupport)deserializerFactory.getDeserializer();
obj = des.deserialize(container, serContext);
if (obj != null)
{
Class objType = obj.getClass();
boolean isAssignable = JavaUtils.isAssignableFrom(javaType, objType);
if (!isAssignable && javaType.isArray())
{
try
{
Method toArrayMethod = objType.getMethod("toArray");
Class returnType = toArrayMethod.getReturnType();
if (JavaUtils.isAssignableFrom(javaType, returnType))
{
Method getValueMethod = objType.getMethod("getValue");
Object value = getValueMethod.invoke(obj);
if (value != null)
{
// Do not invoke toArray if getValue returns null
obj = toArrayMethod.invoke(obj);
}
else
{
// if the fragment did not indicate a null return
// by an xsi:nil we return an empty array
Class componentType = javaType.getComponentType();
obj = Array.newInstance(componentType, 0);
}
isAssignable = true;
}
}
catch (Exception e)
{
// ignore
}
}
if (!isAssignable)
{
// handle XOP simple types, i.e. in RPC/LIT
try
{
String contentType = MimeUtils.resolveMimeType(javaType);
log.debug("Adopt DataHandler to " + javaType + ", contentType " + contentType);
DataSource ds = new SwapableMemoryDataSource(((DataHandler)obj).getInputStream(), contentType);
DataHandler dh = new DataHandler(ds);
obj = dh.getContent();
// 'application/octet-stream' will return a byte[] instead fo the stream
if (obj instanceof InputStream)
{
ByteArrayOutputStream bout = new ByteArrayOutputStream();
dh.writeTo(bout);
obj = bout.toByteArray();
}
}
catch (IOException e)
{
throw new WSException("Failed to adopt XOP content type", e);
}
if (!JavaUtils.isAssignableFrom(javaType, obj.getClass()))
{
throw new WSException("Java type '" + javaType + "' is not assignable from: " + objType.getName());
}
}
}
}
catch (BindingException e)
{
throw new WSException(e);
}
log.debug("objectValue: " + (obj != null ? obj.getClass().getName() : null));
return obj;