Skip to content

Commit

Permalink
Merge pull request #877 from apache/merge-master-to-7xx-2024-02-16
Browse files Browse the repository at this point in the history
Merge master to 7-x-x 2024-02-16
  • Loading branch information
lukaszlenart authored Feb 21, 2024
2 parents 7edcac8 + 45b06dd commit c5d760c
Show file tree
Hide file tree
Showing 18 changed files with 591 additions and 332 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/scorecards-analysis.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ jobs:
publish_results: true

- name: "Upload artifact"
uses: actions/upload-artifact@26f96dfa697d77e81fd5907df203aa23a56210a8 # 4.3.0
uses: actions/upload-artifact@5d5d22a31266ced268874388b861e4b58bb5c2f3 # 4.3.1
with:
name: SARIF file
path: results.sarif
Expand Down
6 changes: 6 additions & 0 deletions core/src/main/java/org/apache/struts2/components/File.java
Original file line number Diff line number Diff line change
Expand Up @@ -99,4 +99,10 @@ public void setAccept(String accept) {
public void setSize(String size) {
this.size = size;
}

@Override
@StrutsTagAttribute(description="Ignored during file upload")
public void setValue(String value) {
// ignores provided value
}
}
2 changes: 1 addition & 1 deletion core/src/site/resources/tags/file-attributes.html
Original file line number Diff line number Diff line change
Expand Up @@ -371,6 +371,6 @@
<td class="tag-attribute"></td>
<td class="tag-attribute">false</td>
<td class="tag-attribute">String</td>
<td class="tag-attribute">Preset the value of input element.</td>
<td class="tag-attribute">Ignored during file upload</td>
</tr>
</table>
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
import org.apache.struts2.dispatcher.multipart.MultiPartRequest;
import org.apache.struts2.views.freemarker.FreemarkerManager;
import org.apache.struts2.views.velocity.VelocityConstants;
import org.apache.struts2.views.velocity.VelocityManager;
import org.apache.struts2.views.velocity.VelocityManagerInterface;

import java.util.Map;
import java.util.Set;
Expand All @@ -56,7 +56,7 @@ public void setContainer(Container container) {
bindings.put(ActionMapper.class.getName(), addBindings(container, ActionMapper.class, StrutsConstants.STRUTS_MAPPER_CLASS));
bindings.put(MultiPartRequest.class.getName(), addBindings(container, MultiPartRequest.class, StrutsConstants.STRUTS_MULTIPART_PARSER));
bindings.put(FreemarkerManager.class.getName(), addBindings(container, FreemarkerManager.class, StrutsConstants.STRUTS_FREEMARKER_MANAGER_CLASSNAME));
bindings.put(VelocityManager.class.getName(), addBindings(container, VelocityManager.class, VelocityConstants.STRUTS_VELOCITY_MANAGER_CLASSNAME));
bindings.put(VelocityManagerInterface.class.getName(), addBindings(container, VelocityManagerInterface.class, VelocityConstants.STRUTS_VELOCITY_MANAGER_CLASSNAME));
bindings.put(UrlRenderer.class.getName(), addBindings(container, UrlRenderer.class, StrutsConstants.STRUTS_URL_RENDERER));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,6 @@ public void generate() throws IOException {
a.addDefaultToEmpty("name", params.get("name"))
.add("type", "file")
.addIfExists("size", params.get("size"))
.addIfExists("value", params.get("nameValue"))
.addIfTrue("disabled", params.get("disabled"))
.addIfExists("accept", params.get("accept"))
.addIfExists("tabindex", params.get("tabindex"))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ public class FileTest extends AbstractCommonAttributesTest {

public void testRenderTextField() {
tag.setName("name");
tag.setValue("val1");
tag.setSize("10");
tag.setDisabled("true");
tag.setAccept("accept_");
Expand All @@ -43,7 +42,7 @@ public void testRenderTextField() {
map.putAll(tag.getParameters());
theme.renderTag(getTagName(), context);
String output = writer.getBuffer().toString();
String expected = s("<input name='name' type='file' size='10' value='val1' disabled='disabled' accept='accept_' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
String expected = s("<input name='name' type='file' size='10' disabled='disabled' accept='accept_' tabindex='1' id='id1' class='class1' style='style1' title='title'></input>");
assertEquals(expected, output);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -51,8 +51,9 @@ public String intercept(ActionInvocation invocation) throws Exception {
}

if (request.getContentLength() > 0) {
final String encoding = request.getCharacterEncoding();
InputStream is = request.getInputStream();
InputStreamReader reader = new InputStreamReader(is);
InputStreamReader reader = encoding == null ? new InputStreamReader(is) : new InputStreamReader(is, encoding);
handler.toObject(invocation, reader, target);
}
return invocation.invoke();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,154 @@
/*
* 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.rest;

import com.mockobjects.dynamic.AnyConstraintMatcher;
import com.mockobjects.dynamic.Mock;
import com.opensymphony.xwork2.Action;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionInvocation;
import com.opensymphony.xwork2.ActionSupport;
import junit.framework.TestCase;

import java.io.InputStreamReader;
import java.nio.charset.Charset;
import java.nio.charset.StandardCharsets;

import org.apache.struts2.dispatcher.mapper.ActionMapping;
import org.apache.struts2.rest.handler.ContentTypeHandler;
import org.springframework.mock.web.MockHttpServletRequest;

public class ContentTypeInterceptorTest extends TestCase {

public void testRequestWithoutEncoding() throws Exception {
ContentTypeInterceptor interceptor = new ContentTypeInterceptor();

ActionSupport action = new ActionSupport();

Mock mockActionInvocation = new Mock(ActionInvocation.class);
Mock mockContentTypeHandler = new Mock(ContentTypeHandler.class);
mockContentTypeHandler.expect("toObject", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
return true;
}
});
mockActionInvocation.expectAndReturn("invoke", Action.SUCCESS);
mockActionInvocation.expectAndReturn("getAction", action);
Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
mockContentTypeHandlerManager.expectAndReturn("getHandlerForRequest", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
return true;
}
}, mockContentTypeHandler.proxy());
interceptor.setContentTypeHandlerSelector((ContentTypeHandlerManager) mockContentTypeHandlerManager.proxy());

MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(new byte[] {1});

ActionContext.of()
.withActionMapping(new ActionMapping())
.withServletRequest(request)
.bind();

interceptor.intercept((ActionInvocation) mockActionInvocation.proxy());
mockContentTypeHandlerManager.verify();
mockActionInvocation.verify();
mockContentTypeHandler.verify();
}

public void testRequestWithEncodingAscii() throws Exception {
final Charset charset = StandardCharsets.US_ASCII;

ContentTypeInterceptor interceptor = new ContentTypeInterceptor();

ActionSupport action = new ActionSupport();

Mock mockActionInvocation = new Mock(ActionInvocation.class);
Mock mockContentTypeHandler = new Mock(ContentTypeHandler.class);
mockContentTypeHandler.expect("toObject", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
InputStreamReader in = (InputStreamReader) args[1];
return charset.equals(Charset.forName(in.getEncoding()));
}
});
mockActionInvocation.expectAndReturn("invoke", Action.SUCCESS);
mockActionInvocation.expectAndReturn("getAction", action);
Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
mockContentTypeHandlerManager.expectAndReturn("getHandlerForRequest", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
return true;
}
}, mockContentTypeHandler.proxy());
interceptor.setContentTypeHandlerSelector((ContentTypeHandlerManager) mockContentTypeHandlerManager.proxy());

MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(new byte[] {1});
request.setCharacterEncoding(charset.name());

ActionContext.of()
.withActionMapping(new ActionMapping())
.withServletRequest(request)
.bind();

interceptor.intercept((ActionInvocation) mockActionInvocation.proxy());
mockContentTypeHandlerManager.verify();
mockActionInvocation.verify();
mockContentTypeHandler.verify();
}

public void testRequestWithEncodingUtf() throws Exception {
final Charset charset = StandardCharsets.UTF_8;

ContentTypeInterceptor interceptor = new ContentTypeInterceptor();

ActionSupport action = new ActionSupport();

Mock mockActionInvocation = new Mock(ActionInvocation.class);
Mock mockContentTypeHandler = new Mock(ContentTypeHandler.class);
mockContentTypeHandler.expect("toObject", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
InputStreamReader in = (InputStreamReader) args[1];
return charset.equals(Charset.forName(in.getEncoding()));
}
});
mockActionInvocation.expectAndReturn("invoke", Action.SUCCESS);
mockActionInvocation.expectAndReturn("getAction", action);
Mock mockContentTypeHandlerManager = new Mock(ContentTypeHandlerManager.class);
mockContentTypeHandlerManager.expectAndReturn("getHandlerForRequest", new AnyConstraintMatcher() {
public boolean matches(Object[] args) {
return true;
}
}, mockContentTypeHandler.proxy());
interceptor.setContentTypeHandlerSelector((ContentTypeHandlerManager) mockContentTypeHandlerManager.proxy());

MockHttpServletRequest request = new MockHttpServletRequest();
request.setContent(new byte[] {1});
request.setCharacterEncoding(charset.name());

ActionContext.of()
.withActionMapping(new ActionMapping())
.withServletRequest(request)
.bind();

interceptor.intercept((ActionInvocation) mockActionInvocation.proxy());
mockContentTypeHandlerManager.verify();
mockActionInvocation.verify();
mockContentTypeHandler.verify();
}
}
Loading

0 comments on commit c5d760c

Please sign in to comment.