Skip to content

Instantly share code, notes, and snippets.

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.channels.Channels;
import java.time.Instant;
import java.util.stream.Collectors;
public class Blocking {
@magro
magro / nginx-debug-backtick.log
Last active December 15, 2015 08:12
snippets for blog post on SCSs integration with nginx and insights into SSI handling
2015/12/14 22:18:49 [debug] 2356#0: *976 ssi include: "/breadcrumb/1001212364/De`Longhi Cappuccino Thermo-Gläser doppelwandig 2 Stück"
2015/12/14 22:18:49 [debug] 2356#0: *976 http subrequest "/breadcrumb/1001212364/De`Longhi Cappuccino Thermo-Gläser doppelwandig 2 Stück?"
2015/12/14 22:18:49 [debug] 2356#0: *976 http posted request: "/breadcrumb/1001212364/De`Longhi Cappuccino Thermo-Gläser doppelwandig 2 Stück?"
2015/12/14 22:18:49 [debug] 2356#0: *976 http proxy header:
"GET /breadcrumb/1001212364/De`Longhi%20Cappuccino%20Thermo-Gl%c3%a4ser%20%20doppelwandig%202%20St%c3%bcck HTTP/1.1
2015/12/14 22:18:49 [debug] 2356#0: *976 http finalize request: -4, "/breadcrumb/1001212364/De`Longhi Cappuccino Thermo-Gläser doppelwandig 2 Stück?" a:0, c:7
2015/12/14 22:18:49 [debug] 2356#0: *976 http proxy status 400 "400 Bad Request"
2015/12/14 22:18:49 [debug] 2356#0: *976 http proxy header: "X-Error: Illegal character in path at index 25: /breadcrumb/1001212364/De`Longhi%20Cappuccino%20Thermo-Gl%c3%a4ser%20%20doppe
@magro
magro / Proxy.scala
Created September 30, 2015 10:07
Scala proxy for lazy values
import java.util.concurrent.atomic.AtomicReference
import scala.reflect.ClassTag
import java.lang.reflect.{Proxy => JProxy, Method, InvocationHandler}
/**
* Creates a proxy for a lazy value, in a project moving to dependency injection this
* was meant to replace legacy stuff where at app start time dependencies were loaded
* too early.
*
@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
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 / 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
@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 / 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 =>
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 / 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;