Add spring-cloud-function-serverless-web module
This commit is contained in:
@@ -0,0 +1,14 @@
|
|||||||
|
#### Introduction
|
||||||
|
|
||||||
|
This module represents a concept of a light weight AWS forwarding proxy which deploys and interacts with existing
|
||||||
|
Spring Boot web application deployed as AWS Lambda.
|
||||||
|
|
||||||
|
|
||||||
|
A sample is provided in [sample](https://github.com/spring-cloud/spring-cloud-function/tree/serverless-web/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store) directory. It contain README and SAM template file to simplify the deployment. This module is identified as the only additional dependnecy to the existing web-app.
|
||||||
|
|
||||||
|
_NOTE: Although this module is AWS specific, this dependency is protocol only (not binary), therefore there is no AWS dependnecies._
|
||||||
|
|
||||||
|
The aformentioned proxy is identified as AWS Lambda [handler](https://github.com/spring-cloud/spring-cloud-function/blob/serverless-web/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/template.yml#L14)
|
||||||
|
|
||||||
|
The main Spring Boot configuration file is identified as [MAIN_CLASS](https://github.com/spring-cloud/spring-cloud-function/blob/serverless-web/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/template.yml#L22)
|
||||||
|
|
||||||
@@ -0,0 +1,44 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
<artifactId>spring-cloud-function-serverless-web</artifactId>
|
||||||
|
<packaging>jar</packaging>
|
||||||
|
<name>spring-cloud-function-serverless-web</name>
|
||||||
|
<description>Base serverless web adapter</description>
|
||||||
|
<parent>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-function-adapter-parent</artifactId>
|
||||||
|
<version>3.2.9-SNAPSHOT</version>
|
||||||
|
</parent>
|
||||||
|
<properties>
|
||||||
|
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
|
||||||
|
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
|
||||||
|
<java.version>1.8</java.version>
|
||||||
|
</properties>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.fasterxml.jackson.core</groupId>
|
||||||
|
<artifactId>jackson-databind</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-webmvc</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>javax.servlet</groupId>
|
||||||
|
<artifactId>javax.servlet-api</artifactId>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-test</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,13 @@
|
|||||||
|
# This file is auto generated by SAM CLI build command
|
||||||
|
|
||||||
|
[function_build_definitions]
|
||||||
|
[function_build_definitions.9341c1d5-9265-48ef-836e-25df000b0c59]
|
||||||
|
codeuri = "/Users/ozhurakousky/Documents/dev/repo/spring-cloud-function/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store"
|
||||||
|
runtime = "java11"
|
||||||
|
architecture = "x86_64"
|
||||||
|
handler = "org.springframework.cloud.function.adapter.aws.web.WebProxyInvoker::handleRequest"
|
||||||
|
manifest_hash = ""
|
||||||
|
packagetype = "Zip"
|
||||||
|
functions = ["PetStoreFunction"]
|
||||||
|
|
||||||
|
[layer_build_definitions]
|
||||||
@@ -0,0 +1,38 @@
|
|||||||
|
Copied from https://github.com/awslabs/aws-serverless-java-container/tree/main/samples/spring/pet-store
|
||||||
|
|
||||||
|
# Serverless Spring example
|
||||||
|
A basic pet store written with the [Spring framework](https://projects.spring.io/spring-framework/). The `StreamLambdaHandler` object is the main entry point for Lambda.
|
||||||
|
|
||||||
|
The application can be deployed in an AWS account using the [Serverless Application Model](https://github.com/awslabs/serverless-application-model). The `template.yml` file in the root folder contains the application definition.
|
||||||
|
|
||||||
|
## Pre-requisites
|
||||||
|
* [AWS CLI](https://aws.amazon.com/cli/)
|
||||||
|
* [SAM CLI](https://github.com/awslabs/aws-sam-cli)
|
||||||
|
* [Gradle](https://gradle.org/) or [Maven](https://maven.apache.org/)
|
||||||
|
|
||||||
|
## Deployment
|
||||||
|
In a shell, navigate to the sample's folder and use the SAM CLI to build a deployable package
|
||||||
|
```
|
||||||
|
$ sam build
|
||||||
|
```
|
||||||
|
|
||||||
|
This command compiles the application and prepares a deployment package in the `.aws-sam` sub-directory.
|
||||||
|
|
||||||
|
To deploy the application in your AWS account, you can use the SAM CLI's guided deployment process and follow the instructions on the screen
|
||||||
|
|
||||||
|
```
|
||||||
|
$ sam deploy --guided
|
||||||
|
```
|
||||||
|
|
||||||
|
Once the deployment is completed, the SAM CLI will print out the stack's outputs, including the new application URL. You can use `curl` or a web browser to make a call to the URL
|
||||||
|
|
||||||
|
```
|
||||||
|
...
|
||||||
|
---------------------------------------------------------------------------------------------------------
|
||||||
|
OutputKey-Description OutputValue
|
||||||
|
---------------------------------------------------------------------------------------------------------
|
||||||
|
PetStoreApi - URL for application https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/pets
|
||||||
|
---------------------------------------------------------------------------------------------------------
|
||||||
|
|
||||||
|
$ curl https://xxxxxxxxxx.execute-api.us-west-2.amazonaws.com/pets
|
||||||
|
```
|
||||||
@@ -0,0 +1,192 @@
|
|||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
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">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>oz.spring.petstore</groupId>
|
||||||
|
<artifactId>pet-store</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<name>pet-store</name>
|
||||||
|
<description>Simple pet store written with the Spring framework</description>
|
||||||
|
<url>https://aws.amazon.com/lambda/</url>
|
||||||
|
|
||||||
|
<scm>
|
||||||
|
<url>https://github.com/awslabs/aws-serverless-java-container.git</url>
|
||||||
|
</scm>
|
||||||
|
|
||||||
|
<licenses>
|
||||||
|
<license>
|
||||||
|
<name>The Apache Software License, Version 2.0</name>
|
||||||
|
<url>http://www.apache.org/licenses/LICENSE-2.0.txt</url>
|
||||||
|
<distribution>repo</distribution>
|
||||||
|
</license>
|
||||||
|
</licenses>
|
||||||
|
|
||||||
|
<properties>
|
||||||
|
<maven.compiler.source>1.8</maven.compiler.source>
|
||||||
|
<maven.compiler.target>1.8</maven.compiler.target>
|
||||||
|
<spring.version>5.3.25</spring.version>
|
||||||
|
<junit.version>4.13.2</junit.version>
|
||||||
|
<log4j.version>2.19.0</log4j.version>
|
||||||
|
</properties>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.cloud</groupId>
|
||||||
|
<artifactId>spring-cloud-function-adapter-aws-web</artifactId>
|
||||||
|
<version>3.2.9-SNAPSHOT</version>
|
||||||
|
</dependency>
|
||||||
|
<!--
|
||||||
|
the Spring Context Indexer run an annotation processor at compile time and generates
|
||||||
|
a META-INF/spring.components file that Spring can use to speed up component scanning at boot time.
|
||||||
|
For small applications, this doesn't make a big difference. However, for large applications with
|
||||||
|
complex dependencies this may improve your cold start time significantly.
|
||||||
|
-->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework</groupId>
|
||||||
|
<artifactId>spring-context-indexer</artifactId>
|
||||||
|
<version>${spring.version}</version>
|
||||||
|
<optional>true</optional>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-core</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-api</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.apache.logging.log4j</groupId>
|
||||||
|
<artifactId>log4j-slf4j-impl</artifactId>
|
||||||
|
<version>${log4j.version}</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.amazonaws</groupId>
|
||||||
|
<artifactId>aws-lambda-java-log4j2</artifactId>
|
||||||
|
<version>1.5.1</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
<dependency>
|
||||||
|
<groupId>junit</groupId>
|
||||||
|
<artifactId>junit</artifactId>
|
||||||
|
<version>${junit.version}</version>
|
||||||
|
<scope>test</scope>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
<profiles>
|
||||||
|
<profile>
|
||||||
|
<id>shaded-jar</id>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-shade-plugin</artifactId>
|
||||||
|
<version>3.2.4</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>shade</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<createDependencyReducedPom>false</createDependencyReducedPom>
|
||||||
|
<transformers>
|
||||||
|
<transformer
|
||||||
|
implementation="com.github.edwgiz.mavenShadePlugin.log4j2CacheTransformer.PluginsCacheFileTransformer">
|
||||||
|
</transformer>
|
||||||
|
</transformers>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
<dependencies>
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.github.edwgiz</groupId>
|
||||||
|
<artifactId>maven-shade-plugin.log4j2-cachefile-transformer</artifactId>
|
||||||
|
<version>2.8.1</version>
|
||||||
|
</dependency>
|
||||||
|
</dependencies>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
<profile>
|
||||||
|
<id>assembly-zip</id>
|
||||||
|
<activation>
|
||||||
|
<activeByDefault>true</activeByDefault>
|
||||||
|
</activation>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<!-- don't build a jar, we'll use the classes dir -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-jar-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>default-jar</id>
|
||||||
|
<phase>none</phase>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-install-plugin</artifactId>
|
||||||
|
<version>3.0.0-M1</version>
|
||||||
|
<configuration>
|
||||||
|
<skip>true</skip>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
<!-- select and copy only runtime dependencies to a temporary lib folder -->
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-dependency-plugin</artifactId>
|
||||||
|
<version>3.2.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>copy-dependencies</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>copy-dependencies</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<outputDirectory>${project.build.directory}/lib</outputDirectory>
|
||||||
|
<includeScope>runtime</includeScope>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-assembly-plugin</artifactId>
|
||||||
|
<version>3.3.0</version>
|
||||||
|
<executions>
|
||||||
|
<execution>
|
||||||
|
<id>zip-assembly</id>
|
||||||
|
<phase>package</phase>
|
||||||
|
<goals>
|
||||||
|
<goal>single</goal>
|
||||||
|
</goals>
|
||||||
|
<configuration>
|
||||||
|
<finalName>${project.artifactId}-${project.version}</finalName>
|
||||||
|
<descriptors>
|
||||||
|
<descriptor>src${file.separator}assembly${file.separator}bin.xml</descriptor>
|
||||||
|
</descriptors>
|
||||||
|
<attach>false</attach>
|
||||||
|
</configuration>
|
||||||
|
</execution>
|
||||||
|
</executions>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
</profile>
|
||||||
|
</profiles>
|
||||||
|
</project>
|
||||||
@@ -0,0 +1,24 @@
|
|||||||
|
<assembly xmlns="http://maven.apache.org/ASSEMBLY/2.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/ASSEMBLY/2.0.0 http://maven.apache.org/xsd/assembly-2.0.0.xsd">
|
||||||
|
<id>lambda-package</id>
|
||||||
|
<formats>
|
||||||
|
<format>zip</format>
|
||||||
|
</formats>
|
||||||
|
<includeBaseDirectory>false</includeBaseDirectory>
|
||||||
|
<fileSets>
|
||||||
|
<!-- copy runtime dependencies with some exclusions -->
|
||||||
|
<fileSet>
|
||||||
|
<directory>${project.build.directory}${file.separator}lib</directory>
|
||||||
|
<outputDirectory>lib</outputDirectory>
|
||||||
|
</fileSet>
|
||||||
|
<!-- copy all classes -->
|
||||||
|
<fileSet>
|
||||||
|
<directory>${project.build.directory}${file.separator}classes</directory>
|
||||||
|
<includes>
|
||||||
|
<include>**</include>
|
||||||
|
</includes>
|
||||||
|
<outputDirectory>${file.separator}</outputDirectory>
|
||||||
|
</fileSet>
|
||||||
|
</fileSets>
|
||||||
|
</assembly>
|
||||||
@@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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 oz.spring.petstore;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.web.servlet.HandlerAdapter;
|
||||||
|
import org.springframework.web.servlet.HandlerExceptionResolver;
|
||||||
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
import org.springframework.web.servlet.ModelAndView;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||||
|
|
||||||
|
|
||||||
|
@Configuration
|
||||||
|
@Import({ PetsController.class })
|
||||||
|
public class PetStoreSpringAppConfig {
|
||||||
|
/*
|
||||||
|
* Create required HandlerMapping, to avoid several default HandlerMapping instances being created
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public HandlerMapping handlerMapping() {
|
||||||
|
return new RequestMappingHandlerMapping();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create required HandlerAdapter, to avoid several default HandlerAdapter instances being created
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public HandlerAdapter handlerAdapter() {
|
||||||
|
return new RequestMappingHandlerAdapter();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* optimization - avoids creating default exception resolvers; not required as the serverless container handles
|
||||||
|
* all exceptions
|
||||||
|
*
|
||||||
|
* By default, an ExceptionHandlerExceptionResolver is created which creates many dependent object, including
|
||||||
|
* an expensive ObjectMapper instance.
|
||||||
|
*
|
||||||
|
* To enable custom @ControllerAdvice classes remove this bean.
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public HandlerExceptionResolver handlerExceptionResolver() {
|
||||||
|
return new HandlerExceptionResolver() {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ModelAndView resolveException(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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 oz.spring.petstore;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.*;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
|
||||||
|
import oz.spring.petstore.model.Pet;
|
||||||
|
import oz.spring.petstore.model.PetData;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@EnableWebMvc
|
||||||
|
public class PetsController {
|
||||||
|
@RequestMapping(path = "/pets", method = RequestMethod.POST)
|
||||||
|
public Pet createPet(@RequestBody Pet newPet) {
|
||||||
|
if (newPet.getName() == null || newPet.getBreed() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pet dbPet = newPet;
|
||||||
|
dbPet.setId(UUID.randomUUID().toString());
|
||||||
|
return dbPet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = "/pets", method = RequestMethod.GET)
|
||||||
|
public Pet[] listPets(@RequestParam("limit") Optional<Integer> limit, Principal principal) {
|
||||||
|
int queryLimit = 10;
|
||||||
|
if (limit.isPresent()) {
|
||||||
|
queryLimit = limit.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Pet[] outputPets = new Pet[queryLimit];
|
||||||
|
|
||||||
|
for (int i = 0; i < queryLimit; i++) {
|
||||||
|
Pet newPet = new Pet();
|
||||||
|
newPet.setId(UUID.randomUUID().toString());
|
||||||
|
newPet.setName(PetData.getRandomName());
|
||||||
|
newPet.setBreed(PetData.getRandomBreed());
|
||||||
|
newPet.setDateOfBirth(PetData.getRandomDoB());
|
||||||
|
outputPets[i] = newPet;
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputPets;
|
||||||
|
}
|
||||||
|
|
||||||
|
@GetMapping("favicon.ico")
|
||||||
|
@ResponseBody
|
||||||
|
void returnNoFavicon() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET)
|
||||||
|
public Pet listPets() {
|
||||||
|
Pet newPet = new Pet();
|
||||||
|
newPet.setId(UUID.randomUUID().toString());
|
||||||
|
newPet.setBreed(PetData.getRandomBreed());
|
||||||
|
newPet.setDateOfBirth(PetData.getRandomDoB());
|
||||||
|
newPet.setName(PetData.getRandomName());
|
||||||
|
return newPet;
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,33 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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 oz.spring.petstore.model;
|
||||||
|
|
||||||
|
public class Error {
|
||||||
|
private String message;
|
||||||
|
|
||||||
|
public Error(String errorMessage) {
|
||||||
|
message = errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getMessage() {
|
||||||
|
return message;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setMessage(String message) {
|
||||||
|
this.message = message;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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 oz.spring.petstore.model;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Pet {
|
||||||
|
private String id;
|
||||||
|
private String breed;
|
||||||
|
private String name;
|
||||||
|
private Date dateOfBirth;
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBreed() {
|
||||||
|
return breed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBreed(String breed) {
|
||||||
|
this.breed = breed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDateOfBirth() {
|
||||||
|
return dateOfBirth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateOfBirth(Date dateOfBirth) {
|
||||||
|
this.dateOfBirth = dateOfBirth;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,115 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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 oz.spring.petstore.model;
|
||||||
|
|
||||||
|
import java.util.*;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class PetData {
|
||||||
|
private static List<String> breeds = new ArrayList<>();
|
||||||
|
static {
|
||||||
|
breeds.add("Afghan Hound");
|
||||||
|
breeds.add("Beagle");
|
||||||
|
breeds.add("Bernese Mountain Dog");
|
||||||
|
breeds.add("Bloodhound");
|
||||||
|
breeds.add("Dalmatian");
|
||||||
|
breeds.add("Jack Russell Terrier");
|
||||||
|
breeds.add("Norwegian Elkhound");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> names = new ArrayList<>();
|
||||||
|
static {
|
||||||
|
names.add("Bailey");
|
||||||
|
names.add("Bella");
|
||||||
|
names.add("Max");
|
||||||
|
names.add("Lucy");
|
||||||
|
names.add("Charlie");
|
||||||
|
names.add("Molly");
|
||||||
|
names.add("Buddy");
|
||||||
|
names.add("Daisy");
|
||||||
|
names.add("Rocky");
|
||||||
|
names.add("Maggie");
|
||||||
|
names.add("Jake");
|
||||||
|
names.add("Sophie");
|
||||||
|
names.add("Jack");
|
||||||
|
names.add("Sadie");
|
||||||
|
names.add("Toby");
|
||||||
|
names.add("Chloe");
|
||||||
|
names.add("Cody");
|
||||||
|
names.add("Bailey");
|
||||||
|
names.add("Buster");
|
||||||
|
names.add("Lola");
|
||||||
|
names.add("Duke");
|
||||||
|
names.add("Zoe");
|
||||||
|
names.add("Cooper");
|
||||||
|
names.add("Abby");
|
||||||
|
names.add("Riley");
|
||||||
|
names.add("Ginger");
|
||||||
|
names.add("Harley");
|
||||||
|
names.add("Roxy");
|
||||||
|
names.add("Bear");
|
||||||
|
names.add("Gracie");
|
||||||
|
names.add("Tucker");
|
||||||
|
names.add("Coco");
|
||||||
|
names.add("Murphy");
|
||||||
|
names.add("Sasha");
|
||||||
|
names.add("Lucky");
|
||||||
|
names.add("Lily");
|
||||||
|
names.add("Oliver");
|
||||||
|
names.add("Angel");
|
||||||
|
names.add("Sam");
|
||||||
|
names.add("Princess");
|
||||||
|
names.add("Oscar");
|
||||||
|
names.add("Emma");
|
||||||
|
names.add("Teddy");
|
||||||
|
names.add("Annie");
|
||||||
|
names.add("Winston");
|
||||||
|
names.add("Rosie");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getBreeds() {
|
||||||
|
return breeds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getNames() {
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRandomBreed() {
|
||||||
|
return breeds.get(ThreadLocalRandom.current().nextInt(0, breeds.size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRandomName() {
|
||||||
|
return names.get(ThreadLocalRandom.current().nextInt(0, names.size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date getRandomDoB() {
|
||||||
|
GregorianCalendar gc = new GregorianCalendar();
|
||||||
|
|
||||||
|
int year = ThreadLocalRandom.current().nextInt(
|
||||||
|
Calendar.getInstance().get(Calendar.YEAR) - 15,
|
||||||
|
Calendar.getInstance().get(Calendar.YEAR)
|
||||||
|
);
|
||||||
|
|
||||||
|
gc.set(Calendar.YEAR, year);
|
||||||
|
|
||||||
|
int dayOfYear = ThreadLocalRandom.current().nextInt(1, gc.getActualMaximum(Calendar.DAY_OF_YEAR));
|
||||||
|
|
||||||
|
gc.set(Calendar.DAY_OF_YEAR, dayOfYear);
|
||||||
|
return gc.getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,37 @@
|
|||||||
|
AWSTemplateFormatVersion: '2010-09-09'
|
||||||
|
Transform: AWS::Serverless-2016-10-31
|
||||||
|
Description: Example Pet Store API written with spring-cloud-function web-proxy support
|
||||||
|
|
||||||
|
Globals:
|
||||||
|
Api:
|
||||||
|
# API Gateway regional endpoints
|
||||||
|
EndpointConfiguration: REGIONAL
|
||||||
|
|
||||||
|
Resources:
|
||||||
|
PetStoreFunction:
|
||||||
|
Type: AWS::Serverless::Function
|
||||||
|
Properties:
|
||||||
|
Handler: org.springframework.cloud.function.adapter.aws.web.WebProxyInvoker::handleRequest
|
||||||
|
Runtime: java11
|
||||||
|
CodeUri: .
|
||||||
|
MemorySize: 512
|
||||||
|
Policies: AWSLambdaBasicExecutionRole
|
||||||
|
Timeout: 30
|
||||||
|
Environment:
|
||||||
|
Variables:
|
||||||
|
MAIN_CLASS: oz.spring.petstore.PetStoreSpringAppConfig
|
||||||
|
Events:
|
||||||
|
HttpApiEvent:
|
||||||
|
Type: HttpApi
|
||||||
|
Properties:
|
||||||
|
TimeoutInMillis: 20000
|
||||||
|
PayloadFormatVersion: '1.0'
|
||||||
|
|
||||||
|
Outputs:
|
||||||
|
SpringPetStoreApi:
|
||||||
|
Description: URL for application
|
||||||
|
Value: !Sub 'https://${ServerlessHttpApi}.execute-api.${AWS::Region}.amazonaws.com/pets'
|
||||||
|
Export:
|
||||||
|
Name: PetStoreLambda
|
||||||
|
|
||||||
|
|
||||||
@@ -0,0 +1,78 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.serverless.web;
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.LinkedList;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.CollectionUtils;
|
||||||
|
|
||||||
|
class HeaderValueHolder {
|
||||||
|
|
||||||
|
private final List<Object> values = new LinkedList<>();
|
||||||
|
|
||||||
|
void setValue(@Nullable Object value) {
|
||||||
|
this.values.clear();
|
||||||
|
if (value != null) {
|
||||||
|
this.values.add(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void addValue(Object value) {
|
||||||
|
this.values.add(value);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addValues(Collection<?> values) {
|
||||||
|
this.values.addAll(values);
|
||||||
|
}
|
||||||
|
|
||||||
|
void addValueArray(Object values) {
|
||||||
|
CollectionUtils.mergeArrayIntoCollection(values, this.values);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<Object> getValues() {
|
||||||
|
return Collections.unmodifiableList(this.values);
|
||||||
|
}
|
||||||
|
|
||||||
|
List<String> getStringValues() {
|
||||||
|
List<String> stringList = new ArrayList<>(this.values.size());
|
||||||
|
for (Object value : this.values) {
|
||||||
|
stringList.add(value.toString());
|
||||||
|
}
|
||||||
|
return Collections.unmodifiableList(stringList);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
Object getValue() {
|
||||||
|
return (!this.values.isEmpty() ? this.values.get(0) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
String getStringValue() {
|
||||||
|
return (!this.values.isEmpty() ? String.valueOf(this.values.get(0)) : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.values.toString();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,601 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.serverless.web;
|
||||||
|
|
||||||
|
import java.io.ByteArrayOutputStream;
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.PrintWriter;
|
||||||
|
import java.io.UnsupportedEncodingException;
|
||||||
|
import java.nio.charset.Charset;
|
||||||
|
import java.text.DateFormat;
|
||||||
|
import java.text.ParseException;
|
||||||
|
import java.text.SimpleDateFormat;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collection;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.Locale;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.TimeZone;
|
||||||
|
|
||||||
|
import javax.servlet.ServletOutputStream;
|
||||||
|
import javax.servlet.WriteListener;
|
||||||
|
import javax.servlet.http.Cookie;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.http.HttpHeaders;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.LinkedCaseInsensitiveMap;
|
||||||
|
import org.springframework.web.util.WebUtils;
|
||||||
|
|
||||||
|
public class ProxyHttpServletResponse implements HttpServletResponse {
|
||||||
|
|
||||||
|
private static final String CHARSET_PREFIX = "charset=";
|
||||||
|
|
||||||
|
private static final String DATE_FORMAT = "EEE, dd MMM yyyy HH:mm:ss zzz";
|
||||||
|
|
||||||
|
private static final TimeZone GMT = TimeZone.getTimeZone("GMT");
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// ServletResponse properties
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
private boolean outputStreamAccessAllowed = true;
|
||||||
|
|
||||||
|
private String defaultCharacterEncoding = WebUtils.DEFAULT_CHARACTER_ENCODING;
|
||||||
|
|
||||||
|
private String characterEncoding = this.defaultCharacterEncoding;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* {@code true} if the character encoding has been explicitly set through
|
||||||
|
* {@link HttpServletResponse} methods or through a {@code charset} parameter on
|
||||||
|
* the {@code Content-Type}.
|
||||||
|
*/
|
||||||
|
private boolean characterEncodingSet = false;
|
||||||
|
|
||||||
|
private final ByteArrayOutputStream content = new ByteArrayOutputStream(1024);
|
||||||
|
|
||||||
|
private final ServletOutputStream outputStream = new ResponseServletOutputStream();
|
||||||
|
|
||||||
|
private long contentLength = 0;
|
||||||
|
|
||||||
|
private String contentType;
|
||||||
|
|
||||||
|
private int bufferSize = 4096;
|
||||||
|
|
||||||
|
private boolean committed;
|
||||||
|
|
||||||
|
private Locale locale = Locale.getDefault();
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// HttpServletResponse properties
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
private final List<Cookie> cookies = new ArrayList<>();
|
||||||
|
|
||||||
|
private final Map<String, HeaderValueHolder> headers = new LinkedCaseInsensitiveMap<>();
|
||||||
|
|
||||||
|
private int status = HttpServletResponse.SC_OK;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private String errorMessage;
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// ServletResponse interface
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setCharacterEncoding(String characterEncoding) {
|
||||||
|
setExplicitCharacterEncoding(characterEncoding);
|
||||||
|
updateContentTypePropertyAndHeader();
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setExplicitCharacterEncoding(String characterEncoding) {
|
||||||
|
Assert.notNull(characterEncoding, "'characterEncoding' must not be null");
|
||||||
|
this.characterEncoding = characterEncoding;
|
||||||
|
this.characterEncodingSet = true;
|
||||||
|
}
|
||||||
|
|
||||||
|
private void updateContentTypePropertyAndHeader() {
|
||||||
|
if (this.contentType != null) {
|
||||||
|
String value = this.contentType;
|
||||||
|
if (this.characterEncodingSet && !value.toLowerCase().contains(CHARSET_PREFIX)) {
|
||||||
|
value += ';' + CHARSET_PREFIX + getCharacterEncoding();
|
||||||
|
this.contentType = value;
|
||||||
|
}
|
||||||
|
doAddHeaderValue(HttpHeaders.CONTENT_TYPE, value, true);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getCharacterEncoding() {
|
||||||
|
return this.characterEncoding;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletOutputStream getOutputStream() {
|
||||||
|
Assert.state(this.outputStreamAccessAllowed, "OutputStream access not allowed");
|
||||||
|
return this.outputStream;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public PrintWriter getWriter() throws UnsupportedEncodingException {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
public byte[] getContentAsByteArray() {
|
||||||
|
return this.content.toByteArray();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the content of the response body as a {@code String}, using the charset
|
||||||
|
* specified for the response by the application, either through
|
||||||
|
* {@link HttpServletResponse} methods or through a charset parameter on the
|
||||||
|
* {@code Content-Type}. If no charset has been explicitly defined, the
|
||||||
|
* {@linkplain #setDefaultCharacterEncoding(String) default character encoding}
|
||||||
|
* will be used.
|
||||||
|
*
|
||||||
|
* @return the content as a {@code String}
|
||||||
|
* @throws UnsupportedEncodingException if the character encoding is not
|
||||||
|
* supported
|
||||||
|
* @see #getContentAsString(Charset)
|
||||||
|
* @see #setCharacterEncoding(String)
|
||||||
|
* @see #setContentType(String)
|
||||||
|
*/
|
||||||
|
public String getContentAsString() throws UnsupportedEncodingException {
|
||||||
|
return this.content.toString(getCharacterEncoding());
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Get the content of the response body as a {@code String}, using the provided
|
||||||
|
* {@code fallbackCharset} if no charset has been explicitly defined and
|
||||||
|
* otherwise using the charset specified for the response by the application,
|
||||||
|
* either through {@link HttpServletResponse} methods or through a charset
|
||||||
|
* parameter on the {@code Content-Type}.
|
||||||
|
*
|
||||||
|
* @return the content as a {@code String}
|
||||||
|
* @throws UnsupportedEncodingException if the character encoding is not
|
||||||
|
* supported
|
||||||
|
* @since 5.2
|
||||||
|
* @see #getContentAsString()
|
||||||
|
* @see #setCharacterEncoding(String)
|
||||||
|
* @see #setContentType(String)
|
||||||
|
*/
|
||||||
|
public String getContentAsString(Charset fallbackCharset) throws UnsupportedEncodingException {
|
||||||
|
String charsetName = (this.characterEncodingSet ? getCharacterEncoding() : fallbackCharset.name());
|
||||||
|
return this.content.toString(charsetName);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setContentLength(int contentLength) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setContentLengthLong(long len) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setContentType(@Nullable String contentType) {
|
||||||
|
this.contentType = contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public String getContentType() {
|
||||||
|
return this.contentType;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setBufferSize(int bufferSize) {
|
||||||
|
this.bufferSize = bufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getBufferSize() {
|
||||||
|
return this.bufferSize;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void flushBuffer() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void resetBuffer() {
|
||||||
|
Assert.state(!isCommitted(), "Cannot reset buffer - response is already committed");
|
||||||
|
this.content.reset();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setCommitted(boolean committed) {
|
||||||
|
this.committed = committed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isCommitted() {
|
||||||
|
return this.committed;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void reset() {
|
||||||
|
resetBuffer();
|
||||||
|
this.characterEncoding = this.defaultCharacterEncoding;
|
||||||
|
this.characterEncodingSet = false;
|
||||||
|
this.contentLength = 0;
|
||||||
|
this.contentType = null;
|
||||||
|
this.locale = Locale.getDefault();
|
||||||
|
this.cookies.clear();
|
||||||
|
this.headers.clear();
|
||||||
|
this.status = HttpServletResponse.SC_OK;
|
||||||
|
this.errorMessage = null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setLocale(@Nullable Locale locale) {
|
||||||
|
// Although the Javadoc for javax.servlet.ServletResponse.setLocale(Locale) does
|
||||||
|
// not
|
||||||
|
// state how a null value for the supplied Locale should be handled, both Tomcat
|
||||||
|
// and
|
||||||
|
// Jetty simply ignore a null value. So we do the same here.
|
||||||
|
if (locale == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
this.locale = locale;
|
||||||
|
doAddHeaderValue(HttpHeaders.CONTENT_LANGUAGE, locale.toLanguageTag(), true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Locale getLocale() {
|
||||||
|
return this.locale;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// HttpServletResponse interface
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addCookie(Cookie cookie) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public Cookie getCookie(String name) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean containsHeader(String name) {
|
||||||
|
return this.headers.containsKey(name);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the names of all specified headers as a Set of Strings.
|
||||||
|
* <p>
|
||||||
|
* As of Servlet 3.0, this method is also defined in
|
||||||
|
* {@link HttpServletResponse}.
|
||||||
|
*
|
||||||
|
* @return the {@code Set} of header name {@code Strings}, or an empty
|
||||||
|
* {@code Set} if none
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public Collection<String> getHeaderNames() {
|
||||||
|
return this.headers.keySet();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the primary value for the given header as a String, if any. Will
|
||||||
|
* return the first value in case of multiple values.
|
||||||
|
* <p>
|
||||||
|
* As of Servlet 3.0, this method is also defined in
|
||||||
|
* {@link HttpServletResponse}. As of Spring 3.1, it returns a stringified value
|
||||||
|
* for Servlet 3.0 compatibility. Consider using {@link #getHeaderValue(String)}
|
||||||
|
* for raw Object access.
|
||||||
|
*
|
||||||
|
* @param name the name of the header
|
||||||
|
* @return the associated header value, or {@code null} if none
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
@Nullable
|
||||||
|
public String getHeader(String name) {
|
||||||
|
HeaderValueHolder header = this.headers.get(name);
|
||||||
|
return (header != null ? header.getStringValue() : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all values for the given header as a List of Strings.
|
||||||
|
* <p>
|
||||||
|
* As of Servlet 3.0, this method is also defined in
|
||||||
|
* {@link HttpServletResponse}. As of Spring 3.1, it returns a List of
|
||||||
|
* stringified values for Servlet 3.0 compatibility. Consider using
|
||||||
|
* {@link #getHeaderValues(String)} for raw Object access.
|
||||||
|
*
|
||||||
|
* @param name the name of the header
|
||||||
|
* @return the associated header values, or an empty List if none
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public List<String> getHeaders(String name) {
|
||||||
|
HeaderValueHolder header = this.headers.get(name);
|
||||||
|
if (header != null) {
|
||||||
|
return header.getStringValues();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the primary value for the given header, if any.
|
||||||
|
* <p>
|
||||||
|
* Will return the first value in case of multiple values.
|
||||||
|
*
|
||||||
|
* @param name the name of the header
|
||||||
|
* @return the associated header value, or {@code null} if none
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public Object getHeaderValue(String name) {
|
||||||
|
HeaderValueHolder header = this.headers.get(name);
|
||||||
|
return (header != null ? header.getValue() : null);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return all values for the given header as a List of value objects.
|
||||||
|
*
|
||||||
|
* @param name the name of the header
|
||||||
|
* @return the associated header values, or an empty List if none
|
||||||
|
*/
|
||||||
|
public List<Object> getHeaderValues(String name) {
|
||||||
|
HeaderValueHolder header = this.headers.get(name);
|
||||||
|
if (header != null) {
|
||||||
|
return header.getValues();
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
return Collections.emptyList();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default implementation returns the given URL String as-is.
|
||||||
|
* <p>
|
||||||
|
* Can be overridden in subclasses, appending a session id or the like.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encodeURL(String url) {
|
||||||
|
return url;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The default implementation delegates to {@link #encodeURL}, returning the
|
||||||
|
* given URL String as-is.
|
||||||
|
* <p>
|
||||||
|
* Can be overridden in subclasses, appending a session id or the like in a
|
||||||
|
* redirect-specific fashion. For general URL encoding rules, override the
|
||||||
|
* common {@link #encodeURL} method instead, applying to redirect URLs as well
|
||||||
|
* as to general URLs.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public String encodeRedirectURL(String url) {
|
||||||
|
return encodeURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public String encodeUrl(String url) {
|
||||||
|
return encodeURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public String encodeRedirectUrl(String url) {
|
||||||
|
return encodeRedirectURL(url);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendError(int status, String errorMessage) throws IOException {
|
||||||
|
Assert.state(!isCommitted(), "Cannot set error status - response is already committed");
|
||||||
|
this.status = status;
|
||||||
|
this.errorMessage = errorMessage;
|
||||||
|
setCommitted(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendError(int status) throws IOException {
|
||||||
|
Assert.state(!isCommitted(), "Cannot set error status - response is already committed");
|
||||||
|
this.status = status;
|
||||||
|
setCommitted(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void sendRedirect(String url) throws IOException {
|
||||||
|
Assert.state(!isCommitted(), "Cannot send redirect - response is already committed");
|
||||||
|
Assert.notNull(url, "Redirect URL must not be null");
|
||||||
|
setHeader(HttpHeaders.LOCATION, url);
|
||||||
|
setStatus(HttpServletResponse.SC_MOVED_TEMPORARILY);
|
||||||
|
setCommitted(true);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getRedirectedUrl() {
|
||||||
|
return getHeader(HttpHeaders.LOCATION);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setDateHeader(String name, long value) {
|
||||||
|
setHeaderValue(name, formatDate(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addDateHeader(String name, long value) {
|
||||||
|
addHeaderValue(name, formatDate(value));
|
||||||
|
}
|
||||||
|
|
||||||
|
public long getDateHeader(String name) {
|
||||||
|
String headerValue = getHeader(name);
|
||||||
|
if (headerValue == null) {
|
||||||
|
return -1;
|
||||||
|
}
|
||||||
|
try {
|
||||||
|
return newDateFormat().parse(getHeader(name)).getTime();
|
||||||
|
}
|
||||||
|
catch (ParseException ex) {
|
||||||
|
throw new IllegalArgumentException("Value for header '" + name + "' is not a valid Date: " + headerValue);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private String formatDate(long date) {
|
||||||
|
return newDateFormat().format(new Date(date));
|
||||||
|
}
|
||||||
|
|
||||||
|
private DateFormat newDateFormat() {
|
||||||
|
SimpleDateFormat dateFormat = new SimpleDateFormat(DATE_FORMAT, Locale.US);
|
||||||
|
dateFormat.setTimeZone(GMT);
|
||||||
|
return dateFormat;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setHeader(String name, @Nullable String value) {
|
||||||
|
setHeaderValue(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addHeader(String name, @Nullable String value) {
|
||||||
|
addHeaderValue(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setIntHeader(String name, int value) {
|
||||||
|
setHeaderValue(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addIntHeader(String name, int value) {
|
||||||
|
addHeaderValue(name, value);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void setHeaderValue(String name, @Nullable Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean replaceHeader = true;
|
||||||
|
doAddHeaderValue(name, value, replaceHeader);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void addHeaderValue(String name, @Nullable Object value) {
|
||||||
|
if (value == null) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
boolean replaceHeader = false;
|
||||||
|
doAddHeaderValue(name, value, replaceHeader);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void doAddHeaderValue(String name, Object value, boolean replace) {
|
||||||
|
Assert.notNull(value, "Header value must not be null");
|
||||||
|
HeaderValueHolder header = this.headers.computeIfAbsent(name, key -> new HeaderValueHolder());
|
||||||
|
if (replace) {
|
||||||
|
header.setValue(value);
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
header.addValue(value);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setStatus(int status) {
|
||||||
|
if (!this.isCommitted()) {
|
||||||
|
this.status = status;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
@Deprecated
|
||||||
|
public void setStatus(int status, String errorMessage) {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getStatus() {
|
||||||
|
return this.status;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getErrorMessage() {
|
||||||
|
return this.errorMessage;
|
||||||
|
}
|
||||||
|
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
// Methods for MockRequestDispatcher
|
||||||
|
// ---------------------------------------------------------------------
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getForwardedUrl() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
public String getIncludedUrl() {
|
||||||
|
throw new UnsupportedOperationException();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Inner class that adapts the ServletOutputStream to mark the response as
|
||||||
|
* committed once the buffer size is exceeded.
|
||||||
|
*/
|
||||||
|
private class ResponseServletOutputStream extends ServletOutputStream {
|
||||||
|
|
||||||
|
private WriteListener listener;
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean isReady() {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setWriteListener(WriteListener writeListener) {
|
||||||
|
if (writeListener != null) {
|
||||||
|
try {
|
||||||
|
writeListener.onWritePossible();
|
||||||
|
}
|
||||||
|
catch (IOException e) {
|
||||||
|
// log.error("Output stream is not writable", e);
|
||||||
|
}
|
||||||
|
|
||||||
|
listener = writeListener;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void write(int b) throws IOException {
|
||||||
|
try {
|
||||||
|
content.write(b);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
if (listener != null) {
|
||||||
|
listener.onError(e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void close() throws IOException {
|
||||||
|
super.close();
|
||||||
|
flushBuffer();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,241 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.serverless.web;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Arrays;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.Iterator;
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.FilterConfig;
|
||||||
|
import javax.servlet.Servlet;
|
||||||
|
import javax.servlet.ServletConfig;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
import javax.servlet.http.HttpServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.AnnotatedBeanDefinitionReader;
|
||||||
|
import org.springframework.lang.Nullable;
|
||||||
|
import org.springframework.util.Assert;
|
||||||
|
import org.springframework.util.ObjectUtils;
|
||||||
|
import org.springframework.web.context.ConfigurableWebApplicationContext;
|
||||||
|
import org.springframework.web.context.WebApplicationContext;
|
||||||
|
import org.springframework.web.context.support.AnnotationConfigWebApplicationContext;
|
||||||
|
import org.springframework.web.context.support.GenericWebApplicationContext;
|
||||||
|
import org.springframework.web.servlet.DispatcherServlet;
|
||||||
|
|
||||||
|
public class ProxyMvc {
|
||||||
|
|
||||||
|
static final String MVC_RESULT_ATTRIBUTE = ProxyMvc.class.getName().concat(".MVC_RESULT_ATTRIBUTE");
|
||||||
|
|
||||||
|
private final DispatcherServlet servlet;
|
||||||
|
|
||||||
|
private final Filter[] filters;
|
||||||
|
|
||||||
|
private final ConfigurableWebApplicationContext applicationContext;
|
||||||
|
|
||||||
|
public static ProxyMvc INSTANCE(Class<?>... componentClasses) {
|
||||||
|
Assert.notEmpty(componentClasses, "'componentClasses' must not be null or empty");
|
||||||
|
|
||||||
|
ProxyServletContext servletContext = new ProxyServletContext();
|
||||||
|
GenericWebApplicationContext applpicationContext = new GenericWebApplicationContext(servletContext);
|
||||||
|
AnnotatedBeanDefinitionReader reader = new AnnotatedBeanDefinitionReader(applpicationContext);
|
||||||
|
reader.register(componentClasses);
|
||||||
|
|
||||||
|
try {
|
||||||
|
DispatcherServlet servlet = new DispatcherServlet(applpicationContext);
|
||||||
|
servlet.init(new ProxyServletConfig(servletContext));
|
||||||
|
applpicationContext.registerBean(DispatcherServlet.class, servlet);
|
||||||
|
|
||||||
|
return new ProxyMvc(servlet, applpicationContext);
|
||||||
|
}
|
||||||
|
catch (Exception e) {
|
||||||
|
throw new IllegalStateException("Failed to create MVC Proxy", e);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Private constructor, not for direct instantiation.
|
||||||
|
*/
|
||||||
|
ProxyMvc(DispatcherServlet servlet, ConfigurableWebApplicationContext applicationContext) {
|
||||||
|
this.applicationContext = applicationContext;
|
||||||
|
this.servlet = servlet;
|
||||||
|
this.filters = applicationContext.getBeansOfType(Filter.class).values().toArray(new Filter[0]);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void stop() {
|
||||||
|
this.applicationContext.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Perform a request and return a type that allows chaining further actions,
|
||||||
|
* such as asserting expectations, on the result.
|
||||||
|
*
|
||||||
|
* @param requestBuilder used to prepare the request to execute; see static
|
||||||
|
* factory methods in
|
||||||
|
* {@link org.springframework.test.web.servlet.request.MockMvcRequestBuilders}
|
||||||
|
* @return an instance of {@link ResultActions} (never {@code null})
|
||||||
|
* @see org.springframework.test.web.servlet.request.MockMvcRequestBuilders
|
||||||
|
* @see org.springframework.test.web.servlet.result.MockMvcResultMatchers
|
||||||
|
*/
|
||||||
|
public void service(HttpServletRequest request, HttpServletResponse response) throws Exception {
|
||||||
|
ProxyFilterChain filterChain = new ProxyFilterChain(this.servlet, this.filters);
|
||||||
|
filterChain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ProxyFilterChain implements FilterChain {
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private ServletRequest request;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private ServletResponse response;
|
||||||
|
|
||||||
|
private final List<Filter> filters;
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
private Iterator<Filter> iterator;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a {@code FilterChain} with Filter's and a Servlet.
|
||||||
|
*
|
||||||
|
* @param servlet the {@link Servlet} to invoke in this {@link FilterChain}
|
||||||
|
* @param filters the {@link Filter}'s to invoke in this {@link FilterChain}
|
||||||
|
* @since 3.2
|
||||||
|
*/
|
||||||
|
ProxyFilterChain(Servlet servlet, Filter... filters) {
|
||||||
|
Assert.notNull(filters, "filters cannot be null");
|
||||||
|
Assert.noNullElements(filters, "filters cannot contain null values");
|
||||||
|
this.filters = initFilterList(servlet, filters);
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<Filter> initFilterList(Servlet servlet, Filter... filters) {
|
||||||
|
Filter[] allFilters = ObjectUtils.addObjectToArray(filters, new ServletFilterProxy(servlet));
|
||||||
|
return Arrays.asList(allFilters);
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the request that {@link #doFilter} has been called with.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public ServletRequest getRequest() {
|
||||||
|
return this.request;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Return the response that {@link #doFilter} has been called with.
|
||||||
|
*/
|
||||||
|
@Nullable
|
||||||
|
public ServletResponse getResponse() {
|
||||||
|
return this.response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Invoke registered {@link Filter Filters} and/or {@link Servlet} also saving
|
||||||
|
* the request and response.
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response) throws IOException, ServletException {
|
||||||
|
Assert.notNull(request, "Request must not be null");
|
||||||
|
Assert.notNull(response, "Response must not be null");
|
||||||
|
Assert.state(this.request == null, "This FilterChain has already been called!");
|
||||||
|
|
||||||
|
if (this.iterator == null) {
|
||||||
|
this.iterator = this.filters.iterator();
|
||||||
|
}
|
||||||
|
|
||||||
|
if (this.iterator.hasNext()) {
|
||||||
|
Filter nextFilter = this.iterator.next();
|
||||||
|
nextFilter.doFilter(request, response, this);
|
||||||
|
}
|
||||||
|
|
||||||
|
this.request = request;
|
||||||
|
this.response = response;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* A filter that simply delegates to a Servlet.
|
||||||
|
*/
|
||||||
|
private static final class ServletFilterProxy implements Filter {
|
||||||
|
|
||||||
|
private final Servlet delegateServlet;
|
||||||
|
|
||||||
|
private ServletFilterProxy(Servlet servlet) {
|
||||||
|
Assert.notNull(servlet, "servlet cannot be null");
|
||||||
|
this.delegateServlet = servlet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
|
||||||
|
this.delegateServlet.service(request, response);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void init(FilterConfig filterConfig) throws ServletException {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void destroy() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String toString() {
|
||||||
|
return this.delegateServlet.toString();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private static class ProxyServletConfig implements ServletConfig {
|
||||||
|
|
||||||
|
private final ServletContext servletContext;
|
||||||
|
|
||||||
|
ProxyServletConfig(ServletContext servletContext) {
|
||||||
|
this.servletContext = servletContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getServletName() {
|
||||||
|
return "serverless-proxy";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletContext getServletContext() {
|
||||||
|
return this.servletContext;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enumeration<String> getInitParameterNames() {
|
||||||
|
return Collections.enumeration(new ArrayList<String>());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInitParameter(String name) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,353 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.serverless.web;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.net.MalformedURLException;
|
||||||
|
import java.net.URL;
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.Enumeration;
|
||||||
|
import java.util.EventListener;
|
||||||
|
import java.util.Map;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterRegistration;
|
||||||
|
import javax.servlet.RequestDispatcher;
|
||||||
|
import javax.servlet.Servlet;
|
||||||
|
import javax.servlet.ServletContext;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRegistration;
|
||||||
|
import javax.servlet.ServletRegistration.Dynamic;
|
||||||
|
import javax.servlet.SessionCookieConfig;
|
||||||
|
import javax.servlet.SessionTrackingMode;
|
||||||
|
import javax.servlet.descriptor.JspConfigDescriptor;
|
||||||
|
|
||||||
|
import org.apache.commons.logging.Log;
|
||||||
|
import org.apache.commons.logging.LogFactory;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Empty no-op representation of {@link ServletContext} to satisfy required dependencies to
|
||||||
|
* successfully proxy incoming web requests to target web application.
|
||||||
|
* Most methods are not implemented.
|
||||||
|
*
|
||||||
|
* @author Oleg Zhurakousky
|
||||||
|
*
|
||||||
|
*/
|
||||||
|
public class ProxyServletContext implements ServletContext {
|
||||||
|
|
||||||
|
private Log logger = LogFactory.getLog(ProxyServletContext.class);
|
||||||
|
|
||||||
|
private static Enumeration<String> EMPTY_ENUM = Collections.enumeration(new ArrayList<String>());
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enumeration<String> getInitParameterNames() {
|
||||||
|
return EMPTY_ENUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enumeration<String> getAttributeNames() {
|
||||||
|
return EMPTY_ENUM;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getContextPath() {
|
||||||
|
return "";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletContext getContext(String uripath) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMajorVersion() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getMinorVersion() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getEffectiveMajorVersion() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getEffectiveMinorVersion() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getMimeType(String file) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<String> getResourcePaths(String path) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public URL getResource(String path) throws MalformedURLException {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public InputStream getResourceAsStream(String path) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RequestDispatcher getRequestDispatcher(String path) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public RequestDispatcher getNamedDispatcher(String name) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Servlet getServlet(String name) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enumeration<Servlet> getServlets() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Enumeration<String> getServletNames() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void log(String msg) {
|
||||||
|
this.logger.info(msg);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void log(Exception exception, String msg) {
|
||||||
|
this.logger.error(msg, exception);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void log(String message, Throwable throwable) {
|
||||||
|
this.logger.error(message, throwable);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRealPath(String path) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getServerInfo() {
|
||||||
|
return "serverless-web-proxy";
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getInitParameter(String name) {
|
||||||
|
return null;
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean setInitParameter(String name, String value) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Object getAttribute(String name) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setAttribute(String name, Object object) {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void removeAttribute(String name) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getServletContextName() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dynamic addServlet(String servletName, String className) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dynamic addServlet(String servletName, Servlet servlet) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dynamic addServlet(String servletName, Class<? extends Servlet> servletClass) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Dynamic addJspFile(String jspName, String jspFile) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Servlet> T createServlet(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ServletRegistration getServletRegistration(String servletName) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, ? extends ServletRegistration> getServletRegistrations() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, String className) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Filter filter) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public javax.servlet.FilterRegistration.Dynamic addFilter(String filterName, Class<? extends Filter> filterClass) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends Filter> T createFilter(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public FilterRegistration getFilterRegistration(String filterName) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Map<String, ? extends FilterRegistration> getFilterRegistrations() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public SessionCookieConfig getSessionCookieConfig() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSessionTrackingModes(Set<SessionTrackingMode> sessionTrackingModes) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<javax.servlet.SessionTrackingMode> getDefaultSessionTrackingModes() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public Set<javax.servlet.SessionTrackingMode> getEffectiveSessionTrackingModes() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(String className) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends EventListener> void addListener(T t) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void addListener(Class<? extends EventListener> listenerClass) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public <T extends EventListener> T createListener(Class<T> c) throws ServletException {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public JspConfigDescriptor getJspConfigDescriptor() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ClassLoader getClassLoader() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void declareRoles(String... roleNames) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getVirtualServerName() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public int getSessionTimeout() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setSessionTimeout(int sessionTimeout) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getRequestCharacterEncoding() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setRequestCharacterEncoding(String encoding) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public String getResponseCharacterEncoding() {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void setResponseCharacterEncoding(String encoding) {
|
||||||
|
throw new UnsupportedOperationException("This ServletContext does not represent a running web container");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,58 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.adapter.aws.web;
|
||||||
|
|
||||||
|
import java.util.Date;
|
||||||
|
|
||||||
|
public class Pet {
|
||||||
|
private String id;
|
||||||
|
private String breed;
|
||||||
|
private String name;
|
||||||
|
private Date dateOfBirth;
|
||||||
|
|
||||||
|
public String getId() {
|
||||||
|
return id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setId(String id) {
|
||||||
|
this.id = id;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getBreed() {
|
||||||
|
return breed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setBreed(String breed) {
|
||||||
|
this.breed = breed;
|
||||||
|
}
|
||||||
|
|
||||||
|
public String getName() {
|
||||||
|
return name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setName(String name) {
|
||||||
|
this.name = name;
|
||||||
|
}
|
||||||
|
|
||||||
|
public Date getDateOfBirth() {
|
||||||
|
return dateOfBirth;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void setDateOfBirth(Date dateOfBirth) {
|
||||||
|
this.dateOfBirth = dateOfBirth;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,118 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.adapter.aws.web;
|
||||||
|
|
||||||
|
|
||||||
|
import java.util.ArrayList;
|
||||||
|
import java.util.Calendar;
|
||||||
|
import java.util.Date;
|
||||||
|
import java.util.GregorianCalendar;
|
||||||
|
import java.util.List;
|
||||||
|
import java.util.concurrent.ThreadLocalRandom;
|
||||||
|
|
||||||
|
public class PetData {
|
||||||
|
private static List<String> breeds = new ArrayList<>();
|
||||||
|
static {
|
||||||
|
breeds.add("Afghan Hound");
|
||||||
|
breeds.add("Beagle");
|
||||||
|
breeds.add("Bernese Mountain Dog");
|
||||||
|
breeds.add("Bloodhound");
|
||||||
|
breeds.add("Dalmatian");
|
||||||
|
breeds.add("Jack Russell Terrier");
|
||||||
|
breeds.add("Norwegian Elkhound");
|
||||||
|
}
|
||||||
|
|
||||||
|
private static List<String> names = new ArrayList<>();
|
||||||
|
static {
|
||||||
|
names.add("Bailey");
|
||||||
|
names.add("Bella");
|
||||||
|
names.add("Max");
|
||||||
|
names.add("Lucy");
|
||||||
|
names.add("Charlie");
|
||||||
|
names.add("Molly");
|
||||||
|
names.add("Buddy");
|
||||||
|
names.add("Daisy");
|
||||||
|
names.add("Rocky");
|
||||||
|
names.add("Maggie");
|
||||||
|
names.add("Jake");
|
||||||
|
names.add("Sophie");
|
||||||
|
names.add("Jack");
|
||||||
|
names.add("Sadie");
|
||||||
|
names.add("Toby");
|
||||||
|
names.add("Chloe");
|
||||||
|
names.add("Cody");
|
||||||
|
names.add("Bailey");
|
||||||
|
names.add("Buster");
|
||||||
|
names.add("Lola");
|
||||||
|
names.add("Duke");
|
||||||
|
names.add("Zoe");
|
||||||
|
names.add("Cooper");
|
||||||
|
names.add("Abby");
|
||||||
|
names.add("Riley");
|
||||||
|
names.add("Ginger");
|
||||||
|
names.add("Harley");
|
||||||
|
names.add("Roxy");
|
||||||
|
names.add("Bear");
|
||||||
|
names.add("Gracie");
|
||||||
|
names.add("Tucker");
|
||||||
|
names.add("Coco");
|
||||||
|
names.add("Murphy");
|
||||||
|
names.add("Sasha");
|
||||||
|
names.add("Lucky");
|
||||||
|
names.add("Lily");
|
||||||
|
names.add("Oliver");
|
||||||
|
names.add("Angel");
|
||||||
|
names.add("Sam");
|
||||||
|
names.add("Princess");
|
||||||
|
names.add("Oscar");
|
||||||
|
names.add("Emma");
|
||||||
|
names.add("Teddy");
|
||||||
|
names.add("Annie");
|
||||||
|
names.add("Winston");
|
||||||
|
names.add("Rosie");
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getBreeds() {
|
||||||
|
return breeds;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static List<String> getNames() {
|
||||||
|
return names;
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRandomBreed() {
|
||||||
|
return breeds.get(ThreadLocalRandom.current().nextInt(0, breeds.size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static String getRandomName() {
|
||||||
|
return names.get(ThreadLocalRandom.current().nextInt(0, names.size() - 1));
|
||||||
|
}
|
||||||
|
|
||||||
|
public static Date getRandomDoB() {
|
||||||
|
GregorianCalendar gc = new GregorianCalendar();
|
||||||
|
|
||||||
|
int year = ThreadLocalRandom.current().nextInt(Calendar.getInstance().get(Calendar.YEAR) - 15,
|
||||||
|
Calendar.getInstance().get(Calendar.YEAR));
|
||||||
|
|
||||||
|
gc.set(Calendar.YEAR, year);
|
||||||
|
|
||||||
|
int dayOfYear = ThreadLocalRandom.current().nextInt(1, gc.getActualMaximum(Calendar.DAY_OF_YEAR));
|
||||||
|
|
||||||
|
gc.set(Calendar.DAY_OF_YEAR, dayOfYear);
|
||||||
|
return gc.getTime();
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,69 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.adapter.aws.web;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
|
||||||
|
import javax.servlet.Filter;
|
||||||
|
import javax.servlet.FilterChain;
|
||||||
|
import javax.servlet.ServletException;
|
||||||
|
import javax.servlet.ServletRequest;
|
||||||
|
import javax.servlet.ServletResponse;
|
||||||
|
|
||||||
|
import org.springframework.context.annotation.Bean;
|
||||||
|
import org.springframework.context.annotation.Configuration;
|
||||||
|
import org.springframework.context.annotation.Import;
|
||||||
|
import org.springframework.web.servlet.HandlerAdapter;
|
||||||
|
import org.springframework.web.servlet.HandlerMapping;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter;
|
||||||
|
import org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping;
|
||||||
|
|
||||||
|
//@SpringBootApplication
|
||||||
|
@Configuration
|
||||||
|
@Import({ PetsController.class })
|
||||||
|
public class PetStoreSpringAppConfig {
|
||||||
|
/*
|
||||||
|
* Create required HandlerMapping, to avoid several default HandlerMapping
|
||||||
|
* instances being created
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public HandlerMapping handlerMapping() {
|
||||||
|
return new RequestMappingHandlerMapping();
|
||||||
|
}
|
||||||
|
|
||||||
|
/*
|
||||||
|
* Create required HandlerAdapter, to avoid several default HandlerAdapter
|
||||||
|
* instances being created
|
||||||
|
*/
|
||||||
|
@Bean
|
||||||
|
public HandlerAdapter handlerAdapter() {
|
||||||
|
return new RequestMappingHandlerAdapter();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Bean
|
||||||
|
public Filter filter() {
|
||||||
|
return new Filter() {
|
||||||
|
@Override
|
||||||
|
public void doFilter(ServletRequest request, ServletResponse response, FilterChain chain)
|
||||||
|
throws IOException, ServletException {
|
||||||
|
System.out.println("FILTER ===> Hello from: " + request.getLocalAddr());
|
||||||
|
chain.doFilter(request, response);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -0,0 +1,75 @@
|
|||||||
|
/*
|
||||||
|
* Copyright 2023-2023 the original author or authors.
|
||||||
|
*
|
||||||
|
* Licensed 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
|
||||||
|
*
|
||||||
|
* https://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.springframework.cloud.function.adapter.aws.web;
|
||||||
|
|
||||||
|
import java.security.Principal;
|
||||||
|
import java.util.Optional;
|
||||||
|
import java.util.UUID;
|
||||||
|
|
||||||
|
import org.springframework.web.bind.annotation.RequestBody;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMapping;
|
||||||
|
import org.springframework.web.bind.annotation.RequestMethod;
|
||||||
|
import org.springframework.web.bind.annotation.RequestParam;
|
||||||
|
import org.springframework.web.bind.annotation.RestController;
|
||||||
|
import org.springframework.web.servlet.config.annotation.EnableWebMvc;
|
||||||
|
|
||||||
|
@RestController
|
||||||
|
@EnableWebMvc
|
||||||
|
public class PetsController {
|
||||||
|
@RequestMapping(path = "/pets", method = RequestMethod.POST)
|
||||||
|
public Pet createPet(@RequestBody Pet newPet) {
|
||||||
|
if (newPet.getName() == null || newPet.getBreed() == null) {
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
Pet dbPet = newPet;
|
||||||
|
dbPet.setId(UUID.randomUUID().toString());
|
||||||
|
return dbPet;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = "/pets", method = RequestMethod.GET)
|
||||||
|
public Pet[] listPets(@RequestParam("limit") Optional<Integer> limit, Principal principal) {
|
||||||
|
int queryLimit = 10;
|
||||||
|
if (limit.isPresent()) {
|
||||||
|
queryLimit = limit.get();
|
||||||
|
}
|
||||||
|
|
||||||
|
Pet[] outputPets = new Pet[queryLimit];
|
||||||
|
|
||||||
|
for (int i = 0; i < queryLimit; i++) {
|
||||||
|
Pet newPet = new Pet();
|
||||||
|
newPet.setId(UUID.randomUUID().toString());
|
||||||
|
newPet.setName(PetData.getRandomName());
|
||||||
|
newPet.setBreed(PetData.getRandomBreed());
|
||||||
|
newPet.setDateOfBirth(PetData.getRandomDoB());
|
||||||
|
outputPets[i] = newPet;
|
||||||
|
}
|
||||||
|
|
||||||
|
return outputPets;
|
||||||
|
}
|
||||||
|
|
||||||
|
@RequestMapping(path = "/pets/{petId}", method = RequestMethod.GET)
|
||||||
|
public Pet listPets() {
|
||||||
|
System.out.println("=====> Getting pet by id");
|
||||||
|
Pet newPet = new Pet();
|
||||||
|
newPet.setId(UUID.randomUUID().toString());
|
||||||
|
newPet.setBreed(PetData.getRandomBreed());
|
||||||
|
newPet.setDateOfBirth(PetData.getRandomDoB());
|
||||||
|
newPet.setName(PetData.getRandomName());
|
||||||
|
return newPet;
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,88 @@
|
|||||||
|
package org.springframework.cloud.function.adapter.aws.web;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.Assertions.assertThat;
|
||||||
|
|
||||||
|
import java.util.List;
|
||||||
|
|
||||||
|
import javax.servlet.http.HttpServletRequest;
|
||||||
|
|
||||||
|
import org.junit.jupiter.api.AfterEach;
|
||||||
|
import org.junit.jupiter.api.BeforeEach;
|
||||||
|
import org.junit.jupiter.api.Test;
|
||||||
|
import org.springframework.cloud.function.serverless.web.ProxyHttpServletRequest;
|
||||||
|
import org.springframework.cloud.function.serverless.web.ProxyHttpServletResponse;
|
||||||
|
import org.springframework.cloud.function.serverless.web.ProxyMvc;
|
||||||
|
|
||||||
|
import com.fasterxml.jackson.core.type.TypeReference;
|
||||||
|
import com.fasterxml.jackson.databind.ObjectMapper;
|
||||||
|
|
||||||
|
public class RequestResponseTests {
|
||||||
|
|
||||||
|
private ObjectMapper mapper = new ObjectMapper();
|
||||||
|
|
||||||
|
private ProxyMvc mvc;
|
||||||
|
|
||||||
|
@BeforeEach
|
||||||
|
public void before() {
|
||||||
|
this.mvc = ProxyMvc.INSTANCE(PetStoreSpringAppConfig.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@AfterEach
|
||||||
|
public void after() {
|
||||||
|
this.mvc.stop();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validateGetListOfPojos() throws Exception {
|
||||||
|
HttpServletRequest request = new ProxyHttpServletRequest(null, "GET", "/pets");
|
||||||
|
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
|
||||||
|
mvc.service(request, response);
|
||||||
|
TypeReference<List<Pet>> tr = new TypeReference<List<Pet>>() {
|
||||||
|
};
|
||||||
|
List<Pet> pets = mapper.readValue(response.getContentAsByteArray(), tr);
|
||||||
|
assertThat(pets.size()).isEqualTo(10);
|
||||||
|
assertThat(pets.get(0)).isInstanceOf(Pet.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validateGetListOfPojosWithParam() throws Exception {
|
||||||
|
ProxyHttpServletRequest request = new ProxyHttpServletRequest(null, "GET", "/pets");
|
||||||
|
request.setParameter("limit", "5");
|
||||||
|
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
|
||||||
|
mvc.service(request, response);
|
||||||
|
TypeReference<List<Pet>> tr = new TypeReference<List<Pet>>() {
|
||||||
|
};
|
||||||
|
List<Pet> pets = mapper.readValue(response.getContentAsByteArray(), tr);
|
||||||
|
assertThat(pets.size()).isEqualTo(5);
|
||||||
|
assertThat(pets.get(0)).isInstanceOf(Pet.class);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validateGetPojo() throws Exception {
|
||||||
|
HttpServletRequest request = new ProxyHttpServletRequest(null, "GET", "/pets/6e3cc370-892f-4efe-a9eb-82926ff8cc5b");
|
||||||
|
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
|
||||||
|
mvc.service(request, response);
|
||||||
|
Pet pet = mapper.readValue(response.getContentAsByteArray(), Pet.class);
|
||||||
|
assertThat(pet).isNotNull();
|
||||||
|
assertThat(pet.getName()).isNotEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void validatePostWithBody() throws Exception {
|
||||||
|
ProxyHttpServletRequest request = new ProxyHttpServletRequest(null, "POST", "/pets/");
|
||||||
|
String jsonPet = "{\n"
|
||||||
|
+ " \"id\":\"1234\",\n"
|
||||||
|
+ " \"breed\":\"Canish\",\n"
|
||||||
|
+ " \"name\":\"Foo\",\n"
|
||||||
|
+ " \"date\":\"2012-04-23T18:25:43.511Z\"\n"
|
||||||
|
+ "}";
|
||||||
|
request.setContent(jsonPet.getBytes());
|
||||||
|
request.setContentType("application/json");
|
||||||
|
ProxyHttpServletResponse response = new ProxyHttpServletResponse();
|
||||||
|
mvc.service(request, response);
|
||||||
|
Pet pet = mapper.readValue(response.getContentAsByteArray(), Pet.class);
|
||||||
|
assertThat(pet).isNotNull();
|
||||||
|
assertThat(pet.getName()).isNotEmpty();
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
Reference in New Issue
Block a user