[2/2] struts-examples git commit: Ignore jenv settings
Ignore jenv settings Project: http://git-wip-us.apache.org/repos/asf/struts-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-examples/commit/9d0a2f30 Tree: http://git-wip-us.apache.org/repos/asf/struts-examples/tree/9d0a2f30 Diff: http://git-wip-us.apache.org/repos/asf/struts-examples/diff/9d0a2f30 Branch: refs/heads/master Commit: 9d0a2f30458f02efb642b879236811f0d42da7ad Parents: b8eb547 Author: Lukasz Lenart Authored: Mon Sep 5 10:12:19 2016 +0200 Committer: Lukasz Lenart Committed: Mon Sep 5 10:12:19 2016 +0200 -- .gitignore | 2 ++ 1 file changed, 2 insertions(+) -- http://git-wip-us.apache.org/repos/asf/struts-examples/blob/9d0a2f30/.gitignore -- diff --git a/.gitignore b/.gitignore index f188d98..c3c65ec 100644 --- a/.gitignore +++ b/.gitignore @@ -17,3 +17,5 @@ # Maven target + +.java-version \ No newline at end of file
[1/2] struts-examples git commit: Adds example how to consume JSON
Repository: struts-examples Updated Branches: refs/heads/master bd7959d98 -> 9d0a2f304 Adds example how to consume JSON Project: http://git-wip-us.apache.org/repos/asf/struts-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-examples/commit/b8eb5472 Tree: http://git-wip-us.apache.org/repos/asf/struts-examples/tree/b8eb5472 Diff: http://git-wip-us.apache.org/repos/asf/struts-examples/diff/b8eb5472 Branch: refs/heads/master Commit: b8eb547247389e3a97f0c1629cde6784d595fa03 Parents: bd7959d Author: Lukasz Lenart Authored: Mon Sep 5 10:10:50 2016 +0200 Committer: Lukasz Lenart Committed: Mon Sep 5 10:10:50 2016 +0200 -- json/src/main/java/org/demo/ConsumeAction.java | 12 - json/src/main/resources/struts.xml | 12 +++-- json/src/main/webapp/WEB-INF/index.jsp | 13 + json/src/main/webapp/consume.html | 53 + json/src/main/webapp/index.html| 10 5 files changed, 86 insertions(+), 14 deletions(-) -- http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/java/org/demo/ConsumeAction.java -- diff --git a/json/src/main/java/org/demo/ConsumeAction.java b/json/src/main/java/org/demo/ConsumeAction.java index c142581..e7e6804 100644 --- a/json/src/main/java/org/demo/ConsumeAction.java +++ b/json/src/main/java/org/demo/ConsumeAction.java @@ -22,12 +22,19 @@ package org.demo; import com.opensymphony.xwork2.ActionSupport; +import org.apache.struts2.interceptor.ServletRequestAware; -public class ConsumeAction extends ActionSupport { +import javax.servlet.http.HttpServletRequest; + +public class ConsumeAction extends ActionSupport implements ServletRequestAware { private MyBean bean = new MyBean(); +private boolean responseAsJson = true; public String execute() throws Exception { +if (responseAsJson) { +return "JSON"; +} return SUCCESS; } @@ -35,4 +42,7 @@ public class ConsumeAction extends ActionSupport { return bean; } +public void setServletRequest(HttpServletRequest request) { +responseAsJson = request.getHeader("Accept").contains("application/json"); +} } \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/resources/struts.xml -- diff --git a/json/src/main/resources/struts.xml b/json/src/main/resources/struts.xml index b82521b..75b1d92 100644 --- a/json/src/main/resources/struts.xml +++ b/json/src/main/resources/struts.xml @@ -8,7 +8,11 @@ - + + + + /WEB-INF/index.jsp + @@ -20,9 +24,11 @@ bean -application/json - WEB-INF/result.jsp + /WEB-INF/result.jsp + +bean + http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/webapp/WEB-INF/index.jsp -- diff --git a/json/src/main/webapp/WEB-INF/index.jsp b/json/src/main/webapp/WEB-INF/index.jsp new file mode 100644 index 000..651f3f1 --- /dev/null +++ b/json/src/main/webapp/WEB-INF/index.jsp @@ -0,0 +1,13 @@ +<%@ page contentType="text/html; charset=UTF-8" %> +<%@ taglib prefix="s" uri="/struts-tags" %> + + +JSON Result + + + +Produce JSON +Consume JSON + + + \ No newline at end of file http://git-wip-us.apache.org/repos/asf/struts-examples/blob/b8eb5472/json/src/main/webapp/consume.html -- diff --git a/json/src/main/webapp/consume.html b/json/src/main/webapp/consume.html new file mode 100644 index 000..6f19177 --- /dev/null +++ b/json/src/main/webapp/consume.html @@ -0,0 +1,53 @@ + + + + How to consume a JSON request + https://ajax.googleapis.com/ajax/libs/jquery/1.12.4/jquery.min.js";> + + +function performSubmit(responseAs) { + var arrNames = []; + + $("[name=names]").each(function() { + arrNames.push($(this).val()); + }); + + var params = { +counter: $("[name=counter]").val(), +names: arrNames + }; + + var acceptHeader = 'application/json'; + if (responseAs === 'html') { +acceptHeader = 'text/html'; + } + + $.ajax({ +url: 'consume.action', +type: 'POST', +data: JSON.stringify(params), +headers: { + Accept: acceptHeader, + "Content-Type": "application/json; charset=utf-8" +}, +dataType: 'json' + }) + .always(function (data) { +$("#response").text(JSON.stringify(data)); + }); +} + + + + + + Counter: + Name 1: + Name 2: + S
struts git commit: WW-4684 Uses Content-Type to perform action based on defined type
Repository: struts Updated Branches: refs/heads/master 7dbe3ea8b -> 547718fcc WW-4684 Uses Content-Type to perform action based on defined type Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/547718fc Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/547718fc Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/547718fc Branch: refs/heads/master Commit: 547718fcc6e1d694cfd6ef5a65a5c42edb49a3dc Parents: 7dbe3ea Author: Lukasz Lenart Authored: Mon Sep 5 12:02:00 2016 +0200 Committer: Lukasz Lenart Committed: Mon Sep 5 12:02:00 2016 +0200 -- .../apache/struts2/json/JSONInterceptor.java| 180 ++- .../struts2/json/JSONInterceptorTest.java | 28 +-- 2 files changed, 112 insertions(+), 96 deletions(-) -- http://git-wip-us.apache.org/repos/asf/struts/blob/547718fc/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java -- diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java index b4ad4b7..d7836eb 100644 --- a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java +++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java @@ -70,20 +70,17 @@ public class JSONInterceptor extends AbstractInterceptor { private boolean noCache = false; private boolean excludeNullProperties; private String callbackParameter; -private String accept; +private String jsonContentType = "application/json"; +private String jsonRpcContentType = "application/json-rpc"; @SuppressWarnings("unchecked") public String intercept(ActionInvocation invocation) throws Exception { HttpServletRequest request = ServletActionContext.getRequest(); HttpServletResponse response = ServletActionContext.getResponse(); -//parameter wasn't set by the interceptor -if (accept == null) { -accept = request.getHeader("accept"); -} - -String[] accepts = accept.split(","); - +String requestContentType = readContentType(request); +String requestContentTypeEncoding = readContentTypeEncoding(request); + Object rootObject = null; final ValueStack stack = invocation.getStack(); if (this.root != null) { @@ -94,103 +91,118 @@ public class JSONInterceptor extends AbstractInterceptor { } } -for (String accept : accepts) { -if ((accept != null) && accept.equalsIgnoreCase("application/json")) { -// load JSON object -Object obj = JSONUtil.deserialize(request.getReader()); +if (jsonContentType.equalsIgnoreCase(requestContentType)) { +// load JSON object +Object obj = JSONUtil.deserialize(request.getReader()); -// JSON array (this.root cannot be null in this case) -if(obj instanceof List && this.root != null) { -String mapKey = this.root; -rootObject = null; +// JSON array (this.root cannot be null in this case) +if(obj instanceof List && this.root != null) { +String mapKey = this.root; +rootObject = null; -if(this.root.indexOf('.') != -1) { -mapKey = this.root.substring(this.root.lastIndexOf('.') + 1); +if(this.root.indexOf('.') != -1) { +mapKey = this.root.substring(this.root.lastIndexOf('.') + 1); -rootObject = stack.findValue(this.root.substring(0, this.root.lastIndexOf('.'))); -if (rootObject == null) { -throw new RuntimeException("JSON array: Invalid root expression: '" + this.root + "'."); -} +rootObject = stack.findValue(this.root.substring(0, this.root.lastIndexOf('.'))); +if (rootObject == null) { +throw new RuntimeException("JSON array: Invalid root expression: '" + this.root + "'."); } - -// create a map with a list inside -Map m = new HashMap(); -m.put(mapKey, new ArrayList((List) obj)); -obj = m; } -if (obj instanceof Map) { -Map json = (Map) obj; +// create a map with a list inside +Map m = new HashMap(); +m.put(mapKey, new ArrayList((List) obj)); +obj = m; +} -// clean up the values -
struts git commit: WW-4684 Uses charset and adds some logging
Repository: struts Updated Branches: refs/heads/master 547718fcc -> 175c852ee WW-4684 Uses charset and adds some logging Project: http://git-wip-us.apache.org/repos/asf/struts/repo Commit: http://git-wip-us.apache.org/repos/asf/struts/commit/175c852e Tree: http://git-wip-us.apache.org/repos/asf/struts/tree/175c852e Diff: http://git-wip-us.apache.org/repos/asf/struts/diff/175c852e Branch: refs/heads/master Commit: 175c852eefbd1353991db6f691264af072ab058c Parents: 547718f Author: Lukasz Lenart Authored: Mon Sep 5 12:14:12 2016 +0200 Committer: Lukasz Lenart Committed: Mon Sep 5 12:14:12 2016 +0200 -- .../java/org/apache/struts2/json/JSONInterceptor.java | 12 +--- .../org/apache/struts2/json/JSONInterceptorTest.java| 2 +- 2 files changed, 10 insertions(+), 4 deletions(-) -- http://git-wip-us.apache.org/repos/asf/struts/blob/175c852e/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java -- diff --git a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java index d7836eb..66be65f 100644 --- a/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java +++ b/plugins/json/src/main/java/org/apache/struts2/json/JSONInterceptor.java @@ -187,19 +187,25 @@ public class JSONInterceptor extends AbstractInterceptor { protected String readContentType(HttpServletRequest request) { String contentType = request.getHeader("Content-Type"); +LOG.debug("Content Type from request: {}", contentType); + if (contentType != null && contentType.contains(";")) { -contentType = contentType.substring(0, contentType.indexOf(";")); +contentType = contentType.substring(0, contentType.indexOf(";")).trim(); } return contentType; } protected String readContentTypeEncoding(HttpServletRequest request) { String contentTypeEncoding = request.getHeader("Content-Type"); -if (contentTypeEncoding != null && contentTypeEncoding.contains(";encoding=")) { -contentTypeEncoding = contentTypeEncoding.substring(contentTypeEncoding.indexOf(";encoding=") + ";encoding=".length()); +LOG.debug("Content Type encoding from request: {}", contentTypeEncoding); + +if (contentTypeEncoding != null && contentTypeEncoding.contains(";charset=")) { +contentTypeEncoding = contentTypeEncoding.substring(contentTypeEncoding.indexOf(";charset=") + ";charset=".length()).trim(); } else { contentTypeEncoding = defaultEncoding; } + +LOG.debug("Content Type encoding to be used in de-serialisation: {}", contentTypeEncoding); return contentTypeEncoding; } http://git-wip-us.apache.org/repos/asf/struts/blob/175c852e/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java -- diff --git a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java index ac4c39b..5ff929c 100644 --- a/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java +++ b/plugins/json/src/test/java/org/apache/struts2/json/JSONInterceptorTest.java @@ -71,7 +71,7 @@ public class JSONInterceptorTest extends StrutsTestCase { private void tryBadJSON(String fileName) throws Exception { // request setRequestContent(fileName); -this.request.addHeader("Content-Type", "application/json;encoding=UTF-8"); +this.request.addHeader("Content-Type", "application/json; charset=UTF-8"); JSONInterceptor interceptor = new JSONInterceptor(); interceptor.setEnableSMD(true);
struts-examples git commit: Uses proper case
Repository: struts-examples Updated Branches: refs/heads/master 9d0a2f304 -> ffb321fa3 Uses proper case Project: http://git-wip-us.apache.org/repos/asf/struts-examples/repo Commit: http://git-wip-us.apache.org/repos/asf/struts-examples/commit/ffb321fa Tree: http://git-wip-us.apache.org/repos/asf/struts-examples/tree/ffb321fa Diff: http://git-wip-us.apache.org/repos/asf/struts-examples/diff/ffb321fa Branch: refs/heads/master Commit: ffb321fa333d1afef93b5674884944e762974094 Parents: 9d0a2f3 Author: Lukasz Lenart Authored: Mon Sep 5 12:15:20 2016 +0200 Committer: Lukasz Lenart Committed: Mon Sep 5 12:15:20 2016 +0200 -- json/src/main/webapp/consume.html | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) -- http://git-wip-us.apache.org/repos/asf/struts-examples/blob/ffb321fa/json/src/main/webapp/consume.html -- diff --git a/json/src/main/webapp/consume.html b/json/src/main/webapp/consume.html index 6f19177..362933a 100644 --- a/json/src/main/webapp/consume.html +++ b/json/src/main/webapp/consume.html @@ -28,7 +28,7 @@ data: JSON.stringify(params), headers: { Accept: acceptHeader, - "Content-Type": "application/json; charset=utf-8" + "Content-Type": "application/json; charset=UTF-8" }, dataType: 'json' })