Skip to content

Instantly share code, notes, and snippets.

Joni Karppinen jonikarppinen

Block or report user

Report or block jonikarppinen

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
@jonikarppinen
jonikarppinen / play-error-response.html
Last active Sep 30, 2016
HTML error response generated automatically by Play Framework (related to the question http://stackoverflow.com/q/39794500/56285)
View play-error-response.html
<!DOCTYPE html>
<html lang="en">
<head>
<title>Bad request</title>
<link rel="shortcut icon" href="data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAABAAAAAQCAYAAAAf8/9hAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAlFJREFUeNqUU8tOFEEUPVVdNV3dPe8xYRBnjGhmBgKjKzCIiQvBoIaNbly5Z+PSv3Aj7DSiP2B0rwkLGVdGgxITSCRIJGSMEQWZR3eVt5sEFBgTb/dN1yvnnHtPNTPG4PqdHgCMXnPRSZrpSuH8vUJu4DE4rYHDGAZDX62BZttHqTiIayM3gGiXQsgYLEvATaqxU+dy1U13YXapXptpNHY8iwn8KyIAzm1KBdtRZWErpI5lEWTXp5Z/vHpZ3/wyKKwYGGOdAYwR0EZwoezTYApBEIObyELl/aE1/83cp40Pt5mxqCKrE4Ck+mVWKKcI5tA8BLEhRBKJLjez6a7MLq7XZtp+yyOawwCBtkiBVZDKzRk4NN7NQBMYPHiZDFhXY+p9ff7F961vVcnl4R5I2ykJ5XFN7Ab7Gc61VoipNBKF+PDyztu5lfrSLT/wIwCxq0CAGtXHZTzqR2jtwQiXONma6hHpj9sLT7YaPxfTXuZdBGA02Wi7FS48YiTfj+i2NhqtdhP5RC8mh2/Op7y0v6eAcWVLFT8D7kWX5S9mepp+C450MV6aWL1cGnvkxbwHtLW2B9AOkLeUd9KEDuh9fl/7CEj7YH5g+3r/lWfF9In7tPz6T4IIwBJOr1SJyIGQMZQbsh5P9uBq5VJtqHh2mo49pdw5WFoEwKWqWHacaWOjQXWGcifKo6vj5RGS6zykI587XeUIQDqJSmAp+lE4qt19W5P9o8+Lma5DcjsC8JiT607lMVkdqQ0Vyh3lHhmh52tfNy78ajXv0rgYzv8
View ContactsFragment.java
// Fixes to compilation errors in "Define the onItemClick() method" section
// Doesn't compile
Cursor cursor = parent.getAdapter().getCursor();
// Fixed (not sure if this is the cleanest way though)
Cursor cursor = ((SimpleCursorAdapter) parent.getAdapter()).getCursor();
// Doesn't compile
mContactId = getLong(CONTACT_ID_INDEX);
// Fixed
@jonikarppinen
jonikarppinen / 1_RxJava-connectivity-status-example.md
Last active Feb 22, 2019
Example of listening to connectivity status in Android and reacting to going offline. (Subscription, Observable, Observer, PublishSubject are from RxJava.)
View 1_RxJava-connectivity-status-example.md

Example of using RxJava to listen to connectivity status in Android

You could use the same approach to listen to any status, but this example includes network connectivity specifics too (ConnectionChangeReceiver and AndroidUtils.isConnected).

View Specs2-output.txt
Testing started at 22:28 ...
services.ReportServiceSpec$
java.lang.ClassNotFoundException: services.ReportServiceSpec$
STACKTRACE
java.net.URLClassLoader.findClass(URLClassLoader.java:381)
java.lang.ClassLoader.loadClass(ClassLoader.java:424)
sun.misc.Launcher$AppClassLoader.loadClass(Launcher.java:331)
@jonikarppinen
jonikarppinen / Messages.java
Last active Aug 19, 2019
Example of using message resources in Spring Boot service layer code, in as simple way as possible (hopefully!)
View Messages.java
package com.company.project.components;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.context.support.MessageSourceAccessor;
import org.springframework.stereotype.Component;
import javax.annotation.PostConstruct;
import java.util.Locale;
@jonikarppinen
jonikarppinen / PdfOrErrorController.java
Last active Jan 25, 2018
Example of using ExceptionHandler in Spring Boot: a controller method that returns either binary data or error JSON
View PdfOrErrorController.java
package com.company.project.controllers;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpStatus;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.util.Random;
@jonikarppinen
jonikarppinen / StringUtils.java
Last active Aug 29, 2015
StringUtils – common utility methods for Java
View StringUtils.java
/**
* @author Joni Karppinen
* Licence: WTFPL, http://www.wtfpl.net/txt/copying/
*/
public class StringUtils {
public static String encodeUrlParameters(Map<String, String> parameters) {
if (parameters == null || parameters.isEmpty()) {
return "";
}
@jonikarppinen
jonikarppinen / CustomObjectMapper.java
Last active Jun 28, 2018
Example of making Jackson behave more sensibly by default (also, more like Gson)
View CustomObjectMapper.java
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
/**
* ObjectMapper customised for my tastes and most typical needs
*
* @author Joni Karppinen
*/
@jonikarppinen
jonikarppinen / CustomErrorController.java
Last active Jul 11, 2019
Example of replacing Spring Boot "whitelabel" error page with custom error responses (with JSON response body)
View CustomErrorController.java
package com.company.project.controllers;
import com.company.project.model.api.ErrorJson;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.web.ErrorAttributes;
import org.springframework.boot.autoconfigure.web.ErrorController;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.context.request.RequestAttributes;
@jonikarppinen
jonikarppinen / proguard-project.txt
Last active Aug 1, 2017
Example ProGuard config for a project using RxJava, Guava and Gson
View proguard-project.txt
# Add project specific ProGuard rules here.
# By default, the flags in this file are appended to flags specified
# in ${sdk.dir}/tools/proguard/proguard-android.txt
# You can edit the include path and order by changing the ProGuard
# include property in project.properties.
#
# For more details, see
# http://developer.android.com/guide/developing/tools/proguard.html
You can’t perform that action at this time.