Skip to content

Commit e70e89e

Browse files
authored
chore: upgrade images to java21 (#10166)
* chore: upgrade images to java21 * chore: remove test try-catch
1 parent 73333b1 commit e70e89e

File tree

7 files changed

+343
-0
lines changed

7 files changed

+343
-0
lines changed
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
# Google App Engine Standard Environment Images Sample
2+
3+
<a href="https://console.cloud.google.com/cloudshell/open?git_repo=https://github.com/GoogleCloudPlatform/java-docs-samples&page=editor&open_in_editor=appengine-java21/images/README.md">
4+
<img alt="Open in Cloud Shell" src ="http://gstatic.com/cloudssh/images/open-btn.png"></a>
5+
6+
This sample demonstrates how to use the Images Java API.
7+
8+
See the [Google App Engine standard environment documentation][ae-docs] for more
9+
detailed instructions.
10+
11+
[ae-docs]: https://cloud.google.com/appengine/docs/java/
12+
13+
## Modify the app
14+
15+
Using the [Google Cloud SDK](https://cloud.google.com/sdk/) create a bucket
16+
17+
$ gsutil mb YOUR-PROJECT-ID.appspot.com
18+
19+
* Edit `src/main/java/com/example/appengine/images/ImageServlet.java` and set your `bucket` name.
20+
21+
## Running locally
22+
23+
This example uses the
24+
[App Engine maven plugin](https://cloud.google.com/appengine/docs/java/tools/maven).
25+
To run this sample locally:
26+
27+
$ mvn appengine:run
28+
29+
To see the results of the sample application, open
30+
[localhost:8080](http://localhost:8080) in a web browser.
31+
32+
33+
## Deploying
34+
35+
In the following command, replace YOUR-PROJECT-ID with your
36+
[Google Cloud Project ID](https://developers.google.com/console/help/new/#projectnumber)
37+
and SOME-VERSION with a valid version number.
38+
39+
$ mvn appengine:update -Dappengine.appId=YOUR-PROJECT-ID -Dappengine.version=SOME-VERSION
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
<!--
2+
Copyright 2015 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+
<project xmlns="http://maven.apache.org/POM/4.0.0"
17+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
18+
<modelVersion>4.0.0</modelVersion>
19+
<packaging>war</packaging>
20+
<version>1.0-SNAPSHOT</version>
21+
<groupId>com.example.appengine</groupId>
22+
<artifactId>appengine-images-j21</artifactId>
23+
24+
<!--
25+
The parent pom defines common style checks and testing strategies for our samples.
26+
Removing or replacing it should not affect the execution of the samples in anyway.
27+
-->
28+
<parent>
29+
<groupId>com.google.cloud.samples</groupId>
30+
<artifactId>shared-configuration</artifactId>
31+
<version>1.2.0</version>
32+
</parent>
33+
34+
<properties>
35+
<maven.compiler.target>21</maven.compiler.target>
36+
<maven.compiler.source>21</maven.compiler.source>
37+
</properties>
38+
39+
<dependencies>
40+
<dependency>
41+
<groupId>com.google.appengine</groupId>
42+
<artifactId>appengine-api-1.0-sdk</artifactId>
43+
<version>2.0.38</version>
44+
</dependency>
45+
46+
<dependency>
47+
<groupId>com.google.appengine.tools</groupId>
48+
<artifactId>appengine-gcs-client</artifactId>
49+
<version>0.8.3</version>
50+
</dependency>
51+
52+
<dependency>
53+
<groupId>jakarta.servlet</groupId>
54+
<artifactId>jakarta.servlet-api</artifactId>
55+
<version>6.1.0</version>
56+
<type>jar</type>
57+
<scope>provided</scope>
58+
</dependency>
59+
</dependencies>
60+
61+
<build>
62+
<!-- for hot reload of the web application -->
63+
<outputDirectory>${project.build.directory}/${project.build.finalName}/WEB-INF/classes</outputDirectory>
64+
<plugins>
65+
<plugin>
66+
<groupId>com.google.cloud.tools</groupId>
67+
<artifactId>appengine-maven-plugin</artifactId>
68+
<version>2.5.0</version>
69+
<configuration>
70+
<projectId>GCLOUD_CONFIG</projectId>
71+
<version>GCLOUD_CONFIG</version>
72+
<deploy.promote>true</deploy.promote>
73+
<deploy.stopPreviousVersion>true</deploy.stopPreviousVersion>
74+
</configuration>
75+
</plugin>
76+
77+
<plugin>
78+
<groupId>org.apache.maven.plugins</groupId>
79+
<artifactId>maven-war-plugin</artifactId>
80+
<version>3.4.0</version>
81+
</plugin>
82+
</plugins>
83+
</build>
84+
</project>
Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
1+
/*
2+
* Copyright 2015 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 com.example.appengine.images;
18+
19+
import com.google.appengine.api.blobstore.BlobKey;
20+
import com.google.appengine.api.blobstore.BlobstoreService;
21+
import com.google.appengine.api.blobstore.BlobstoreServiceFactory;
22+
import com.google.appengine.api.images.Image;
23+
import com.google.appengine.api.images.ImagesService;
24+
import com.google.appengine.api.images.ImagesServiceFactory;
25+
import com.google.appengine.api.images.ServingUrlOptions;
26+
import com.google.appengine.api.images.Transform;
27+
import com.google.appengine.tools.cloudstorage.GcsFileOptions;
28+
import com.google.appengine.tools.cloudstorage.GcsFilename;
29+
import com.google.appengine.tools.cloudstorage.GcsService;
30+
import com.google.appengine.tools.cloudstorage.GcsServiceFactory;
31+
import com.google.appengine.tools.cloudstorage.RetryParams;
32+
import jakarta.servlet.annotation.WebServlet;
33+
import jakarta.servlet.http.HttpServlet;
34+
import jakarta.servlet.http.HttpServletRequest;
35+
import jakarta.servlet.http.HttpServletResponse;
36+
import java.io.File;
37+
import java.io.FileInputStream;
38+
import java.io.IOException;
39+
import java.io.PrintWriter;
40+
import java.nio.ByteBuffer;
41+
import java.nio.channels.FileChannel;
42+
43+
// [START gae_java21_images_example]
44+
@SuppressWarnings("serial")
45+
// With @WebServlet annotation the webapp/WEB-INF/web.xml is no longer required.
46+
@WebServlet(
47+
name = "images",
48+
description = "Images: Write an image to a bucket and display it in various sizes",
49+
urlPatterns = "/images")
50+
public class ImagesServlet extends HttpServlet {
51+
final String bucket = "YOUR-BUCKETNAME-HERE";
52+
53+
// [START gae_java21_images_gcs]
54+
private final GcsService gcsService =
55+
GcsServiceFactory.createGcsService(
56+
new RetryParams.Builder()
57+
.initialRetryDelayMillis(10)
58+
.retryMaxAttempts(10)
59+
.totalRetryPeriodMillis(15000)
60+
.build());
61+
// [END gae_java21_images_gcs]
62+
63+
@Override
64+
public void doGet(HttpServletRequest req, HttpServletResponse resp) throws IOException {
65+
66+
// [START gae_java21_images_original_image]
67+
// Read the image.jpg resource into a ByteBuffer.
68+
FileInputStream fileInputStream = new FileInputStream(new File("WEB-INF/image.jpg"));
69+
FileChannel fileChannel = fileInputStream.getChannel();
70+
ByteBuffer byteBuffer = ByteBuffer.allocate((int) fileChannel.size());
71+
fileChannel.read(byteBuffer);
72+
73+
byte[] imageBytes = byteBuffer.array();
74+
75+
// Write the original image to Cloud Storage
76+
gcsService.createOrReplace(
77+
new GcsFilename(bucket, "image.jpeg"),
78+
new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
79+
ByteBuffer.wrap(imageBytes));
80+
// [END gae_java21_images_original_image]
81+
82+
// [START gae_java21_images_resize]
83+
// Get an instance of the imagesService we can use to transform images.
84+
ImagesService imagesService = ImagesServiceFactory.getImagesService();
85+
86+
// Make an image directly from a byte array, and transform it.
87+
Image image = ImagesServiceFactory.makeImage(imageBytes);
88+
Transform resize = ImagesServiceFactory.makeResize(100, 50);
89+
Image resizedImage = imagesService.applyTransform(resize, image);
90+
91+
// Write the transformed image back to a Cloud Storage object.
92+
gcsService.createOrReplace(
93+
new GcsFilename(bucket, "resizedImage.jpeg"),
94+
new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
95+
ByteBuffer.wrap(resizedImage.getImageData()));
96+
// [END gae_java21_images_resize]
97+
98+
// [START gae_java21_images_rotate]
99+
// Make an image from a Cloud Storage object, and transform it.
100+
BlobstoreService blobstoreService = BlobstoreServiceFactory.getBlobstoreService();
101+
BlobKey blobKey = blobstoreService.createGsBlobKey("/gs/" + bucket + "/image.jpeg");
102+
Image blobImage = ImagesServiceFactory.makeImageFromBlob(blobKey);
103+
Transform rotate = ImagesServiceFactory.makeRotate(90);
104+
Image rotatedImage = imagesService.applyTransform(rotate, blobImage);
105+
106+
// Write the transformed image back to a Cloud Storage object.
107+
gcsService.createOrReplace(
108+
new GcsFilename(bucket, "rotatedImage.jpeg"),
109+
new GcsFileOptions.Builder().mimeType("image/jpeg").build(),
110+
ByteBuffer.wrap(rotatedImage.getImageData()));
111+
// [END gae_java21_images_rotate]
112+
113+
// [START gae_java21_images_servingUrl]
114+
// Create a fixed dedicated URL that points to the GCS hosted file
115+
ServingUrlOptions options =
116+
ServingUrlOptions.Builder.withGoogleStorageFileName("/gs/" + bucket + "/image.jpeg")
117+
.imageSize(150)
118+
.crop(true)
119+
.secureUrl(true);
120+
String url = imagesService.getServingUrl(options);
121+
// [END gae_java21_images_servingUrl]
122+
123+
// Output some simple HTML to display the images we wrote to Cloud Storage
124+
// in the browser.
125+
PrintWriter out = resp.getWriter();
126+
out.println("<html><body>\n");
127+
out.println(
128+
"<img src='//storage.cloud.google.com/" + bucket + "/image.jpeg' alt='AppEngine logo' />");
129+
out.println(
130+
"<img src='//storage.cloud.google.com/"
131+
+ bucket
132+
+ "/resizedImage.jpeg' alt='AppEngine logo resized' />");
133+
out.println(
134+
"<img src='//storage.cloud.google.com/"
135+
+ bucket
136+
+ "/rotatedImage.jpeg' alt='AppEngine logo rotated' />");
137+
out.println("<img src='" + url + "' alt='Hosted logo' />");
138+
out.println("</body></html>\n");
139+
}
140+
}
141+
// [END gae_java21_images_example]
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!-- [START_EXCLUDE] -->
3+
<!--
4+
Copyright 2016 Google LLC
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
http://www.apache.org/licenses/LICENSE-2.0
9+
Unless required by applicable law or agreed to in writing, software
10+
distributed under the License is distributed on an "AS IS" BASIS,
11+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
See the License for the specific language governing permissions and
13+
limitations under the License.
14+
-->
15+
<!-- [END_EXCLUDE] -->
16+
<appengine-web-app xmlns="http://appengine.google.com/ns/1.0">
17+
<runtime>java21</runtime>
18+
<app-engine-apis>true</app-engine-apis>
19+
</appengine-web-app>
7.75 KB
Loading
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright 2017 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+
<web-app xmlns="https://jakarta.ee/xml/ns/jakartaee"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="https://jakarta.ee/xml/ns/jakartaee https://jakarta.ee/xml/ns/jakartaee/web-app_6_0.xsd"
22+
version="6.0">
23+
<runtime>java21</runtime> <!-- or another supported version -->
24+
<servlet>
25+
<servlet-name>images</servlet-name>
26+
<servlet-class>com.example.appengine.images.ImagesServlet</servlet-class>
27+
</servlet>
28+
<servlet-mapping>
29+
<servlet-name>images</servlet-name>
30+
<url-pattern>/*</url-pattern>
31+
</servlet-mapping>
32+
<app-engine-apis>true</app-engine-apis>
33+
</web-app>
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
4+
Copyright 2017 Google Inc.
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+
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
20+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
21+
xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee
22+
http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
23+
version="3.1">
24+
<welcome-file-list>
25+
<welcome-file>gaeinfo</welcome-file>
26+
</welcome-file-list>
27+
</web-app>

0 commit comments

Comments
 (0)