// Copyright (c) 2003-2013, Jodd Team (jodd.org). All Rights Reserved.

package jodd.madvoc.result;

import jodd.io.StreamUtil;
import jodd.madvoc.ActionRequest;
import jodd.util.MimeTypes;

import javax.servlet.http.HttpServletResponse;
import java.io.PrintWriter;

/**
 * Text result returns a result value, i.e. a string.
 * Useful for JSON responses, when resulting string is built
 * in the action.
 */
public class TextResult extends ActionResult {

	public static final String NAME = "text";

	public TextResult() {
		super(NAME);
	}

	@Override
	public void render(ActionRequest actionRequest, Object resultObject, String resultValue, String resultPath) throws Exception {
		HttpServletResponse response = actionRequest.getHttpServletResponse();
		response.setContentType(MimeTypes.MIME_TEXT_PLAIN);
		PrintWriter writer = null;
		try {
			writer = response.getWriter();
			writer.println(resultValue);
		} finally {
			StreamUtil.close(writer);
		}
	}
}

@@ -161,16 +161,120 @@
 quest;%0D%0A
+import jodd.madvoc.ScopeType;%0D%0Aimport jodd.madvoc.component.MadvocConfig;%0D%0Aimport jodd.madvoc.meta.In;%0D%0A
 import j
@@ -340,24 +340,54 @@
 tResponse;%0D%0A
+import java.io.OutputStream;%0D%0A
 import java.
@@ -643,24 +643,98 @@
  %22text%22;%0D%0A%0D%0A
+%09@In(scope = ScopeType.CONTEXT)%0D%0A%09protected MadvocConfig madvocConfig;%0D%0A%0D%0A
 %09public Text
@@ -982,16 +982,18 @@
 nse();%0D%0A
+%0D%0A
 %09%09respon
@@ -1045,26 +1045,200 @@
 %0D%0A%09%09
-PrintWriter writer
+response.setCharacterEncoding(madvocConfig.getEncoding());%0D%0A%0D%0A%09%09byte%5B%5D data = resultValue.getBytes(madvocConfig.getEncoding());%0D%0A%09%09response.setContentLength(data.length);%0D%0A%0D%0A%09%09OutputStream out
  = n
@@ -1255,22 +1255,19 @@
 y %7B%0D%0A%09%09%09
-writer
+out
  = respo
@@ -1277,48 +1277,42 @@
 .get
-Writer();%0D%0A%09%09%09writer.println(resultValue
+OutputStream();%0D%0A%09%09%09out.write(data
 );%0D%0A
@@ -1350,14 +1350,11 @@
 ose(
-writer
+out
 );%0D%0A

return text in correct encoding, fixes #25