KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > openlaszlo > data > XMLRPCDataSource


1 /* ****************************************************************************
2  * XMLRPCDataSource.java
3 * ****************************************************************************/

4
5 /* J_LZ_COPYRIGHT_BEGIN *******************************************************
6 * Copyright 2001-2004 Laszlo Systems, Inc. All Rights Reserved. *
7 * Use is subject to license terms. *
8 * J_LZ_COPYRIGHT_END *********************************************************/

9
10 package org.openlaszlo.data;
11
12 import java.io.*;
13 import java.util.*;
14 import java.net.MalformedURLException JavaDoc;
15 import javax.servlet.http.*;
16 import org.apache.xmlrpc.*;
17 import org.openlaszlo.server.LPS;
18 // LoadCount belongs in utils
19
import org.openlaszlo.servlets.LoadCount;
20 import org.openlaszlo.xml.internal.*;
21 import org.openlaszlo.media.MimeType;
22 import org.apache.log4j.*;
23
24 /**
25  *
26  */

27 public class XMLRPCDataSource extends DataSource
28 {
29     private static Logger mLogger = Logger.getLogger(XMLRPCDataSource.class);
30
31     XmlRpcServer xmlrpc = new XmlRpcServer();
32
33     static long mLastCleared = -1;
34     static LoadCount mXMLRPCLoad = new LoadCount(10);
35
36     public XMLRPCDataSource() {
37         clearLoadInfo();
38     }
39
40     /**
41      * @return unique name of this data source
42      */

43     public String JavaDoc name()
44     {
45         return "xmlrpc";
46     }
47
48     /**
49      * Sends system information to client.
50      *
51      * @throws DataSourceException if there was a problem retrieving or sending
52      * the data.
53      */

54     public Data getData(String JavaDoc app, HttpServletRequest req,
55                         HttpServletResponse res, long lastModifiedTime)
56         throws DataSourceException {
57         mLogger.debug("getData");
58
59         int swfversion = LPS.getSWFVersionNum(req);
60         try {
61             if (! req.getMethod().equals("POST"))
62                 return compileFault("Remote request must be POST", swfversion);
63
64             String JavaDoc url = getHTTPURL(getURL(req));
65             if (url == null) {
66                 return compileFault("invalid url specified: " + url, swfversion);
67             }
68
69             String JavaDoc postbody = req.getParameter("lzpostbody");
70             if (postbody != null) {
71                 url += "?lzpostbody=" + postbody;
72             }
73
74             long t0, t1;
75             t0 = System.currentTimeMillis();
76             mXMLRPCLoad.increment();
77             try {
78                 Data data = HTTPDataSource.getHTTPData(req, res, url, -1);
79                 return new XMLRPCData(data.getAsString().getBytes(), swfversion);
80             } finally {
81                 t1 = System.currentTimeMillis();
82                 mXMLRPCLoad.decrement((int)(t1-t0));
83             }
84
85         } catch (Exception JavaDoc e) {
86             return compileFault(e, swfversion);
87         }
88     }
89
90     String JavaDoc getHTTPURL(String JavaDoc url) {
91         if (url != null && url.startsWith("xmlrpc://"))
92             return "http" + url.substring(6);
93         return null;
94     }
95
96
97     /**
98      * Compile fault exception message.
99      */

100     Data compileFault(Exception JavaDoc e, int swfversion) {
101         mLogger.error("compileFault", e);
102         return compileFault(e.getMessage(), swfversion);
103     }
104
105     /**
106      * Compile fault response.
107      */

108     Data compileFault(String JavaDoc mesg, int swfversion) {
109         mLogger.error("compileFault mesg: " + mesg);
110         try {
111             byte[] d = XMLRPCCompiler.compileFault(XMLUtils.escapeXml(mesg),
112                                                    swfversion);
113             return new XMLRPCData().setResult(d);
114         } catch (Exception JavaDoc e) {
115             mLogger.error("Exception", e);
116             // this is an error since we can't build a fault response
117
throw new Error JavaDoc(e.getMessage());
118         }
119     }
120
121     public static void clearLoadInfo() {
122         mXMLRPCLoad.reset();
123         mLastCleared = System.currentTimeMillis();
124     }
125
126     public static void toXML(StringBuffer JavaDoc sb) {
127         Date lc = new Date(mLastCleared);
128         sb.append("<xmlrpcinfo ")
129             .append(" last-cleared=\"").append(lc).append("\"")
130             .append(">");
131         sb.append(mXMLRPCLoad.toXML("xmlrpc_load"));
132         sb.append("</xmlrpcinfo>");
133     }
134
135     /**
136      * A data object to hold an xmlrpc response.
137      */

138     public class XMLRPCData extends Data
139     {
140         byte[] mResult;
141
142         public XMLRPCData() { }
143
144         public XMLRPCData(byte[] result, int swfversion)
145             throws IOException {
146             InputStreamReader reader = new InputStreamReader(new ByteArrayInputStream(result));
147             mResult = XMLRPCCompiler.compile(reader, result.length, swfversion);
148         }
149
150         public String JavaDoc getMimeType() {
151             return MimeType.SWF;
152         }
153
154
155         public XMLRPCData setResult(byte[] result) {
156             mResult = result;
157             return this;
158         }
159
160         /**
161          * @return the encoded XML
162          */

163         public InputStream getInputStream()
164             throws IOException {
165             return new ByteArrayInputStream(mResult);
166         }
167
168         public long size() {
169             return mResult.length;
170         }
171     }
172 }
173
Popular Tags