| 1 | package edu.ucsb.cs156.example.handlers; | |
| 2 | ||
| 3 | import edu.ucsb.cs156.example.entities.Url; | |
| 4 | import org.springframework.data.rest.core.annotation.HandleBeforeCreate; | |
| 5 | import org.springframework.data.rest.core.annotation.RepositoryEventHandler; | |
| 6 | import org.springframework.stereotype.Component; | |
| 7 | ||
| 8 | @Component | |
| 9 | @RepositoryEventHandler(Url.class) | |
| 10 | public class UrlEventHandler { | |
| 11 | ||
| 12 | // This method runs *before* a new entity is saved (POST) or an existing one is updated | |
| 13 | // (PUT/PATCH) | |
| 14 | @HandleBeforeCreate | |
| 15 | public void handleBeforeCreate(Url url) { | |
| 16 | ||
| 17 | // Custom business logic validation | |
| 18 |
2
1. handleBeforeCreate : negated conditional → KILLED 2. handleBeforeCreate : negated conditional → KILLED |
if (!url.getUrl().startsWith("http://") && !url.getUrl().startsWith("https://")) { |
| 19 | throw new IllegalArgumentException("URL must start with http:// or https://"); | |
| 20 | } | |
| 21 | // Additional validation logic can be added here | |
| 22 |
2
1. handleBeforeCreate : negated conditional → KILLED 2. handleBeforeCreate : changed conditional boundary → KILLED |
if (url.getUrl().length() > 2000) { |
| 23 | throw new IllegalArgumentException("URL is too long."); | |
| 24 | } | |
| 25 | // Match against a regex pattern for legal URL formats | |
| 26 | String urlPattern = "^(http|https)://[a-zA-Z0-9.-]+(:[0-9]+)?(/.*)?$"; | |
| 27 |
1
1. handleBeforeCreate : negated conditional → KILLED |
if (!url.getUrl().matches(urlPattern)) { |
| 28 | throw new IllegalArgumentException("URL format is invalid."); | |
| 29 | } | |
| 30 | } | |
| 31 | } | |
Mutations | ||
| 18 |
1.1 2.2 |
|
| 22 |
1.1 2.2 |
|
| 27 |
1.1 |