Search in sources :

Example 1 with OlingoBridge

use of org.teiid.olingo.service.OlingoBridge in project teiid by teiid.

the class ODataFilter method internalDoFilter.

public void internalDoFilter(ServletRequest request, ServletResponse response, FilterChain chain) throws IOException, ServletException, TeiidProcessingException {
    HttpServletRequest httpRequest = (HttpServletRequest) request;
    String proxyURI = this.proxyBaseURI;
    if (proxyURI != null) {
        httpRequest = new ProxyHttpServletRequest(httpRequest, proxyURI);
    }
    VDBKey key = null;
    String vdbName = null;
    String version = null;
    String modelName = null;
    String uri = ((HttpServletRequest) request).getRequestURI().toString();
    String fullURL = ((HttpServletRequest) request).getRequestURL().toString();
    if (uri.startsWith("/odata4/static/") || uri.startsWith("/odata4/keycloak/")) {
        // $NON-NLS-1$ //$NON-NLS-2$
        chain.doFilter(httpRequest, response);
        return;
    }
    String contextPath = httpRequest.getContextPath();
    String baseURI = fullURL.substring(0, fullURL.indexOf(contextPath));
    int endIdx = uri.indexOf('/', contextPath.length() + 1);
    int beginIdx = contextPath.length() + 1;
    if (contextPath.equals("/odata4")) {
        // $NON-NLS-1$
        if (endIdx == -1) {
            throw new TeiidProcessingException(ODataPlugin.Event.TEIID16020, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16020));
        }
        // $NON-NLS-1$
        baseURI = baseURI + "/odata4";
        vdbName = uri.substring(beginIdx, endIdx);
        int modelIdx = uri.indexOf('/', endIdx + 1);
        if (modelIdx == -1) {
            modelName = uri.substring(endIdx + 1).trim();
            if (modelName.isEmpty()) {
                throw new TeiidProcessingException(ODataPlugin.Event.TEIID16019, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16019));
            }
        } else {
            modelName = uri.substring(endIdx + 1, modelIdx);
        }
        // $NON-NLS-1$ //$NON-NLS-2$
        contextPath = contextPath + "/" + vdbName + "/" + modelName;
        vdbName = vdbName.trim();
        if (vdbName.isEmpty()) {
            throw new TeiidProcessingException(ODataPlugin.Event.TEIID16008, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16008));
        }
    } else {
        if (this.initProperties.getProperty("vdb-name") == null) {
            // $NON-NLS-1$
            throw new TeiidProcessingException(ODataPlugin.Event.TEIID16018, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16018));
        }
        // $NON-NLS-1$
        vdbName = this.initProperties.getProperty("vdb-name");
        // $NON-NLS-1$
        version = this.initProperties.getProperty("vdb-version");
        if (endIdx == -1) {
            modelName = uri.substring(beginIdx).trim();
            if (modelName.isEmpty()) {
                throw new TeiidProcessingException(ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16021));
            }
        } else {
            modelName = uri.substring(beginIdx, endIdx);
        }
        // $NON-NLS-1$
        contextPath = contextPath + "/" + modelName;
    }
    ContextAwareHttpSerlvetRequest contextAwareRequest = new ContextAwareHttpSerlvetRequest(httpRequest);
    contextAwareRequest.setContextPath(contextPath);
    httpRequest = contextAwareRequest;
    key = new VDBKey(vdbName, version);
    if (key.isAtMost()) {
        if (key.getVersion() != null) {
            throw new TeiidProcessingException(ODataPlugin.Event.TEIID16044, ODataPlugin.Util.gs(ODataPlugin.Event.TEIID16044, key));
        }
        // $NON-NLS-1$ //legacy behavior, default to version 1
        key = new VDBKey(vdbName, "1");
    }
    SoftReference<OlingoBridge> ref = this.contextMap.get(key);
    OlingoBridge context = null;
    if (ref != null) {
        context = ref.get();
    }
    if (context == null) {
        context = new OlingoBridge();
        ref = new SoftReference<OlingoBridge>(context);
        this.contextMap.put(key, ref);
    }
    Client client = buildClient(key.getName(), key.getVersion(), this.initProperties);
    try {
        Connection connection = client.open();
        registerVDBListener(client, connection);
        ODataHttpHandler handler = context.getHandler(baseURI, client, modelName);
        httpRequest.setAttribute(ODataHttpHandler.class.getName(), handler);
        httpRequest.setAttribute(Client.class.getName(), client);
        chain.doFilter(httpRequest, response);
    } catch (SQLException e) {
        throw new TeiidProcessingException(e);
    } finally {
        try {
            client.close();
        } catch (SQLException e) {
        // ignore
        }
    }
}
Also used : SQLException(java.sql.SQLException) Connection(java.sql.Connection) LocalServerConnection(org.teiid.transport.LocalServerConnection) OlingoBridge(org.teiid.olingo.service.OlingoBridge) ODataHttpHandler(org.apache.olingo.server.api.ODataHttpHandler) TeiidProcessingException(org.teiid.core.TeiidProcessingException) HttpServletRequest(javax.servlet.http.HttpServletRequest) VDBKey(org.teiid.vdb.runtime.VDBKey) Client(org.teiid.odata.api.Client) LocalClient(org.teiid.olingo.service.LocalClient)

Aggregations

Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 ODataHttpHandler (org.apache.olingo.server.api.ODataHttpHandler)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 Client (org.teiid.odata.api.Client)1 LocalClient (org.teiid.olingo.service.LocalClient)1 OlingoBridge (org.teiid.olingo.service.OlingoBridge)1 LocalServerConnection (org.teiid.transport.LocalServerConnection)1 VDBKey (org.teiid.vdb.runtime.VDBKey)1