Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[#2523]fix problem when client api do not provide file parameter #4022

Merged
merged 2 commits into from
Nov 10, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,6 @@
import javax.servlet.http.Part;
import javax.ws.rs.core.MediaType;

import com.google.common.annotations.VisibleForTesting;
import org.apache.servicecomb.common.rest.codec.RestClientRequest;
import org.apache.servicecomb.common.rest.codec.RestObjectMapperFactory;
import org.apache.servicecomb.foundation.common.utils.PartUtils;
Expand All @@ -44,6 +43,7 @@
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import com.google.common.annotations.VisibleForTesting;
import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.Multimap;

Expand All @@ -63,7 +63,7 @@ public class RestClientRequestImpl implements RestClientRequest {
protected AsyncResponse asyncResp;

@VisibleForTesting
final Multimap<String, Part> uploads = ArrayListMultimap.create();
protected Multimap<String, Part> uploads;

protected HttpClientRequest request;

Expand Down Expand Up @@ -101,8 +101,11 @@ public Buffer getBodyBuffer() throws Exception {
@Override
@SuppressWarnings("unchecked")
public void attach(String name, Object partOrList) {
if (uploads == null) {
uploads = ArrayListMultimap.create();
}

if (null == partOrList) {
LOGGER.debug("null file is ignored, file name = [{}]", name);
return;
}

Expand All @@ -126,7 +129,7 @@ public void attach(String name, Object partOrList) {
public Future<Void> end() {
writeCookies();

if (!uploads.isEmpty()) {
if (uploads != null) {
return doEndWithUpload();
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -161,6 +161,7 @@ public void doEndWithUploadForJre8() {
try (MockedStatic<UUID> mockedStatic = Mockito.mockStatic(UUID.class)) {
mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
RestClientRequestImpl restClientRequest = new RestClientRequestImpl(request, context, null);
restClientRequest.attach("file", null);
restClientRequest.doEndWithUpload();

Assertions.assertEquals("multipart/form-data; charset=UTF-8; boundary=boundarynull-null-null-null-null",
Expand All @@ -181,6 +182,7 @@ public void doEndWithUploadAfterJre8() {
try (MockedStatic<UUID> mockedStatic = Mockito.mockStatic(UUID.class)) {
mockedStatic.when(UUID::randomUUID).thenReturn(uuid);
RestClientRequestImpl restClientRequest = new RestClientRequestImpl(request, context, null);
restClientRequest.attach("file", null);
restClientRequest.doEndWithUpload();

Assertions.assertEquals("multipart/form-data; charset=UTF-8; boundary=boundary00000000-0000-0000-0000-000000000000",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
/*
* 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.servicecomb.springboot.springmvc.client;

import java.io.File;
import java.nio.charset.StandardCharsets;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.io.FileUtils;
import org.apache.servicecomb.demo.CategorizedTestCase;
import org.apache.servicecomb.demo.TestMgr;
import org.apache.servicecomb.provider.springmvc.reference.RestTemplateBuilder;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestOperations;

@Component
public class UploadDownloadSchemaTest implements CategorizedTestCase {
RestOperations restOperations = RestTemplateBuilder.create();

@Override
public void testRestTransport() throws Exception {
testEmptyFileUploadWork();
testNonEmptyFileUploadWorkd();
}

private void testNonEmptyFileUploadWorkd() throws Exception {
String file2Content = " bonjour";
File someFile = File.createTempFile("upload2", ".txt");
FileUtils.writeStringToFile(someFile, file2Content, StandardCharsets.UTF_8, false);
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
Map<String, Object> params = new HashMap<>();
params.put("name", "test");
params.put("file", someFile);
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(params, headers);
String url = "servicecomb://springmvc/up/down/upload";
String result = restOperations.postForObject(url, entity, String.class);
TestMgr.check("test; bonjour", result);
}

private void testEmptyFileUploadWork() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
Map<String, Object> params = new HashMap<>();
params.put("name", "test");
HttpEntity<Map<String, Object>> entity = new HttpEntity<>(params, headers);
String url = "servicecomb://springmvc/up/down/upload";
String result = restOperations.postForObject(url, entity, String.class);
TestMgr.check("test;", result);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* 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.servicecomb.springboot.springmvc.server;

import java.io.InputStream;
import java.nio.charset.StandardCharsets;

import javax.ws.rs.core.MediaType;

import org.apache.commons.io.IOUtils;
import org.apache.servicecomb.provider.rest.common.RestSchema;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestPart;
import org.springframework.web.multipart.MultipartFile;


@RestSchema(schemaId = "UploadDownloadSchema")
@RequestMapping(path = "/up/down")
public class UploadDownloadSchema {
@PostMapping(path = "/upload", consumes = MediaType.MULTIPART_FORM_DATA)
public String fileUpload(@RequestPart MultipartFile file, @RequestPart(value = "name") String name) throws Exception {
StringBuilder result = new StringBuilder();
result.append(name).append(";");
if (file == null || file.isEmpty()) {
return result.toString();
}
try (InputStream is = file.getInputStream()) {
result.append(IOUtils.toString(is, StandardCharsets.UTF_8));
}
return result.toString();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ public void testRegisteredBasePath() {
if (DynamicPropertyFactory.getInstance().getBooleanProperty("servicecomb.test.vert.transport", true).get()) {
TestMgr.check(22, RegistrationManager.INSTANCE.getMicroservice().getPaths().size());
} else {
TestMgr.check(23, RegistrationManager.INSTANCE.getMicroservice().getPaths().size());
TestMgr.check(24, RegistrationManager.INSTANCE.getMicroservice().getPaths().size());
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,15 +108,14 @@ public Multimap<String, Part> getUploads() {
@SuppressWarnings("unchecked")
@Override
public void attach(String name, Object partOrList) {
if (partOrList == null) {
LOGGER.debug("null file is ignored, file name = [{}]", name);
return;
}

if (uploads == null) {
uploads = ArrayListMultimap.create();
}

if (partOrList == null) {
return;
}

if (partOrList.getClass().isArray()) {
for (Object part : (Object[]) partOrList) {
uploads.put(name, PartUtils.getSinglePart(name, part));
Expand Down
Loading