Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.service;
  
  import  com.fasterxml.clustermate.service.ServiceResponse;
Interface class that defines interface of (HTTP) Responses server returns to caller.

Separated out to allow handlers to operate independent of the container like Servlet or JAX-RS container.

 
 public abstract class ServiceResponse
 {
    
Raw entity to serialize and return, if any.

Note that either this OR _streamingContent can be non-null.

 
     protected Object _entity;

    
Content to write out, if any.

Note that either this OR _entity can be non-null.

 
 
     /*
     /**********************************************************************
     /* Statistics
     /**********************************************************************
      */

    
Accessor for number of bytes written through this response.
 
     public abstract long getBytesWritten();
     
     /*
     /**********************************************************************
     /* Low-level response building
     /**********************************************************************
      */
     
     public abstract <RESP extends ServiceResponse> RESP  set(int codeObject entity);
 
     public abstract <RESP extends ServiceResponse> RESP  setStatus(int code);
     
     public abstract ServiceResponse addHeader(String keyString value);
 
     public abstract ServiceResponse addHeader(String keyint value);
 
     public abstract ServiceResponse addHeader(String keylong value);
    
    
Method for specifying POJO to serialize as content of response; either as streaming content (if entity is of type com.fasterxml.clustermate.service.msg.StreamingResponseContent); or as something to serialize using default serialization mechanism (usually JSON).
 
     @SuppressWarnings("unchecked")
     public final <RESP extends ServiceResponse> RESP setEntity(Object e)
     {
         if (e instanceof StreamingResponseContent) {
              = null;
              = (StreamingResponseContente;
         } else {
              = e;
              = null;
         }
         return (RESP) this;
     }
     
     /*
     /**********************************************************************
     /* Basic accessors
     /**********************************************************************
      */
 
     public abstract int getStatus();
 
     public final boolean isError() { return getStatus() >= 300; }
 
     public final boolean hasEntity() { return  != null; }
     public final boolean hasStreamingContent() { return  != null; }
 
     @SuppressWarnings("unchecked")
     public final <T> T getEntity() {
         return (T) ;
     }
     
     /*
     /**********************************************************************
     /* High(er)-level response building; semantic headers
     /**********************************************************************
      */
 
    public abstract ServiceResponse setContentType(String contentType);
        return setContentType("application/json");
    }
        return setContentType("text/plain");
    }
    
    public final ServiceResponse setBodyCompression(String type) {
    }
    
    public abstract ServiceResponse setContentLength(long length);
    /*
    /**********************************************************************
    /* High(er)-level response building; ok cases
    /**********************************************************************
     */
    
    public final ServiceResponse ok() {
    }
    public final ServiceResponse ok(Object entity) {
        return ok().setEntity(entity);
    }
    public final ServiceResponse ok(String contentTypeObject entity) {
        return ok().setContentType(contentType).setEntity(entity);
    }
    
    public final ServiceResponse noContent() {
        return setStatus(204);
    }
    public final ServiceResponse partialContent(Object entityString rangeDesc) {
        // 206 means "partial content"
        return set(.entity)
                .addHeader(.rangeDesc);
    }
    /*
    /**********************************************************************
    /* High(er)-level response building; error cases
    /**********************************************************************
     */
    public final <RESP extends ServiceResponse> RESP  notChanged() {
        return setStatus(304);
    }
    
    public final <RESP extends ServiceResponse> RESP badMethod() {
        // Method Not Allowed
        return setStatus(405);
    }
    
    public final <RESP extends ServiceResponse> RESP  badRange(Object entity) {
        // 416 is used for invalid Range requests
        return set(416, entity);
    }
    public final <RESP extends ServiceResponse> RESP  badRequest(Object entity) {
        return set(400, entity);
    }
    public final <RESP extends ServiceResponse> RESP  conflict(Object entity) {
        return set(409, entity);
    }
    public final <RESP extends ServiceResponse> RESP  gone(Object entity) {
        return set(410, entity);
    }
    
    public final <RESP extends ServiceResponse> RESP  notFound() {
        return setStatus(404);
    }
    
    public final <RESP extends ServiceResponse> RESP  notFound(Object entity) {
        return set(404, entity);
    }
    public final <RESP extends ServiceResponse> RESP  internalError(Object entity) {
        return set(500, entity);
    }

    
Couple of choices here, but use 504 to distinguish from "unknown" 500 problem
    public final <RESP extends ServiceResponse> RESP serviceTimeout(Object entity) {
        return set(504, entity);
    }
New to GrepCode? Check out our FAQ X