Skip to content

Instantly share code, notes, and snippets.

@kindlychung
Created October 29, 2015 14:49
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kindlychung/7a6badc72140a5094b5b to your computer and use it in GitHub Desktop.
Save kindlychung/7a6badc72140a5094b5b to your computer and use it in GitHub Desktop.
package bootstrap.liftweb
import net.liftweb._
import util._
import Helpers._
import common._
import http._
import sitemap._
import Loc._
import net.liftmodules.JQueryModule
import net.liftweb.http.js.jquery._
import net.liftweb.common.Logger
/**
* A class that's instantiated early and run. It allows the application
* to modify lift's environment
*/
class Boot extends Logger {
def boot {
// where to search snippet
LiftRules.addToPackages("code")
configureMailer()
val canManage_? = If(
// always true means everybody can manage
() => {
true
},
// if false, redirect to root
() => RedirectResponse("/"))
val isAdmin_? = If(
// always false means nobody is admin
() => {
false
},
// if false, redirect to /contacts/list
() => RedirectWithState("/contacts/list", MessageState("Authorized personnel only" -> NoticeType.Warning)))
// Build SiteMap
val entries = List(
Menu.i("Home") / "index", // the simple way to declare a menu
Menu.i("List contacts") / "contacts" / "list" >> canManage_?,
Menu.i("Create") / "contacts" / "create" >> canManage_?,
Menu.i("Edit") / "contacts" / "edit" >> canManage_?,
Menu.i("Delete") / "contacts" / "delete" >> canManage_?,
Menu.i("View") / "contacts" / "view" >> canManage_?,
Menu.i("Send email") / "contacts" / "send",
// more complex because this menu allows anything in the
// /static path to be visible
Menu(Loc("Static", Link(List("static"), true, "/static/index"),
"Static Content")))
// set the sitemap. Note if you don't want access control for
// each page, just comment this line out.
LiftRules.setSiteMap(SiteMap(entries: _*))
//Show the spinny image when an Ajax call starts
LiftRules.ajaxStart =
Full(() => LiftRules.jsArtifacts.show("ajax-loader").cmd)
// Make the spinny image go away when it ends
LiftRules.ajaxEnd =
Full(() => LiftRules.jsArtifacts.hide("ajax-loader").cmd)
// Force the request to be UTF-8
LiftRules.early.append(_.setCharacterEncoding("UTF-8"))
//Init the jQuery module, see http://liftweb.net/jquery for more information.
LiftRules.jsArtifacts = JQueryArtifacts
JQueryModule.InitParam.JQuery = JQueryModule.JQuery191
JQueryModule.init()
}
def configureMailer(): Unit = {
import javax.mail.{PasswordAuthentication, Authenticator}
System.setProperty("mail.smtp.starttls.enable", "true")
System.setProperty("mail.smtp.ssl.enable", "true")
System.setProperty("mail.smtp.host", "smtp.gmail.com")
System.setProperty("mail.smtp.auth", "true")
System.setProperty("mail.smtp.port", "587")
Mailer.authenticator = for {
user <- {val u = Props.get("mail.user"); info(u); u}
pass <- {val p = Props.get("mail.password"); info(p); p}
} yield new Authenticator {
override def getPasswordAuthentication =
new PasswordAuthentication(user, pass)
}
}
}
Exception occured while processing /contacts/send
Message: javax.mail.AuthenticationFailedException
javax.mail.Service.connect(Service.java:319)
javax.mail.Service.connect(Service.java:169)
javax.mail.Service.connect(Service.java:118)
javax.mail.Transport.send0(Transport.java:188)
javax.mail.Transport.send(Transport.java:118)
net.liftweb.util.Mailer$$anon$1$$anonfun$$lessinit$greater$1.apply(Mailer.scala:191)
net.liftweb.util.Mailer$$anon$1$$anonfun$$lessinit$greater$1.apply(Mailer.scala:191)
net.liftweb.util.Mailer$class.performTransportSend(Mailer.scala:185)
net.liftweb.util.Mailer$.performTransportSend(Mailer.scala:32)
net.liftweb.util.Mailer$class.msgSendImpl(Mailer.scala:260)
net.liftweb.util.Mailer$.msgSendImpl(Mailer.scala:32)
code.SendEmail$.send_$bang(SendEmail.scala:12)
code.snippet.SEmail.sendNow(SEmail.scala:15)
code.snippet.SEmail$$anonfun$sendEmail$1$$anonfun$apply$1.apply$mcV$sp(SEmail.scala:24)
code.snippet.SEmail$$anonfun$sendEmail$1$$anonfun$apply$1.apply(SEmail.scala:24)
code.snippet.SEmail$$anonfun$sendEmail$1$$anonfun$apply$1.apply(SEmail.scala:24)
net.liftweb.http.SHtml$$anonfun$link$1.apply(SHtml.scala:1075)
net.liftweb.http.SHtml$$anonfun$link$1.apply(SHtml.scala:1075)
net.liftweb.http.S$LFuncHolder.apply(S.scala:171)
net.liftweb.http.S$ProxyFuncHolder.apply(S.scala:114)
net.liftweb.http.LiftSession$$anonfun$net$liftweb$http$LiftSession$$buildFunc$1$2.apply(LiftSession.scala:783)
net.liftweb.http.LiftSession$$anonfun$21$$anonfun$apply$37.apply(LiftSession.scala:797)
net.liftweb.http.LiftSession$$anonfun$21$$anonfun$apply$37.apply(LiftSession.scala:797)
scala.collection.immutable.List.map(List.scala:272)
net.liftweb.http.LiftSession$$anonfun$21.apply(LiftSession.scala:797)
net.liftweb.http.LiftSession$$anonfun$21.apply(LiftSession.scala:788)
scala.collection.immutable.List.flatMap(List.scala:327)
net.liftweb.http.LiftSession.runParams(LiftSession.scala:787)
net.liftweb.http.LiftSession.processRequest(LiftSession.scala:1234)
net.liftweb.http.LiftServlet.net$liftweb$http$LiftServlet$$dispatchStatefulRequest(LiftServlet.scala:423)
net.liftweb.http.LiftServlet$$anonfun$doSession$1$1.apply(LiftServlet.scala:301)
net.liftweb.http.LiftServlet$$anonfun$doSession$1$1.apply(LiftServlet.scala:301)
net.liftweb.http.S$class.net$liftweb$http$S$$wrapQuery(S.scala:1470)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_nest2InnerInit$1$$anonfun$apply$37.apply(S.scala:1635)
net.liftweb.http.S$class.net$liftweb$http$S$$doAround(S.scala:1399)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_nest2InnerInit$1.apply(S.scala:1633)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$class.net$liftweb$http$S$$_nest2InnerInit(S.scala:1632)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_innerInit$1$$anonfun$apply$41$$anonfun$apply$42$$anonfun$apply$43$$anonfun$apply$44.apply(S.scala:1676)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$$anonfun$withReq$1.apply(S.scala:1686)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$class.withReq(S.scala:1685)
net.liftweb.http.S$.withReq(S.scala:47)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_innerInit$1$$anonfun$apply$41$$anonfun$apply$42$$anonfun$apply$43.apply(S.scala:1672)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_innerInit$1$$anonfun$apply$41$$anonfun$apply$42.apply(S.scala:1670)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_innerInit$1$$anonfun$apply$41.apply(S.scala:1669)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_innerInit$1.apply(S.scala:1668)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$class.net$liftweb$http$S$$_innerInit(S.scala:1667)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_init$1$$anonfun$apply$47$$anonfun$apply$48$$anonfun$apply$49$$anonfun$apply$50$$anonfun$apply$51.apply(S.scala:1709)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_init$1$$anonfun$apply$47$$anonfun$apply$48$$anonfun$apply$49$$anonfun$apply$50.apply(S.scala:1707)
net.liftweb.http.CoreRequestVarHandler$class.apply(Vars.scala:605)
net.liftweb.http.RequestVarHandler$.apply(Vars.scala:507)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_init$1$$anonfun$apply$47$$anonfun$apply$48$$anonfun$apply$49.apply(S.scala:1706)
net.liftweb.http.CoreRequestVarHandler$class.apply(Vars.scala:605)
net.liftweb.http.TransientRequestVarHandler$.apply(Vars.scala:520)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_init$1$$anonfun$apply$47$$anonfun$apply$48.apply(S.scala:1705)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_init$1$$anonfun$apply$47.apply(S.scala:1704)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$$anonfun$net$liftweb$http$S$$_init$1.apply(S.scala:1703)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.S$class.net$liftweb$http$S$$_init(S.scala:1702)
net.liftweb.http.S$class.init(S.scala:1284)
net.liftweb.http.S$.init(S.scala:47)
net.liftweb.http.LiftServlet.doSession$1(LiftServlet.scala:300)
net.liftweb.http.LiftServlet.doService(LiftServlet.scala:310)
net.liftweb.http.LiftServlet$$anonfun$doIt$1$1.apply(LiftServlet.scala:148)
net.liftweb.http.LiftServlet$$anonfun$doIt$1$1.apply(LiftServlet.scala:147)
net.liftweb.util.TimeHelpers$class.calcTime(TimeHelpers.scala:346)
net.liftweb.util.Helpers$.calcTime(Helpers.scala:34)
net.liftweb.util.TimeHelpers$class.logTime(TimeHelpers.scala:365)
net.liftweb.util.Helpers$.logTime(Helpers.scala:34)
net.liftweb.http.LiftServlet.doIt$1(LiftServlet.scala:147)
net.liftweb.http.LiftServlet.service(LiftServlet.scala:158)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2$$anonfun$apply$mcV$sp$1$$anonfun$apply$mcV$sp$2.apply$mcV$sp(HTTPProvider.scala:72)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2$$anonfun$apply$mcV$sp$1$$anonfun$apply$mcV$sp$2.apply(HTTPProvider.scala:71)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2$$anonfun$apply$mcV$sp$1$$anonfun$apply$mcV$sp$2.apply(HTTPProvider.scala:71)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.URLRewriter$.doWith(Req.scala:1327)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2$$anonfun$apply$mcV$sp$1.apply$mcV$sp(HTTPProvider.scala:70)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2$$anonfun$apply$mcV$sp$1.apply(HTTPProvider.scala:70)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2$$anonfun$apply$mcV$sp$1.apply(HTTPProvider.scala:70)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2.apply$mcV$sp(HTTPProvider.scala:66)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2.apply(HTTPProvider.scala:60)
net.liftweb.http.provider.HTTPProvider$$anonfun$service$2.apply(HTTPProvider.scala:60)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.provider.HTTPProvider$class.service(HTTPProvider.scala:60)
net.liftweb.http.LiftFilter.service(LiftServlet.scala:928)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$mcV$sp$2.apply$mcV$sp(ServletFilterProvider.scala:82)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$mcV$sp$2.apply(ServletFilterProvider.scala:82)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1$$anonfun$apply$mcV$sp$1$$anonfun$apply$mcV$sp$2.apply(ServletFilterProvider.scala:82)
net.liftweb.http.provider.servlet.ServletFilterProvider$class.handleLoan$1(ServletFilterProvider.scala:59)
net.liftweb.http.provider.servlet.ServletFilterProvider$class.net$liftweb$http$provider$servlet$ServletFilterProvider$$handleLoanWrappers(ServletFilterProvider.scala:63)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1$$anonfun$apply$mcV$sp$1.apply$mcV$sp(ServletFilterProvider.scala:82)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1$$anonfun$apply$mcV$sp$1.apply(ServletFilterProvider.scala:77)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1$$anonfun$apply$mcV$sp$1.apply(ServletFilterProvider.scala:77)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16$$anonfun$apply$17$$anonfun$apply$18$$anonfun$apply$19.apply(Vars.scala:611)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16$$anonfun$apply$17$$anonfun$apply$18.apply(Vars.scala:610)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16$$anonfun$apply$17.apply(Vars.scala:609)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16.apply(Vars.scala:608)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$class.apply(Vars.scala:607)
net.liftweb.http.RequestVarHandler$.apply(Vars.scala:507)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1.apply$mcV$sp(ServletFilterProvider.scala:75)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1.apply(ServletFilterProvider.scala:75)
net.liftweb.http.provider.servlet.ServletFilterProvider$$anonfun$doFilter$1.apply(ServletFilterProvider.scala:75)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16$$anonfun$apply$17$$anonfun$apply$18$$anonfun$apply$19.apply(Vars.scala:611)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16$$anonfun$apply$17$$anonfun$apply$18.apply(Vars.scala:610)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16$$anonfun$apply$17.apply(Vars.scala:609)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$$anonfun$apply$16.apply(Vars.scala:608)
net.liftweb.util.ThreadGlobal.doWith(ThreadGlobal.scala:71)
net.liftweb.http.CoreRequestVarHandler$class.apply(Vars.scala:607)
net.liftweb.http.TransientRequestVarHandler$.apply(Vars.scala:520)
net.liftweb.http.provider.servlet.ServletFilterProvider$class.doFilter(ServletFilterProvider.scala:74)
net.liftweb.http.LiftFilter.doFilter(LiftServlet.scala:928)
org.eclipse.jetty.servlet.ServletHandler$CachedChain.doFilter(ServletHandler.java:1307)
org.eclipse.jetty.servlet.ServletHandler.doHandle(ServletHandler.java:453)
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:137)
org.eclipse.jetty.security.SecurityHandler.handle(SecurityHandler.java:559)
org.eclipse.jetty.server.session.SessionHandler.doHandle(SessionHandler.java:231)
org.eclipse.jetty.server.handler.ContextHandler.__doHandle(ContextHandler.java:1072)
org.eclipse.jetty.server.handler.ContextHandler.doHandle(ContextHandler.java)
org.eclipse.jetty.servlet.ServletHandler.doScope(ServletHandler.java:382)
org.eclipse.jetty.server.session.SessionHandler.doScope(SessionHandler.java:193)
org.eclipse.jetty.server.handler.ContextHandler.doScope(ContextHandler.java:1006)
org.eclipse.jetty.server.handler.ScopedHandler.handle(ScopedHandler.java:135)
org.eclipse.jetty.server.handler.ContextHandlerCollection.handle(ContextHandlerCollection.java:255)
org.eclipse.jetty.server.handler.HandlerWrapper.handle(HandlerWrapper.java:116)
org.eclipse.jetty.server.Server.handle(Server.java:365)
org.eclipse.jetty.server.AbstractHttpConnection.handleRequest(AbstractHttpConnection.java:485)
org.eclipse.jetty.server.AbstractHttpConnection.headerComplete(AbstractHttpConnection.java:926)
org.eclipse.jetty.server.AbstractHttpConnection$RequestHandler.headerComplete(AbstractHttpConnection.java:988)
org.eclipse.jetty.http.HttpParser.parseNext(HttpParser.java:635)
org.eclipse.jetty.http.HttpParser.parseAvailable(HttpParser.java:235)
org.eclipse.jetty.server.AsyncHttpConnection.handle(AsyncHttpConnection.java:82)
org.eclipse.jetty.io.nio.SelectChannelEndPoint.handle(SelectChannelEndPoint.java:627)
org.eclipse.jetty.io.nio.SelectChannelEndPoint$1.run(SelectChannelEndPoint.java:51)
org.eclipse.jetty.util.thread.QueuedThreadPool.runJob(QueuedThreadPool.java:608)
org.eclipse.jetty.util.thread.QueuedThreadPool$3.run(QueuedThreadPool.java:543)
java.lang.Thread.run(Thread.java:745)
package code.snippet
import code.SendEmail
import net.liftweb.http.SHtml
import net.liftweb.util.Props
import net.liftweb.util.Helpers._
import scala.xml.Text
/**
* Created by IDEA on 29/10/15.
*/
class SEmail {
def sendNow() = {
SendEmail.send_!(
Props.get("mail.user").openOrThrowException("Can't get username"),
"www@gmail.com",
"Sending an email",
"Here is from lift!"
)
}
def sendEmail = {
"*" #> SHtml.link("#", () => sendNow(), Text("Send email"))
}
}
package code
import net.liftweb.util.Mailer
import net.liftweb.util.Mailer.{From, Subject, To, PlainMailBodyType}
/**
* Created by IDEA on 29/10/15.
*/
object SendEmail {
def send_!(from: String, recipient: String, subject: String, body: String): Unit = {
val mailTypes = List(PlainMailBodyType(body), To(recipient))
Mailer.msgSendImpl(From(from), Subject(subject), mailTypes)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment