Skip to content

Instantly share code, notes, and snippets.

View amolbrid's full-sized avatar

Amol Brid amolbrid

View GitHub Profile
@amolbrid
amolbrid / year_month.css
Created January 2, 2011 01:46
Year and month selector
.horizontal_list {
font-size: 14px;
width: 100%;
overflow: hidden;
}
.horizontal_list ul {
margin: 3px;
text-align: center;
float: right;
@amolbrid
amolbrid / Mailer.java
Created February 16, 2011 04:54
Send email in java
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;
import javax.mail.Address;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
@amolbrid
amolbrid / UserDao.java
Created September 10, 2011 14:34
Spring: How to get auto generated keys
public class UserDao extends SimpleJdbcDaoSupport {
private static final String CREATE_USER_SQL = "insert into users(name, created_at, updated_at) values (?,?,?)";
public int createUser(String username) {
Connection cnn = null;
PreparedStatement pstmt = null;
ResultSet rsId = null;
int id = 0;
try {
@amolbrid
amolbrid / AppError.java
Created September 23, 2011 19:39
Simple REST service using Jersey and Jackson
package com.ab.jersey.dto;
public class AppError {
private String message;
public AppError() { }
public AppError(String message) {
this.message = message;
}
@amolbrid
amolbrid / Gemfile
Created September 13, 2012 18:35
Rails Lightweight Stack. Most of this is detailed on Crafting Rails Applications - http://pragprog.com/book/jvrails/crafting-rails-applications
source :rubygems
# We are not loading Active Record, nor Active Resources etc.
# We can do this in any app by simply replacing the rails gem
# by the parts we want to use.
gem "actionpack", "~> 3.2"
gem "railties", "~> 3.2"
gem "tzinfo"
# Let's use thin
@amolbrid
amolbrid / build.gradle
Last active December 23, 2015 14:09
Basic gradle build file
apply plugin: 'java'
apply plugin: 'eclipse'
repositories {
mavenCentral()
}
dependencies {
compile "joda-time:joda-time:2.2"
}
@amolbrid
amolbrid / SecuredRoutes.scala
Created February 13, 2015 16:00
Users route
package manta
import manta.routes.Users
import spray.routing.HttpService
import scala.concurrent.ExecutionContext.Implicits.global
trait SecuredRoutes extends HttpService with Users with TokenAuthenticator {
val routes = authenticate(basicTokenAuth) { implicit user =>
usersRoute
@amolbrid
amolbrid / notworking.scala
Created February 23, 2015 17:43
TokenAuthenticator with implicit Session
trait TokenAuthenticator {
def apiTokenAuth(implicit dbSession: Session) = new ApiTokenAuthenticator()
}
class ApiTokenAuthenticator(implicit val dbSession: Session, implicit val executionContext: ExecutionContext) extends HttpAuthenticator[User] {
override def getChallengeHeaders(httpRequest: HttpRequest): List[HttpHeader] =
`WWW-Authenticate`(HttpChallenge(scheme = "Token", realm = "Manta", params = Map.empty)) :: Nil
override def authenticate(credentials: Option[HttpCredentials], ctx: RequestContext): Future[Option[User]] =
Future.successful(
@amolbrid
amolbrid / authmanager.scala
Created February 23, 2015 17:44
Authentication with DI
class AuthenticationManager {
// inject db: Database, repo: Users
def authenticateWithToken(token: String): Option[User] = {
db withTransaction { implicit session =>
repo.authenticateWithToken(token)
}
}
}
trait TokenAuthenticator {
package com.example.test
import scala.slick.driver.PostgresDriver
object UserComponentRegistry extends UserRepositoryComponent {
val userRepository = new UserRepository(PostgresDriver)
}