Skip to content

Instantly share code, notes, and snippets.

@magro
magro / ValueObjectFix.java
Last active August 29, 2015 13:55
Test for deserialization issue with kryo and class extending HashMap
package de.javakaffee.web.msm.serializer.kryo;
import static org.testng.Assert.assertEquals;
import static org.testng.Assert.assertNotNull;
import java.util.HashMap;
import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;
import org.testng.annotations.Test;
def multipartParser[A](partHandler: Map[String, String] => Iteratee[Array[Byte], A]):
BodyParser[Seq[A]] = parse.using { request =>
val maybeBoundary = for {
mt <- request.mediaType
(_, value) <- mt.parameters.find(_._1.equalsIgnoreCase("boundary"))
boundary <- value
} yield ("\r\n--" + boundary).getBytes("utf-8")
maybeBoundary.map { boundary =>
@magro
magro / WSProcessLargeResponseExample.scala
Created May 11, 2014 21:15
Play WS client: process large response
import play.api.libs.iteratee._
def fromStream(stream: OutputStream): Iteratee[Array[Byte], Unit] = Cont {
case e@Input.EOF =>
stream.close()
Done((), e)
case Input.El(data) =>
stream.write(data)
fromStream(stream)
case Input.Empty =>
@magro
magro / WSMultipartHandler.scala
Last active August 29, 2015 14:01
Helper to incrementally consume a multipart response (stream-like) using Play's WS client (Play 2.3.0-RC1)
package wsmultipart
import play.api.libs.ws.WSResponseHeaders
import play.api.libs.iteratee._
import play.api.libs.iteratee.Parsing.MatchInfo
import play.api.http.{MediaType, HeaderNames}
import scala.concurrent.ExecutionContext.Implicits.global
object WSMultipartHandler {
@magro
magro / WSMultipartHandlerSpec.scala
Last active August 29, 2015 14:01
Scalatest Plus Play Spec to test the WSMultipartHandler
package wsmultipart
import org.scalatestplus.play.{PlaySpec, OneServerPerSuite}
import org.scalatest.EitherValues
import play.api.mvc.Action
import play.api.mvc.Results._
import play.api.libs.iteratee.{Done, Input, Cont, Iteratee, Enumerator}
import play.api.test.Helpers._
import scala.concurrent.ExecutionContext.Implicits.global
import scala.collection.mutable
object CassandraTracing {
def traceSegment(res: Future[ResultSet]): Future[ResultSet] = {
TraceRecorder.withTraceContextAndSystem { (ctx, system) =>
// Because we don't know the actual table that was queried for now we
// use a general name for the segment, it's renamed once we have the result
val segment = ctx.startSegment("db", "cassandra-client", "datastax")
res.onComplete {
case Success(rs) =>
// Get the table name from the result so that we can give the segment
@magro
magro / SolrsBenchmark.scala
Last active August 29, 2015 14:26
A benchmark for solrs, allows to compare it with solrj. Can be copied in the solrs project to `src/test/scala/io/ino/solrs/Benchmark.scala`, build.sbt additionally needs `connectInput in (Test,run) := true`. Can be run from within sbt via `test:runMain io.ino.solrs.Benchmark 4 solrj` (or alternatively "solrs").
package io.ino.solrs
import java.util.Arrays._
import java.util.concurrent.atomic.{AtomicBoolean, AtomicInteger}
import java.util.concurrent.{Executors, TimeUnit}
import io.ino.solrs.SolrUtils._
import org.apache.curator.test.TestingServer
import org.apache.solr.client.solrj.SolrQuery
import org.apache.solr.client.solrj.impl.CloudSolrClient
@magro
magro / MongoDBUpdateFieldByAnotherField.java
Created March 10, 2011 22:24
MongoDB hello: Shows how to update a field with the value of another field by using either eval (nolock, for mongodb >= 1.8) or map/reduce (mongodb 1.6).
package hello.mongodb;
import java.net.UnknownHostException;
import com.mongodb.BasicDBObject;
import com.mongodb.BasicDBObjectBuilder;
import com.mongodb.DB;
import com.mongodb.DBCollection;
import com.mongodb.DBObject;
import com.mongodb.MapReduceOutput;
@magro
magro / setup-jdk-alternatives.sh
Created April 25, 2011 12:47
Installs sun/oracle jdk stuff as alternatives, tested for fedora (14) 64bit (see also http://www.javakaffee.de/blog/2010/12/26/installing-java-on-fedora-using-alternatives/)
#!/bin/sh
#
# Installs sun/oracle jdk stuff as alternatives, tested for fedora (14) 64bit.
# See also http://www.javakaffee.de/blog/2010/12/26/installing-java-on-fedora-using-alternatives/
# Author: martin(dot)grotzke(at)googlemail(dot)com
if [ $# -ne 3 ]; then
echo 1>&2 "Usage: $0 <major-version> <minor-version> <priority>"
echo "E.g. $0 1.6.0 2 20000"
exit 127
@magro
magro / FullObjectCache.java
Created October 14, 2011 21:45
A simple cache that holds all entries for a given type and updates itself in the background to keep the cache up to date.
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;
import java.util.concurrent.atomic.AtomicReference;