-
Notifications
You must be signed in to change notification settings - Fork 165
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add character encoding info to REST responses
- Loading branch information
Showing
7 changed files
with
139 additions
and
20 deletions.
There are no files selected for viewing
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
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
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
100 changes: 100 additions & 0 deletions
100
Frameworks/EOF/ERRest/Sources/er/rest/format/ERXRestWriter.java
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
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(); | ||
} | ||
} |
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
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
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