Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.fasterxml.clustermate.service.servlet;
  
  
  
  import org.slf4j.Logger;
 
 
 @SuppressWarnings("serial")
 public class ServletBase extends HttpServlet
 {
     protected final Logger LOG = LoggerFactory.getLogger(getClass());
 
     protected final ClusterViewByServer _clusterView;
 
     protected final String _basePath;
    
    

Parameters:
clusterView Handler to cluster information; needed to handling piggy-backed cluster state information
servletPathBase (optional) Server base path that is to be ignored when matching request paths: if null, uses method getServletPath of HttpServletRequest.
 
     protected ServletBase(ClusterViewByServer clusterView,
             String servletPathBase)
     {
          = clusterView;
         if (servletPathBase != null) {
             /* One tweak: ensure that base path ends with slash, to ensure
              * we are not left with leading slash in path passed.
              * Except in case of empty base.
              */
             if (!servletPathBase.endsWith("/") && servletPathBase.length() > 0) {
                 servletPathBase += "/";
             }
         }
          = servletPathBase;
     }
 
     protected ClusterViewByServer getClusterView() { return ; }
     
     /*
     /**********************************************************************
     /* REST operation end points
     /**********************************************************************
      */
 
     @Override
     public final void doGet(HttpServletRequest req0HttpServletResponse resp0throws IOException
     {
         handleGet(constructRequest(req0), constructResponse(resp0), constructMetadata());
     }
 
     @Override
     public final void doHead(HttpServletRequest req0HttpServletResponse resp0throws IOException
     {
         handleHead(constructRequest(req0), constructResponse(resp0), constructMetadata());
     }
     
     @Override
     public final void doPost(HttpServletRequest req0HttpServletResponse resp0throws IOException
     {
         handlePost(constructRequest(req0), constructResponse(resp0), constructMetadata());
     }
     
     @Override
     public final void doPut(HttpServletRequest req0HttpServletResponse resp0throws IOException
     {
         handlePut(constructRequest(req0), constructResponse(resp0), constructMetadata());
     }
 
     @Override
     public final void doDelete(HttpServletRequest req0HttpServletResponse resp0throws IOException
     {
     }
 
     /*
     /**********************************************************************
     /* Overridable methods
     /**********************************************************************
      */
 
     public void handleGet(ServletServiceRequest requestServletServiceResponse response,
             OperationDiagnostics stats)
         throws IOException
     {
         response = response.badMethod()
                 .setContentTypeText().setEntity("No GET available for endpoint");
         // no need to track bytes returned since it's not real payload
         response.writeOut(null);
    }
    public void handleHead(ServletServiceRequest requestServletServiceResponse response,
            OperationDiagnostics stats)
        throws IOException { }
    public void handlePut(ServletServiceRequest requestServletServiceResponse response,
            OperationDiagnostics stats)
        throws IOException { }
    public void handlePost(ServletServiceRequest requestServletServiceResponse response,
            OperationDiagnostics stats)
        throws IOException { }
    public void handleDelete(ServletServiceRequest requestServletServiceResponse response,
            OperationDiagnostics stats)
        throws IOException { }
    /*
    /**********************************************************************
    /* Helper methods, overridable
    /**********************************************************************
     */
    {
        String path = orig.getRequestURI();
        
        /* First things first: remove servlet path prefix (either override,
         * or one used for registration)
         */
        if ( == null) { // no base path; use whatever it was registered with
            path = _trimPath(pathorig.getServletPath(), true);
        } else {
            path = _trimPath(pathfalse);
        }
        // false -> path not yet URL decoded
        return new ServletServiceRequest(origpathfalse);
    }

    
Overridable factory method that is used for creating optional com.fasterxml.clustermate.service.OperationDiagnostics object to pass through, to collect statistics.
        return null;
    }
    
        return new ServletServiceResponse(orig);
    }
    
    /*
    /**********************************************************************
    /* Helper methods, other
    /**********************************************************************
     */
    protected String _trimPath(String originalPathString expectedPrefix,
            boolean checkSlash)
    {
        int prefixLength = expectedPrefix.length();
        if (prefixLength == 0) {
            return originalPath;
        }
        if (!originalPath.startsWith(expectedPrefix)) {
            .warn("Unexpected Servlet path to trim '{}'; does not start with '{}'; passing as-is",
                    originalPathexpectedPrefix);
            return originalPath;
        }
        // need to trim one char, slash?
        if (checkSlash
                && (originalPath.length() > prefixLength)
                && (expectedPrefix.charAt(prefixLength-1) != '/')
                && (originalPath.charAt(prefixLength) == '/')
        ) {
            ++prefixLength;
        }
        return originalPath.substring(prefixLength);
    }
    
    protected ServiceResponse _addStdHeaders(ServiceResponse response)
    {
        if ( != null) {
            response = .addClusterStateInfo(response);
        }
        return response;
    }
New to GrepCode? Check out our FAQ X