From b36402a0538c17ba1551034b94907688153d8c5b Mon Sep 17 00:00:00 2001 From: Oleg Zhurakousky Date: Thu, 6 Jul 2023 08:18:27 +0200 Subject: [PATCH] Remove spring-cloud-function-adapter-aws-web --- spring-cloud-function-adapters/pom.xml | 1 - .../.jdk8 | 0 .../README.md | 20 -- .../pom.xml | 47 ----- .../sample/pet-store/.aws-sam/build.toml | 12 -- .../sample/pet-store/README.md | 39 ---- .../sample/pet-store/pom.xml | 161 ---------------- .../sample/pet-store/src/assembly/bin.xml | 24 --- .../petstore/PetStoreSpringAppConfig.java | 56 ------ .../oz/spring/petstore/PetsController.java | 80 -------- .../java/oz/spring/petstore/model/Error.java | 33 ---- .../java/oz/spring/petstore/model/Pet.java | 58 ------ .../oz/spring/petstore/model/PetData.java | 115 ------------ .../sample/pet-store/template.yml | 41 ---- .../adapter/aws/web/FunctionClassUtils.java | 140 -------------- .../cloud/function/adapter/aws/web/README.md | 5 - .../adapter/aws/web/WebProxyInvoker.java | 146 --------------- .../cloud/function/adapter/aws/web/Pet.java | 58 ------ .../function/adapter/aws/web/PetData.java | 118 ------------ .../aws/web/PetStoreSpringAppConfig.java | 69 ------- .../adapter/aws/web/PetsController.java | 76 -------- .../adapter/aws/web/WebProxyInvokerTests.java | 175 ------------------ 22 files changed, 1474 deletions(-) delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/.jdk8 delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/README.md delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/pom.xml delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/.aws-sam/build.toml delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/README.md delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/pom.xml delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/assembly/bin.xml delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetStoreSpringAppConfig.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetsController.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Error.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Pet.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/PetData.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/template.yml delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/FunctionClassUtils.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/README.md delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvoker.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/Pet.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetData.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetStoreSpringAppConfig.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetsController.java delete mode 100644 spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvokerTests.java diff --git a/spring-cloud-function-adapters/pom.xml b/spring-cloud-function-adapters/pom.xml index 0491c006d..c4051d06f 100644 --- a/spring-cloud-function-adapters/pom.xml +++ b/spring-cloud-function-adapters/pom.xml @@ -23,7 +23,6 @@ spring-cloud-function-grpc spring-cloud-function-grpc-cloudevent-ext spring-cloud-function-serverless-web - spring-cloud-function-adapter-aws-web spring-cloud-function-adapter-azure-web diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/.jdk8 b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/.jdk8 deleted file mode 100644 index e69de29bb..000000000 diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/README.md b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/README.md deleted file mode 100644 index c2da62ba4..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/README.md +++ /dev/null @@ -1,20 +0,0 @@ -#### 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` directory -======= -Classes in this package should ideally reside in spring-web somewhere as a light weight HTTP proxy, since they are independent of the -context of the execution (i.e., AWS or Azure or whatever). -In fact classes in these package is a slimed-down copy of similar classes in MockMVC. - -A sample is provided in `/sample` directory - -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) diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/pom.xml b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/pom.xml deleted file mode 100644 index 3269738db..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/pom.xml +++ /dev/null @@ -1,47 +0,0 @@ - - - 4.0.0 - spring-cloud-function-adapter-aws-web - jar - spring-cloud-function-adapter-aws-web - AWS Lambda Adapter for Spring Cloud Function - - org.springframework.cloud - spring-cloud-function-adapter-parent - 4.0.4-SNAPSHOT - - - UTF-8 - UTF-8 - - - - com.fasterxml.jackson.core - jackson-databind - - - org.springframework.cloud - spring-cloud-function-serverless-web - - - jakarta.servlet - jakarta.servlet-api - - - org.springframework - spring-webmvc - - - org.springframework.boot - spring-boot-starter-test - test - - - org.springframework.boot - spring-boot-starter-web - test - - - diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/.aws-sam/build.toml b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/.aws-sam/build.toml deleted file mode 100644 index 85a4a82df..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/.aws-sam/build.toml +++ /dev/null @@ -1,12 +0,0 @@ -# This file is auto generated by SAM CLI build command - -[function_build_definitions.4c7d6b2d-fe8c-493a-945e-f0eaf34aaf65] -codeuri = "/Users/ozhurakousky/Documents/dev/repo/spring-cloud-function/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store" -runtime = "java17" -architecture = "x86_64" -handler = "org.springframework.cloud.function.adapter.aws.web.WebProxyInvoker::handleRequest" -manifest_hash = "" -packagetype = "Zip" -functions = ["PetStoreFunction"] - -[layer_build_definitions] diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/README.md b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/README.md deleted file mode 100644 index ed9b3b3d4..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/README.md +++ /dev/null @@ -1,39 +0,0 @@ -Modeled after 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 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 -``` \ No newline at end of file diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/pom.xml b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/pom.xml deleted file mode 100644 index 936815149..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/pom.xml +++ /dev/null @@ -1,161 +0,0 @@ - - - 4.0.0 - - oz.spring.petstore - pet-store - 1.0-SNAPSHOT - pet-store - Simple pet store written with the Spring framework - https://aws.amazon.com/lambda/ - - - https://github.com/awslabs/aws-serverless-java-container.git - - - - - The Apache Software License, Version 2.0 - http://www.apache.org/licenses/LICENSE-2.0.txt - repo - - - - - 17 - 17 - 6.0.8 - 4.13.2 - 2.19.0 - - - - - io.github.crac - org-crac - 0.1.3 - - - org.springframework.cloud - spring-cloud-function-adapter-aws-web - 4.0.3-SNAPSHOT - - - - junit - junit - ${junit.version} - test - - - - - - shaded-jar - - - - org.apache.maven.plugins - maven-shade-plugin - 3.2.4 - - - package - - shade - - - false - - - - - - - - - - com.github.edwgiz - maven-shade-plugin.log4j2-cachefile-transformer - 2.8.1 - - - - - - - - assembly-zip - - true - - - - - - org.apache.maven.plugins - maven-jar-plugin - 3.2.0 - - - default-jar - none - - - - - org.apache.maven.plugins - maven-install-plugin - 3.0.0-M1 - - true - - - - - org.apache.maven.plugins - maven-dependency-plugin - 3.2.0 - - - copy-dependencies - package - - copy-dependencies - - - ${project.build.directory}/lib - runtime - - - - - - org.apache.maven.plugins - maven-assembly-plugin - 3.3.0 - - - zip-assembly - package - - single - - - ${project.artifactId}-${project.version} - - src${file.separator}assembly${file.separator}bin.xml - - false - - - - - - - - - diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/assembly/bin.xml b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/assembly/bin.xml deleted file mode 100644 index 1ffd82d1c..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/assembly/bin.xml +++ /dev/null @@ -1,24 +0,0 @@ - - lambda-package - - zip - - false - - - - ${project.build.directory}${file.separator}lib - lib - - - - ${project.build.directory}${file.separator}classes - - ** - - ${file.separator} - - - \ No newline at end of file diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetStoreSpringAppConfig.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetStoreSpringAppConfig.java deleted file mode 100644 index c3c8dca64..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetStoreSpringAppConfig.java +++ /dev/null @@ -1,56 +0,0 @@ -/* - * 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.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; - -import jakarta.servlet.http.HttpServletRequest; -import jakarta.servlet.http.HttpServletResponse; - - -@Configuration -@Import({ PetsController.class }) -public class PetStoreSpringAppConfig { - - /* - * 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; - } - }; - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetsController.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetsController.java deleted file mode 100644 index b04a26d92..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/PetsController.java +++ /dev/null @@ -1,80 +0,0 @@ -/* - * 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 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; - } - -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Error.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Error.java deleted file mode 100644 index bb19a9027..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Error.java +++ /dev/null @@ -1,33 +0,0 @@ -/* - * 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; - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Pet.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Pet.java deleted file mode 100644 index 20f170a99..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/Pet.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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; - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/PetData.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/PetData.java deleted file mode 100644 index 1df3632cc..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/src/main/java/oz/spring/petstore/model/PetData.java +++ /dev/null @@ -1,115 +0,0 @@ -/* - * 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 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 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 getBreeds() { - return breeds; - } - - public static List 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(); - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/template.yml b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/template.yml deleted file mode 100644 index 75d452776..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/sample/pet-store/template.yml +++ /dev/null @@ -1,41 +0,0 @@ -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: -# AutoPublishAlias: bcn - FunctionName: pet-store-17 - Handler: org.springframework.cloud.function.adapter.aws.web.WebProxyInvoker::handleRequest - Runtime: java17 - SnapStart: - ApplyOn: PublishedVersions - CodeUri: . - MemorySize: 1024 - 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: SpringPetStoreApi - - diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/FunctionClassUtils.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/FunctionClassUtils.java deleted file mode 100644 index 240390bde..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/FunctionClassUtils.java +++ /dev/null @@ -1,140 +0,0 @@ -/* - * Copyright 2019-2021 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.InputStream; -import java.net.URL; -import java.util.Collections; -import java.util.List; -import java.util.jar.JarFile; -import java.util.jar.Manifest; - -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -//import org.springframework.boot.SpringBootConfiguration; -//import org.springframework.boot.autoconfigure.SpringBootApplication; -import org.springframework.core.KotlinDetector; -import org.springframework.core.io.Resource; -import org.springframework.core.io.support.PathMatchingResourcePatternResolver; -import org.springframework.util.Assert; -import org.springframework.util.ClassUtils; -import org.springframework.util.StringUtils; - -/** - * General utility class which aggregates various class-level utility functions - * used by the framework. - * - * @author Oleg Zhurakousky - * @since 3.0.1 - */ -public final class FunctionClassUtils { - - private static Log logger = LogFactory.getLog(FunctionClassUtils.class); - - private FunctionClassUtils() { - - } - - /** - * Discovers the start class in the currently running application. - * The discover search order is 'MAIN_CLASS' environment property, - * 'MAIN_CLASS' system property, META-INF/MANIFEST.MF:'Start-Class' attribute, - * meta-inf/manifest.mf:'Start-Class' attribute. - * - * @return instance of Class which represent the start class of the application. - */ - public static Class getStartClass() { - ClassLoader classLoader = FunctionClassUtils.class.getClassLoader(); - return getStartClass(classLoader); - } - - static Class getStartClass(ClassLoader classLoader) { - Class mainClass = null; - if (System.getenv("MAIN_CLASS") != null) { - mainClass = ClassUtils.resolveClassName(System.getenv("MAIN_CLASS"), classLoader); - } - else if (System.getProperty("MAIN_CLASS") != null) { - mainClass = ClassUtils.resolveClassName(System.getProperty("MAIN_CLASS"), classLoader); - } - else { - try { - Class result = getStartClass( - Collections.list(classLoader.getResources(JarFile.MANIFEST_NAME)), classLoader); - if (result == null) { - result = getStartClass(Collections - .list(classLoader.getResources("meta-inf/manifest.mf")), classLoader); - } - Assert.notNull(result, "Failed to locate main class"); - mainClass = result; - } - catch (Exception ex) { - throw new IllegalStateException("Failed to discover main class. An attempt was made to discover " - + "main class as 'MAIN_CLASS' environment variable, system property as well as " - + "entry in META-INF/MANIFEST.MF (in that order).", ex); - } - } - logger.info("Main class: " + mainClass); - return mainClass; - } - - private static Class getStartClass(List list, ClassLoader classLoader) { - if (logger.isTraceEnabled()) { - logger.trace("Searching manifests: " + list); - } - for (URL url : list) { - try { - InputStream inputStream = null; - Manifest manifest = new Manifest(url.openStream()); - logger.info("Searching for start class in manifest: " + url); - if (logger.isDebugEnabled()) { - manifest.write(System.out); - } - try { - String startClassName = manifest.getMainAttributes().getValue("Start-Class"); - if (!StringUtils.hasText(startClassName)) { - startClassName = manifest.getMainAttributes().getValue("Main-Class"); - } - - if (StringUtils.hasText(startClassName)) { - Class startClass = ClassUtils.forName(startClassName, classLoader); - - if (KotlinDetector.isKotlinType(startClass)) { - PathMatchingResourcePatternResolver r = new PathMatchingResourcePatternResolver(classLoader); - String packageName = startClass.getPackage().getName(); - Resource[] resources = r.getResources("classpath:" + packageName.replace(".", "/") + "/*.class"); - for (int i = 0; i < resources.length; i++) { - Resource resource = resources[i]; - String className = packageName + "." + (resource.getFilename().replace("/", ".")).replace(".class", ""); - startClass = ClassUtils.forName(className, classLoader); - } - } - } - } - finally { - if (inputStream != null) { - inputStream.close(); - } - } - } - catch (Exception ex) { - logger.debug("Failed to determine Start-Class in manifest file of " + url, ex); - } - } - return null; - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/README.md b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/README.md deleted file mode 100644 index de06b2658..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/README.md +++ /dev/null @@ -1,5 +0,0 @@ -Classes in this package would remain specific to AWS (in this case). There would be something similar in Azure and others. - -And these classes would depend on what is currently in `org.springframework.web.client` package of this module. -However, ideally the contents of the `org.springframework.web.client` package should reside in spring-web somewhere as a light weight -HTTP proxy as we technically already have it in a form of MockMVC. \ No newline at end of file diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvoker.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvoker.java deleted file mode 100644 index 5b0a2330a..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/main/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvoker.java +++ /dev/null @@ -1,146 +0,0 @@ -/* - * 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 java.io.InputStream; -import java.io.OutputStream; -import java.util.Collections; -import java.util.HashMap; -import java.util.List; -import java.util.Map; -import java.util.Map.Entry; - -import com.fasterxml.jackson.databind.ObjectMapper; -import jakarta.servlet.ServletException; -import jakarta.servlet.http.HttpServletRequest; -import org.apache.commons.logging.Log; -import org.apache.commons.logging.LogFactory; - -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 org.springframework.http.HttpHeaders; -import org.springframework.http.HttpStatus; -import org.springframework.util.StreamUtils; -import org.springframework.util.StringUtils; - -/** - * - * AWS Lambda specific handler that will proxy API Gateway request to Spring Web-app - * This class represents AWS Lambda fronted by API Gateway and is identified as 'handler' during the deployment. - * - * @author Oleg Zhurakousky - * - */ -public class WebProxyInvoker { - - private static Log logger = LogFactory.getLog(WebProxyInvoker.class); - - private final ProxyMvc mvc; - - - ObjectMapper mapper = new ObjectMapper(); - - public WebProxyInvoker() throws ServletException { - Class startClass = FunctionClassUtils.getStartClass(); - this.mvc = ProxyMvc.INSTANCE(startClass); - } - - /* - * TODO - * - Security context propagation from AWS API Gateway (easy) - * - Error handling - */ - @SuppressWarnings("unchecked") - private HttpServletRequest prepareRequest(InputStream input) throws IOException { - Map request = mapper.readValue(input, Map.class); - if (logger.isInfoEnabled()) { - logger.info("Request: " + request); - } - String httpMethod = (String) request.get("httpMethod"); - String path = (String) request.get("path"); - if (logger.isDebugEnabled()) { - logger.debug("httpMethod: " + httpMethod); - logger.debug("path: " + path); - } - - ProxyHttpServletRequest httpRequest = new ProxyHttpServletRequest(this.mvc.getServletContext(), httpMethod, path); - - // CONTENT - if (StringUtils.hasText((String) request.get("body"))) { - httpRequest.setContent(((String) request.get("body")).getBytes()); - } - - // REQUEST PARAM - if (request.get("multiValueQueryStringParameters") != null) { - Map> parameters = (Map>) request.get("multiValueQueryStringParameters"); - for (Entry> parameter : parameters.entrySet()) { - httpRequest.setParameter(parameter.getKey(), parameter.getValue().toArray(new String[] {})); - } - } - - // HEADERS - Map> headers = (Map>) request.get("multiValueHeaders"); - HttpHeaders httpHeaders = new HttpHeaders(); - for (Entry> entry : headers.entrySet()) { - // TODO may need to do some header formatting - httpHeaders.addAll(entry.getKey(), entry.getValue()); - } - httpRequest.setHeaders(httpHeaders); - - return httpRequest; - } - - - public void handleRequest(InputStream input, OutputStream output) throws IOException { - HttpServletRequest httpRequest = this.prepareRequest(input); - - ProxyHttpServletResponse httpResponse = new ProxyHttpServletResponse(); - try { - this.mvc.service(httpRequest, httpResponse); - } - catch (Exception e) { - e.printStackTrace(); - throw new IllegalStateException(e); - } - - String responseString = httpResponse.getContentAsString(); - if (StringUtils.hasText(responseString)) { - if (logger.isDebugEnabled()) { - logger.debug("Response: " + responseString); - } - Map apiGatewayResponseStructure = new HashMap(); - apiGatewayResponseStructure.put("isBase64Encoded", false); - apiGatewayResponseStructure.put("statusCode", HttpStatus.OK.value()); - apiGatewayResponseStructure.put("body", responseString); - Map> multiValueHeaders = new HashMap<>(); - Map headers = new HashMap<>(); - for (String headerName : httpResponse.getHeaderNames()) { - multiValueHeaders.put(headerName, httpResponse.getHeaders(headerName)); - headers.put(headerName, httpResponse.getHeaders(headerName).toString()); - } - headers.put(HttpHeaders.CONTENT_TYPE, httpResponse.getContentType()); - multiValueHeaders.put(HttpHeaders.CONTENT_TYPE, Collections.singletonList(httpResponse.getContentType())); - apiGatewayResponseStructure.put("multiValueHeaders", multiValueHeaders); - apiGatewayResponseStructure.put("headers", headers); - - byte[] apiGatewayResponseBytes = mapper.writeValueAsBytes(apiGatewayResponseStructure); - StreamUtils.copy(apiGatewayResponseBytes, output); - } - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/Pet.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/Pet.java deleted file mode 100644 index 846faca8e..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/Pet.java +++ /dev/null @@ -1,58 +0,0 @@ -/* - * 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; - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetData.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetData.java deleted file mode 100644 index c0fda4d4a..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetData.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * 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 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 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 getBreeds() { - return breeds; - } - - public static List 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(); - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetStoreSpringAppConfig.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetStoreSpringAppConfig.java deleted file mode 100644 index 11c80130d..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetStoreSpringAppConfig.java +++ /dev/null @@ -1,69 +0,0 @@ -/* - * 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 jakarta.servlet.Filter; -import jakarta.servlet.FilterChain; -import jakarta.servlet.ServletException; -import jakarta.servlet.ServletRequest; -import jakarta.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); - } - }; - } - -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetsController.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetsController.java deleted file mode 100644 index a22e57dc0..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/PetsController.java +++ /dev/null @@ -1,76 +0,0 @@ -/* - * 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 limit, Principal principal) { - System.out.println("=====> EXECUTING"); - 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; - } -} diff --git a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvokerTests.java b/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvokerTests.java deleted file mode 100644 index 764e73b34..000000000 --- a/spring-cloud-function-adapters/spring-cloud-function-adapter-aws-web/src/test/java/org/springframework/cloud/function/adapter/aws/web/WebProxyInvokerTests.java +++ /dev/null @@ -1,175 +0,0 @@ -/* - * 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.ByteArrayInputStream; -import java.io.ByteArrayOutputStream; -import java.io.InputStream; -import java.lang.reflect.Method; -import java.util.List; -import java.util.Map; - -import com.fasterxml.jackson.databind.ObjectMapper; -import org.junit.jupiter.api.Test; - -import org.springframework.cloud.function.serverless.web.ProxyHttpServletRequest; -import org.springframework.http.HttpHeaders; -import org.springframework.util.ReflectionUtils; - -import static org.assertj.core.api.Assertions.assertThat; - -/** - * @author Oleg Zhurakousky - */ -public class WebProxyInvokerTests { - - static String API_GATEWAY_EVENT = "{\n" - + " \"version\": \"1.0\",\n" - + " \"resource\": \"$default\",\n" - + " \"path\": \"/pets\",\n" - + " \"httpMethod\": \"POST\",\n" - + " \"headers\": {\n" - + " \"Content-Length\": \"45\",\n" - + " \"Content-Type\": \"application/json\",\n" - + " \"Host\": \"i76bfhczs0.execute-api.eu-west-3.amazonaws.com\",\n" - + " \"User-Agent\": \"curl/7.79.1\",\n" - + " \"X-Amzn-Trace-Id\": \"Root=1-64087690-2151375b219d3ba3389ea84e\",\n" - + " \"X-Forwarded-For\": \"109.210.252.44\",\n" - + " \"X-Forwarded-Port\": \"443\",\n" - + " \"X-Forwarded-Proto\": \"https\",\n" - + " \"accept\": \"*/*\"\n" - + " },\n" - + " \"multiValueHeaders\": {\n" - + " \"Content-Length\": [\n" - + " \"45\"\n" - + " ],\n" - + " \"Content-Type\": [\n" - + " \"application/json\"\n" - + " ],\n" - + " \"Host\": [\n" - + " \"i76bfhczs0.execute-api.eu-west-3.amazonaws.com\"\n" - + " ],\n" - + " \"User-Agent\": [\n" - + " \"curl/7.79.1\"\n" - + " ],\n" - + " \"X-Amzn-Trace-Id\": [\n" - + " \"Root=1-64087690-2151375b219d3ba3389ea84e\"\n" - + " ],\n" - + " \"X-Forwarded-For\": [\n" - + " \"109.210.252.44\"\n" - + " ],\n" - + " \"X-Forwarded-Port\": [\n" - + " \"443\"\n" - + " ],\n" - + " \"X-Forwarded-Proto\": [\n" - + " \"https\"\n" - + " ],\n" - + " \"accept\": [\n" - + " \"*/*\"\n" - + " ]\n" - + " },\n" - + " \"queryStringParameters\": {\n" - + " \"abc\": \"xyz\",\n" - + " \"foo\": \"baz\"\n" - + " },\n" - + " \"multiValueQueryStringParameters\": {\n" - + " \"abc\": [\n" - + " \"xyz\"\n" - + " ],\n" - + " \"foo\": [\n" - + " \"bar\",\n" - + " \"baz\"\n" - + " ]\n" - + " },\n" - + " \"requestContext\": {\n" - + " \"accountId\": \"123456789098\",\n" - + " \"apiId\": \"i76bfhczs0\",\n" - + " \"domainName\": \"i76bfhczs0.execute-api.eu-west-3.amazonaws.com\",\n" - + " \"domainPrefix\": \"i76bfhczs0\",\n" - + " \"extendedRequestId\": \"Bdd2ngt5iGYEMIg=\",\n" - + " \"httpMethod\": \"POST\",\n" - + " \"identity\": {\n" - + " \"accessKey\": null,\n" - + " \"accountId\": null,\n" - + " \"caller\": null,\n" - + " \"cognitoAmr\": null,\n" - + " \"cognitoAuthenticationProvider\": null,\n" - + " \"cognitoAuthenticationType\": null,\n" - + " \"cognitoIdentityId\": null,\n" - + " \"cognitoIdentityPoolId\": null,\n" - + " \"principalOrgId\": null,\n" - + " \"sourceIp\": \"109.210.252.44\",\n" - + " \"user\": null,\n" - + " \"userAgent\": \"curl/7.79.1\",\n" - + " \"userArn\": null\n" - + " },\n" - + " \"path\": \"/pets\",\n" - + " \"protocol\": \"HTTP/1.1\",\n" - + " \"requestId\": \"Bdd2ngt5iGYEMIg=\",\n" - + " \"requestTime\": \"08/Mar/2023:11:50:40 +0000\",\n" - + " \"requestTimeEpoch\": 1678276240455,\n" - + " \"resourceId\": \"$default\",\n" - + " \"resourcePath\": \"$default\",\n" - + " \"stage\": \"$default\"\n" - + " },\n" - + " \"pathParameters\": null,\n" - + " \"stageVariables\": null,\n" - + " \"body\": \"{\\\"id\\\":\\\"123\\\",\\\"breed\\\":\\\"Datsun\\\",\\\"name\\\":\\\"Donald\\\"}\",\n" - + " \"isBase64Encoded\": false\n" - + "}"; - - - - - @Test - public void validateHttpServletRequestConstruction() throws Exception { - System.setProperty("MAIN_CLASS", PetStoreSpringAppConfig.class.getName()); - WebProxyInvoker invoker = new WebProxyInvoker(); - InputStream targetStream = new ByteArrayInputStream(API_GATEWAY_EVENT.getBytes()); - Method prepareRequest = ReflectionUtils.findMethod(WebProxyInvoker.class, "prepareRequest", InputStream.class); - prepareRequest.setAccessible(true); - ProxyHttpServletRequest request = (ProxyHttpServletRequest) prepareRequest.invoke(invoker, targetStream); - - assertThat(request.getContentType()).isEqualTo("application/json"); - assertThat(request.getParameterValues("foo").length).isEqualTo(2); - assertThat(request.getParameterValues("foo")[0]).isEqualTo("bar"); - assertThat(request.getParameterValues("abc").length).isEqualTo(1); - assertThat(request.getParameterValues("abc")[0]).isEqualTo("xyz"); - assertThat(request.getHeaders(HttpHeaders.CONTENT_TYPE).nextElement()).isEqualTo("application/json"); - assertThat(request.getContentAsString()).isEqualTo("{\"id\":\"123\",\"breed\":\"Datsun\",\"name\":\"Donald\"}"); - assertThat(request.getContentLength()).isEqualTo(45); - assertThat(request.getMethod()).isEqualTo("POST"); - } - - @Test - public void testApiGatewayProxy() throws Exception { - System.setProperty("MAIN_CLASS", PetStoreSpringAppConfig.class.getName()); - WebProxyInvoker invoker = new WebProxyInvoker(); - - InputStream targetStream = new ByteArrayInputStream(API_GATEWAY_EVENT.getBytes()); - ByteArrayOutputStream output = new ByteArrayOutputStream(); - invoker.handleRequest(targetStream, output); - - ObjectMapper mapper = new ObjectMapper(); - Map result = mapper.readValue(output.toByteArray(), Map.class); - assertThat((boolean) result.get("isBase64Encoded")).isFalse(); - assertThat(((Map>) result.get("multiValueHeaders")).get("Content-Type").get(0)).isEqualTo("application/json"); - assertThat(result.get("statusCode")).isEqualTo(200); - Pet pet = mapper.readValue((String) result.get("body"), Pet.class); - assertThat(pet.getName()).isEqualTo("Donald"); - } -}