| 1 |
|
|
| 2 |
|
|
| 3 |
|
|
| 4 |
|
|
| 5 |
|
|
| 6 |
|
|
| 7 |
|
|
| 8 |
|
|
| 9 |
|
|
| 10 |
|
|
| 11 |
|
|
| 12 |
|
|
| 13 |
|
|
| 14 |
|
|
| 15 |
|
|
| 16 |
|
|
| 17 |
|
|
| 18 |
|
|
| 19 |
|
package net.wotonomy.web.xml; |
| 20 |
|
|
| 21 |
|
import java.io.BufferedInputStream; |
| 22 |
|
import java.io.ByteArrayInputStream; |
| 23 |
|
import java.io.ByteArrayOutputStream; |
| 24 |
|
import java.io.FileNotFoundException; |
| 25 |
|
import java.io.InputStream; |
| 26 |
|
import java.io.OutputStream; |
| 27 |
|
import java.lang.reflect.InvocationTargetException; |
| 28 |
|
import java.net.URL; |
| 29 |
|
import java.net.URLConnection; |
| 30 |
|
|
| 31 |
|
import net.wotonomy.foundation.NSSelector; |
| 32 |
|
import net.wotonomy.foundation.internal.WotonomyException; |
| 33 |
|
|
| 34 |
|
|
| 35 |
|
|
| 36 |
|
|
| 37 |
|
|
| 38 |
|
|
| 39 |
|
|
| 40 |
|
|
| 41 |
|
|
| 42 |
|
|
| 43 |
|
|
| 44 |
|
|
| 45 |
|
|
| 46 |
|
|
| 47 |
|
|
| 48 |
|
public class XMLRPCSelector extends NSSelector |
| 49 |
|
{ |
| 50 |
45 |
private boolean copyStream = false; |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
|
| 54 |
|
|
| 55 |
|
public XMLRPCSelector (String aMethodName) |
| 56 |
|
{ |
| 57 |
45 |
super( aMethodName, EMPTY_CLASS_ARRAY ); |
| 58 |
45 |
} |
| 59 |
|
|
| 60 |
|
|
| 61 |
|
|
| 62 |
|
|
| 63 |
|
|
| 64 |
|
|
| 65 |
|
public XMLRPCSelector (String aMethodName, Class[] aParameterTypeArray) |
| 66 |
|
{ |
| 67 |
0 |
super( aMethodName, aParameterTypeArray ); |
| 68 |
0 |
} |
| 69 |
|
|
| 70 |
|
|
| 71 |
|
|
| 72 |
|
|
| 73 |
|
|
| 74 |
|
public Object invoke (Object anObject, Object[] parameters) |
| 75 |
|
throws IllegalAccessException, IllegalArgumentException, |
| 76 |
|
InvocationTargetException, NoSuchMethodException |
| 77 |
|
{ |
| 78 |
9 |
if ( anObject instanceof URL ) |
| 79 |
|
{ |
| 80 |
9 |
Receiver receiver = new Receiver(); |
| 81 |
9 |
byte[] copyOfResponse = null; |
| 82 |
|
try |
| 83 |
|
{ |
| 84 |
9 |
URLConnection cn = ((URL)anObject).openConnection(); |
| 85 |
|
|
| 86 |
|
|
| 87 |
9 |
cn.setDoOutput(true); |
| 88 |
9 |
cn.setDoInput(true); |
| 89 |
10 |
cn.setRequestProperty( |
| 90 |
1 |
"content-type","text/xml"); |
| 91 |
|
|
| 92 |
|
|
| 93 |
9 |
OutputStream out = cn.getOutputStream(); |
| 94 |
10 |
new XMLRPCEncoder().encodeRequest( |
| 95 |
1 |
name(), parameters, out ); |
| 96 |
0 |
out.flush(); |
| 97 |
0 |
out.close(); |
| 98 |
|
|
| 99 |
|
|
| 100 |
0 |
InputStream input = |
| 101 |
0 |
new BufferedInputStream( cn.getInputStream() ); |
| 102 |
0 |
if ( copyStream ) |
| 103 |
|
{ |
| 104 |
0 |
ByteArrayOutputStream byteArray = |
| 105 |
0 |
new ByteArrayOutputStream(); |
| 106 |
|
int b; |
| 107 |
0 |
while ( ( b = input.read() ) != -1 ) |
| 108 |
|
{ |
| 109 |
0 |
byteArray.write( b ); |
| 110 |
0 |
} |
| 111 |
0 |
copyOfResponse = byteArray.toByteArray(); |
| 112 |
0 |
input = new ByteArrayInputStream( copyOfResponse ); |
| 113 |
|
} |
| 114 |
0 |
new XMLRPCDecoder().decode( input, receiver ); |
| 115 |
|
} |
| 116 |
0 |
catch ( FileNotFoundException exc ) |
| 117 |
|
{ |
| 118 |
0 |
throw new WotonomyException( "Server did not return a response." ); |
| 119 |
|
} |
| 120 |
9 |
catch ( Exception exc ) |
| 121 |
|
{ |
| 122 |
9 |
if ( copyOfResponse != null ) |
| 123 |
|
{ |
| 124 |
0 |
System.out.println( new String( copyOfResponse ) ); |
| 125 |
0 |
exc.printStackTrace(); |
| 126 |
|
} |
| 127 |
9 |
throw new InvocationTargetException( exc ); |
| 128 |
0 |
} |
| 129 |
|
|
| 130 |
0 |
if ( receiver.faultString == null ) |
| 131 |
|
{ |
| 132 |
0 |
return receiver.result; |
| 133 |
|
} |
| 134 |
|
else |
| 135 |
|
{ |
| 136 |
0 |
throw new InvocationTargetException( |
| 137 |
0 |
new WotonomyException( |
| 138 |
0 |
receiver.faultCode + ": " + receiver.faultString ) ); |
| 139 |
|
} |
| 140 |
|
} |
| 141 |
|
|
| 142 |
|
|
| 143 |
0 |
return super.invoke( anObject, parameters ); |
| 144 |
|
} |
| 145 |
|
|
| 146 |
|
public static Object invoke |
| 147 |
|
(String methodName, Class[] parameterTypes, Object anObject, Object[] parameters) |
| 148 |
|
throws IllegalAccessException, IllegalArgumentException, |
| 149 |
|
InvocationTargetException, NoSuchMethodException |
| 150 |
|
{ |
| 151 |
0 |
return new XMLRPCSelector( methodName, parameterTypes ).invoke( anObject, parameters ); |
| 152 |
|
} |
| 153 |
|
|
| 154 |
|
public static Object invoke |
| 155 |
|
(String methodName, Object anObject) |
| 156 |
|
throws IllegalAccessException, IllegalArgumentException, |
| 157 |
|
InvocationTargetException, NoSuchMethodException |
| 158 |
|
{ |
| 159 |
0 |
return XMLRPCSelector.invoke( |
| 160 |
0 |
methodName, EMPTY_CLASS_ARRAY, anObject, EMPTY_OBJECT_ARRAY ); |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
public static Object invoke |
| 164 |
|
(String methodName, Class[] parameterTypes, |
| 165 |
|
Object anObject, Object aParameter) |
| 166 |
|
throws IllegalAccessException, IllegalArgumentException, |
| 167 |
|
InvocationTargetException, NoSuchMethodException |
| 168 |
|
{ |
| 169 |
0 |
return XMLRPCSelector.invoke( |
| 170 |
0 |
methodName, parameterTypes, anObject, new Object[] { aParameter } ); |
| 171 |
|
} |
| 172 |
|
|
| 173 |
|
public static Object invoke |
| 174 |
|
(String methodName, Class[] parameterTypes, |
| 175 |
|
Object anObject, Object p1, Object p2) |
| 176 |
|
throws IllegalAccessException, IllegalArgumentException, |
| 177 |
|
InvocationTargetException, NoSuchMethodException |
| 178 |
|
{ |
| 179 |
0 |
return XMLRPCSelector.invoke( |
| 180 |
0 |
methodName, parameterTypes, anObject, new Object[] { p1, p2 } ); |
| 181 |
|
} |
| 182 |
|
|
| 183 |
|
private class Receiver implements XMLRPCReceiver |
| 184 |
|
{ |
| 185 |
|
public Object result; |
| 186 |
|
public int faultCode; |
| 187 |
|
public String faultString; |
| 188 |
|
|
| 189 |
1 |
public Receiver() |
| 190 |
8 |
{ |
| 191 |
9 |
result = null; |
| 192 |
9 |
faultCode = -1; |
| 193 |
9 |
faultString = null; |
| 194 |
9 |
} |
| 195 |
|
|
| 196 |
|
public void request( |
| 197 |
|
String aMethodName, Object[] aParameterArray ) |
| 198 |
|
{ |
| 199 |
0 |
throw new WotonomyException( |
| 200 |
0 |
"Invalid response: Expected response but received request." ); |
| 201 |
|
} |
| 202 |
|
|
| 203 |
|
public void response( |
| 204 |
|
Object aResult ) |
| 205 |
|
{ |
| 206 |
0 |
result = aResult; |
| 207 |
0 |
faultCode = -1; |
| 208 |
0 |
faultString = null; |
| 209 |
0 |
} |
| 210 |
|
|
| 211 |
|
public void fault( |
| 212 |
|
int aFaultCode, String aFaultString) |
| 213 |
|
{ |
| 214 |
0 |
result = null; |
| 215 |
0 |
faultCode = aFaultCode; |
| 216 |
0 |
faultString = aFaultString; |
| 217 |
0 |
} |
| 218 |
|
} |
| 219 |
|
|
| 220 |
|
|
| 221 |
|
} |
| 222 |
|
|
| 223 |
|
|
| 224 |
|
|
| 225 |
|
|
| 226 |
|
|
| 227 |
|
|
| 228 |
|
|
| 229 |
|
|
| 230 |
|
|
| 231 |
|
|
| 232 |
|
|
| 233 |
|
|
| 234 |
|
|
| 235 |
|
|
| 236 |
|
|
| 237 |
|
|
| 238 |
|
|