Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/release.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ jobs:
-H "Authorization: Bearer ${TOKEN}" \
-H 'Content-Type: application/json-patch+json' \
"https://${RANCHER_HOST}/k8s/clusters/${CLUSTER_ID}/apis/apps/v1/namespaces/${NAMESPACE_NAME}/${K8S_OBJECT_TYPE}/${K8S_OBJECT_NAME}" \
-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}\"}]}]"
-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\"}]}]"
then
echo 'ERROR - image update k8s API call failed!'
echo "Exiting build..."
Expand Down
2 changes: 1 addition & 1 deletion Dockerfile-telemetry
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,6 @@ ENV JAVA_TOOL_OPTIONS="-javaagent:/swagger-petstore/otel-javaagent.jar" \
OTEL_INSTRUMENTATION_HTTP_SERVER_CAPTURE_ROUTE=true \
OTEL_ATTRIBUTE_VALUE_LENGTH_LIMIT=256

CMD ["sh", "-c", "export OTEL_EXPORTER_OTLP_ENDPOINT=https://$BUGSNAG_API_KEY.otlp.bugsnag.com && \
CMD ["sh", "-c", "export BUGSNAG_API_KEY=$BUGSNAG_API_KEY && export OTEL_EXPORTER_OTLP_ENDPOINT=https://$BUGSNAG_API_KEY.otlp.bugsnag.com && \
exec java $JAVA_TOOL_OPTIONS -Dorg.eclipse.jetty.server.RequestLog=DEBUG -Dorg.eclipse.jetty.server.HttpChannel=DEBUG \
-jar -DswaggerUrl=openapi.yaml /swagger-petstore/jetty-runner.jar --log /var/log/yyyy_mm_dd-requests.log /swagger-petstore/server.war"]
5 changes: 5 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,11 @@
<artifactId>jaxb-impl</artifactId>
<version>2.3.1</version>
</dependency>
<dependency>
<groupId>com.bugsnag</groupId>
<version>[3.0,4.0)</version>
<artifactId>bugsnag</artifactId>
</dependency>

</dependencies>
<repositories>
Expand Down
31 changes: 31 additions & 0 deletions src/main/java/io/swagger/petstore/controller/PetController.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@
import io.swagger.petstore.model.Category;
import io.swagger.petstore.model.Pet;
import io.swagger.petstore.model.Tag;
import io.swagger.petstore.notification.Notifier;
import io.swagger.petstore.notification.NullNotifier;
import io.swagger.petstore.utils.Util;

import javax.ws.rs.core.MediaType;
Expand All @@ -33,9 +35,21 @@
public class PetController {

private static PetData petData = new PetData();
private Notifier notifier = new NullNotifier();

public PetController() {
if (System.getenv("notifierClass") != null) {
try {
notifier = (Notifier) this.getClass().forName(System.getenv("notifierClass")).newInstance();
} catch (Exception e) {
//
}
}
}

public ResponseContext findPetsByStatus(final RequestContext request, final String status) {
if (status == null) {
notifier.notify(new RuntimeException("No status provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No status provided. Try again?");
Expand All @@ -44,16 +58,19 @@ public ResponseContext findPetsByStatus(final RequestContext request, final Stri
final List<Pet> petByStatus = petData.findPetByStatus(status);

if (petByStatus == null) {
notifier.notify(new RuntimeException("Pets not found"));
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pets not found");
}

notifier.notify(new RuntimeException("Pets not found"));
return new ResponseContext()
.contentType(Util.getMediaType(request))
.entity(petByStatus);
}

public ResponseContext getPetById(final RequestContext request, final Long petId) {
if (petId == null) {
notifier.notify(new RuntimeException("No petId provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No petId provided. Try again?");
Expand All @@ -67,17 +84,20 @@ public ResponseContext getPetById(final RequestContext request, final Long petId
.entity(pet);
}

notifier.notify(new RuntimeException("Pets not found"));
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
}

public ResponseContext updatePetWithForm(final RequestContext request, final Long petId, final String name, final String status) {
if (petId == null) {
notifier.notify(new RuntimeException("No petId provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No Pet provided. Try again?");
}

if (name == null) {
notifier.notify(new RuntimeException("No name provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No Name provided. Try again?");
Expand All @@ -87,6 +107,7 @@ public ResponseContext updatePetWithForm(final RequestContext request, final Lon
final Pet existingPet = petData.getPetById(petId);

if (existingPet == null) {
notifier.notify(new RuntimeException("No pet provided"));
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
}

Expand All @@ -102,6 +123,7 @@ public ResponseContext updatePetWithForm(final RequestContext request, final Lon

public ResponseContext deletePet(final RequestContext request, final String apiKey, final Long petId) {
if (petId == null) {
notifier.notify(new RuntimeException("No petId provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No petId provided. Try again?");
Expand All @@ -118,24 +140,28 @@ public ResponseContext deletePet(final RequestContext request, final String apiK
.contentType(outputType)
.entity("Pet deleted");
} else {
notifier.notify(new RuntimeException("Pet couldn't be deleted"));
return new ResponseContext().status(Response.Status.NOT_MODIFIED).entity("Pet couldn't be deleted.");
}

}

public ResponseContext uploadFile(final RequestContext request, final Long petId, final String apiKey, final File file) {
if (petId == null) {
notifier.notify(new RuntimeException("No petId provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No petId provided. Try again?");
}

if (file == null) {
notifier.notify(new RuntimeException("No file provided"));
return new ResponseContext().status(Response.Status.BAD_REQUEST).entity("No file uploaded");
}

final Pet existingPet = petData.getPetById(petId);
if (existingPet == null) {
notifier.notify(new RuntimeException("No pet provided"));
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
}

Expand All @@ -150,12 +176,14 @@ public ResponseContext uploadFile(final RequestContext request, final Long petId
.contentType(Util.getMediaType(request))
.entity(pet);
} else {
notifier.notify(new RuntimeException("Pet couldn't be updated"));
return new ResponseContext().status(Response.Status.NOT_MODIFIED).entity("Pet couldn't be updated.");
}
}

public ResponseContext addPet(final RequestContext request, final Pet pet) {
if (pet == null) {
notifier.notify(new RuntimeException("No pet provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No Pet provided. Try again?");
Expand All @@ -176,13 +204,15 @@ public ResponseContext addPet(final RequestContext request, final Long id, final

public ResponseContext updatePet(final RequestContext request, final Pet pet) {
if (pet == null) {
notifier.notify(new RuntimeException("No pet provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No Pet provided. Try again?");
}

final Pet existingPet = petData.getPetById(pet.getId());
if (existingPet == null) {
notifier.notify(new RuntimeException("No pet provided"));
return new ResponseContext().status(Response.Status.NOT_FOUND).entity("Pet not found");
}

Expand All @@ -202,6 +232,7 @@ public ResponseContext updatePet(final RequestContext request, final Long id, fi

public ResponseContext findPetsByTags(final RequestContext request, final List<String> tags) {
if (tags == null || tags.size() == 0) {
notifier.notify(new RuntimeException("No tags provided"));
return new ResponseContext()
.status(Response.Status.BAD_REQUEST)
.entity("No tags provided. Try again?");
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package io.swagger.petstore.notification;

import com.bugsnag.Bugsnag;

public class BugSnagNotifier implements Notifier {

protected Bugsnag bugsnag;

public void init() {
String bugsnagApiKey = System.getenv("BUGSNAG_API_KEY");
if (bugsnagApiKey != null) {
bugsnag = new Bugsnag(bugsnagApiKey);
} else {
System.err.println("BUGSNAG_API_KEY environment variable is not set");
}
}

@Override
public void notify(Throwable e) {
if (bugsnag != null) {
bugsnag.notify(e);
}
}
}
6 changes: 6 additions & 0 deletions src/main/java/io/swagger/petstore/notification/Notifier.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
package io.swagger.petstore.notification;

public interface Notifier {

void notify(Throwable e);
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
package io.swagger.petstore.notification;

public class NullNotifier implements Notifier {

@Override
public void notify(Throwable e) {
//
}
}
Loading