/xmlrpc/vendor/current/src/java/org/apache/xmlrpc/XmlRpcWorker.java

https://gitlab.com/manoj-makkuboy/magnetism · Java · 189 lines · 111 code · 12 blank · 66 comment · 13 complexity · 1c54f8ee4bdae75ebd580664f13ca5e7 MD5 · raw file

  1. /*
  2. * Copyright 1999,2005 The Apache Software Foundation.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. package org.apache.xmlrpc;
  17. import java.io.InputStream;
  18. /**
  19. * Tie together the XmlRequestProcessor and XmlResponseProcessor to handle
  20. * a request serially in a single thread.
  21. *
  22. * @author <a href="mailto:hannes@apache.org">Hannes Wallnoefer</a>
  23. * @author Daniel L. Rall
  24. * @author <a href="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
  25. * @see org.apache.xmlrpc.XmlRpcServer
  26. * @since 1.2
  27. */
  28. public class XmlRpcWorker
  29. {
  30. protected XmlRpcRequestProcessor requestProcessor;
  31. protected XmlRpcResponseProcessor responseProcessor;
  32. protected XmlRpcHandlerMapping handlerMapping;
  33. /**
  34. * Create a new instance that will use the specified mapping.
  35. */
  36. public XmlRpcWorker(XmlRpcHandlerMapping handlerMapping)
  37. {
  38. requestProcessor = new XmlRpcRequestProcessor();
  39. responseProcessor = new XmlRpcResponseProcessor();
  40. this.handlerMapping = handlerMapping;
  41. }
  42. /**
  43. * Pass the specified request to the handler. The handler should be an
  44. * instance of {@link org.apache.xmlrpc.XmlRpcHandler} or
  45. * {@link org.apache.xmlrpc.AuthenticatedXmlRpcHandler}.
  46. *
  47. * @param handler the handler to call.
  48. * @param request the request information to use.
  49. * @param context the context information to use.
  50. * @return Object the result of calling the handler.
  51. * @throws ClassCastException if the handler is not of an appropriate type.
  52. * @throws NullPointerException if the handler is null.
  53. * @throws Exception if the handler throws an exception.
  54. */
  55. protected static Object invokeHandler(Object handler, XmlRpcServerRequest request, XmlRpcContext context)
  56. throws Exception
  57. {
  58. long now = 0;
  59. try
  60. {
  61. if (XmlRpc.debug)
  62. {
  63. now = System.currentTimeMillis();
  64. }
  65. if (handler == null)
  66. {
  67. throw new NullPointerException
  68. ("Null handler passed to XmlRpcWorker.invokeHandler");
  69. }
  70. else if (handler instanceof ContextXmlRpcHandler)
  71. {
  72. return ((ContextXmlRpcHandler) handler).execute
  73. (request.getMethodName(), request.getParameters(), context);
  74. }
  75. else if (handler instanceof XmlRpcHandler)
  76. {
  77. return ((XmlRpcHandler) handler).execute
  78. (request.getMethodName(), request.getParameters());
  79. }
  80. else if (handler instanceof AuthenticatedXmlRpcHandler)
  81. {
  82. return ((AuthenticatedXmlRpcHandler) handler)
  83. .execute(request.getMethodName(), request.getParameters(),
  84. context.getUserName(), context.getPassword());
  85. }
  86. else
  87. {
  88. throw new ClassCastException("Handler class " +
  89. handler.getClass().getName() +
  90. " is not a valid XML-RPC handler");
  91. }
  92. }
  93. finally
  94. {
  95. if (XmlRpc.debug)
  96. {
  97. System.out.println("Spent " + (System.currentTimeMillis() - now)
  98. + " millis processing request");
  99. }
  100. }
  101. }
  102. /**
  103. * Decode, process and encode the response or exception for an XML-RPC
  104. * request. This method executes the handler method with the default context.
  105. */
  106. public byte[] execute(InputStream is, String user, String password)
  107. {
  108. return execute(is, defaultContext(user, password));
  109. }
  110. /**
  111. * Decode, process and encode the response or exception for an XML-RPC
  112. * request. This method executes will pass the specified context to the
  113. * handler if the handler supports context.
  114. *
  115. * @param is the InputStream to read the request from.
  116. * @param context the context for the request (may be null).
  117. * @return byte[] the response.
  118. * @throws org.apache.xmlrpc.ParseFailed if the request could not be parsed.
  119. * @throws org.apache.xmlrpc.AuthenticationFailed if the handler for the
  120. * specific method required authentication and insufficient credentials were
  121. * supplied.
  122. */
  123. public byte[] execute(InputStream is, XmlRpcContext context)
  124. {
  125. long now = 0;
  126. if (XmlRpc.debug)
  127. {
  128. now = System.currentTimeMillis();
  129. }
  130. try
  131. {
  132. XmlRpcServerRequest request = requestProcessor.decodeRequest(is);
  133. Object handler = handlerMapping.getHandler(request.
  134. getMethodName());
  135. Object response = invokeHandler(handler, request, context);
  136. return responseProcessor.encodeResponse
  137. (response, requestProcessor.getEncoding());
  138. }
  139. catch (AuthenticationFailed alertCallerAuth)
  140. {
  141. throw alertCallerAuth;
  142. }
  143. catch (ParseFailed alertCallerParse)
  144. {
  145. throw alertCallerParse;
  146. }
  147. catch (Exception x)
  148. {
  149. if (XmlRpc.debug)
  150. {
  151. x.printStackTrace();
  152. }
  153. return responseProcessor.encodeException
  154. (x, requestProcessor.getEncoding());
  155. }
  156. finally
  157. {
  158. if (XmlRpc.debug)
  159. {
  160. System.out.println("Spent " + (System.currentTimeMillis() - now)
  161. + " millis in request/process/response");
  162. }
  163. }
  164. }
  165. /**
  166. * Factory method to return a default context object for the execute() method.
  167. * This method can be overridden to return a custom sub-class of XmlRpcContext.
  168. *
  169. * @param user the username of the user making the request.
  170. * @param password the password of the user making the request.
  171. * @return XmlRpcContext the context for the reqeust.
  172. */
  173. protected XmlRpcContext defaultContext(String user, String password)
  174. {
  175. return new DefaultXmlRpcContext(user, password, handlerMapping);
  176. }
  177. }