Java Code Examples for org.springframework.web.util.JavaScriptUtils

The following examples show how to use org.springframework.web.util.JavaScriptUtils. These examples are extracted from open source projects. You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source Project: sinavi-jfw   Source File: TagUtils.java    License: Apache License 2.0 6 votes vote down vote up
/**
 * リンクとして出力するURLを生成します。
 * @param url パス
 * @param params パスに付与するパラメータ
 * @param pageContext ページコンテキスト
 * @param isHtmlEscape HTMLの特殊文字をエスケープするかどうか
 * @param isJavaScriptEscape JavaScriptの特殊文字をエスケープするかどうか
 * @return パス
 * @throws JspException 予期しない例外
 */
public static String createUrl(String url, Map<String, String[]> params, PageContext pageContext, boolean isHtmlEscape, boolean isJavaScriptEscape) throws JspException {
    HttpServletRequest request = (HttpServletRequest)pageContext.getRequest();
    HttpServletResponse response = (HttpServletResponse)pageContext.getResponse();

    StringBuilder buffer = new StringBuilder();
    UrlType urlType = getUrlType(url);
    if (urlType == UrlType.CONTEXT_RELATIVE) {
        buffer.append(request.getContextPath());
        if (!url.startsWith("/")) {
            buffer.append("/");
        }
    }
    buffer.append(replaceUriTemplateParams(url, params, pageContext));
    buffer.append(createQueryString(params, (url.indexOf("?") == -1), pageContext));

    String urlStr = buffer.toString();
    if (urlType != UrlType.ABSOLUTE) {
        urlStr = response.encodeURL(urlStr);
    }

    urlStr = isHtmlEscape ? HtmlUtils.htmlEscape(urlStr) : urlStr;
    urlStr = isJavaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;

    return urlStr;
}
 
Example 2
Source Project: spring-analysis-note   Source File: UrlTag.java    License: MIT License 5 votes vote down vote up
/**
 * Build the URL for the tag from the tag attributes and parameters.
 * @return the URL value as a String
 */
String createUrl() throws JspException {
	Assert.state(this.value != null, "No value set");
	HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
	HttpServletResponse response = (HttpServletResponse) this.pageContext.getResponse();

	StringBuilder url = new StringBuilder();
	if (this.type == UrlType.CONTEXT_RELATIVE) {
		// add application context to url
		if (this.context == null) {
			url.append(request.getContextPath());
		}
		else {
			if (this.context.endsWith("/")) {
				url.append(this.context.substring(0, this.context.length() - 1));
			}
			else {
				url.append(this.context);
			}
		}
	}
	if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {
		url.append("/");
	}
	url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
	url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));

	String urlStr = url.toString();
	if (this.type != UrlType.ABSOLUTE) {
		// Add the session identifier if needed
		// (Do not embed the session identifier in a remote link!)
		urlStr = response.encodeURL(urlStr);
	}

	// HTML and/or JavaScript escape, if demanded.
	urlStr = htmlEscape(urlStr);
	urlStr = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr);

	return urlStr;
}
 
Example 3
Source Project: spring-analysis-note   Source File: EscapeBodyTag.java    License: MIT License 5 votes vote down vote up
@Override
public int doAfterBody() throws JspException {
	try {
		String content = readBodyContent();
		// HTML and/or JavaScript escape, if demanded
		content = htmlEscape(content);
		content = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content);
		writeBodyContent(content);
	}
	catch (IOException ex) {
		throw new JspException("Could not write escaped body", ex);
	}
	return (SKIP_BODY);
}
 
Example 4
Source Project: spring-analysis-note   Source File: HtmlFileTransportHandler.java    License: MIT License 5 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("<script>\np(\"%s\");\n</script>\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example 5
Source Project: java-technology-stack   Source File: UrlTag.java    License: MIT License 5 votes vote down vote up
/**
 * Build the URL for the tag from the tag attributes and parameters.
 * @return the URL value as a String
 */
String createUrl() throws JspException {
	Assert.state(this.value != null, "No value set");
	HttpServletRequest request = (HttpServletRequest) this.pageContext.getRequest();
	HttpServletResponse response = (HttpServletResponse) this.pageContext.getResponse();

	StringBuilder url = new StringBuilder();
	if (this.type == UrlType.CONTEXT_RELATIVE) {
		// add application context to url
		if (this.context == null) {
			url.append(request.getContextPath());
		}
		else {
			if (this.context.endsWith("/")) {
				url.append(this.context.substring(0, this.context.length() - 1));
			}
			else {
				url.append(this.context);
			}
		}
	}
	if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {
		url.append("/");
	}
	url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
	url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));

	String urlStr = url.toString();
	if (this.type != UrlType.ABSOLUTE) {
		// Add the session identifier if needed
		// (Do not embed the session identifier in a remote link!)
		urlStr = response.encodeURL(urlStr);
	}

	// HTML and/or JavaScript escape, if demanded.
	urlStr = htmlEscape(urlStr);
	urlStr = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr);

	return urlStr;
}
 
Example 6
Source Project: java-technology-stack   Source File: EscapeBodyTag.java    License: MIT License 5 votes vote down vote up
@Override
public int doAfterBody() throws JspException {
	try {
		String content = readBodyContent();
		// HTML and/or JavaScript escape, if demanded
		content = htmlEscape(content);
		content = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content);
		writeBodyContent(content);
	}
	catch (IOException ex) {
		throw new JspException("Could not write escaped body", ex);
	}
	return (SKIP_BODY);
}
 
Example 7
Source Project: java-technology-stack   Source File: HtmlFileTransportHandler.java    License: MIT License 5 votes vote down vote up
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("<script>\np(\"%s\");\n</script>\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example 8
Source Project: lams   Source File: UrlTag.java    License: GNU General Public License v2.0 5 votes vote down vote up
/**
 * Build the URL for the tag from the tag attributes and parameters.
 * @return the URL value as a String
 */
String createUrl() throws JspException {
	HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
	HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();

	StringBuilder url = new StringBuilder();
	if (this.type == UrlType.CONTEXT_RELATIVE) {
		// add application context to url
		if (this.context == null) {
			url.append(request.getContextPath());
		}
		else {
			if (this.context.endsWith("/")) {
				url.append(this.context.substring(0, this.context.length() - 1));
			}
			else {
				url.append(this.context);
			}
		}
	}
	if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {
		url.append("/");
	}
	url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
	url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));

	String urlStr = url.toString();
	if (this.type != UrlType.ABSOLUTE) {
		// Add the session identifier if needed
		// (Do not embed the session identifier in a remote link!)
		urlStr = response.encodeURL(urlStr);
	}

	// HTML and/or JavaScript escape, if demanded.
	urlStr = htmlEscape(urlStr);
	urlStr = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr);

	return urlStr;
}
 
Example 9
Source Project: lams   Source File: EscapeBodyTag.java    License: GNU General Public License v2.0 5 votes vote down vote up
@Override
public int doAfterBody() throws JspException {
	try {
		String content = readBodyContent();
		// HTML and/or JavaScript escape, if demanded
		content = htmlEscape(content);
		content = (this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content);
		writeBodyContent(content);
	}
	catch (IOException ex) {
		throw new JspException("Could not write escaped body", ex);
	}
	return (SKIP_BODY);
}
 
Example 10
Source Project: spring4-understanding   Source File: UrlTag.java    License: Apache License 2.0 5 votes vote down vote up
/**
 * Build the URL for the tag from the tag attributes and parameters.
 * @return the URL value as a String
 * @throws JspException
 */
private String createUrl() throws JspException {
	HttpServletRequest request = (HttpServletRequest) pageContext.getRequest();
	HttpServletResponse response = (HttpServletResponse) pageContext.getResponse();
	StringBuilder url = new StringBuilder();
	if (this.type == UrlType.CONTEXT_RELATIVE) {
		// add application context to url
		if (this.context == null) {
			url.append(request.getContextPath());
		}
		else {
			if (this.context.endsWith("/")) {
				url.append(this.context.substring(0, this.context.length() - 1));
			}
			else {
				url.append(this.context);
			}
		}
	}
	if (this.type != UrlType.RELATIVE && this.type != UrlType.ABSOLUTE && !this.value.startsWith("/")) {
		url.append("/");
	}
	url.append(replaceUriTemplateParams(this.value, this.params, this.templateParams));
	url.append(createQueryString(this.params, this.templateParams, (url.indexOf("?") == -1)));

	String urlStr = url.toString();
	if (this.type != UrlType.ABSOLUTE) {
		// Add the session identifier if needed
		// (Do not embed the session identifier in a remote link!)
		urlStr = response.encodeURL(urlStr);
	}

	// HTML and/or JavaScript escape, if demanded.
	urlStr = htmlEscape(urlStr);
	urlStr = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(urlStr) : urlStr;

	return urlStr;
}
 
Example 11
Source Project: spring4-understanding   Source File: EscapeBodyTag.java    License: Apache License 2.0 5 votes vote down vote up
@Override
public int doAfterBody() throws JspException {
	try {
		String content = readBodyContent();
		// HTML and/or JavaScript escape, if demanded
		content = htmlEscape(content);
		content = this.javaScriptEscape ? JavaScriptUtils.javaScriptEscape(content) : content;
		writeBodyContent(content);
	}
	catch (IOException ex) {
		throw new JspException("Could not write escaped body", ex);
	}
	return (SKIP_BODY);
}
 
Example 12
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	return new DefaultSockJsFrameFormat("<script>\np(\"%s\");\n</script>\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example 13
@Override
protected SockJsFrameFormat getFrameFormat(ServerHttpRequest request) {
	// We already validated the parameter above...
	String callback = getCallbackParam(request);

	return new DefaultSockJsFrameFormat("/**/" + callback + "(\"%s\");\r\n") {
		@Override
		protected String preProcessContent(String content) {
			return JavaScriptUtils.javaScriptEscape(content);
		}
	};
}
 
Example 14
private JSONObject testCaseExecutionToJSONObject(
            TestCaseExecution testCaseExecution) throws JSONException {
        JSONObject result = new JSONObject();
        result.put("ID", String.valueOf(testCaseExecution.getId()));
        result.put("Test", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTest()));
        result.put("TestCase", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCase()));
        result.put("Environment", JavaScriptUtils.javaScriptEscape(testCaseExecution.getEnvironment()));
        result.put("Start", testCaseExecution.getStart());
        result.put("End", testCaseExecution.getEnd());
        result.put("Country", JavaScriptUtils.javaScriptEscape(testCaseExecution.getCountry()));
        result.put("Browser", JavaScriptUtils.javaScriptEscape(testCaseExecution.getBrowser()));
        result.put("ControlStatus", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlStatus()));
        result.put("ControlMessage", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlMessage()));
        result.put("Status", JavaScriptUtils.javaScriptEscape(testCaseExecution.getStatus()));

        JSONArray bugs = new JSONArray();
        if (testCaseExecution.getApplicationObj() != null && testCaseExecution.getApplicationObj().getBugTrackerUrl() != null
                && !"".equals(testCaseExecution.getApplicationObj().getBugTrackerUrl()) && testCaseExecution.getTestCaseObj().getBugs() != null) {
//            bugId = testCaseExecution.getApplicationObj().getBugTrackerUrl().replace("%BUGID%", testCaseExecution.getTestCaseObj().getBugID());
//            bugId = new StringBuffer("<a href='")
//                    .append(bugId)
//                    .append("' target='reportBugID'>")
//                    .append(testCaseExecution.getTestCaseObj().getBugID())
//                    .append("</a>")
//                    .toString();
        } else {
            bugs = testCaseExecution.getTestCaseObj().getBugs();
        }
        result.put("bugs", bugs);

        result.put("Comment", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCaseObj().getComment()));
        result.put("Priority", JavaScriptUtils.javaScriptEscape(String.valueOf(testCaseExecution.getTestCaseObj().getPriority())));
        result.put("Application", JavaScriptUtils.javaScriptEscape(testCaseExecution.getApplication()));
        result.put("ShortDescription", testCaseExecution.getTestCaseObj().getDescription());

        return result;
    }
 
Example 15
Source Project: onetwo   Source File: XssUtils.java    License: Apache License 2.0 4 votes vote down vote up
public static String escape(String content){
	return HtmlUtils.htmlEscape(JavaScriptUtils.javaScriptEscape(content));
}
 
Example 16
private JSONObject testCaseExecutionToJSONObject(TestCaseExecution testCaseExecution) throws JSONException {
    JSONObject result = new JSONObject();
    result.put("ID", String.valueOf(testCaseExecution.getId()));
    result.put("QueueID", String.valueOf(testCaseExecution.getQueueID()));
    result.put("Test", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTest()));
    result.put("TestCase", JavaScriptUtils.javaScriptEscape(testCaseExecution.getTestCase()));
    result.put("Environment", JavaScriptUtils.javaScriptEscape(testCaseExecution.getEnvironment()));
    result.put("Start", testCaseExecution.getStart());
    result.put("End", testCaseExecution.getEnd());
    result.put("Country", JavaScriptUtils.javaScriptEscape(testCaseExecution.getCountry()));
    result.put("RobotDecli", JavaScriptUtils.javaScriptEscape(testCaseExecution.getRobotDecli()));
    result.put("ManualExecution", JavaScriptUtils.javaScriptEscape(testCaseExecution.getManualExecution()));
    if (testCaseExecution.getExecutor() != null) {
        result.put("Executor", JavaScriptUtils.javaScriptEscape(testCaseExecution.getExecutor()));
    }
    result.put("ControlStatus", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlStatus()));
    result.put("ControlMessage", JavaScriptUtils.javaScriptEscape(testCaseExecution.getControlMessage()));
    result.put("Status", JavaScriptUtils.javaScriptEscape(testCaseExecution.getStatus()));
    result.put("NbExecutions", String.valueOf(testCaseExecution.getNbExecutions()));
    result.put("previousExeId", testCaseExecution.getPreviousExeId());
    if (testCaseExecution.getPreviousExeStatus() != null) {
        result.put("previousExeControlStatus", JavaScriptUtils.javaScriptEscape(testCaseExecution.getPreviousExeStatus()));
    }
    if (testCaseExecution.getQueueState() != null) {
        result.put("QueueState", JavaScriptUtils.javaScriptEscape(testCaseExecution.getQueueState()));
    }

    List<JSONObject> testCaseDep = new ArrayList<>();

    if (testCaseExecution.getTestCaseExecutionQueueDepList() != null) {
        for (TestCaseExecutionQueueDep tce : testCaseExecution.getTestCaseExecutionQueueDepList()) {
            JSONObject obj = new JSONObject();
            obj.put("test", tce.getDepTest());
            obj.put("testcase", tce.getDepTestCase());
            testCaseDep.add(obj);
        }
    }
    result.put("TestCaseDep", testCaseDep);

    return result;
}
 
Example 17
/**
 * 对值进行JavaScript转义
 *
 * @param input
 *            输入文本
 * @return 转义文本
 */
public String javaScript(String input) {
	return JavaScriptUtils.javaScriptEscape(input);
}