1 4 5 9 10 package org.openlaszlo.data; 11 12 import java.io.*; 13 import java.util.*; 14 import java.net.MalformedURLException ; 15 import javax.servlet.http.*; 16 import org.apache.xmlrpc.*; 17 import org.openlaszlo.server.LPS; 18 import org.openlaszlo.servlets.LoadCount; 20 import org.openlaszlo.xml.internal.*; 21 import org.openlaszlo.media.MimeType; 22 import org.apache.log4j.*; 23 24 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 43 public String name() 44 { 45 return "xmlrpc"; 46 } 47 48 54 public Data getData(String 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 url = getHTTPURL(getURL(req)); 65 if (url == null) { 66 return compileFault("invalid url specified: " + url, swfversion); 67 } 68 69 String 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 e) { 86 return compileFault(e, swfversion); 87 } 88 } 89 90 String getHTTPURL(String url) { 91 if (url != null && url.startsWith("xmlrpc://")) 92 return "http" + url.substring(6); 93 return null; 94 } 95 96 97 100 Data compileFault(Exception e, int swfversion) { 101 mLogger.error("compileFault", e); 102 return compileFault(e.getMessage(), swfversion); 103 } 104 105 108 Data compileFault(String 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 e) { 115 mLogger.error("Exception", e); 116 throw new Error (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 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 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 getMimeType() { 151 return MimeType.SWF; 152 } 153 154 155 public XMLRPCData setResult(byte[] result) { 156 mResult = result; 157 return this; 158 } 159 160 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 |