|
1 | | -package com.microsoft.azure.functions.endtoend; |
2 | | - |
3 | | -import com.microsoft.azure.functions.annotation.*; |
4 | | -import com.microsoft.azure.functions.*; |
5 | | -import java.util.*; |
6 | | - |
7 | | -/** |
8 | | - * Azure Functions with HTTP trigger. |
9 | | - */ |
10 | | -public class HttpTriggerTests { |
11 | | - /** |
12 | | - * This function will listen at HTTP endpoint "/api/HttpTrigger". |
13 | | - */ |
14 | | - @FunctionName("HttpTriggerJava") |
15 | | - public HttpResponseMessage HttpTriggerJava( |
16 | | - @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
17 | | - final ExecutionContext context |
18 | | - ) { |
19 | | - context.getLogger().info("Java HTTP trigger processed a request."); |
20 | | - |
21 | | - // Parse query parameters |
22 | | - String query = request.getQueryParameters().get("name"); |
23 | | - String name = request.getBody().orElse(query); |
24 | | - String readEnv = System.getenv("AzureWebJobsStorage"); |
25 | | - |
26 | | - if (name == null ) { |
27 | | - return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build(); |
28 | | - } |
29 | | - if (readEnv == null ) { |
30 | | - return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("AzureWebJobsStorage is empty").build(); |
31 | | - } |
32 | | - return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build(); |
33 | | - } |
34 | | - |
35 | | - @FunctionName("HttpTriggerJavaThrows") |
36 | | - public HttpResponseMessage HttpTriggerThrows( |
37 | | - @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
38 | | - final ExecutionContext context) throws Exception{ |
39 | | - context.getLogger().info("Java HTTP trigger processed a request."); |
40 | | - throw new Exception("Test Exception"); |
41 | | - } |
42 | | - |
43 | | - @FunctionName("HttpTriggerJavaMetadata") |
44 | | - public static String HttpTriggerJavaMetadata( |
45 | | - @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
46 | | - @BindingName("firstName") String queryValue1, @BindingName("lastName") String queryValue2 |
47 | | - ) { |
48 | | - return queryValue1+queryValue2; |
49 | | - } |
50 | | - |
51 | | - @FunctionName("HttpTriggerCustomCode") |
52 | | - public HttpResponseMessage HttpTriggerCustomCode( |
53 | | - @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
54 | | - final ExecutionContext context |
55 | | - ) { |
56 | | - context.getLogger().info("Java HTTP trigger processed a request."); |
57 | | - |
58 | | - // Parse query parameters |
59 | | - String query = request.getQueryParameters().get("name"); |
60 | | - String name = request.getBody().orElse(query); |
61 | | - |
62 | | - if (name == null) { |
63 | | - return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build(); |
64 | | - } else { |
65 | | - return request.createResponseBuilder(HttpStatusType.custom(209)).body("Hello, " + name).build(); |
66 | | - } |
67 | | - } |
68 | | -} |
| 1 | +package com.microsoft.azure.functions.endtoend; |
| 2 | + |
| 3 | +import com.microsoft.azure.functions.annotation.*; |
| 4 | +import com.microsoft.azure.functions.*; |
| 5 | +import java.util.*; |
| 6 | + |
| 7 | +import com.google.gson.Gson; |
| 8 | +import org.apache.http.HttpResponse; |
| 9 | +import org.apache.http.client.methods.HttpGet; |
| 10 | +import org.apache.http.impl.client.CloseableHttpClient; |
| 11 | +import org.apache.http.impl.client.HttpClients; |
| 12 | + |
| 13 | +import java.sql.Connection; |
| 14 | +import java.sql.DriverManager; |
| 15 | +import java.sql.Statement; |
| 16 | + |
| 17 | +import java.io.InputStream; |
| 18 | +import com.google.common.io.CharStreams; |
| 19 | +import java.io.IOException; |
| 20 | +import java.io.InputStreamReader; |
| 21 | + |
| 22 | +import org.apache.commons.lang3.SystemUtils; |
| 23 | + |
| 24 | +/** |
| 25 | + * Azure Functions with HTTP trigger. |
| 26 | + */ |
| 27 | +public class HttpTriggerTests { |
| 28 | + /** |
| 29 | + * This function will listen at HTTP endpoint "/api/HttpTrigger". |
| 30 | + */ |
| 31 | + @FunctionName("HttpTriggerJava") |
| 32 | + public HttpResponseMessage HttpTriggerJava( |
| 33 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 34 | + final ExecutionContext context |
| 35 | + ) throws Exception { |
| 36 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 37 | + |
| 38 | + // Parse query parameters |
| 39 | + String query = request.getQueryParameters().get("name"); |
| 40 | + String name = request.getBody().orElse(query); |
| 41 | + String readEnv = System.getenv("AzureWebJobsStorage"); |
| 42 | + |
| 43 | + try (Connection connection = DriverManager.getConnection("jdbc:h2:mem:test")) { |
| 44 | + try (Statement statement = connection.createStatement()) { |
| 45 | + statement.execute("select 1"); |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + Gson a = new Gson(); |
| 50 | + |
| 51 | +// if(!SystemUtils.IS_JAVA_15) { |
| 52 | +// context.getLogger().info("Java version not 15"); |
| 53 | +// } |
| 54 | + |
| 55 | + get("https://httpstat.us/200"); |
| 56 | + |
| 57 | + if (name == null ) { |
| 58 | + return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build(); |
| 59 | + } |
| 60 | + if (readEnv == null ) { |
| 61 | + return request.createResponseBuilder(HttpStatus.INTERNAL_SERVER_ERROR).body("AzureWebJobsStorage is empty").build(); |
| 62 | + } |
| 63 | + return request.createResponseBuilder(HttpStatus.OK).body("Hello, " + name).build(); |
| 64 | + } |
| 65 | + |
| 66 | + private static String get(String url) throws IOException { |
| 67 | + CloseableHttpClient httpClient = HttpClients.createDefault(); |
| 68 | + HttpGet httpGet = new HttpGet(url); |
| 69 | + httpGet.setHeader("Accept", "application/json"); |
| 70 | + HttpResponse response = httpClient.execute(httpGet); |
| 71 | + InputStream content = response.getEntity().getContent(); |
| 72 | + String body = CharStreams.toString(new InputStreamReader(content)); |
| 73 | + content.close(); |
| 74 | + httpClient.close(); |
| 75 | + return "Response from " + url + " was: " + body; |
| 76 | + } |
| 77 | + |
| 78 | + @FunctionName("HttpTriggerJavaThrows") |
| 79 | + public HttpResponseMessage HttpTriggerThrows( |
| 80 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 81 | + final ExecutionContext context) throws Exception{ |
| 82 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 83 | + throw new Exception("Test Exception"); |
| 84 | + } |
| 85 | + |
| 86 | + @FunctionName("HttpTriggerJavaMetadata") |
| 87 | + public static String HttpTriggerJavaMetadata( |
| 88 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 89 | + @BindingName("firstName") String queryValue1, @BindingName("lastName") String queryValue2 |
| 90 | + ) { |
| 91 | + return queryValue1+queryValue2; |
| 92 | + } |
| 93 | + |
| 94 | + @FunctionName("HttpTriggerCustomCode") |
| 95 | + public HttpResponseMessage HttpTriggerCustomCode( |
| 96 | + @HttpTrigger(name = "req", methods = {HttpMethod.GET, HttpMethod.POST}, authLevel = AuthorizationLevel.ANONYMOUS) HttpRequestMessage<Optional<String>> request, |
| 97 | + final ExecutionContext context |
| 98 | + ) { |
| 99 | + context.getLogger().info("Java HTTP trigger processed a request."); |
| 100 | + |
| 101 | + // Parse query parameters |
| 102 | + String query = request.getQueryParameters().get("name"); |
| 103 | + String name = request.getBody().orElse(query); |
| 104 | + |
| 105 | + if (name == null) { |
| 106 | + return request.createResponseBuilder(HttpStatus.BAD_REQUEST).body("Please pass a name on the query string or in the request body").build(); |
| 107 | + } else { |
| 108 | + return request.createResponseBuilder(HttpStatusType.custom(209)).body("Hello, " + name).build(); |
| 109 | + } |
| 110 | + } |
| 111 | +} |
0 commit comments