[struts] branch WW-5080-plain-result updated (f15d85b -> f02e0d2)
This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a change to branch WW-5080-plain-result in repository https://gitbox.apache.org/repos/asf/struts.git. discard f15d85b WW-5080 Defines a new result type plain to use directly with Java code new f02e0d2 WW-5080 Defines a new result type plain to use directly with Java code This update added new revisions after undoing existing revisions. That is to say, some revisions that were in the old version of the branch are not in the new version. This situation occurs when a user --force pushes a change and generates a repository containing something like this: * -- * -- B -- O -- O -- O (f15d85b) \ N -- N -- N refs/heads/WW-5080-plain-result (f02e0d2) You should already have received notification emails for all of the O revisions, and so the following emails describe only the N revisions from the common base, B. Any revisions marked "omit" are not gone; other references still refer to them. Any revisions marked "discard" are gone forever. The 1 revisions listed above as "new" are entirely new to this repository and will be described in separate emails. The revisions listed as "add" were already present in the repository and have only been added to this reference. Summary of changes: .../org/apache/struts2/result/PlainResult.java | 6 +-- .../org/apache/struts2/result/PlainResultTest.java | 58 +++--- 2 files changed, 20 insertions(+), 44 deletions(-)
[struts] 01/01: WW-5080 Defines a new result type plain to use directly with Java code
This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch WW-5080-plain-result in repository https://gitbox.apache.org/repos/asf/struts.git commit f02e0d2b815aff49ce88b5e0f0296733586b28d2 Author: Lukasz Lenart AuthorDate: Mon Jun 29 08:16:48 2020 +0200 WW-5080 Defines a new result type plain to use directly with Java code --- .../org/apache/struts2/result/PlainResult.java | 64 .../apache/struts2/result/plain/BodyWriter.java| 41 .../struts2/result/plain/DateHttpHeader.java | 38 +++ .../apache/struts2/result/plain/HttpCookies.java | 39 .../apache/struts2/result/plain/HttpHeader.java| 27 + .../apache/struts2/result/plain/HttpHeaders.java | 58 +++ .../apache/struts2/result/plain/IntHttpHeader.java | 39 .../struts2/result/plain/ResponseBuilder.java | 111 + .../struts2/result/plain/StringHttpHeader.java | 39 .../org/apache/struts2/result/PlainResultTest.java | 99 ++ 10 files changed, 555 insertions(+) diff --git a/core/src/main/java/org/apache/struts2/result/PlainResult.java b/core/src/main/java/org/apache/struts2/result/PlainResult.java new file mode 100644 index 000..3cbeb09 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/result/PlainResult.java @@ -0,0 +1,64 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.result; + +import com.opensymphony.xwork2.ActionInvocation; +import com.opensymphony.xwork2.Result; +import org.apache.struts2.StrutsException; +import org.apache.struts2.result.plain.HttpHeader; +import org.apache.struts2.result.plain.ResponseBuilder; + +import javax.servlet.http.Cookie; +import javax.servlet.http.HttpServletResponse; + +public interface PlainResult extends Result { + +@Override +default void execute(ActionInvocation invocation) throws Exception { +ResponseBuilder builder = new ResponseBuilder(); +write(builder); + +HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); + +if (response.isCommitted()) { +throw new StrutsException("Http response already committed, cannot modify it!"); +} + +for (HttpHeader header : builder.getStringHeaders()) { +response.addHeader(header.getName(), header.getValue()); +} +for (HttpHeader header : builder.getDateHeaders()) { +response.addDateHeader(header.getName(), header.getValue()); +} +for (HttpHeader header : builder.getIntHeaders()) { +response.addIntHeader(header.getName(), header.getValue()); +} + +for (Cookie cookie : builder.getCookies()) { +response.addCookie(cookie); +} + +response.getWriter().write(builder.getBody()); +response.flushBuffer(); +} + +void write(ResponseBuilder response); + +} + diff --git a/core/src/main/java/org/apache/struts2/result/plain/BodyWriter.java b/core/src/main/java/org/apache/struts2/result/plain/BodyWriter.java new file mode 100644 index 000..ff22769 --- /dev/null +++ b/core/src/main/java/org/apache/struts2/result/plain/BodyWriter.java @@ -0,0 +1,41 @@ +/* + * Licensed to the Apache Software Foundation (ASF) under one + * or more contributor license agreements. See the NOTICE file + * distributed with this work for additional information + * regarding copyright ownership. The ASF licenses this file + * to you under the Apache License, Version 2.0 (the + * "License"); you may not use this file except in compliance + * with the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, + * software distributed under the License is distributed on an + * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY + * KIND, either express or implied. See the License for the + * specific language governing permissions and limitations + * under the License. + */ +package org.apache.struts2.result.plain; + +import java.io.StringWriter; + +class BodyWriter { + +private final
[struts] branch WW-5080-plain-result updated: WW-5080 Adds some docs and logging, and option to skip committed response
This is an automated email from the ASF dual-hosted git repository. lukaszlenart pushed a commit to branch WW-5080-plain-result in repository https://gitbox.apache.org/repos/asf/struts.git The following commit(s) were added to refs/heads/WW-5080-plain-result by this push: new f2d73ee WW-5080 Adds some docs and logging, and option to skip committed response f2d73ee is described below commit f2d73ee4f1a71ba03703a6abdb8eeffbcc1a75b2 Author: Lukasz Lenart AuthorDate: Tue Jun 30 07:34:52 2020 +0200 WW-5080 Adds some docs and logging, and option to skip committed response --- .../org/apache/struts2/result/PlainResult.java | 42 +- .../org/apache/struts2/result/PlainResultTest.java | 39 2 files changed, 80 insertions(+), 1 deletion(-) diff --git a/core/src/main/java/org/apache/struts2/result/PlainResult.java b/core/src/main/java/org/apache/struts2/result/PlainResult.java index 3cbeb09..b398b93 100644 --- a/core/src/main/java/org/apache/struts2/result/PlainResult.java +++ b/core/src/main/java/org/apache/struts2/result/PlainResult.java @@ -20,6 +20,9 @@ package org.apache.struts2.result; import com.opensymphony.xwork2.ActionInvocation; import com.opensymphony.xwork2.Result; +import org.apache.logging.log4j.LogManager; +import org.apache.logging.log4j.Logger; +import org.apache.logging.log4j.message.ParameterizedMessage; import org.apache.struts2.StrutsException; import org.apache.struts2.result.plain.HttpHeader; import org.apache.struts2.result.plain.ResponseBuilder; @@ -27,30 +30,51 @@ import org.apache.struts2.result.plain.ResponseBuilder; import javax.servlet.http.Cookie; import javax.servlet.http.HttpServletResponse; +/** + * This result can only be used in code, as a result of action's method, eg.: + * + * public PlainResult execute() { + * return response -> response.write(""); + * } + * + * Please notice the result type of the method is a PlainResult not a String. + */ public interface PlainResult extends Result { +Logger LOG = LogManager.getLogger(PlainResult.class); + @Override default void execute(ActionInvocation invocation) throws Exception { +LOG.debug("Executing plain result"); ResponseBuilder builder = new ResponseBuilder(); write(builder); HttpServletResponse response = invocation.getInvocationContext().getServletResponse(); if (response.isCommitted()) { -throw new StrutsException("Http response already committed, cannot modify it!"); +if (ignoreCommitted()) { +LOG.warn("Http response already committed, ignoring & skipping!"); +return; +} else { +throw new StrutsException("Http response already committed, cannot modify it!"); +} } for (HttpHeader header : builder.getStringHeaders()) { +LOG.debug(new ParameterizedMessage("A string header: {} = {}", header.getName(), header.getValue())); response.addHeader(header.getName(), header.getValue()); } for (HttpHeader header : builder.getDateHeaders()) { +LOG.debug(new ParameterizedMessage("A date header: {} = {}", header.getName(), header.getValue())); response.addDateHeader(header.getName(), header.getValue()); } for (HttpHeader header : builder.getIntHeaders()) { +LOG.debug(new ParameterizedMessage("An int header: {} = {}", header.getName(), header.getValue())); response.addIntHeader(header.getName(), header.getValue()); } for (Cookie cookie : builder.getCookies()) { +LOG.debug(new ParameterizedMessage("A cookie: {} = {}", cookie.getName(), cookie.getValue())); response.addCookie(cookie); } @@ -58,7 +82,23 @@ public interface PlainResult extends Result { response.flushBuffer(); } +/** + * Implement this method in action using lambdas + * + * @param response a response builder used to build a Http response + */ void write(ResponseBuilder response); +/** + * Controls if result should ignore already committed Http response + * If set to true only a warning will be issued and the rest of the result + * will be skipped + * + * @return boolean false by default which means an exception will be thrown + */ +default boolean ignoreCommitted() { +return false; +} + } diff --git a/core/src/test/java/org/apache/struts2/result/PlainResultTest.java b/core/src/test/java/org/apache/struts2/result/PlainResultTest.java index 9e53cee..4cd193c 100644 --- a/core/src/test/java/org/apache/struts2/result/PlainResultTest.java +++ b/core/src/test/java/org/apache/struts2/result/PlainResultTest.java @@ -20,7 +20,9 @@ package org.apache.struts2.result; import com.opensymphony.xwork2.ActionContext; import com.opensymphony.xwork2.mock.MockActionInvocation; +import o