Skip to content

Instantly share code, notes, and snippets.

View lefloh's full-sized avatar

Florian Hirsch lefloh

View GitHub Profile
public class LinkedSerializer extends JsonSerializer<Object> {
private static final Pattern PARAM_PATTERN = Pattern.compile("\\{(.+?)\\}");
@Override
public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
Linked linked = value.getClass().getAnnotation(Linked.class);
if (linked == null) {
jgen.writeNull();
return;
@Retention(RetentionPolicy.RUNTIME)
@Target(ElementType.TYPE)
public @interface Linked {
Class<?> resource();
String method() default "";
String rel() default "";
@lefloh
lefloh / resteasy-fileupload-5.html
Created July 13, 2015 20:23
Input type _charset_
<input type="hidden" name="_charset_" />
@lefloh
lefloh / resteasy-fileupload-2
Last active November 19, 2015 14:45
HTTP multipart POST
POST /some-resource HTTP/1.1
Content-Type: multipart/form-data; boundary=AaB03x
--AaB03x
Content-Disposition: form-data; name="file1"; filename="file1.txt"
Content-Type: text/plain; charset=utf-8
... content of file1.txt ...
--AaB03x--
@lefloh
lefloh / resteasy-fileupload-1
Created July 13, 2015 20:09
HTTP utf-8 POST
POST /some-resource HTTP/1.1
Content-Type: text/plain; charset=utf-8
füübär
@lefloh
lefloh / resteasy-fileupload-6.java
Last active August 29, 2015 14:24
RESTesy resource guessing the encoding
@POST
@Path("/form")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadForm(MultipartFormDataInput input) throws IOException {
String charset = input.getFormDataMap().get("_charset_").get(0).getBodyAsString();
InputPart file = input.getFormDataMap().get("file").get(0);
InputStream inputStream = file.getBody(InputStream.class, null);
BufferedReader br = new BufferedReader(new InputStreamReader(inputStream, charset));
String line;
@lefloh
lefloh / resteasy-fileupload-4.java
Created July 13, 2015 20:07
RESTesy ContainerRequestFilter overwriting the DefaultEncoding
@Provider
public class CharsetFilter implements ContainerRequestFilter {
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
String charset = requestContext.getHeaderString("X-Charset");
if (charset != null) {
requestContext.setProperty(InputPart.DEFAULT_CHARSET_PROPERTY, charset);
}
}
@lefloh
lefloh / resteasy-fileupload-3.java
Last active August 29, 2015 14:24
RESTesy client using charset for file upload
WebTarget target = client.target("/some-resource");
MultipartFormDataOutput formData = new MultipartFormDataOutput();
formData.addFormData("file", fileContent, MediaType.TEXT_PLAIN_TYPE.withCharset("utf-8"));
Entity<MultipartFormDataOutput> entity = Entity.entity(formData, MediaType.MULTIPART_FORM_DATA);
Response response = target.request().post(entity);
@lefloh
lefloh / es-aliases
Created February 24, 2015 15:19
Elasticsearch and unique indexes
# create two indexes
curl -XPUT 'http://docker:9200/idx-1/user/1' -d '{
"name" : "John Doe"
}'
curl -XPUT 'http://docker:9200/idx-2/user/1' -d '{
"name" : "Jane Doe"
}'
@lefloh
lefloh / dart-editor-keybindings.xml
Last active August 29, 2015 14:05
Dart Editor MacOS Key Bindings
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<dartKeyBindings version="1">
<keyBinding commandName="Activate Editor" context="org.eclipse.ui.contexts.window" keySequence="COMMAND+F12" />
<keyBinding commandName="Backward History" context="org.eclipse.ui.contexts.window" keySequence="ALT+COMMAND+ARROW_LEFT" />
<keyBinding commandName="Close" context="org.eclipse.ui.contexts.window" keySequence="COMMAND+W"/>
<keyBinding commandName="Close All" context="org.eclipse.ui.contexts.window" keySequence="COMMAND+SHIFT+W"/>
<keyBinding commandName="Close Glance" context="com.xored.glance.ui.context" keySequence="ESC"/>
<keyBinding commandName="Content Assist" context="org.eclipse.ui.contexts.dialogAndWindow" keySequence="CTRL+SPACE"/>
<keyBinding commandName="Copy" context="org.eclipse.ui.contexts.dialogAndWindow" keySequence="COMMAND+C"/>
<keyBinding commandName="Copy Lines" context="org.eclipse.ui.textEditorScope" keySequence="ALT+COMMAND+ARROW_DOWN"/>