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;
 
 import java.util.Map;
 
 
 
 public class ODataFilter implements FilterVDBLifeCycleListener {
 
     public static final String SCHEMA_NAME = "schema-name"//$NON-NLS-1$
     protected String proxyBaseURI;
     protected Properties initProperties;
     protected Map<VDBKeySoftReference<Client>> clientMap = Collections
             .synchronizedMap(new LRUCache<VDBKeySoftReference<Client>>());
     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 TeiidRuntimeException(..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 TeiidRuntimeException(..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 TeiidRuntimeException(..gs(..));
            }
            ContextAwareHttpSerlvetRequest contextAwareRequest = new ContextAwareHttpSerlvetRequest(httpRequest);
            contextAwareRequest.setContextPath(contextPath);
            httpRequest = contextAwareRequest;
            key = new VDBKey(vdbNameversion);
        } 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 TeiidRuntimeException(..gs(..));
                }
            }
            modelName = uri.substring(uri.indexOf('/'), uri.indexOf('/'uri.indexOf('/')));
        }
        SoftReference<Clientref = this..get(key);
        Client client = null;
        if (ref != null) {
            client = ref.get();
        }
        if (client == null) {
            client = buildClient(vdbNameversionthis.);
            if (!this.) {
                synchronized (this) {
                    if (!this.) {
                        ConnectionImpl connection = null;
                        if (client instanceof LocalClient) {
                            try {
                                connection = ((LocalClientclient).getConnection();
                                LocalServerConnection lsc = (LocalServerConnectionconnection.getServerConnection();
                                lsc.addListener(this);
                                this. = true;
                            } catch (SQLException e) {
                                LogManager.logWarning(.,
                                        ..gs(..));
                            } finally {
                                if (connection != null) {
                                    try {
                                        connection.close();
                                    } catch (SQLException e) {
                                    }
                                }
                            }
                        }
                    }
                }
            }
            ref = new SoftReference<Client>(client);
            this..put(keyref);
        }
        httpRequest.setAttribute(Client.class.getName(), client);
        httpRequest.setAttribute(modelName);
        chain.doFilter(httpRequestresponse);
    }
    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