Skip to content

Commit 08d4d1f

Browse files
Simplify the http-form-data sample and use Java 11 features. (GoogleCloudPlatform#2765)
* Remove unused dependencies from `pom.xml`. * Remove `@MultipartConfig`, an attempted workaround for a bug in multipart handling that doesn't actually help. * Use conventional spelling for `logger` field. It is not a constant so it should not be spelled `LOGGER`. * Use `Map.forEach` to iterate over a map. * Use `List.of(...)` and `Map.of(...)` instead of constructing lists and maps with statements. * Remove unnecessary `throws IOException`.
1 parent 62a7550 commit 08d4d1f

File tree

4 files changed

+18
-67
lines changed

4 files changed

+18
-67
lines changed

functions/http/http-form-data/pom.xml

Lines changed: 1 addition & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -56,21 +56,11 @@
5656
</profiles>
5757

5858
<dependencies>
59-
<dependency>
60-
<groupId>com.google.code.gson</groupId>
61-
<artifactId>gson</artifactId>
62-
<version>2.8.6</version>
63-
</dependency>
64-
<dependency>
65-
<groupId>javax.servlet</groupId>
66-
<artifactId>javax.servlet-api</artifactId>
67-
<version>4.0.1</version>
68-
<scope>provided</scope>
69-
</dependency>
7059
<dependency>
7160
<groupId>com.google.cloud.functions</groupId>
7261
<artifactId>functions-framework-api</artifactId>
7362
<version>1.0.1</version>
63+
<scope>provided</scope>
7464
</dependency>
7565

7666
<!-- The following dependencies are only required for testing -->
@@ -111,12 +101,6 @@
111101
<version>4.13</version>
112102
<scope>test</scope>
113103
</dependency>
114-
<dependency>
115-
<groupId>com.google.guava</groupId>
116-
<artifactId>guava-testlib</artifactId>
117-
<version>29.0-jre</version>
118-
<scope>compile</scope>
119-
</dependency>
120104
</dependencies>
121105

122106
<build>
@@ -148,31 +132,6 @@
148132
<trimStackTrace>false</trimStackTrace>
149133
</configuration>
150134
</plugin>
151-
<plugin> <!-- Required for Java 8 (Alpha) functions in the inline editor -->
152-
<groupId>org.apache.maven.plugins</groupId>
153-
<artifactId>maven-compiler-plugin</artifactId>
154-
<executions>
155-
<execution>
156-
<id>compile</id>
157-
<phase>compile</phase>
158-
<goals>
159-
<goal>compile</goal>
160-
</goals>
161-
</execution>
162-
<execution>
163-
<id>testCompile</id>
164-
<phase>test-compile</phase>
165-
<goals>
166-
<goal>testCompile</goal>
167-
</goals>
168-
</execution>
169-
</executions>
170-
<configuration>
171-
<excludes>
172-
<exclude>.google/</exclude>
173-
</excludes>
174-
</configuration>
175-
</plugin>
176135
</plugins>
177136
</build>
178137
</project>

functions/http/http-form-data/src/main/java/functions/HttpFormData.java

Lines changed: 10 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -28,11 +28,9 @@
2828
import java.nio.file.Paths;
2929
import java.nio.file.StandardCopyOption;
3030
import java.util.logging.Logger;
31-
import javax.servlet.annotation.MultipartConfig;
3231

33-
@MultipartConfig
3432
public class HttpFormData implements HttpFunction {
35-
private static final Logger LOGGER = Logger.getLogger(HttpFormData.class.getName());
33+
private static final Logger logger = Logger.getLogger(HttpFormData.class.getName());
3634

3735
@Override
3836
public void service(HttpRequest request, HttpResponse response)
@@ -51,7 +49,7 @@ public void service(HttpRequest request, HttpResponse response)
5149
continue;
5250
}
5351

54-
LOGGER.info("Processed file: " + filename);
52+
logger.info("Processed file: " + filename);
5553

5654
// Note: GCF's temp directory is an in-memory file system
5755
// Thus, any files in it must fit in the instance's memory.
@@ -66,13 +64,14 @@ public void service(HttpRequest request, HttpResponse response)
6664
}
6765

6866
// This code will process other form fields.
69-
for (String fieldName : request.getQueryParameters().keySet()) {
70-
String firstFieldValue = request.getFirstQueryParameter(fieldName).get();
67+
request.getQueryParameters().forEach(
68+
(fieldName, fieldValues) -> {
69+
String firstFieldValue = fieldValues.get(0);
7170

72-
// TODO(developer): process field values here
73-
LOGGER.info(String.format(
74-
"Processed field: %s (value: %s)", fieldName, firstFieldValue));
75-
}
71+
// TODO(developer): process field values here
72+
logger.info(String.format(
73+
"Processed field: %s (value: %s)", fieldName, firstFieldValue));
74+
});
7675
}
7776
}
78-
// [END functions_http_form_data]
77+
// [END functions_http_form_data]

functions/http/http-form-data/src/test/java/functions/HttpFormDataTest.java

Lines changed: 5 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import static org.powermock.api.mockito.PowerMockito.when;
2424

2525
import com.google.cloud.functions.HttpRequest;
26+
import com.google.cloud.functions.HttpRequest.HttpPart;
2627
import com.google.cloud.functions.HttpResponse;
2728
import com.google.common.testing.TestLogHandler;
2829
import java.io.BufferedWriter;
@@ -32,10 +33,8 @@
3233
import java.io.StringWriter;
3334
import java.net.HttpURLConnection;
3435
import java.nio.charset.StandardCharsets;
35-
import java.util.ArrayList;
36-
import java.util.Arrays;
37-
import java.util.HashMap;
3836
import java.util.List;
37+
import java.util.Map;
3938
import java.util.Optional;
4039
import java.util.logging.Logger;
4140
import org.junit.Before;
@@ -91,17 +90,13 @@ public void functionsHttpMethod_shouldErrorOnGet() throws IOException {
9190
public void functionsHttpFormData_shouldSaveFiles() throws IOException {
9291
when(request.getMethod()).thenReturn("POST");
9392

94-
ArrayList<HttpRequest.HttpPart> partsList = new ArrayList<>();
95-
9693
InputStream stream = new ByteArrayInputStream("foo text%n".getBytes(StandardCharsets.UTF_8));
9794

9895
MockHttpPart mockHttpPart = new MockHttpPart();
9996
mockHttpPart.setFileName("foo.txt");
10097
mockHttpPart.setInputStream(stream);
101-
partsList.add(mockHttpPart);
10298

103-
HashMap<String, HttpRequest.HttpPart> httpParts = new HashMap<>();
104-
httpParts.put("mock", mockHttpPart);
99+
Map<String, HttpPart> httpParts = Map.of("mock", mockHttpPart);
105100
when(request.getParts()).thenReturn(httpParts);
106101

107102
new HttpFormData().service(request, response);
@@ -113,10 +108,9 @@ public void functionsHttpFormData_shouldSaveFiles() throws IOException {
113108
@Test
114109
public void functionsHttpFormData_shouldProcessFields() throws IOException {
115110
when(request.getMethod()).thenReturn("POST");
116-
when(request.getParts()).thenReturn(new HashMap<>());
111+
when(request.getParts()).thenReturn(Map.of());
117112

118-
HashMap<String, List<String>> queryParams = new HashMap<>();
119-
queryParams.put("foo", Arrays.asList(new String[]{"bar"}));
113+
Map<String, List<String>> queryParams = Map.of("foo", List.of("bar"));
120114

121115
when(request.getQueryParameters()).thenReturn(queryParams);
122116
when(request.getFirstQueryParameter("foo")).thenReturn(Optional.of("bar"));

functions/http/http-form-data/src/test/java/functions/MockHttpPart.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818

1919
import com.google.cloud.functions.HttpRequest;
2020
import java.io.BufferedReader;
21-
import java.io.IOException;
2221
import java.io.InputStream;
2322
import java.util.List;
2423
import java.util.Map;
@@ -42,7 +41,7 @@ public Optional<String> getFileName() {
4241
}
4342

4443
@Override
45-
public InputStream getInputStream() throws IOException {
44+
public InputStream getInputStream() {
4645
return inputStream;
4746
}
4847

@@ -63,7 +62,7 @@ public Optional<String> getCharacterEncoding() {
6362
}
6463

6564
@Override
66-
public BufferedReader getReader() throws IOException {
65+
public BufferedReader getReader() {
6766
return null;
6867
}
6968

0 commit comments

Comments
 (0)