Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.service.cluster;
  
  
Handler that provides information about current cluster state, as seen by this node, as well as handles simple update notifications.
 
 public class ClusterInfoHandler
     extends HandlerBase
 {
     protected final ClusterViewByServerUpdatable _cluster;
 
     protected final ObjectWriter _writer;
     
     {
          = cluster;
         // Should we indent? Not for prod?
 //        _writer = stuff.jsonWriter(ClusterStatusResponse.class).withDefaultPrettyPrinter();
          = stuff.jsonWriter(ClusterStatusMessage.class);
     }
    
    
Simple read request for getting snapshot of Cluster status as seen by this node.
 
     @SuppressWarnings("unchecked")
     public <RESP extends ServiceResponse> RESP getStatus(ServiceRequest request, RESP response,
             OperationDiagnostics metadata)
     {
         // use streaming impl just so we'll use specific ObjectWriter
         return (RESP) response.ok(new StreamingEntityImpl(.asMessage()))
             .setContentTypeJson();
     }

    
Handler for notification POSTs
 
     @SuppressWarnings("unchecked")
     public <RESP extends ServiceResponse> RESP handlePost(ServiceRequest request, RESP response,
             OperationDiagnostics metadata)
     {
         // First things first: need to have a caller
         IpAndPort caller = getCallerQueryParam(request);
         if (caller == null) {
             return badRequest(response,
                     "Missing or invalid '"+.+"' value");
         }
         // as well as known state...
         String stateStr = request.getQueryParameter(.);
         // including timestamp by sender
         String timestampStr = request.getQueryParameter(.);
         long timestamp = 0L;
         
         if (timestampStr != null) {
             try {
                 timestamp = Long.parseLong(timestampStr);
             } catch (IllegalArgumentException e) { }
         }
         if (timestamp <= 0L) {
             return (RESP) badRequest(response"Invalid '"+.
                     +"': '{}'"timestampStr);
         }
         
         if (..equals(stateStr)) {
             KeyRange range = null;
 
             // For activation, would prefer having key range too:
             Integer keyRangeStart = _findIntParam(request.);
             if (keyRangeStart != null) {
                 Integer keyRangeLength = _findIntParam(request.);
                 if (keyRangeLength != null) {
                     try {
                         range = .getKeySpace().range(keyRangeStartkeyRangeLength);
                     } catch (Exception e) {
                         return (RESP) badRequest(response"Invalid key-range definition (start '%s', end '%s'): %s",
                                 keyRangeStartkeyRangeLengthe.getMessage());
                     }
                 }
             }
             if (range == null) {
                 range = .getKeySpace().emptyRange();
             }
             .nodeActivated(callertimestamprange);
         } else if (..equals(stateStr)) {
             .nodeDeactivated(callertimestamp);
        } else {
            return badRequest(response,
                    "Unrecognized '"+.+"' value '"+stateStr+"'");
        }
        return (RESP) response.ok();
    }
    /*
    /**********************************************************************
    /* Helper methods, error responses
    /**********************************************************************
     */
    @SuppressWarnings("unchecked")
    @Override
    protected <OUT extends ServiceResponse> OUT _badRequest(ServiceResponse responseString msg) {
        return (OUT) response
                .badRequest(new ErrorResponse(msg))
                .setContentTypeJson();
    }
New to GrepCode? Check out our FAQ X