private void handleRule ( TSDB tsdb , HttpQuery query ) { final TreeRule rule ; if ( query . hasContent ( ) ) { rule = query . serializer ( ) . parseTreeRuleV1 ( ) ; } else { rule = parseRule ( query ) ; } try { // no matter what, we'll need a tree to work with, so make sure it exists Tree tree = null ; tree = Tree . fetchTree ( tsdb , rule . getTreeId ( ) ) . joinUninterruptibly ( ) ; if ( tree == null ) { throw new BadRequestException ( HttpResponseStatus . NOT_FOUND , "Unable to locate tree: " + rule . getTreeId ( ) ) ; } // if get, then we're just returning a rule from a tree if ( query . getAPIMethod ( ) == HttpMethod . GET ) { final TreeRule tree_rule = tree . getRule ( rule . getLevel ( ) , rule . getOrder ( ) ) ; if ( tree_rule == null ) { throw new BadRequestException ( HttpResponseStatus . NOT_FOUND , "Unable to locate rule: " + rule ) ; } query . sendReply ( query . serializer ( ) . formatTreeRuleV1 ( tree_rule ) ) ; } else if ( query . getAPIMethod ( ) == HttpMethod . POST || query . getAPIMethod ( ) == HttpMethod . PUT ) { if ( rule . syncToStorage ( tsdb , ( query . getAPIMethod ( ) == HttpMethod . PUT ) ) . joinUninterruptibly ( ) ) { final TreeRule stored_rule = TreeRule . fetchRule ( tsdb , rule . getTreeId ( ) , rule . getLevel ( ) , rule . getOrder ( ) ) . joinUninterruptibly ( ) ; query . sendReply ( query . serializer ( ) . formatTreeRuleV1 ( stored_rule ) ) ; } else { throw new RuntimeException ( "Unable to save rule " + rule + " to storage" ) ; } } else if ( query . getAPIMethod ( ) == HttpMethod . DELETE ) { if ( tree . getRule ( rule . getLevel ( ) , rule . getOrder ( ) ) == null ) { throw new BadRequestException ( HttpResponseStatus . NOT_FOUND , "Unable to locate rule: " + rule ) ; } TreeRule . deleteRule ( tsdb , tree . getTreeId ( ) , rule . getLevel ( ) , rule . getOrder ( ) ) . joinUninterruptibly ( ) ; query . sendStatusOnly ( HttpResponseStatus . NO_CONTENT ) ; } else { throw new BadRequestException ( HttpResponseStatus . BAD_REQUEST , "Unsupported HTTP request method" ) ; } } catch ( BadRequestException e ) { throw e ; } catch ( IllegalStateException e ) { query . sendStatusOnly ( HttpResponseStatus . NOT_MODIFIED ) ; } catch ( IllegalArgumentException e ) { throw new BadRequestException ( e ) ; } catch ( Exception e ) { throw new RuntimeException ( e ) ; } }
Handles the CRUD calls for a single rule enabling adding editing or deleting the rule