1 ########################################
3 # jruby - bot_remote_sync
5 # (DEPRECATED approach for remoting)
6 ########################################
9 include_class 'java.net.HttpURLConnection' unless defined? HttpURLConnection
10 include_class 'com.ibatis.sqlmap.client.SqlMapClient' unless defined? SqlMapClient
11 include_class 'com.ibatis.sqlmap.client.SqlMapClientBuilder' unless defined? SqlMapClientBuilder
12 include_class 'com.ibatis.common.resources.Resources' unless defined? Resources
14 include_class 'org.spirit.loadtest.LoadTestManager' unless defined? LoadTestManager
16 include_class 'org.spirit.bean.impl.BotListSystemAuditLog' unless defined? BotListSystemAuditLog
21 attr_accessor :key_request_url, :remote_send_url, :sqlMapper
23 @key_request_url = nil
24 @remote_send_url = nil
30 def shortenField(str_val)
31 str = str_val[0, MAX_LEN_FIELD] if str_val.length > MAX_LEN_FIELD
32 return str if !str.nil?
36 def urlItemCleanup(item)
39 if !item.urlTitle.nil?
40 item.urlTitle.gsub!("\,", "")
41 item.urlTitle.gsub!(/\r\n?/, "")
43 item.urlTitle = shortenField(item.urlTitle)
45 # Cleanup URL with encoded value
46 item.mainUrl.gsub!("\,", "%2C") if !item.mainUrl.nil?
49 return false if item.nil?
50 return false if item.urlTitle.nil? or item.urlTitle.strip.empty?
51 return false if item.mainUrl.nil? or item.mainUrl.strip.empty?
57 feedItems = @sqlMapper.queryForList("selectAllFeedItems")
59 feedItems.each { |item|
62 formatted_data = "#{item.mainUrl},#{item.urlTitle},#{item.urlTitle}"
63 buf << formatted_data << "\n"
68 @feed_items = feedItems
71 def sendFeedItemData()
73 # Connect for example: "http://localhost:8080/botlist/spring/pipes/remotesync.html"
74 # And then post to http://localhost:8080/botlist/spring/pipes/remotesync_send.html
75 connect_res = LoadTestManager.connectURL(@key_request_url, false)
76 requestKey = connect_res[1]
79 puts "INFO: there are no feed items to send"
83 # Submit key and other link data
85 "developerKey" => "none",
86 "remoteData" => @send_data,
87 "remoteSyncKey" => requestKey
90 str_url = @remote_send_url
91 url = LoadTestManager.getSSLURL(str_url)
92 HttpURLConnection.setFollowRedirects(false)
93 conn = url.openConnection()
94 post_res = LoadTestManager.postDataSSL(reqestMap, conn, url, str_url, false, true)
96 # Update that we sent the content
97 # Very optmistic case, update all records regardless if they were actual saved
98 @feed_items.each { |item|
100 res = sqlMapper.update("updateFeedItem", item)
101 rescue Exception => e1
105 # Update the audit table to inform that the remote sync has completed
106 serv_response = post_res[1]
107 log = BotListSystemAuditLog.new
108 log.applicationName = "client_remote_sync"
109 log.message = "remote sync client res=[#{serv_response}]"
110 log.logLevel = "INFO"
111 log.messageId = "000051"
112 log.sendTo = "botlist@botlist.com"
114 res = sqlMapper.update("insertAuditLog", log)
115 rescue Exception => e1
119 rescue Exception => e
127 def connect(key_url, send_url)
128 puts "INFO: keyurl=#{key_url}"
129 puts "INFO: syncurl=#{send_url}"
130 reader = Resources.getResourceAsReader("conf/SqlMapConfig.xml")
131 sqlMapper = SqlMapClientBuilder.buildSqlMapClient(reader)
134 remoteSync = RemoteSync.new
135 remoteSync.sqlMapper = sqlMapper
136 remoteSync.key_request_url = key_url
137 remoteSync.remote_send_url = send_url
139 remoteSync.sendFeedItemData
145 puts "usage: bot_remote_sync.rb <key url> <post to url>"
151 start_time = Time.now
156 connect(key_url, send_url)
158 diff_time = end_time - start_time
159 printf "processing remote sync in %.5f s\n", diff_time