Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source.
   * See the COPYRIGHT.txt file distributed with this work for information
   * regarding copyright ownership.  Some portions may be licensed
   * to Red Hat, Inc. under one or more contributor license agreements.
   *
   * This library is free software; you can redistribute it and/or
   * modify it under the terms of the GNU Lesser General Public
   * License as published by the Free Software Foundation; either
  * version 2.1 of the License, or (at your option) any later version.
  *
  * This library is distributed in the hope that it will be useful,
  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  * Lesser General Public License for more details.
  *
  * You should have received a copy of the GNU Lesser General Public
  * License along with this library; if not, write to the Free Software
  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  * 02110-1301 USA.
  */
 package org.teiid.olingo.web;
 
 import java.util.Map;
 
 
 
 public class ODataFilter implements FilterVDBLifeCycleListener {
 
     protected String proxyBaseURI;
     protected Properties initProperties;
     protected Map<VDBKeySoftReference<OlingoBridge>> contextMap = Collections
             .synchronizedMap(new LRUCache<VDBKeySoftReference<OlingoBridge>>());
     private volatile boolean listenerRegistered = false;
     
     @Override
     public void init(FilterConfig configthrows ServletException {
         // handle proxy-uri in the case of cloud environments
         String proxyURI = config.getInitParameter("proxy-base-uri"); //$NON-NLS-1$
         if (proxyURI != null && proxyURI.startsWith("${") && proxyURI.endsWith("}")) { //$NON-NLS-1$ //$NON-NLS-2$
             proxyURI = proxyURI.substring(2, proxyURI.length() - 1);
             proxyURI = System.getProperty(proxyURI);
         }
 
         if (proxyURI != null) {
             this. = proxyURI;
         }
 
         Properties props = new Properties();
         Enumeration<Stringnames = config.getInitParameterNames();
         while (names.hasMoreElements()) {
             String name = names.nextElement();
             props.setProperty(nameconfig.getInitParameter(name));
         }
         this. = props;
     }
 
     @Override
     public void doFilter(ServletRequest requestServletResponse response,
             FilterChain chainthrows IOExceptionServletException {
 
         HttpServletRequest httpRequest = (HttpServletRequestrequest;
 
         String proxyURI = this.;
         if (proxyURI != null) {
             httpRequest = new ProxyHttpServletRequest(httpRequestproxyURI);
         }
 
         VDBKey key = null;
         String vdbName = null;
         int version = 1;
         String modelName = null;
        String uri = ((HttpServletRequestrequest).getRequestURL().toString();
        int idx = uri.indexOf("/odata4/"); //$NON-NLS-1$
        if (idx != -1) {
            String contextPath = httpRequest.getContextPath();
            if (contextPath == null) {
                contextPath = "/odata4"//$NON-NLS-1$
            }
            int endIdx = uri.indexOf('/'idx + 8);
            if (endIdx == -1) {
                throw new ServletException(..gs(..));
            }
            vdbName = uri.substring(idx + 8, endIdx);
            int modelIdx = uri.indexOf('/'endIdx + 1);
            if (modelIdx == -1) {
                modelName = uri.substring(endIdx + 1).trim();
                if (modelName.isEmpty()) {
                    throw new ServletException(..gs(..));
                }
            } else {
                modelName = uri.substring(endIdx + 1, modelIdx);
            }
            contextPath = contextPath + "/" + vdbName + "/" + modelName//$NON-NLS-1$ //$NON-NLS-2$
            int versionIdx = vdbName.indexOf('.');
            if (versionIdx != -1) {
                version = Integer.parseInt(vdbName.substring(versionIdx + 1));
                vdbName = vdbName.substring(0, versionIdx);
            }
            vdbName = vdbName.trim();
            if (vdbName.isEmpty()) {
                throw new ServletException(..gs(..));
            }
            ContextAwareHttpSerlvetRequest contextAwareRequest = new ContextAwareHttpSerlvetRequest(httpRequest);
            contextAwareRequest.setContextPath(contextPath);
            httpRequest = contextAwareRequest;
        } else {
            if (this..getProperty("vdb-name") == null || 
                    this..getProperty("vdb-version") == null) { //$NON-NLS-1$ //$NON-NLS-2$
                throw new ServletException(..gs(..));
            }
            vdbName = this..getProperty("vdb-name"); //$NON-NLS-1$
            version = Integer.parseInt(this..getProperty("vdb-version")); //$NON-NLS-1$
            int modelIdx = uri.indexOf('/'uri.indexOf('/'));
            if (modelIdx == -1) {
                modelName = uri.substring(uri.indexOf('/') + 1).trim();
                if (modelName.isEmpty()) {
                    throw new ServletException(..gs(..));
                }
            }
            modelName = uri.substring(uri.indexOf('/'), uri.indexOf('/'uri.indexOf('/')));
        }
        
        key = new VDBKey(vdbNameversion);
        
        SoftReference<OlingoBridgeref = this..get(key);
        OlingoBridge context = null;
        if (ref != null) {
            context = ref.get();
        }
        
        if (context == null) {
            context = new OlingoBridge();
            ref = new SoftReference<OlingoBridge>(context);
            this..put(keyref);
        }
        
        Client client = buildClient(vdbNameversionthis.);
        try {
            Connection connection = client.open();
            registerVDBListener(clientconnection);
            ODataHttpHandler handler = context.getHandler(clientmodelName);
            httpRequest.setAttribute(ODataHttpHandler.class.getName(), handler);
            httpRequest.setAttribute(Client.class.getName(), client);
            chain.doFilter(httpRequestresponse);
        } catch(SQLException e) {
            throw new ServletException(e);
        } finally {
            try {
                client.close();
            } catch (SQLException e) {
                //ignore
            }
        }
    }
    
    private void registerVDBListener(Client clientConnection conn) {
        if (!this.) {
            synchronized (this) {
                if (!this.) {
                    if (client instanceof LocalClient) {
                        try {
                            ConnectionImpl connection = (ConnectionImpl)conn;
                            LocalServerConnection lsc = (LocalServerConnectionconnection.getServerConnection();
                            lsc.addListener(this);
                            this. = true;
                        } catch (SQLException e) {
                            LogManager.logWarning(...gs(..));
                        } 
                    }
                }
            }
        }
    }
    
    public Client buildClient(String vdbNameint versionProperties props) {
        return new LocalClient(vdbNameversionprops);        
    }
        
    @Override
    public void destroy() {
        this..clear();
    }
    @Override
    public void removed(String nameint versionCompositeVDB vdb) {
        this..remove(new VDBKey(nameversion));
    }
    @Override
    public void finishedDeployment(String nameint versionCompositeVDB vdb,
            boolean reloading) {
        this..remove(new VDBKey(nameversion));
    }
    @Override
    public void beforeRemove(String nameint versionCompositeVDB vdb) {
    }
    @Override
    public void added(String nameint versionCompositeVDB vdbboolean reloading) {
    }
New to GrepCode? Check out our FAQ X