1 16 17 18 package org.apache.xmlrpc; 19 20 import java.io.InputStream ; 21 22 32 public class XmlRpcWorker 33 { 34 protected XmlRpcRequestProcessor requestProcessor; 35 protected XmlRpcResponseProcessor responseProcessor; 36 protected XmlRpcHandlerMapping handlerMapping; 37 38 41 public XmlRpcWorker(XmlRpcHandlerMapping handlerMapping) 42 { 43 requestProcessor = new XmlRpcRequestProcessor(); 44 responseProcessor = new XmlRpcResponseProcessor(); 45 this.handlerMapping = handlerMapping; 46 } 47 48 61 protected static Object invokeHandler(Object handler, XmlRpcServerRequest request, XmlRpcContext context) 62 throws Exception 63 { 64 long now = 0; 65 66 try 67 { 68 if (XmlRpc.debug) 69 { 70 now = System.currentTimeMillis(); 71 } 72 if (handler == null) 73 { 74 throw new NullPointerException 75 ("Null handler passed to XmlRpcWorker.invokeHandler"); 76 } 77 else if (handler instanceof ContextXmlRpcHandler) 78 { 79 return ((ContextXmlRpcHandler) handler).execute 80 (request.getMethodName(), request.getParameters(), context); 81 } 82 else if (handler instanceof XmlRpcHandler) 83 { 84 return ((XmlRpcHandler) handler).execute 85 (request.getMethodName(), request.getParameters()); 86 } 87 else if (handler instanceof AuthenticatedXmlRpcHandler) 88 { 89 return ((AuthenticatedXmlRpcHandler) handler) 90 .execute(request.getMethodName(), request.getParameters(), 91 context.getUserName(), context.getPassword()); 92 } 93 else 94 { 95 throw new ClassCastException ("Handler class " + 96 handler.getClass().getName() + 97 " is not a valid XML-RPC handler"); 98 } 99 } 100 finally 101 { 102 if (XmlRpc.debug) 103 { 104 System.out.println("Spent " + (System.currentTimeMillis() - now) 105 + " millis processing request"); 106 } 107 } 108 } 109 110 114 public byte[] execute(InputStream is, String user, String password) 115 { 116 return execute(is, defaultContext(user, password)); 117 } 118 119 132 public byte[] execute(InputStream is, XmlRpcContext context) 133 { 134 long now = 0; 135 136 if (XmlRpc.debug) 137 { 138 now = System.currentTimeMillis(); 139 } 140 141 try 142 { 143 XmlRpcServerRequest request = requestProcessor.decodeRequest(is); 144 Object handler = handlerMapping.getHandler(request. 145 getMethodName()); 146 Object response = invokeHandler(handler, request, context); 147 return responseProcessor.encodeResponse 148 (response, requestProcessor.getEncoding()); 149 } 150 catch (AuthenticationFailed alertCallerAuth) 151 { 152 throw alertCallerAuth; 153 } 154 catch (ParseFailed alertCallerParse) 155 { 156 throw alertCallerParse; 157 } 158 catch (Exception x) 159 { 160 if (XmlRpc.debug) 161 { 162 x.printStackTrace(); 163 } 164 return responseProcessor.encodeException 165 (x, requestProcessor.getEncoding()); 166 } 167 finally 168 { 169 if (XmlRpc.debug) 170 { 171 System.out.println("Spent " + (System.currentTimeMillis() - now) 172 + " millis in request/process/response"); 173 } 174 } 175 } 176 177 185 protected XmlRpcContext defaultContext(String user, String password) 186 { 187 return new DefaultXmlRpcContext(user, password, handlerMapping); 188 } 189 } 190 | Popular Tags |