package com.gentics.mesh.rest.client.impl;

import com.gentics.mesh.rest.client.MeshBinaryResponse;
import okhttp3.Response;

import java.io.InputStream;

public class OkHttpBinaryResponse implements MeshBinaryResponse {
	private final Response response;

	public OkHttpBinaryResponse(Response response) {
		this.response = response;
	}

	@Override
	public InputStream getStream() {
		return response.body().byteStream();
	}

	@Override
	public String getFilename() {
		String disposition = response.header("Content-Disposition");
		// TODO Use proper parser
		return disposition.substring(disposition.indexOf("=") + 1);
	}

	@Override
	public String getContentType() {
		return response.body().contentType().toString();
	}

	@Override
	public void close() {
		response.close();
	}
}

@@ -37,16 +37,118 @@
 .impl;%0A%0A
+import java.io.InputStream;%0Aimport java.io.UnsupportedEncodingException;%0Aimport java.net.URLDecoder;%0A%0A
 import c
@@ -195,16 +195,17 @@
 sponse;%0A
+%0A
 import o
@@ -226,37 +226,8 @@
 e;%0A%0A
-import java.io.InputStream;%0A%0A
 publ
@@ -323,16 +323,94 @@
 ponse;%0A%0A
+%09public static final String FILENAME_DISPOSITION_ATTR = %22filename*=utf-8''%22;%0A%0A
 %09public 
@@ -682,95 +682,301 @@
 ;%0A%09%09
-// TODO Use proper parser%0A%09%09return disposition.substring(disposition.indexOf(%22=%22) + 1);
+try %7B%0A%09%09%09int start = disposition.indexOf(FILENAME_DISPOSITION_ATTR);%0A%09%09%09String encodedName = disposition.substring(start + FILENAME_DISPOSITION_ATTR.length());%0A%09%09%09return URLDecoder.decode(encodedName, %22UTF-8%22);%0A%09%09%7D catch (UnsupportedEncodingException e) %7B%0A%09%09%09throw new RuntimeException(e);%0A%09%09%7D
 %0A%09%7D%0A

Fix filename decoding