Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
 package org.kie.remote.services.rest.jaxb;
 
 
 import  javax.servlet.Filter;
 import  javax.servlet.FilterChain;
 import  javax.servlet.FilterConfig;
 import  javax.servlet.ServletException;
 import  javax.servlet.ServletRequest;
import  javax.servlet.ServletResponse;
import  javax.servlet.http.HttpServletRequest;
public class DynamicJaxbContextFilter implements Filter {
    // private FilterConfig _filterConfig; // not used
  
    // "**" not accepted in URL's.. 
    public static final String DEFAULT_JAXB_CONTEXT_ID = "**DEFAULT";
    
    public void init(FilterConfig filterConfigthrows ServletException {
        // do nothing
    }
    public void doFilter(ServletRequest request, ServletResponse response, FilterChain chainthrows IOException, ServletException {
        HttpServletRequest httpRequest = (HttpServletRequest) request;
        String deploymentId = getDeploymentId(httpRequest);
        DynamicJaxbContext.setDeploymentJaxbContext(deploymentId);
        try { 
            chain.doFilter(requestresponse);
        } finally { 
            DynamicJaxbContext.clearDeploymentJaxbContext();
        }
    }
    @Override
    public void destroy() {
        // _filterConfig = null; // not used
    }
    // pkg static for tests
    static String getDeploymentId(HttpServletRequest request) {
        String deploymentId = null;
       
        // extract from the proper url
        String requestUri = request.getRequestURI();
        String [] urlParts = requestUri.split("/");
        forint i = 0; i < urlParts.length; ++i ) { 
           ifurlParts[i].equals("deployment")
               || urlParts[i].equals("runtime") ) { 
              ifi+1 < urlParts.length ) { 
                  deploymentId = urlParts[i+1];
                  break;
              }
           }
        }
        
        // get parameter
        ifdeploymentId == null ) { 
            deploymentId = request.getParameter("deploymentId");
        }
        // default id
        ifdeploymentId == null ) { 
           return 
        }
        
        return deploymentId;
    } 
New to GrepCode? Check out our FAQ X