2121
2222import java .util .*;
2323import java .util .regex .Pattern ;
24+ import java .util .regex .PatternSyntaxException ;
2425import java .util .stream .Collectors ;
2526
2627import static com .uid2 .admin .vertx .Endpoints .*;
@@ -154,10 +155,8 @@ private void handleServiceAdd(RoutingContext rc) {
154155 return ;
155156 }
156157
157- if (linkIdRegex != null && !linkIdRegex .isEmpty () && !linkIdRegex .isBlank ()) {
158- try {
159- Pattern .compile (linkIdRegex );
160- } catch (Exception e ) {
158+ if (linkIdRegex != null && !linkIdRegex .isBlank ()) {
159+ if (!isValidRegex (linkIdRegex )) {
161160 ResponseUtil .error (rc , 400 , "invalid parameter: link_id_regex; not a valid regex" );
162161 return ;
163162 }
@@ -184,17 +183,17 @@ private void handleServiceAdd(RoutingContext rc) {
184183 // Can update the site_id, name, roles, and link_id_regex
185184 private void handleUpdate (RoutingContext rc ) {
186185 try {
187- JsonObject body = rc . body () != null ? rc . body (). asJsonObject () : null ;
186+ serviceProvider . loadContent () ;
188187 Service service = findServiceFromRequest (rc );
189188 if (service == null ) return ; // error already handled
190189
191-
192- Integer siteId = body != null ? body .getInteger ("site_id" ) : null ;
193- String name = body != null ? body .getString ("name" ) : null ;
194- String linkIdRegex = body != null ? body .getString ("link_id_regex" ) : null ;
190+ JsonObject body = rc . body (). asJsonObject ();
191+ Integer siteId = body .getInteger ("site_id" );
192+ String name = body .getString ("name" );
193+ String linkIdRegex = body .getString ("link_id_regex" );
195194
196195 JsonArray rolesSpec = null ;
197- if (body != null && body .getString ("roles" ) != null && !body .getString ("roles" ).isEmpty ()) {
196+ if (body .getString ("roles" ) != null && !body .getString ("roles" ).isEmpty ()) {
198197 try {
199198 rolesSpec = body .getJsonArray ("roles" );
200199 } catch (ClassCastException c ) {
@@ -235,14 +234,12 @@ private void handleUpdate(RoutingContext rc) {
235234 service .setRoles (roles );
236235 }
237236
238- if (linkIdRegex != null && !linkIdRegex .isEmpty () && !linkIdRegex .isBlank ()) {
239- try {
240- Pattern .compile (linkIdRegex );
241- service .setLinkIdRegex (linkIdRegex );
242- } catch (Exception e ) {
237+ if (linkIdRegex != null && !linkIdRegex .isBlank ()) {
238+ if (!isValidRegex (linkIdRegex )) {
243239 ResponseUtil .error (rc , 400 , "invalid parameter: link_id_regex; not a valid regex" );
244240 return ;
245241 }
242+ service .setLinkIdRegex (linkIdRegex );
246243 }
247244
248245 if (siteId != null && siteId != 0 ) {
@@ -264,10 +261,10 @@ private void handleUpdate(RoutingContext rc) {
264261 }
265262
266263 private void handleDelete (RoutingContext rc ) {
267- Service service = findServiceFromRequest (rc );
268- if (service == null ) return ; // error already handled
269-
270264 try {
265+ serviceProvider .loadContent ();
266+ Service service = findServiceFromRequest (rc );
267+ if (service == null ) return ; // error already handled
271268 List <Service > services = getSortedServices ();
272269 services .remove (service );
273270 storeWriter .upload (services , null );
@@ -278,10 +275,10 @@ private void handleDelete(RoutingContext rc) {
278275 }
279276
280277 private void handleRemoveLinkIdRegex (RoutingContext rc ) {
281- Service service = findServiceFromRequest (rc );
282- if (service == null ) return ; // error already handled
283-
284278 try {
279+ serviceProvider .loadContent ();
280+ Service service = findServiceFromRequest (rc );
281+ if (service == null ) return ; // error already handled
285282 service .setLinkIdRegex (null );
286283 List <Service > services = getSortedServices ();
287284 storeWriter .upload (services , null );
@@ -325,18 +322,12 @@ private Service findServiceFromRequest(RoutingContext rc) {
325322 return null ;
326323 }
327324
328- try {
329- serviceProvider .loadContent ();
330- Service service = serviceProvider .getService (serviceId );
331- if (service == null ) {
332- ResponseUtil .error (rc , 404 , "failed to find a service for service_id: " + serviceId );
333- return null ;
334- }
335- return service ;
336- } catch (Exception e ) {
337- ResponseUtil .errorInternal (rc , "Internal Server Error" , e );
325+ Service service = serviceProvider .getService (serviceId );
326+ if (service == null ) {
327+ ResponseUtil .error (rc , 404 , "failed to find a service for service_id: " + serviceId );
338328 return null ;
339329 }
330+ return service ;
340331 }
341332
342333 private List <Service > getSortedServices () {
@@ -345,4 +336,13 @@ private List<Service> getSortedServices() {
345336 .sorted (Comparator .comparingInt (Service ::getServiceId ))
346337 .collect (Collectors .toList ());
347338 }
339+
340+ private boolean isValidRegex (String regex ) {
341+ try {
342+ Pattern .compile (regex );
343+ return true ;
344+ } catch (PatternSyntaxException e ) {
345+ return false ;
346+ }
347+ }
348348}
0 commit comments