10000 feat(functions): add missing XML parsing sample · coderatgoogle/java-docs-samples@ad37c55 · GitHub
[go: up one dir, main page]

Skip to content

Commit ad37c55

Browse files
author
ace-n
committed
feat(functions): add missing XML parsing sample
1 parent 9bcb0d0 commit ad37c55

File tree

3 files changed

+253
-0
lines changed

3 files changed

+253
-0
lines changed

functions/http/parse-xml/pom.xml

Lines changed: 105 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,105 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
3+
<!--
4+
Copyright 2022 Google LLC
5+
6+
Licensed under the Apache License, Version 2.0 (the "License");
7+
you may not use this file except in compliance with the License.
8+
You may obtain a copy of the License at
9+
10+
http://www.apache.org/licenses/LICENSE-2.0
11+
12+
Unless required by applicable law or agreed to in writing, software
13+
distributed under the License is distributed on an "AS IS" BASIS,
14+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
See the License for the specific language governing permissions and
16+
limitations under the License.
17+
-->
18+
19+
<project xmlns="http://maven.apache.org/POM/4.0.0"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
22+
<modelVersion>4.0.0</modelVersion>
23+
24+
<groupId>com.example.cloud.functions</groupId>
25+
<artifactId>functions-http-parse-xml</artifactId>
26+
27+
<parent>
28+
<groupId>com.google.cloud.samples</groupId>
29+
<artifactId>shared-configuration</artifactId>
30+
<version>1.2.0</version>
31+
</parent>
32+
33+
<properties>
34+
<maven.compiler.target>11</maven.compiler.target>
35+
<maven.compiler.source>11</maven.compiler.source>
36+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
37+
</properties>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.code.gson</groupId>
42+
<artifactId>gson</artifactId>
43+
<version>2.8.8</version>
44+
</dependency>
45+
<dependency>
46+
<groupId>com.google.cloud.functions</groupId>
47+
<artifactId>functions-framework-api</artifactId>
48+
<version>1.0.4</version>
49+
<scope>provided</scope>
50+
</dependency>
51+
52+
<!-- The following dependencies are only required for testing -->
53+
<dependency>
54+
<groupId>com.google.truth</groupId>
55+
<artifactId>truth</artifactId>
56+
<version>1.1.3</version>
57+
<scope>test</scope>
58+
</dependency>
59+
60+
<dependency>
61+
<groupId>junit</groupId>
62+
<artifactId>junit</artifactId>
63+
<version>4.13.2</version>
64+
<scope>test</scope>
65+
</dependency>
66+
<dependency>
67+
<groupId>org.mockito</groupId>
68+
<artifactId>mockito-core</artifactId>
69+
<version>4.1.0</version>
70+
<scope>test</scope>
71+
</dependency>
72+
</dependencies>
73+
74+
<build>
75+
<plugins>
76+
<plugin>
77+
<!--
78+
Google Cloud Functions Framework Maven plugin
79+
80+
This plugin allows you to run Cloud Functions Java code
81+
locally. Use the following terminal command to run a
82+
given function locally:
83+
84+
mvn function:run -Drun.functionTarget=your.package.yourFunction
85+
-->
86+
<groupId>com.google.cloud.functions</groupId>
87+
<artifactId>function-maven-plugin</artifactId>
88+
<version>0.10.0</version>
89+
<configuration>
90+
<functionTarget>functions.ParseContentType</functionTarget>
91+
</configuration>
92+
</plugin>
93+
<plugin>
94+
<groupId>org.apache.maven.plugins</groupId>
95+
<artifactId>maven-surefire-plugin</artifactId>
96+
<version>3.0.0-M5</version>
97+
<configuration>
98+
<skipTests>${skipTests}</skipTests>
99+
<reportNameSuffix>sponge_log</reportNameSuffix>
100+
<trimStackTrace>false</trimStackTrace>
101+
</configuration>
102+
</plugin>
103+
</plugins>
104+
</build>
105+
</project>
Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package functions;
18+
19+
// [START functions_http_xml]
20+
import com.google.cloud.functions.HttpFunction;
21+
import com.google.cloud.functions.HttpRequest;
22+
import com.google.cloud.functions.HttpResponse;
23+
import java.io.ByteArrayInputStream;
24+
import java.io.IOException;
25+
import java.io.InputStream;
26+
import java.io.PrintWriter;
27+
import java.net.HttpURLConnection;
28+
import javax.xml.parsers.DocumentBuilder;
29+
import javax.xml.parsers.DocumentBuilderFactory;
30+
import javax.xml.parsers.ParserConfigurationException;
31+
import org.w3c.dom.Document;
32+
import org.xml.sax.SAXException;
33+
34+
public class ParseXml implements HttpFunction {
35+
private static DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
36+
37+
// Parses a HTTP request in XML format
38+
// (Responds with a 400 error if the HTTP request isn't valid XML.)
39+
@Override
40+
public void service(HttpRequest request, HttpResponse response)
41+
throws IOException, ParserConfigurationException {
42+
43+
try {
44+
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
45+
var writer = new PrintWriter(response.getWriter());
46+
47+
// Get request body
48+
InputStream bodyStream = new ByteArrayInputStream(
49+
request.getInputStream().readAllBytes());
50+
51+
// Parse + process XML
52+
Document doc = dBuilder.parse(bodyStream);
53+
writer.printf("Root element: %s", doc.getDocumentElement().getNodeName());
54+
} catch (SAXException e) {
55+
response.setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST);
56+
return;
57+
}
58+
}
59+
}
60+
// [END functions_http_xml]
Lines changed: 88 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,88 @@
1+
/*
2+
* Copyright 2022 Google LLC
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
package functions;
18+
19+
import static com.google.common.truth.Truth.assertThat;
20+
import static org.mockito.Mockito.times;
21+
import static org.mockito.Mockito.verify;
22+
import static org.mockito.Mockito.when;
23+
24+
import com.google.cloud.functions.HttpRequest;
25+
import com.google.cloud.functions.HttpResponse;
26+
import java.io.BufferedReader;
27+
import java.io.BufferedWriter;
28+
import java.io.ByteArrayInputStream;
29+
import java.io.IOException;
30+
import java.io.StringReader;
31+
import java.io.StringWriter;
32+
import java.net.HttpURLConnection;
33+
import java.nio.charset.StandardCharsets;
34+
import javax.xml.parsers.ParserConfigurationException;
35+
import org.junit.Before;
36+
import org.junit.Test;
37+
import org.junit.runner.RunWith;
38+
import org.junit.runners.JUnit4;
39+
import org.mockito.Mock;
40+
import org.mockito.MockitoAnnotations;
41+
42+
@RunWith(JUnit4.class)
43+
public class ParseXmlTest {
44+
@Mock private HttpRequest request;
45+
@Mock private HttpResponse response;
46+
47+
private BufferedWriter writerOut;
48+
private StringWriter responseOut;
49+
50+
@Before
51+
public void beforeTest() throws IOException {
52+
MockitoAnnotations.initMocks(this);
53+
54+
responseOut = new StringWriter();
55+
writerOut = new BufferedWriter(responseOut);
56+
when(response.getWriter()).thenReturn(writerOut);
57+
}
58+
59+
@Test
60+
public void parseXmlTest_handlesXml() throws IOException, ParserConfigurationException {
61+
// Send a request with XML data
62+
String requestContent = "<?xml version=\"1.0\"?>\n<name>John</name>\n";
63+
BufferedReader bodyReader = new BufferedReader(new StringReader(requestContent));
64+
65+
when(request.getInputStream()).thenReturn(
66+
new ByteArrayInputStream(requestContent.getBytes(StandardCharsets.UTF_8)));
67+
68+
new ParseXml().service(request, response);
69+
70+
writerOut.flush();
71+
assertThat(responseOut.toString()).contains("Root element: name");
72+
}
73+
74+
75+
@Test
76+
public void parseXmlTest_handlesNonXml() throws IOException, ParserConfigurationException {
77+
// Send a request with plain text
78+
String requestContent = "I am not XML!";
79+
BufferedReader bodyReader = new BufferedReader(new StringReader(requestContent));
80+
81+
when(request.getInputStream()).thenReturn(
82+
new ByteArrayInputStream(requestContent.getBytes(StandardCharsets.UTF_8)));
83+
84+
new ParseXml().service(request, response);
85+
86+
verify(response, times(1)).setStatusCode(HttpURLConnection.HTTP_BAD_REQUEST);
87+
}
88+
}

0 commit comments

Comments
 (0)
0