Skip to content

Commit

Permalink
Make TextPlainEngine return some String-based values
Browse files Browse the repository at this point in the history
  • Loading branch information
gitblit committed Jun 17, 2016
1 parent 5e18a29 commit ab0d9aa
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ This project adheres to [Semantic Versioning](http://semver.org/).
- Upgrade [pippo-jade] to Jade 1.1.4
- Upgrade [pippo-pebble] to Pebble 2.2.1
- Make `chunked` transfer-encoding optional, not the default
- Make the text/plain content type engine handle returning reasonable types like String, CharSequence, char[], and byte[]

#### Added
- [#245]: Route groups
Expand Down
12 changes: 12 additions & 0 deletions pippo-core/src/main/java/ro/pippo/core/TextPlainEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package ro.pippo.core;


import java.nio.charset.StandardCharsets;

/**
* A text/plain engine based on toString()
*
Expand All @@ -39,6 +41,16 @@ public String toString(Object object) {

@Override
public <T> T fromString(String content, Class<T> classOfT) {
if (String.class.equals(classOfT)) {
return (T) content;
} else if (CharSequence.class.equals(classOfT)) {
return (T) content;
} else if (char[].class.equals(classOfT)) {
return (T) content.toCharArray();
} else if (byte[].class.equals(classOfT)) {
return (T) content.getBytes(StandardCharsets.UTF_8);
}

throw new PippoRuntimeException("Sorry, can not transform '{}' content to '{}'", getContentType(),
classOfT.getName());
}
Expand Down

0 comments on commit ab0d9aa

Please sign in to comment.