001: package org.objectweb.celtix.bus.bindings.soap;
002:
003: import java.lang.reflect.Array;
004: import java.lang.reflect.Method;
005:
006: import javax.xml.namespace.QName;
007: import javax.xml.ws.Holder;
008:
009: public final class SOAPMessageUtil {
010:
011: private SOAPMessageUtil() {
012: // Utility class - never constructed
013: }
014:
015: public static Method getMethod(Class<?> clazz, String methodName)
016: throws Exception {
017: Method[] declMethods = clazz.getDeclaredMethods();
018: for (Method method : declMethods) {
019: if (method.getName().equals(methodName)) {
020: return method;
021: }
022: }
023: return null;
024: }
025:
026: /* The method creates a object array based on the number of method parameters
027: * also creates holder objects for in-out and out params.
028: */
029: public static Object[] getMessageObjects(Method method)
030: throws Exception {
031: int idx = 0;
032: Object[] methodArgs = (Object[]) Array.newInstance(
033: Object.class, method.getParameterTypes().length);
034: for (Class<?> cls : method.getParameterTypes()) {
035: if (cls.isAssignableFrom(Holder.class)) {
036: methodArgs[idx] = cls.newInstance();
037: }
038: idx++;
039: }
040:
041: return methodArgs;
042: }
043:
044: /* The method wraps inout, out method parameters into javax.xml.ws.Holder<T>
045: * and returns a array of objects.
046: */
047:
048: public static Object[] getMessageObjects(Method method,
049: Object... args) throws Exception {
050: assert args.length == method.getParameterTypes().length;
051: int idx = 0;
052: Object[] methodArgs = (Object[]) Array.newInstance(
053: Object.class, method.getParameterTypes().length);
054: for (Class<?> cls : method.getParameterTypes()) {
055: if (cls.isAssignableFrom(Holder.class)) {
056: methodArgs[idx] = cls.newInstance();
057: Holder.class.getField("value").set(methodArgs[idx],
058: args[idx]);
059: } else {
060: methodArgs[idx] = args[idx];
061: }
062: idx++;
063: }
064:
065: return methodArgs;
066: }
067:
068: public static String createWrapDocLitSOAPMessage(QName wrapName,
069: QName elName, String data) {
070: StringBuffer str = new StringBuffer();
071:
072: str.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
073: str
074: .append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
075: str.append("xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" ");
076: str
077: .append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
078: str.append("<SOAP-ENV:Body>");
079:
080: str.append("<ns4:" + wrapName.getLocalPart() + " xmlns:ns4=\""
081: + wrapName.getNamespaceURI() + "\">");
082: if (elName != null) {
083: str.append("<ns4:" + elName.getLocalPart() + ">");
084: str.append(data);
085: str.append("</ns4:" + elName.getLocalPart() + ">");
086: }
087: str.append("</ns4:" + wrapName.getLocalPart() + ">");
088:
089: str.append("</SOAP-ENV:Body>");
090: str.append("</SOAP-ENV:Envelope>");
091:
092: return str.toString();
093: }
094:
095: public static String createRPCLitSOAPMessage(QName operation,
096: QName elName, String data) {
097: StringBuffer str = new StringBuffer();
098:
099: str.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
100: str
101: .append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
102: str.append("xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" ");
103: str
104: .append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
105: str.append("<SOAP-ENV:Body>");
106:
107: str.append("<ns4:" + operation.getLocalPart());
108: str.append(" xmlns:ns4=\"" + operation.getNamespaceURI()
109: + "\">");
110: if (elName != null) {
111: str.append("<" + elName.getLocalPart() + ">");
112: str.append(data);
113: str.append("</" + elName.getLocalPart() + ">");
114: }
115: str.append("</ns4:" + operation.getLocalPart() + ">");
116:
117: str.append("</SOAP-ENV:Body>");
118: str.append("</SOAP-ENV:Envelope>");
119:
120: return str.toString();
121: }
122:
123: public static String createBareDocLitSOAPMessage(QName elName,
124: String data) {
125: StringBuffer str = new StringBuffer();
126:
127: str.append("<?xml version=\"1.0\" encoding=\"utf-8\" ?>");
128: str
129: .append("<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\" ");
130: str.append("xmlns:xs=\"http://www.w3.org/2001/XMLSchema\" ");
131: str
132: .append("xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\">");
133: str.append("<SOAP-ENV:Header/>");
134: str.append("<SOAP-ENV:Body>");
135:
136: str.append("<ns4:" + elName.getLocalPart() + " xmlns:ns4=\""
137: + elName.getNamespaceURI() + "\">");
138: str.append(data);
139: str.append("</ns4:" + elName.getLocalPart() + ">");
140:
141: str.append("</SOAP-ENV:Body>");
142: str.append("</SOAP-ENV:Envelope>");
143:
144: return str.toString();
145: }
146: }
|