Created
June 10, 2016 19:58
-
-
Save raphaelbauer/18257fa52c026566bf784047ad832c75 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import ninja.Context; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
import com.google.inject.Singleton; | |
import java.io.IOException; | |
import java.io.StringWriter; | |
import ninja.bodyparser.BodyParserEngine; | |
import org.apache.commons.io.IOUtils; | |
@Singleton | |
public class BodyParserEngineTextPlain implements BodyParserEngine { | |
private final Logger logger = LoggerFactory.getLogger(BodyParserEngineTextPlain.class); | |
@Override | |
public <T> T invoke(Context context, Class<T> classOfT) { | |
String result = null; | |
if (!classOfT.equals(String.class)) { | |
logger.error( "Can parse text/plain only into String. Requested is a {}", classOfT); | |
throw new RuntimeException("Can parse text/plain only into String."); | |
} | |
try { | |
StringWriter writer = new StringWriter(); | |
IOUtils.copy(context.getInputStream(), writer, "UTF-8"); | |
result = writer.toString(); | |
} catch (IOException ioException) { | |
logger.error("Error parsing incoming text/plain", ioException); | |
} | |
return (T) result; | |
} | |
@Override | |
public String getContentType() { | |
return "text/plain"; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment