use of jakarta.servlet.http.Part in project spring-framework by spring-projects.
the class HtmlUnitRequestBuilderTests method buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter.
// gh-26799
@Test
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithNullFileToUploadAsParameter() throws Exception {
webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", null, null, null, (Charset) null)));
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
assertThat(actualRequest.getParts()).hasSize(1);
Part part = actualRequest.getPart("key");
assertSoftly(softly -> {
softly.assertThat(part).as("part").isNotNull();
softly.assertThat(part.getName()).as("name").isEqualTo("key");
softly.assertThat(part.getSize()).as("size").isEqualTo(0);
try {
softly.assertThat(part.getInputStream()).as("input stream").isEmpty();
} catch (IOException ex) {
softly.fail("failed to get InputStream", ex);
}
softly.assertThat(part.getSubmittedFileName()).as("filename").isEqualTo("");
softly.assertThat(part.getContentType()).as("content-type").isEqualTo("application/octet-stream");
});
}
use of jakarta.servlet.http.Part in project tomcat by apache.
the class Request method recycle.
/**
* Release all object references, and initialize instance variables, in
* preparation for reuse of this object.
*/
public void recycle() {
internalDispatcherType = null;
requestDispatcherPath = null;
authType = null;
inputBuffer.recycle();
usingInputStream = false;
usingReader = false;
userPrincipal = null;
subject = null;
parametersParsed = false;
if (parts != null) {
for (Part part : parts) {
try {
part.delete();
} catch (IOException ignored) {
// ApplicationPart.delete() never throws an IOEx
}
}
parts = null;
}
partsParseException = null;
locales.clear();
localesParsed = false;
secure = false;
remoteAddr = null;
peerAddr = null;
remoteHost = null;
remotePort = -1;
localPort = -1;
localAddr = null;
localName = null;
attributes.clear();
sslAttributesParsed = false;
notes.clear();
recycleSessionInfo();
recycleCookieInfo(false);
if (getDiscardFacades()) {
parameterMap = new ParameterMap<>();
} else {
parameterMap.setLocked(false);
parameterMap.clear();
}
mappingData.recycle();
applicationMapping.recycle();
applicationRequest = null;
if (getDiscardFacades()) {
if (facade != null) {
facade.clear();
facade = null;
}
if (inputStream != null) {
inputStream.clear();
inputStream = null;
}
if (reader != null) {
reader.clear();
reader = null;
}
}
asyncSupported = null;
if (asyncContext != null) {
asyncContext.recycle();
}
asyncContext = null;
}
use of jakarta.servlet.http.Part in project tomcat by apache.
the class HTMLManagerServlet method upload.
protected String upload(HttpServletRequest request, StringManager smClient) {
String message = "";
try {
while (true) {
Part warPart = request.getPart("deployWar");
if (warPart == null) {
message = smClient.getString("htmlManagerServlet.deployUploadNoFile");
break;
}
String filename = warPart.getSubmittedFileName();
if (!filename.toLowerCase(Locale.ENGLISH).endsWith(".war")) {
message = smClient.getString("htmlManagerServlet.deployUploadNotWar", filename);
break;
}
// Get the filename if uploaded name includes a path
if (filename.lastIndexOf('\\') >= 0) {
filename = filename.substring(filename.lastIndexOf('\\') + 1);
}
if (filename.lastIndexOf('/') >= 0) {
filename = filename.substring(filename.lastIndexOf('/') + 1);
}
// Identify the appBase of the owning Host of this Context
// (if any)
File file = new File(host.getAppBaseFile(), filename);
if (file.exists()) {
message = smClient.getString("htmlManagerServlet.deployUploadWarExists", filename);
break;
}
ContextName cn = new ContextName(filename, true);
String name = cn.getName();
if ((host.findChild(name) != null) && !isDeployed(name)) {
message = smClient.getString("htmlManagerServlet.deployUploadInServerXml", filename);
break;
}
if (tryAddServiced(name)) {
try {
warPart.write(file.getAbsolutePath());
} finally {
removeServiced(name);
}
// Perform new deployment
check(name);
} else {
message = smClient.getString("managerServlet.inService", name);
}
break;
}
} catch (Exception e) {
message = smClient.getString("htmlManagerServlet.deployUploadFail", e.getMessage());
log(message, e);
}
return message;
}
use of jakarta.servlet.http.Part in project spring-framework by spring-projects.
the class HtmlUnitRequestBuilderTests method buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter.
// gh-24926
@Test
public void buildRequestParameterMapViaWebRequestDotSetRequestParametersWithFileToUploadAsParameter() throws Exception {
webRequest.setRequestParameters(Collections.singletonList(new KeyDataPair("key", new ClassPathResource("org/springframework/test/web/htmlunit/test.txt").getFile(), "test.txt", MimeType.TEXT_PLAIN, StandardCharsets.UTF_8)));
MockHttpServletRequest actualRequest = requestBuilder.buildRequest(servletContext);
assertThat(actualRequest.getParts()).hasSize(1);
Part part = actualRequest.getPart("key");
assertThat(part).isNotNull();
assertThat(part.getName()).isEqualTo("key");
assertThat(IOUtils.toString(part.getInputStream(), StandardCharsets.UTF_8)).isEqualTo("test file");
assertThat(part.getSubmittedFileName()).isEqualTo("test.txt");
assertThat(part.getContentType()).isEqualTo(MimeType.TEXT_PLAIN);
}
use of jakarta.servlet.http.Part in project spring-framework by spring-projects.
the class RequestParamMapMethodArgumentResolverTests method resolveMultiValueMapOfPart.
@Test
@SuppressWarnings("unchecked")
public void resolveMultiValueMapOfPart() throws Exception {
MockHttpServletRequest request = new MockHttpServletRequest();
request.setContentType("multipart/form-data");
Part expected1 = new MockPart("mfilelist", "Hello World 1".getBytes());
Part expected2 = new MockPart("mfilelist", "Hello World 2".getBytes());
Part expected3 = new MockPart("other", "Hello World 3".getBytes());
request.addPart(expected1);
request.addPart(expected2);
request.addPart(expected3);
webRequest = new ServletWebRequest(request);
MethodParameter param = this.testMethod.annot(requestParam().noName()).arg(MultiValueMap.class, String.class, Part.class);
Object result = resolver.resolveArgument(param, null, webRequest, null);
boolean condition = result instanceof MultiValueMap;
assertThat(condition).isTrue();
MultiValueMap<String, Part> resultMap = (MultiValueMap<String, Part>) result;
assertThat(resultMap.size()).isEqualTo(2);
assertThat(resultMap.get("mfilelist").size()).isEqualTo(2);
assertThat(resultMap.get("mfilelist").get(0)).isEqualTo(expected1);
assertThat(resultMap.get("mfilelist").get(1)).isEqualTo(expected2);
assertThat(resultMap.get("other").size()).isEqualTo(1);
assertThat(resultMap.get("other").get(0)).isEqualTo(expected3);
}
Aggregations