Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  package com.dottydingo.service.endpoint.context;
  
 
 import java.util.Set;
 import java.util.UUID;
 
 public abstract class AbstractContextBuilder<C extends EndpointContext,REQ extends EndpointRequest,RES extends EndpointResponse>
 {
     private AtomicLong counter = new AtomicLong(0);
     protected TraceFactory traceFactory;
     protected CompletionHandler<C> completionHandler;
 
     public void setEndpointConfiguration(EndpointConfiguration endpointConfiguration)
     {
         this. = endpointConfiguration;
     }
 
     public void setTraceFactory(TraceFactory traceFactory)
     {
         this. = traceFactory;
     }
 
     public void setCompletionHandler(CompletionHandler<C> completionHandler)
     {
         this. = completionHandler;
     }
 
     public C buildContext(HttpServletRequest httpServletRequestHttpServletResponse httpServletResponse)
             throws IOException
     {
         C context = createContextInstance();
         context.setRequestId(.incrementAndGet());
 
         REQ request = createRequestInstance();
         setupRequest(httpServletRequest,request);
         request.setInputStream(wrapInputStream(httpServletRequest.getInputStream()));
 
         RES response = createResponseInstance();
         setupResponse(httpServletResponseresponse);
         response.setOutputStream(wrapOutputStream(httpServletResponse.getOutputStream()));
 
         context.setEndpointRequest(request);
         context.setEndpointResponse(response);
 
         // set or assign a correlation ID
         context.setCorrelationId(getCorrelationId(request));
 
         // set the correlation ID on the response
 
         // create a trace if requested
         context.setTrace(getTrace(request,context.getCorrelationId()));
 
         context.setCompletionHandler();
 
         return context;
     }
 
     public C buildErrorContext(HttpServletRequest httpServletRequestHttpServletResponse httpServletResponse)
 
     {
         C context = createContextInstance();
         context.setRequestId(.incrementAndGet());
 
         REQ request = createRequestInstance();
         try
         {
             setupRequest(httpServletRequest,request);
         }
         catch (Throwable ignore){}
 
         RES response = createResponseInstance();
 
         try
         {
             setupResponse(httpServletResponseresponse);
         }
         catch (Throwable ignore){}
 
         context.setEndpointRequest(request);
         context.setEndpointResponse(response);
 
        // set or assign a correlation ID
        context.setCorrelationId(getCorrelationId(request));
        // create a trace if requested
        try
        {
            context.setTrace(getTrace(request,context.getCorrelationId()));
        }
        catch (Throwable ignore){}
        return context;
    }
    protected abstract C createContextInstance();
    protected abstract RES createResponseInstance();
    protected abstract REQ createRequestInstance();
    protected void setupResponse(HttpServletResponse httpServletResponse, RES response)
    {
        response.setHttpServletResponse(httpServletResponse);
            response.setHeader("Connection""close");
    }
    protected void setupRequest(HttpServletRequest httpServletRequest, REQ request)
    {
        request.setHttpServletRequest(httpServletRequest);
        Enumeration<StringheaderNames = httpServletRequest.getHeaderNames();
        while (headerNames.hasMoreElements())
        {
            String name = headerNames.nextElement();
            request.addHeader(name,httpServletRequest.getHeader(name));
        }
        Enumeration<StringparameterNames = httpServletRequest.getParameterNames();
        while (parameterNames.hasMoreElements())
        {
            String name = parameterNames.nextElement();
            request.addParameter(namehttpServletRequest.getParameter(name));
        }
        request.setRequestMethod(httpServletRequest.getMethod());
        request.setContentType(httpServletRequest.getContentType());
        request.setRequestUrl(httpServletRequest.getRequestURL().toString());
        request.setQueryString(httpServletRequest.getQueryString());
        request.setRequestUri(httpServletRequest.getRequestURI());
        request.setBaseUrl(getBaseUrl(httpServletRequest));
        request.setAuthType(httpServletRequest.getAuthType());
        request.setRemoteAddress(httpServletRequest.getRemoteAddr());
        request.setServerName(httpServletRequest.getServerName());
    }
    protected InputStream wrapInputStream(InputStream inputStream)
    {
        return new BufferingInputStreamWrapper(inputStream,.getMaxRequestBodySize(),true);
    }
    protected OutputStream wrapOutputStream(OutputStream outputStream)
    {
        return new SizeTrackingOutputStream(outputStream,true);
    }
    protected String getCorrelationId(REQ request)
    {
        if(correlationId == null || correlationId.length()==0)
            correlationId = request.getFirstHeader(.getCorrelationIdHeaderName());
        if(correlationId == null || correlationId.length()==0)
            correlationId = UUID.randomUUID().toString();
        return correlationId;
    }
    protected Trace getTrace(REQ request,String correlationId)
    {
        if(!.isAllowTrace())
            return null;
        String traceString = request.getFirstParameter(.getTraceParameterName());
        if(traceString == null || traceString.length() ==0)
            traceString = request.getFirstHeader(.getTraceHeaderName());
        if(traceString == null || traceString.length() == 0)
            return null;
        String[] split = traceString.split(":");
        TraceType traceType = null;
        try
        {
            traceType = TraceType.valueOf(split[0]);
        }
        catch (IllegalArgumentException e)
        {
            return null// todo should this throw an exception?
        }
        switch (traceType)
        {
            case :
            {
                if(split.length != 2)
                    return null;  // todo should this throw an exception?
                Set<StringrestrictedTraceDomains = .getRestrictedTraceDomains();
                if(restrictedTraceDomains != null && restrictedTraceDomains.size() > 0)
                {
                    if(!restrictedTraceDomains.contains(split[1]))
                        return null// not allowed. todo should this throw an exception?
                }
                return .createTrace(traceType,split[1]);
            }
            case :
                return .createTrace(traceType,correlationId);
        }
        return null;
    }
    protected String getBaseUrl(HttpServletRequest request)
    {
        StringBuffer url = request.getRequestURL();
        String requestUri = request.getRequestURI();
        int pos = url.indexOf(requestUri);
        if(pos < 0)
            return url.toString();
        return url.substring(0,pos + request.getContextPath().length());
    }
New to GrepCode? Check out our FAQ X