Skip to content

Instantly share code, notes, and snippets.

View mrserverless's full-sized avatar
🌆
Building a Metaverse

Yun Zhi Lin mrserverless

🌆
Building a Metaverse
View GitHub Profile
@mrserverless
mrserverless / RXGlue.groovy
Created December 10, 2015 01:59 — forked from pintowar/RXGlue.groovy
Creating a SSE streaming with Apache Camel and Ratpack, using Rx Groovy as a glue
@Grab('com.netflix.rxjava:rxjava-groovy:0.20.7')
@Grab('io.reactivex:rxjava-reactive-streams:0.3.0')
@Grab('org.apache.camel:camel-rx:2.14.1')
@Grab('io.ratpack:ratpack-groovy:0.9.11')
@Grab('org.slf4j:slf4j-simple:1.6.6')
import org.apache.camel.impl.*
import org.apache.camel.rx.*
import static rx.RxReactiveStreams.toPublisher
import static ratpack.groovy.Groovy.ratpack
import static ratpack.sse.ServerSentEvents.serverSentEvents;
@mrserverless
mrserverless / AbandonedConnectionsAwareDatasourceFactory.java
Last active November 19, 2015 19:54
Prevent Dropwizard Database or Integration Tests running out of Connections, by removing abandoned connections not cleaned up by the test runner. Discovered by @leeAtTrunk and @nagliyvred
public class AbandonedConnectionsAwareDatasourceFactory extends DataSourceFactory {
private int removeAbandonedTimeout = 60;
private boolean removeAbandoned = false;
@Override
public ManagedDataSource build(MetricRegistry metricRegistry, String name) {
ManagedPooledDataSource build = (ManagedPooledDataSource) super.build(metricRegistry, name);
build.setRemoveAbandonedTimeout(removeAbandonedTimeout);
build.setRemoveAbandoned(removeAbandoned);
@mrserverless
mrserverless / HibernateGenericDAO
Created August 18, 2015 23:11
Hibernate GenericDAO using Class-Mate
class Device {}
class Router extends Device {}
class GenericDAO<T, ID extends Serializable> {
protected Class<T> persistentClass;
protected Class<ID> idClass;
private GenericDAO() {
List <ResolvedType> typeParameters = new TypeResolver().resolve(this.getClass()).typeParametersFor(GenericDAO.class);
this.persistentClass = (Class<T>) typeParameters.get(0).getErasedType();
@mrserverless
mrserverless / TypeInferenceGenericGuiceFactory.java
Last active August 29, 2015 14:27
Generic Guice Factory with Type Inference
install(new FactoryModuleBuilder().build(PayloadServiceFactory.class));
public interface GenericPayloadFactory {
PayloadService<? extends Payload> create();
}
public interface Payload {}
public class JsonPayload implements Payload {}
public class XmlPayload implements Payload {}
@mrserverless
mrserverless / docker-compose.yml
Last active March 1, 2017 09:56
Tutum HAProxy sample Stackfile for Virtual Host + SSL setup
hello:
image: 'tutum/hello-world:latest'
environment:
- FORCE_SSL=yes
- 'VIRTUAL_HOST=http://hello.example.com, https://hello.example.com'
ports:
- '80'
web:
image: 'example/web-site:latest'
environment:
@mrserverless
mrserverless / SSLJerseyClient.java
Last active February 17, 2017 09:46
Dropwizard JerseyClient loading JKS certificate into SSL TrustStore at runtime
private TrustManager[] loadTrustManagers(String certificateFile) throws KeyStoreException, CertificateException, NoSuchAlgorithmException, IOException {
KeyStore trustedCertStore = KeyStore.getInstance(KeyStore.getDefaultType());
trustedCertStore.load(SecurityModule.class.getClassLoader().getResourceAsStream(), null);
TrustManagerFactory tmf = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
tmf.init(trustedCertStore);
return tmf.getTrustManagers();
}
@mrserverless
mrserverless / Resource.java
Created June 17, 2015 10:45
Dropwizard @UnitOfWork with Manual Transactions
@GET
@Path("/update")
@UnitOfWork(transactional = false)
public World update(World world) {
return transactionDao.updatesWorld(world);
}
@mrserverless
mrserverless / DropwizardJettyCrossOriginIntegrationTest.java
Last active January 19, 2018 22:09
Dropwizard 0.8.0 and 0.9.0 Jetty CORS Filter with Unit Tests. To prove this works once and for all
import io.dropwizard.Application;
import io.dropwizard.Configuration;
import io.dropwizard.client.JerseyClientBuilder;
import io.dropwizard.setup.Environment;
import io.dropwizard.testing.junit.DropwizardAppRule;
import org.assertj.core.data.MapEntry;
import org.eclipse.jetty.servlets.CrossOriginFilter;
import org.junit.ClassRule;
import org.junit.Test;
@mrserverless
mrserverless / CORSContainerResponseFilter.java
Last active October 5, 2022 20:47
Jersey ContainerResponseFilter example of CORS Filter. The Jetty version is more comprehensive: https://gist.github.com/yunspace/07d80a9ac32901f1e149
import javax.inject.Singleton;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerResponseContext;
import javax.ws.rs.container.ContainerResponseFilter;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.ext.Provider;
import java.io.IOException;
@Singleton
@Provider
import com.fasterxml.jackson.annotation.JsonProperty;
import java.time.LocalDate;
import java.time.LocalDateTime;
public class Dummy {
@JsonProperty
public LocalDate localDate;
@JsonProperty