allConditions = new LinkedList<>();
52 | for (String location : locations) {
53 | try {
54 | allConditions.add(currentConditions(location));
55 | } catch (UnknownLocationException ule) {
56 | throw new GraphQLException(ule, allConditions);
57 | }
58 | }
59 | return allConditions;
60 | }
61 |
62 | @RolesAllowed("Role2")
63 | @Mutation
64 | @Description("Reset the cached conditions so that new queries will return newly randomized weather data." +
65 | "Returns number of entries cleared.")
66 | public int reset() {
67 | int cleared = currentConditionsMap.size();
68 | currentConditionsMap.clear();
69 | return cleared;
70 | }
71 |
72 | public double wetBulbTempF(@Source @Name("conditions") Conditions conditions) {
73 | // TODO: pretend like this is a really expensive operation
74 | System.out.println("wetBulbTempF for location " + conditions.getLocation());
75 | return conditions.getTemperatureF() - 3.0;
76 | }
77 |
78 | private Conditions randomWeatherConditions(String location) {
79 | Conditions c = new Conditions(location);
80 | c.setDayTime(Math.random() > 0.5);
81 | c.setTemperatureF(Math.random() * 100);
82 | c.setTemperatureC( (c.getTemperatureF() - 30) / 2 );
83 | c.setHasPrecipitation(Math.random() > 0.7);
84 | c.setPrecipitationType(c.isHasPrecipitation() ? PrecipType.fromTempF(c.getTemperatureF()) : null);
85 | c.setWeatherText(c.isHasPrecipitation() ? "Overcast" : "Sunny");
86 | double windSpeed = Math.random() * 50;
87 | Wind wind = new Wind((int)(Math.random() * 360), windSpeed, windSpeed + (Math.random() * 50));
88 | c.setWind(wind);
89 | return c;
90 | }
91 | }
92 |
--------------------------------------------------------------------------------
/src/main/java/io/openliberty/graphql/sample/Wind.java:
--------------------------------------------------------------------------------
1 | // ******************************************************************************
2 | // Copyright (c) 2021 IBM Corporation and others.
3 | // All rights reserved. This program and the accompanying materials
4 | // are made available under the terms of the Eclipse Public License v1.0
5 | // which accompanies this distribution, and is available at
6 | // http://www.eclipse.org/legal/epl-v10.html
7 | //
8 | // Contributors:
9 | // IBM Corporation - initial API and implementation
10 | // ******************************************************************************
11 | package io.openliberty.graphql.sample;
12 |
13 | public class Wind {
14 |
15 | private int direction; // valid directions are 0-359
16 | private double speed;
17 | private double gustSpeed;
18 |
19 | public Wind() {}
20 |
21 | public Wind(int direction, double speed, double gustSpeed) {
22 | this.direction = direction;
23 | this.speed = speed;
24 | this.gustSpeed = gustSpeed;
25 | }
26 |
27 | public int getDirection() {
28 | return direction;
29 | }
30 |
31 | public void setDirection(int direction) {
32 | this.direction = direction;
33 | }
34 |
35 | public double getSpeed() {
36 | return speed;
37 | }
38 |
39 | public void setSpeed(double speed) {
40 | this.speed = speed;
41 | }
42 |
43 | public double getGustSpeed() {
44 | return gustSpeed;
45 | }
46 |
47 | public void setGustSpeed(double gustSpeed) {
48 | this.gustSpeed = gustSpeed;
49 | }
50 | }
51 |
--------------------------------------------------------------------------------
/src/main/java/io/openliberty/graphql/sample/client/JaxrsResource.java:
--------------------------------------------------------------------------------
1 | // ******************************************************************************
2 | // Copyright (c) 2020 IBM Corporation and others.
3 | // All rights reserved. This program and the accompanying materials
4 | // are made available under the terms of the Eclipse Public License v1.0
5 | // which accompanies this distribution, and is available at
6 | // http://www.eclipse.org/legal/epl-v10.html
7 | //
8 | // Contributors:
9 | // IBM Corporation - initial API and implementation
10 | // ******************************************************************************
11 | package io.openliberty.graphql.sample.client;
12 |
13 | import javax.ws.rs.ApplicationPath;
14 | import javax.ws.rs.GET;
15 | import javax.ws.rs.Path;
16 | import javax.ws.rs.PathParam;
17 | import javax.ws.rs.Produces;
18 | import javax.ws.rs.core.Application;
19 | import javax.ws.rs.core.MediaType;
20 |
21 | import io.smallrye.graphql.client.typesafe.api.GraphQlClientBuilder;
22 |
23 | @ApplicationPath("/")
24 | @Path("test/client")
25 | @Produces(MediaType.TEXT_PLAIN)
26 | public class JaxrsResource extends Application{
27 |
28 | @GET
29 | @Path("/{location}")
30 | public String tempAndPrecipFor(@PathParam("location") String location) {
31 | WeatherApi api = GraphQlClientBuilder.newBuilder().endpoint("http://localhost:9080/mpGraphQLSample/graphql").build(WeatherApi.class);
32 | TempAndPrecip tempAndPrecip = api.currentConditions(location);
33 | return tempAndPrecip.toString();
34 | }
35 | }
--------------------------------------------------------------------------------
/src/main/java/io/openliberty/graphql/sample/client/TempAndPrecip.java:
--------------------------------------------------------------------------------
1 | // ******************************************************************************
2 | // Copyright (c) 2020 IBM Corporation and others.
3 | // All rights reserved. This program and the accompanying materials
4 | // are made available under the terms of the Eclipse Public License v1.0
5 | // which accompanies this distribution, and is available at
6 | // http://www.eclipse.org/legal/epl-v10.html
7 | //
8 | // Contributors:
9 | // IBM Corporation - initial API and implementation
10 | // ******************************************************************************
11 | package io.openliberty.graphql.sample.client;
12 |
13 |
14 | public class TempAndPrecip {
15 |
16 | private double temperatureF;
17 | private boolean hasPrecipitation;
18 |
19 | public double getTemperatureF() {
20 | return temperatureF;
21 | }
22 |
23 | public void setTemperatureF(double temperatureF) {
24 | this.temperatureF = temperatureF;
25 | }
26 |
27 | public boolean ishasPrecipitation() {
28 | return hasPrecipitation;
29 | }
30 |
31 | public void setHasPrecipitation(boolean hasPrecipitation) {
32 | this.hasPrecipitation = hasPrecipitation;
33 | }
34 |
35 | @Override
36 | public String toString() {
37 | return "Temperature (F): " + temperatureF + " Precipitation: " + hasPrecipitation;
38 | }
39 | }
--------------------------------------------------------------------------------
/src/main/java/io/openliberty/graphql/sample/client/WeatherApi.java:
--------------------------------------------------------------------------------
1 | // ******************************************************************************
2 | // Copyright (c) 2020 IBM Corporation and others.
3 | // All rights reserved. This program and the accompanying materials
4 | // are made available under the terms of the Eclipse Public License v1.0
5 | // which accompanies this distribution, and is available at
6 | // http://www.eclipse.org/legal/epl-v10.html
7 | //
8 | // Contributors:
9 | // IBM Corporation - initial API and implementation
10 | // ******************************************************************************
11 | package io.openliberty.graphql.sample.client;
12 |
13 | import org.eclipse.microprofile.graphql.Name;
14 |
15 | public interface WeatherApi {
16 |
17 | TempAndPrecip currentConditions(@Name("location")String location);
18 | }
--------------------------------------------------------------------------------
/src/main/liberty/config/server.xml:
--------------------------------------------------------------------------------
1 |
2 |
3 | mpGraphQL-1.0
4 | mpMetrics-2.3
5 | jsp-2.3
6 |
7 |
8 |
9 |
10 |
11 |
12 |
15 |
16 |
17 |
18 |
19 |
20 |
21 |
22 |
23 |
24 |
25 |
26 |
27 |
28 |
29 |
30 |
31 |
32 |
--------------------------------------------------------------------------------
/src/main/webapp/WEB-INF/web.xml:
--------------------------------------------------------------------------------
1 |
6 |
7 | MP GraphQL Sample App using made-up weather
8 | Application provides made-up weather conditions
9 |
10 |
11 |
12 | allResources
13 | Requiring authentication for all resources when appSecurity is enabled
14 | /*
15 |
16 |
17 | NONE
18 |
19 |
20 | Role1
21 | Role2
22 |
23 |
24 |
25 |
26 | GeneralResources
27 | /login.html
28 | GET
29 |
30 |
31 | AllUsers
32 |
33 |
34 |
35 |
36 | FORM
37 |
38 | /login.html
39 | /login.html
40 |
41 |
42 |
43 |
44 | Role1
45 |
46 |
47 | Role2
48 |
49 |
50 | AllUsers
51 |
52 |
53 |
--------------------------------------------------------------------------------
/src/main/webapp/graphiql.html:
--------------------------------------------------------------------------------
1 |
8 |
9 |
10 |
11 |
22 |
23 |
30 |
31 |
32 |
33 |
34 |
35 |
40 |
41 |
42 |
43 |
44 |
45 | Loading...
46 |
141 |
142 |
143 |
--------------------------------------------------------------------------------
/src/main/webapp/index.html:
--------------------------------------------------------------------------------
1 |
2 |
3 |
4 |
5 |
6 |
7 |
8 |
--------------------------------------------------------------------------------
/src/main/webapp/login.html:
--------------------------------------------------------------------------------
1 |
2 | Login Page
3 |
4 | Form Login
5 |
11 |
12 |
--------------------------------------------------------------------------------
/src/main/webapp/logout.jsp:
--------------------------------------------------------------------------------
1 |
2 |
3 | <%
4 | request.logout();
5 | %>
6 |
7 | You are Sucessfully logged out...
8 |
9 | Go back To index page
10 |
11 |
--------------------------------------------------------------------------------