1 23 package org.infoglue.deliver.util.webservices; 24 25 import java.util.ArrayList ; 26 import java.util.Collection ; 27 import java.util.HashMap ; 28 import java.util.Iterator ; 29 import java.util.List ; 30 import java.util.Map ; 31 32 import javax.xml.namespace.QName ; 33 import javax.xml.rpc.ParameterMode ; 34 35 import org.apache.axis.client.Call; 36 import org.apache.axis.client.Service; 37 import org.apache.axis.encoding.XMLType; 38 import org.apache.axis.encoding.ser.BeanDeserializerFactory; 39 import org.apache.axis.encoding.ser.BeanSerializerFactory; 40 import org.apache.log4j.Logger; 41 import org.infoglue.cms.security.InfoGluePrincipal; 42 43 46 class Parameter 47 { 48 public final String name; 49 public final QName type; 50 51 public Parameter(final String name, final QName type) 52 { 53 this.name = name; 54 this.type = type; 55 } 56 } 57 58 61 public class DynamicWebservice 62 { 63 66 private final static Logger logger = Logger.getLogger(DynamicWebservice.class.getName()); 67 68 71 private static final String PRINCIPAL_ARGUMENT_NAME = "principal"; 72 73 76 private static final String DEFAULT_NAMESPACE_URI = "http://soapinterop.org/"; 77 78 81 private String targetEndpointAddress; 82 83 86 private String operationName; 87 88 91 private QName returnType; 92 93 96 private final List parameters = new ArrayList (); 97 98 101 private final List arguments = new ArrayList (); 102 103 106 private final Map mappings = new HashMap (); 108 111 private final Map standardMappings = new HashMap (); 113 116 private Service service; 117 118 121 private Call call; 122 123 126 private Object result; 127 128 131 private DynamicWebserviceSerializer serializer; 132 133 134 137 public DynamicWebservice(final InfoGluePrincipal remotePrincipal) 138 { 139 super(); 140 configureStandardMappings(); 141 serializer = new DynamicWebserviceSerializer(standardMappings); 142 addArgument(PRINCIPAL_ARGUMENT_NAME, remotePrincipal.getName()); 143 } 144 145 148 public void callService() 149 { 150 try 151 { 152 service = new Service(); 153 call = (Call) service.createCall(); 154 155 configureBasic(); 156 configureMappings(); 157 configureParameters(); 158 159 result = call.invoke(arguments.toArray()); 160 } 161 catch (Exception e) 162 { 163 e.printStackTrace(); 164 } 165 } 166 167 170 private void configureStandardMappings() 171 { 172 standardMappings.put(Boolean .class, XMLType.XSD_BASE64); 173 standardMappings.put(Boolean .class, XMLType.XSD_BOOLEAN); 174 standardMappings.put(Double .class, XMLType.XSD_DOUBLE); 175 standardMappings.put(Float .class, XMLType.XSD_FLOAT); 176 standardMappings.put(Integer .class, XMLType.XSD_INT); 177 standardMappings.put(Long .class, XMLType.XSD_LONG); 178 standardMappings.put(String .class, XMLType.XSD_STRING); 179 } 180 181 184 private void configureBasic() 185 { 186 call.setTargetEndpointAddress(targetEndpointAddress); 187 call.setOperationName(new QName (DEFAULT_NAMESPACE_URI, operationName)); 188 call.setEncodingStyle(Call.ENCODINGSTYLE_URI_PROPERTY); 189 call.setReturnType(returnType); 190 } 191 192 195 private void configureParameters() 196 { 197 for(final Iterator i = parameters.iterator(); i.hasNext(); ) 198 { 199 final Parameter parameter = (Parameter) i.next(); 200 call.addParameter(parameter.name, parameter.type, ParameterMode.IN); 201 } 202 } 203 204 207 private void configureMappings() 208 { 209 for(final Iterator i = mappings.keySet().iterator(); i.hasNext(); ) 210 { 211 final Class clazz = (Class ) i.next(); 212 final QName type = (QName ) mappings.get(clazz); 213 call.registerTypeMapping(clazz, type, BeanSerializerFactory.class, BeanDeserializerFactory.class); 214 } 215 } 216 217 220 public Object getResult() 221 { 222 return result; 223 } 224 225 228 public void setTargetEndpointAddress(final String targetEndpointAddress) 229 { 230 logger.debug("targetEndpointAddress=[" + targetEndpointAddress + "]"); 231 this.targetEndpointAddress = targetEndpointAddress; 232 } 233 234 237 public void setOperationName(final String operationName) 238 { 239 logger.debug("operationName=[" + operationName + "]"); 240 this.operationName = operationName; 241 } 242 243 246 public void setReturnType(final Class c) 247 { 248 logger.debug("returnType=[" + (c==null ? "null" : getClassName(c)) + "]"); 249 returnType = mappingForClass(c); } 251 252 255 public void addArgument(final String name, final Object value) 256 { 257 assertNameNotNull(name); 258 logger.debug("addArgument=[" + name + "," + value + "]"); 259 addArgument(name, mappingForClass(value.getClass()), value); 260 } 261 262 265 public void addArgument(final String name, final Map value) 266 { 267 assertNameNotNull(name); 268 269 logger.debug("addArgument=[" + name + "," + value + "] (Map)"); 270 addArgument(name, XMLType.SOAP_ARRAY, serializer.serializeMap(value).toArray()); 271 } 272 273 276 public void addArgument(final String name, final Collection value) 277 { 278 assertNameNotNull(name); 279 280 logger.debug("addArgument=[" + name + "," + value + "] (Collection)"); 281 addArgument(name, XMLType.SOAP_ARRAY, serializer.serializeCollection(value).toArray()); 282 } 283 284 287 private void assertNameNotNull(final String argument) 288 { 289 if(argument == null) 290 { 291 throw new IllegalArgumentException ("A parameter name can't be null."); 292 } 293 } 294 295 298 private void addArgument(final String name, final QName type, final Object value) 299 { 300 parameters.add(new Parameter(name, type)); 301 arguments.add(value); 302 } 303 304 307 private QName mappingForClass(final Class c) 308 { 309 if(c == null) 310 { 311 return null; 312 } 313 if(standardMappings.containsKey(c)) 314 { 315 return (QName ) standardMappings.get(c); 316 } 317 if(mappings.containsKey(c)) 318 { 319 return (QName ) mappings.get(c); 320 } 321 return addMapping(c); 322 } 323 324 327 private QName addMapping(final Class c) { 328 final String className = getClassName(c); 329 final QName type = new QName (DEFAULT_NAMESPACE_URI + className, className); 330 mappings.put(c, type); 331 logger.debug("addMapping=[" + c.getName() + "," + type + "]"); 332 return type; 333 } 334 335 338 private String getClassName(final Class c) 339 { 340 final int firstChar = c.getName().lastIndexOf('.') + 1; 341 return (firstChar > 0) ? c.getName().substring(firstChar) : c.getName(); 342 } 343 } | Popular Tags |