1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207
|
/*
* JBoss, Home of Professional Open Source
* Copyright 2005, JBoss Inc., and individual contributors as indicated
* by the @authors tag. See the copyright.txt in the distribution for a
* full listing of individual contributors.
*
* This is free software; you can redistribute it and/or modify it
* under the terms of the GNU Lesser General Public License as
* published by the Free Software Foundation; either version 2.1 of
* the License, or (at your option) any later version.
*
* This software is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this software; if not, write to the Free
* Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
* 02110-1301 USA, or see the FSF site: http://www.fsf.org.
*/
package org.jboss.ws.core.soap;
import java.lang.reflect.Method;
import javax.xml.namespace.QName;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import org.jboss.logging.Logger;
import org.jboss.ws.WSException;
import org.jboss.ws.core.CommonMessageContext;
import org.jboss.ws.core.binding.BindingException;
import org.jboss.ws.core.binding.SerializationContext;
import org.jboss.ws.core.binding.AbstractSerializerFactory;
import org.jboss.ws.core.binding.SerializerSupport;
import org.jboss.ws.core.binding.TypeMappingImpl;
import org.jboss.ws.core.jaxrpc.binding.NullValueSerializer;
import org.jboss.wsf.common.JavaUtils;
/**
* Represents the OBJECT_VALID state of an {@link SOAPContentElement}.<br>
*
* @author Heiko.Braun@jboss.org
* @version $Id$
* @since 05.02.2007
*/
public class ObjectContent extends SOAPContent
{
private static Logger log = Logger.getLogger(ObjectContent.class);
// The java object content of this element.
private Object objectValue;
protected ObjectContent(SOAPContentElement container)
{
super(container);
}
State getState()
{
return State.OBJECT_VALID;
}
SOAPContent transitionTo(State nextState)
{
SOAPContent next = null;
if (nextState == State.XML_VALID)
{
XMLFragment fragment = marshallObjectContents();
XMLContent xmlValid = new XMLContent(container);
xmlValid.setXMLFragment(fragment);
next = xmlValid;
}
else if (nextState == State.OBJECT_VALID)
{
next = this;
}
else if (nextState == State.DOM_VALID)
{
// first transition to XML valid
XMLFragment fragment = marshallObjectContents();
XMLContent tmp = new XMLContent(container);
tmp.setXMLFragment(fragment);
// finally from XML valid to DOM valid
next = tmp.transitionTo(State.DOM_VALID);
}
else
{
throw new IllegalArgumentException("Illegal state requested: " + nextState);
}
return next;
}
public Source getPayload()
{
throw new IllegalStateException("Payload not available");
}
public void setPayload(Source source)
{
throw new IllegalStateException("Payload cannot be set on object content");
}
public XMLFragment getXMLFragment()
{
throw new IllegalStateException("XMLFragment not available");
}
public void setXMLFragment(XMLFragment xmlFragment)
{
throw new IllegalStateException("XMLFragment not available");
}
public Object getObjectValue()
{
return objectValue;
}
public void setObjectValue(Object objValue)
{
this.objectValue = objValue;
}
private XMLFragment marshallObjectContents()
{
QName xmlType = container.getXmlType();
Class javaType = container.getJavaType();
log.debug("getXMLFragment from Object [xmlType=" + xmlType + ",javaType=" + javaType + "]");
CommonMessageContext msgContext = MessageContextAssociation.peekMessageContext();
if (msgContext == null)
throw new WSException("MessageContext not available");
SerializationContext serContext = msgContext.getSerializationContext();
serContext.setJavaType(javaType);
TypeMappingImpl typeMapping = serContext.getTypeMapping();
XMLFragment xmlFragment = null;
try
{
SerializerSupport ser;
if (objectValue != null)
{
AbstractSerializerFactory serializerFactory = getSerializerFactory(typeMapping, javaType, xmlType);
ser = (SerializerSupport)serializerFactory.getSerializer();
}
else
{
ser = new NullValueSerializer();
}
Result result = ser.serialize(container, serContext);
xmlFragment = new XMLFragment(result);
log.debug("xmlFragment: " + xmlFragment);
}
catch (BindingException e)
{
throw new WSException(e);
}
return xmlFragment;
}
/**
* Get the serializer factory for a given javaType and xmlType
*/
private AbstractSerializerFactory getSerializerFactory(TypeMappingImpl typeMapping, Class javaType, QName xmlType)
{
AbstractSerializerFactory serializerFactory = (AbstractSerializerFactory)typeMapping.getSerializer(javaType, xmlType);
// The type mapping might contain a mapping for the array wrapper bean
if (serializerFactory == null && javaType.isArray())
{
Class arrayWrapperType = typeMapping.getJavaType(xmlType);
if (arrayWrapperType != null)
{
try
{
Method toArrayMethod = arrayWrapperType.getMethod("toArray", new Class[] {});
Class returnType = toArrayMethod.getReturnType();
if (JavaUtils.isAssignableFrom(javaType, returnType))
{
serializerFactory = (AbstractSerializerFactory)typeMapping.getSerializer(arrayWrapperType, xmlType);
}
}
catch (NoSuchMethodException e)
{
// ignore
}
}
}
if (serializerFactory == null)
throw new WSException("Cannot obtain serializer factory for: [xmlType=" + xmlType + ",javaType=" + javaType + "]");
return serializerFactory;
}
}
|