adding all of botlist, initial add
[botlist.git] / openbotlist / apps_src / scala / lift / agents / AgentUtil.scala
blobfa9e995435cb995cdb6383bf353232b08d03d2a4
1 //
2 // Author: Berlin Brown
3 // Remote Agents
4 // Date: 2/2/2008
6 package org.spirit.lift.agents
8 import java.util.Random
9 import org.springframework.context.{ApplicationContext => AC}
10 import org.spirit.dao.impl.{BotListUserVisitLogDAOImpl => LogDAO}
11 import org.spirit.dao.impl.{BotListSessionRequestLogDAOImpl => SessDAO}
12 import org.spirit.bean.impl.{BotListUserVisitLog => Log}
13 import org.spirit.bean.impl.{BotListSessionRequestLog => Sess}
15 import org.spirit.dao.impl.{BotListForumGroupDAOImpl => ForumDAO}
16 import org.spirit.bean.impl.{BotListForumGroup => ForumGroup}
17 import org.spirit.bean.impl.{BotListUserComments => ChatComment}
19 import net.liftweb.http._
20 import net.liftweb.http.S._
21 import net.liftweb.http.S
22 import scala.xml.{NodeSeq, Text, Group}
23 import net.liftweb.util.Helpers._
24 import javax.servlet.http.{HttpServlet, HttpServletRequest, HttpServletResponse, HttpSession}
26 import scala.xml._
27 import org.spirit.lift.agents.model.{AgentMessage}
29 object AgentUtil {
30 /**
31 * Create a unique string id based on; random number and client ip.
33 def uniqueMsgId (clientip: String) : String = {
34 val t = (System.currentTimeMillis) + 1
35 val r = new Random()
36 val rand_long = r.nextLong
37 hexEncode(md5( (clientip + rand_long).getBytes ))
39 def auditLogPage (dao: LogDAO, request: HttpServletRequest, curPage: String) = {
40 val link = new Log()
41 link.setRequestUri(request.getRequestURI)
42 link.setRequestPage(curPage)
43 link.setHost(request.getHeader("host"))
44 link.setReferer(request.getHeader("referer"))
45 link.setRemoteHost(request.getRemoteAddr())
46 link.setUserAgent(request.getHeader("user-agent"))
47 dao.createVisitLog(link)
49 def createBotForumMsg(httpRequest: HttpServletRequest, user: String, msg: String, title: String) {
50 // TODO: do not hard code the forum id.
51 val chatForumGroupId = 3
52 val bean_obj = AgentUtil.getAC(httpRequest).getBean("forumGroupDaoBean")
53 val forum_dao = bean_obj.asInstanceOf[ForumDAO]
55 // Populate the comment data
56 val comment = new ChatComment
57 comment.setFullName(user)
58 comment.setSubject(title)
59 comment.setMessage(msg)
61 // Hibernate oriented approach for creating a comment topic
62 val sessionFactory = forum_dao.getSessionFactory
63 val hbm_session = sessionFactory.openSession
64 val tx = hbm_session.beginTransaction
66 val forum_obj = hbm_session.load("org.spirit.bean.impl.BotListForumGroup",
67 new java.lang.Long(chatForumGroupId), null)
68 val forum = forum_obj.asInstanceOf[ForumGroup]
69 comment.setForumId(new java.lang.Long(chatForumGroupId))
70 val topics = forum.getTopics()
71 // TODO: with release 2.7, this code does not work.
72 //topics.add(comment)
73 tx.commit
74 hbm_session.close
76 def buildRequestSession (dao: SessDAO, request: HttpServletRequest, key:String, value:String):String = {
77 val sess = new Sess()
78 val remote_host = request.getRemoteAddr
79 val u_id = uniqueMsgId(remote_host)
80 sess.setRemoteHost(remote_host)
81 sess.setMsgKey(key)
82 sess.setMsgValue(value)
83 sess.setRequestId(u_id)
84 dao.createSessionLog(sess)
85 u_id
87 def getAC(request: HttpServletRequest) = {
88 val sess = request.getSession
89 val sc = sess.getServletContext
91 // Cast to the application context
92 val acobj = sc.getAttribute("org.springframework.web.servlet.FrameworkServlet.CONTEXT.botlistings")
93 acobj.asInstanceOf[AC]
95 def invalidXMLResponse : XmlResponse = {
96 XmlResponse(
97 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
98 <agentmsg>
99 <botid>serverbot</botid>
100 <message>Oops. Something went wrong.</message>
101 <status>500</status>
102 </agentmsg>
103 </rdf:RDF>) }
105 def validPostXMLResponse : XmlResponse = {
106 XmlResponse(
107 <rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#">
108 <agentmsg>
109 <botid>serverbot</botid>
110 <message>Thanks, I hope you enjoy your cake.</message>
111 <status>200</status>
112 </agentmsg>
113 </rdf:RDF>) }
115 } // End of Object