Initial commit from SVN.

This commit is contained in:
wcrisman
2014-05-30 10:31:51 -07:00
commit b45e56b890
1968 changed files with 370949 additions and 0 deletions

View File

@@ -0,0 +1,105 @@
package com.foundation.web;
import com.common.util.StringSupport;
import com.foundation.controller.Controller;
public class BaseWebController extends Controller {
public static final String URI_COMPONENT_ENCODED_CHARS = ",/?:@&=+$#";
/**
* Decodes the URI component. This is the same function as in JavaScript.
*/
public static String decodeURIComponent(String content) {
StringBuilder buffer = new StringBuilder(content.length());
for(int index = 0, count = content.length(); index < count; index++) {
char next = content.charAt(index);
if(next == '%') {
char n1 = content.charAt(index + 1);
char n2 = content.charAt(index + 2);
int value = (n1 < 65 ? n1 - 48 : n1 > 90 ? n1 - 87 : n1 - 55) << 4 | (n2 < 65 ? n2 - 48 : n2 > 90 ? n2 - 87 : n2 - 55);
buffer.append((char) value);
}//if//
else {
buffer.append(next);
}//else//
}//for//
return buffer.toString();
}//decodeURIComponent()//
/**
* Encodes the URI component. This is the same function as in JavaScript.
*/
public static String encodeURIComponent(String content) {
StringBuilder buffer = new StringBuilder(content.length() * 3);
for(int index = 0, count = content.length(); index < count; index++) {
char next = content.charAt(index);
if(URI_COMPONENT_ENCODED_CHARS.indexOf(next) != -1) {
int value = (int) next;
buffer.append('%');
buffer.append(StringSupport.hexCharacters[((value >> 4) & 0xF)]);
buffer.append(StringSupport.hexCharacters[(value & 0xF)]);
}//if//
else {
buffer.append(next);
}//else//
}//for//
return buffer.toString();
}//encodeURIComponent()//
/**
* Escapes potential control characters for sending data to the web browser where the browser will use the framework javascript's unescape method to remove the escape characters.
*/
public static String encodeFramework(String content) {
return content == null ? null : content.replace("\r\n", "\n").replace("\r", "\n").replace("|", "|6").replace("\\", "|1").replace("'", "|2").replace("\"", "|3").replace("\n", "|4").replace("\t", "|5").replace("&", "|7").replace("<", "|8").replace(">", "|9");
}//escapeFramework//
/**
* Removes the bar escape characters from content sent in the URL using the escape method provided in the framework javascript.
*/
public static String decodeFrameworkUrlParam(String content) {
return content == null ? null : content.replace("|1", "\\").replace("|2", "'").replace("|3", "\"").replace("|4", "\n").replace("|5", "\t").replace("|6", "%").replace("|7", "&").replace("|8", "<").replace("|9", ">").replace("|6", "|");
}//unescapeFrameworkUrlParam//
/**
* Encodes the content characters that conflict with JSON formatting characters.
*/
public static String encodeJsonParameter(String content) {
//Note: According to official JSON docs, single quotes should not be replaced with escaped single quotes because only double quotes may be used to quote values in the JSON (so there shouldn't be any conflicts using single quotes in the content).//
//.replace("'", "\\'")
return content == null ? null : content.replace("\r\n", "\n").replace("\r", "\n").replace("\\", "&#92;").replace("\"", "\\\"").replace("\n", "\\n");
}//encodeJsonParameter()//
/**
* Encodes the characters that mess up text areas.
*/
public static String encodeTextAreaContent(String content) {
return content == null ? null : content.replace("\r\n", "\n").replace("\r", "\n").replace("&", "&amp;").replace("<", "&lt;").replace(">", "&gt;");
}//escapeTextAreaContent()//
/**
* Removes encoding of text for display in text areas.
*/
public static String decodeTextAreaContent(String content) {
return content == null ? null : content.replace("\r\n", "\n").replace("\r", "\n").replace("&lt;", "<").replace("&gt;", ">").replace("&amp;", "&");
}//decodeTextAreaContent()//
/**
* Utility code to build a proper JSON response to a view request that is handled by the framework javascript on the client <code>(javascript: brainstormFramework.callView(..))</code>.
* <p>This response is designed to be processed by the framework javascript on the client as a response to the callView method.</p>
* @param response The optional request result that is passed along with the new view data.
* @param isError Whether the result is an error. Errors are handled with an alert on the client by the javascript that processes the response (not the application code).
* @return The text for the response.
*/
public JsonContent buildResponse(String response, boolean isError) {
StringBuffer buffer = new StringBuffer(1000);
if(isError) {
buffer.append("{\"error\": \"").append(response).append("\"");
}//if//
else {
buffer.append("{\"result\": \"").append(response).append("\"");
}//else//
return new JsonContent(buffer.append("}").toString());
}//buildResponse()//
}//BaseWebController//