Skip to content

Instantly share code, notes, and snippets.

View hamnis's full-sized avatar

Erlend Hamnaberg hamnis

View GitHub Profile
<project>
<parent>
<groupId>com.example</groupId>
<artifactId>parent</artifactId>
<version>1.0-SNAPSHOT</version>
</parent>
<modelVersion>4.0.0</modelVersion>
<artifactId>rpm</artifactId>
<packaging>jar</packaging>
<name>Project :: RPM</name>
import java.awt.{Color, Graphics2D}
import java.awt.image.BufferedImage
import javax.imageio.ImageIO
public class Main {
//Does not deal with double buffering, and writing on servers which are headless.
public BufferedImage createBlackImage(int width, int height) {
BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
Graphics2D graphics = (Graphics2D) image.getGraphics();
public class QueriesTest {
@Test
public void findCreatedQuery() throws Exception {
Query query = Query.create(new URITemplateTarget("http://example.com{?q}"), "filter", Optional.<String>none(), Collections.singletonList(Property.template("q")));
Collection collection = Collection.builder().addQuery(query).build();
Query filter = collection.queryByRel("filter").get();
assertThat(URI.create("http://example.com"), equalTo(filter.expand()));
assertThat(URI.create("http://example.com?q=faff"), equalTo(filter.expand(Collections.singletonList(Property.value("q", "faff")))));
public class LinkTypeExt extends Extension<Optional<String>>{
@Override
public Optional<String> extract(ObjectNode node) {
return node.has("type") ? Optional.some(node.get("type").asText()) : Optional.<String>none();
}
@Override
public Map<String, JsonNode> apply(Optional<String> value) {
if (value.isSome()) {
return Collections.singletonMap("type", ValueFactory.createValue(value.get()).asJson());
package com.example.http;
import org.apache.http.client.config.RequestConfig;
import org.codehaus.httpcache4j.HTTPHost;
public class NotRedirectingHttpClientFactory extends HttpClientFactory {
@Override
protected RequestConfig requestConfig(HTTPHost proxyHost, ConnectionConfiguration config) {
RequestConfig c = super.requestConfig(proxyHost, config);
RequestConfig.Builder builder = RequestConfig.copy(c);
@hamnis
hamnis / c+j.json
Last active December 27, 2015 15:59
{
"collection": {
"href": "http://example.com",
"accept-bulk": "application/vnd.collection-templates+json",
"template": {
"data": [
{"name": "foo"}
]
}
}
public class TemplateFill {
public Template fill(Map<String, String> values, template: Template) {
Data data = template.getData();
List<Property> newData = new ArrayList<>();
for (Property p: data) {
if (values.contains(p.getName()) {
newData.add(p.withValue(values.get(p.getName()));
}
}
package api.resource;
import javax.ws.rs.*;
import net.hamnaberg.json.*;
@Path("/events")
@Produces("application/vnd.collection+json")
@Consumes("application/vnd.collection+json")
public class EventResource{
import java.util.Arrays;
import net.hamnaberg.json;
public class TemplateUse {
public static void main(String[] args) {
Template template = Template.create(Arrays.asList(
Property.template("faff"),
Property.template("foof"),
Property.template("feee")
);