Skip to content

Commit

Permalink
add character encoding info to REST responses
Browse files Browse the repository at this point in the history
  • Loading branch information
darkv committed Dec 23, 2015
1 parent 67d5032 commit fa7f206
Show file tree
Hide file tree
Showing 7 changed files with 139 additions and 20 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,19 +10,26 @@
import er.rest.ERXRestContext;
import er.rest.ERXRestRequestNode;

public class ERXBinaryPListRestWriter implements IERXRestWriter {
public void appendHeadersToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestContext context) {
response.setHeader("application/x-plist", "Content-Type");
}

public class ERXBinaryPListRestWriter extends ERXRestWriter {
public void appendToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestFormat.Delegate delegate, ERXRestContext context) {
if (node != null) {
node._removeRedundantTypes();
}
appendHeadersToResponse(node, response, context);
response.setContentEncoding(contentEncoding());
Object object = node.toNSCollection(delegate);
ByteArrayOutputStream out = new ByteArrayOutputStream();
ERXPropertyListSerialization.writePropertyListToStream(object, out, ERXPropertyListSerialization.PListFormat.NSPropertyListBinaryFormat_v1_0, CharEncoding.UTF_8);
response.appendContentData(new NSData(out.toByteArray()));
}

@Override
public String contentType() {
return "application/x-plist";
}

@Override
protected String contentTypeHeaderValue() {
return contentType();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
* @property <code>er.rest.format.ERXJSONRestWriter.shouldPrettyPrint</code> Boolean property to enable pretty-printing of JSON response. Defaults to false.
* @property <code>er.rest.format.ERXJSONRestWriter.prettyPrintIndent</code> Integer property to set the pretty print indentation space count. Defaults to <code>2</code>.
*/
public class ERXJSONRestWriter implements IERXRestWriter {
public class ERXJSONRestWriter extends ERXRestWriter {

// Lazily initialized static constants
private static class CONSTANTS {
Expand All @@ -31,17 +31,14 @@ protected ERXRestRequestNode processNode(ERXRestRequestNode node) {
return node;
}

public void appendHeadersToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestContext context) {
response.setHeader("application/json", "Content-Type");
}

public void appendToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestFormat.Delegate delegate, ERXRestContext context) {
node = processNode(node);
if (node != null) {
node._removeRedundantTypes();
}

appendHeadersToResponse(node, response, context);
response.setContentEncoding(contentEncoding());
Object object = node.toJavaCollection(delegate);
if (object == null) {
response.appendContentString("undefined");
Expand All @@ -56,4 +53,9 @@ else if (ERXRestUtils.isPrimitive(object)) {
}
response.appendContentString("\n");
}

@Override
public String contentType() {
return "application/json";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,20 @@
import er.rest.ERXRestContext;
import er.rest.ERXRestRequestNode;

public class ERXPListRestWriter implements IERXRestWriter {
public void appendHeadersToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestContext context) {
response.setHeader("text/plain", "Content-Type");
}

public class ERXPListRestWriter extends ERXRestWriter {
public void appendToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestFormat.Delegate delegate, ERXRestContext context) {
if (node != null) {
node._removeRedundantTypes();
}
appendHeadersToResponse(node, response, context);
response.setContentEncoding(contentEncoding());
Object object = node.toNSCollection(delegate);
response.appendContentString(NSPropertyListSerialization.stringFromPropertyList(object));
response.appendContentString("\n");
}

@Override
public String contentType() {
return "text/plain";
}
}
100 changes: 100 additions & 0 deletions Frameworks/EOF/ERRest/Sources/er/rest/format/ERXRestWriter.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
package er.rest.format;

import java.io.UnsupportedEncodingException;

import com.webobjects.foundation.NSForwardException;

import er.rest.ERXRestContext;
import er.rest.ERXRestRequestNode;

public abstract class ERXRestWriter implements IERXRestWriter {
/** The HTTP header key for the content type. */
protected static final String ContentTypeHeaderKey = "Content-Type";
/** The default character encoding for the REST responses. */
protected static String TheDefaultResponseEncoding = "UTF-8";
protected String contentEncoding;

public ERXRestWriter() {
contentEncoding = defaultEncoding();
}

@Override
public void appendHeadersToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestContext context) {
response.setHeader(contentTypeHeaderValue(), ContentTypeHeaderKey);
}

/**
* The default character encoding to use for REST responses. The default value for this is UTF-8.
*
* @return the default character encoding
*/
public static String defaultEncoding() {
return TheDefaultResponseEncoding;
}

/**
* Lets you specify the default character encoding to be used for REST responses.
*
* @param encoding
* the default character encoding
*/
public static void setDefaultEncoding(String encoding) {
if (encoding != null && !encoding.equals(TheDefaultResponseEncoding)) {
try {
"test".getBytes(encoding);
}
catch (UnsupportedEncodingException e) {
throw NSForwardException._runtimeExceptionForThrowable(e);
}
TheDefaultResponseEncoding = encoding;
}
}

/**
* The character encoding to use for this REST response.
*
* @return the content's character encoding
*/
public String contentEncoding() {
return contentEncoding;
}

/**
* Lets you specify the content's character encoding to be used for this REST response.
*
* @param encoding
* the content's character encoding
*/
public void setContentEncoding(String encoding) {
if (encoding != null && !encoding.equals(contentEncoding)) {
try {
"test".getBytes(encoding);
}
catch (UnsupportedEncodingException e) {
throw NSForwardException._runtimeExceptionForThrowable(e);
}

contentEncoding = encoding;
}
}

/**
* The corresponding HTTP header content type for this REST response.
*
* @return content type
*/
public abstract String contentType();

/**
* The value to be used for the content type HTTP header.
*
* @return content type header value
*/
protected String contentTypeHeaderValue() {
StringBuilder sb = new StringBuilder();
sb.append(contentType());
sb.append("; charset=");
sb.append(contentEncoding());
return sb.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -25,4 +25,9 @@ public void appendContentString(String str) {
public void appendContentData(NSData data) {
_response.appendContentData(data);
}

@Override
public void setContentEncoding(String encoding) {
_response.setContentEncoding(encoding);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
*
* @property ERXRest.suppressTypeAttributesForSimpleTypes (default "false") If set to true, primitive types, like type = "datetime", won't be added to the output
*/
public class ERXXmlRestWriter implements IERXRestWriter {
public void appendHeadersToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestContext context) {
response.setHeader("text/xml", "Content-Type");
}

public class ERXXmlRestWriter extends ERXRestWriter {
public void appendToResponse(ERXRestRequestNode node, IERXRestResponse response, ERXRestFormat.Delegate delegate, ERXRestContext context) {
appendHeadersToResponse(node, response, context);
response.setContentEncoding(contentEncoding());
response.appendContentString("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
appendNodeToResponse(node, response, 0, delegate, context);
}
Expand Down Expand Up @@ -188,4 +185,8 @@ protected void indent(IERXRestResponse response, int indent) {
}
}

@Override
public String contentType() {
return "text/xml";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,4 +10,6 @@ public interface IERXRestResponse {
public void appendContentString(String _str);

public void appendContentData(NSData data);

default public void setContentEncoding(String encoding) {};
}

0 comments on commit fa7f206

Please sign in to comment.