KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > apache > xmlrpc > XmlRpcWorker


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
17
18 package org.apache.xmlrpc;
19
20 import java.io.InputStream JavaDoc;
21
22 /**
23  * Tie together the XmlRequestProcessor and XmlResponseProcessor to handle
24  * a request serially in a single thread.
25  *
26  * @author <a HREF="mailto:hannes@apache.org">Hannes Wallnoefer</a>
27  * @author Daniel L. Rall
28  * @author <a HREF="mailto:andrew@kungfoocoder.org">Andrew Evers</a>
29  * @see org.apache.xmlrpc.XmlRpcServer
30  * @since 1.2
31  */

32 public class XmlRpcWorker
33 {
34     protected XmlRpcRequestProcessor requestProcessor;
35     protected XmlRpcResponseProcessor responseProcessor;
36     protected XmlRpcHandlerMapping handlerMapping;
37
38     /**
39      * Create a new instance that will use the specified mapping.
40      */

41     public XmlRpcWorker(XmlRpcHandlerMapping handlerMapping)
42     {
43       requestProcessor = new XmlRpcRequestProcessor();
44       responseProcessor = new XmlRpcResponseProcessor();
45       this.handlerMapping = handlerMapping;
46     }
47
48     /**
49      * Pass the specified request to the handler. The handler should be an
50      * instance of {@link org.apache.xmlrpc.XmlRpcHandler} or
51      * {@link org.apache.xmlrpc.AuthenticatedXmlRpcHandler}.
52      *
53      * @param handler the handler to call.
54      * @param request the request information to use.
55      * @param context the context information to use.
56      * @return Object the result of calling the handler.
57      * @throws ClassCastException if the handler is not of an appropriate type.
58      * @throws NullPointerException if the handler is null.
59      * @throws Exception if the handler throws an exception.
60      */

61     protected static Object JavaDoc invokeHandler(Object JavaDoc handler, XmlRpcServerRequest request, XmlRpcContext context)
62         throws Exception JavaDoc
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 JavaDoc
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 JavaDoc("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     /**
111      * Decode, process and encode the response or exception for an XML-RPC
112      * request. This method executes the handler method with the default context.
113      */

114     public byte[] execute(InputStream JavaDoc is, String JavaDoc user, String JavaDoc password)
115     {
116         return execute(is, defaultContext(user, password));
117     }
118
119     /**
120      * Decode, process and encode the response or exception for an XML-RPC
121      * request. This method executes will pass the specified context to the
122      * handler if the handler supports context.
123      *
124      * @param is the InputStream to read the request from.
125      * @param context the context for the request (may be null).
126      * @return byte[] the response.
127      * @throws org.apache.xmlrpc.ParseFailed if the request could not be parsed.
128      * @throws org.apache.xmlrpc.AuthenticationFailed if the handler for the
129      * specific method required authentication and insufficient credentials were
130      * supplied.
131      */

132     public byte[] execute(InputStream JavaDoc 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 JavaDoc handler = handlerMapping.getHandler(request.
145                                                        getMethodName());
146             Object JavaDoc 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 JavaDoc 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     /**
178      * Factory method to return a default context object for the execute() method.
179      * This method can be overridden to return a custom sub-class of XmlRpcContext.
180      *
181      * @param user the username of the user making the request.
182      * @param password the password of the user making the request.
183      * @return XmlRpcContext the context for the reqeust.
184      */

185     protected XmlRpcContext defaultContext(String JavaDoc user, String JavaDoc password)
186     {
187         return new DefaultXmlRpcContext(user, password, handlerMapping);
188     }
189 }
190
Popular Tags