Skip to content

Commit 7877b2a

Browse files
committed
SWG-13871 - generalization
1 parent f459099 commit 7877b2a

File tree

5 files changed

+68
-27
lines changed

5 files changed

+68
-27
lines changed

.github/workflows/release.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,7 @@ jobs:
105105
-H "Authorization: Bearer ${TOKEN}" \
106106
-H 'Content-Type: application/json-patch+json' \
107107
"https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}" \
108-
-d "[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/image\", \"value\": \"${image}\"}, {\"op\":\"add\",\"path\":\"/spec/template/spec/containers/0/env\",\"value\":[{\"name\":\"BUGSNAG_API_KEY\",\"value\":\"${BUGSNAG_API_KEY}\"}]}]"
108+
-d "[{\"op\": \"replace\", \"path\": \"/spec/template/spec/containers/0/image\", \"value\": \"${image}\"}, {\"op\":\"add\",\"path\":\"/spec/template/spec/containers/0/env\",\"value\":[{\"name\":\"BUGSNAG_API_KEY\",\"value\":\"${BUGSNAG_API_KEY}\"},{\"name\":\"notifierClass\",\"value\":\io.swagger.petstore.notification.BugSnagNotifier\"}]}]"
109109
then
110110
echo 'ERROR - image update k8s API call failed!'
111111
echo "Exiting build..."

src/main/java/io/swagger/petstore/controller/PetController.java

Lines changed: 28 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -16,13 +16,14 @@
1616

1717
package io.swagger.petstore.controller;
1818

19-
import com.bugsnag.Bugsnag;
2019
import io.swagger.oas.inflector.models.RequestContext;
2120
import io.swagger.oas.inflector.models.ResponseContext;
2221
import io.swagger.petstore.data.PetData;
2322
import io.swagger.petstore.model.Category;
2423
import io.swagger.petstore.model.Pet;
2524
import io.swagger.petstore.model.Tag;
25+
import io.swagger.petstore.notification.Notifier;
26+
import io.swagger.petstore.notification.NullNotifier;
2627
import io.swagger.petstore.utils.Util;
2728

2829
import javax.ws.rs.core.MediaType;
@@ -34,20 +35,21 @@
3435
public class PetController {
3536

3637
private static PetData petData = new PetData();
37-
private static Bugsnag bugsnag;
38+
private Notifier notifier = new NullNotifier();
3839

39-
static {
40-
String bugsnagApiKey = System.getenv("BUGSNAG_API_KEY");
41-
if (bugsnagApiKey != null) {
42-
bugsnag = new Bugsnag(bugsnagApiKey);
43-
} else {
44-
throw new IllegalStateException("BUGSNAG_API_KEY environment variable is not set");
40+
public PetController() {
41+
if (System.getenv("notifierClass") != null) {
42+
try {
43+
notifier = (Notifier) this.getClass().forName(System.getenv("notifierClass")).newInstance();
44+
} catch (Exception e) {
45+
//
46+
}
4547
}
4648
}
4749

4850
public ResponseContext findPetsByStatus(final RequestContext request, final String status) {
4951
if (status == null) {
50-
bugsnag.notify(new RuntimeException("No status provided"));
52+
notifier.notify(new RuntimeException("No status provided"));
5153
return new ResponseContext()
5254
.status(Response.Status.BAD_REQUEST)
5355
.entity("No status provided. Try again?");
@@ -56,19 +58,19 @@ public ResponseContext findPetsByStatus(final RequestContext request, final Stri
5658
final List<Pet> petByStatus = petData.findPetByStatus(status);
5759

5860
if (petByStatus == null) {
59-
bugsnag.notify(new RuntimeException("Pets not found"));
61+
notifier.notify(new RuntimeException("Pets not found"));
6062
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pets not found");
6163
}
6264

63-
bugsnag.notify(new RuntimeException("Pets not found"));
65+
notifier.notify(new RuntimeException("Pets not found"));
6466
return new ResponseContext()
6567
.contentType(Util.getMediaType(request))
6668
.entity(petByStatus);
6769
}
6870

6971
public ResponseContext getPetById(final RequestContext request, final Long petId) {
7072
if (petId == null) {
71-
bugsnag.notify(new RuntimeException("No petId provided"));
73+
notifier.notify(new RuntimeException("No petId provided"));
7274
return new ResponseContext()
7375
.status(Response.Status.BAD_REQUEST)
7476
.entity("No petId provided. Try again?");
@@ -82,20 +84,20 @@ public ResponseContext getPetById(final RequestContext request, final Long petId
8284
.entity(pet);
8385
}
8486

85-
bugsnag.notify(new RuntimeException("Pets not found"));
87+
notifier.notify(new RuntimeException("Pets not found"));
8688
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
8789
}
8890

8991
public ResponseContext updatePetWithForm(final RequestContext request, final Long petId, final String name, final String status) {
9092
if (petId == null) {
91-
bugsnag.notify(new RuntimeException("No petId provided"));
93+
notifier.notify(new RuntimeException("No petId provided"));
9294
return new ResponseContext()
9395
.status(Response.Status.BAD_REQUEST)
9496
.entity("No Pet provided. Try again?");
9597
}
9698

9799
if (name == null) {
98-
bugsnag.notify(new RuntimeException("No name provided"));
100+
notifier.notify(new RuntimeException("No name provided"));
99101
return new ResponseContext()
100102
.status(Response.Status.BAD_REQUEST)
101103
.entity("No Name provided. Try again?");
@@ -105,7 +107,7 @@ public ResponseContext updatePetWithForm(final RequestContext request, final Lon
105107
final Pet existingPet = petData.getPetById(petId);
106108

107109
if (existingPet == null) {
108-
bugsnag.notify(new RuntimeException("No pet provided"));
110+
notifier.notify(new RuntimeException("No pet provided"));
109111
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
110112
}
111113

@@ -121,7 +123,7 @@ public ResponseContext updatePetWithForm(final RequestContext request, final Lon
121123

122124
public ResponseContext deletePet(final RequestContext request, final String apiKey, final Long petId) {
123125
if (petId == null) {
124-
bugsnag.notify(new RuntimeException("No petId provided"));
126+
notifier.notify(new RuntimeException("No petId provided"));
125127
return new ResponseContext()
126128
.status(Response.Status.BAD_REQUEST)
127129
.entity("No petId provided. Try again?");
@@ -138,28 +140,28 @@ public ResponseContext deletePet(final RequestContext request, final String apiK
138140
.contentType(outputType)
139141
.entity("Pet deleted");
140142
} else {
141-
bugsnag.notify(new RuntimeException("Pet couldn't be deleted"));
143+
notifier.notify(new RuntimeException("Pet couldn't be deleted"));
142144
return new ResponseContext().status(Response.Status.NOT_MODIFIED).entity("Pet couldn't be deleted.");
143145
}
144146

145147
}
146148

147149
public ResponseContext uploadFile(final RequestContext request, final Long petId, final String apiKey, final File file) {
148150
if (petId == null) {
149-
bugsnag.notify(new RuntimeException("No petId provided"));
151+
notifier.notify(new RuntimeException("No petId provided"));
150152
return new ResponseContext()
151153
.status(Response.Status.BAD_REQUEST)
152154
.entity("No petId provided. Try again?");
153155
}
154156

155157
if (file == null) {
156-
bugsnag.notify(new RuntimeException("No file provided"));
158+
notifier.notify(new RuntimeException("No file provided"));
157159
return new ResponseContext().status(Response.Status.BAD_REQUEST).entity("No file uploaded");
158160
}
159161

160162
final Pet existingPet = petData.getPetById(petId);
161163
if (existingPet == null) {
162-
bugsnag.notify(new RuntimeException("No pet provided"));
164+
notifier.notify(new RuntimeException("No pet provided"));
163165
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
164166
}
165167

@@ -174,14 +176,14 @@ public ResponseContext uploadFile(final RequestContext request, final Long petId
174176
.contentType(Util.getMediaType(request))
175177
.entity(pet);
176178
} else {
177-
bugsnag.notify(new RuntimeException("Pet couldn't be updated"));
179+
notifier.notify(new RuntimeException("Pet couldn't be updated"));
178180
return new ResponseContext().status(Response.Status.NOT_MODIFIED).entity("Pet couldn't be updated.");
179181
}
180182
}
181183

182184
public ResponseContext addPet(final RequestContext request, final Pet pet) {
183185
if (pet == null) {
184-
bugsnag.notify(new RuntimeException("No pet provided"));
186+
notifier.notify(new RuntimeException("No pet provided"));
185187
return new ResponseContext()
186188
.status(Response.Status.BAD_REQUEST)
187189
.entity("No Pet provided. Try again?");
@@ -202,15 +204,15 @@ public ResponseContext addPet(final RequestContext request, final Long id, final
202204

203205
public ResponseContext updatePet(final RequestContext request, final Pet pet) {
204206
if (pet == null) {
205-
bugsnag.notify(new RuntimeException("No pet provided"));
207+
notifier.notify(new RuntimeException("No pet provided"));
206208
return new ResponseContext()
207209
.status(Response.Status.BAD_REQUEST)
208210
.entity("No Pet provided. Try again?");
209211
}
210212

211213
final Pet existingPet = petData.getPetById(pet.getId());
212214
if (existingPet == null) {
213-
bugsnag.notify(new RuntimeException("No pet provided"));
215+
notifier.notify(new RuntimeException("No pet provided"));
214216
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
215217
}
216218

@@ -230,7 +232,7 @@ public ResponseContext updatePet(final RequestContext request, final Long id, fi
230232

231233
public ResponseContext findPetsByTags(final RequestContext request, final List<String> tags) {
232234
if (tags == null || tags.size() == 0) {
233-
bugsnag.notify(new RuntimeException("No tags provided"));
235+
notifier.notify(new RuntimeException("No tags provided"));
234236
return new ResponseContext()
235237
.status(Response.Status.BAD_REQUEST)
236238
.entity("No tags provided. Try again?");
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package io.swagger.petstore.notification;
2+
3+
import com.bugsnag.Bugsnag;
4+
5+
public class BugSnagNotifier implements Notifier {
6+
7+
protected Bugsnag bugsnag;
8+
9+
public void init() {
10+
String bugsnagApiKey = System.getenv("BUGSNAG_API_KEY");
11+
if (bugsnagApiKey != null) {
12+
bugsnag = new Bugsnag(bugsnagApiKey);
13+
} else {
14+
System.err.println("BUGSNAG_API_KEY environment variable is not set");
15+
}
16+
}
17+
18+
@Override
19+
public void notify(Throwable e) {
20+
if (bugsnag != null) {
21+
bugsnag.notify(e);
22+
}
23+
}
24+
}
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
package io.swagger.petstore.notification;
2+
3+
public interface Notifier {
4+
5+
void notify(Throwable e);
6+
}
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package io.swagger.petstore.notification;
2+
3+
public class NullNotifier implements Notifier {
4+
5+
@Override
6+
public void notify(Throwable e) {
7+
//
8+
}
9+
}

0 commit comments

Comments
 (0)