Search in sources :

Example 1 with Client

use of org.teiid.odata.api.Client in project teiid by teiid.

the class TestODataSQLBuilder method setup.

private BaseState setup(String ddl, String url, String method, ServletInputStream stream, BaseState state) throws Exception {
    Client client = Mockito.mock(Client.class);
    DDLHolder model = new DDLHolder("PM1", ddl);
    TransformationMetadata metadata = RealMetadataFactory.fromDDL("vdb", model);
    MetadataStore store = metadata.getMetadataStore();
    // TranslationUtility utility = new TranslationUtility(metadata);
    OData odata = OData4Impl.newInstance();
    org.teiid.metadata.Schema teiidSchema = store.getSchema("PM1");
    CsdlSchema schema = ODataSchemaBuilder.buildMetadata("vdb", teiidSchema);
    TeiidEdmProvider edmProvider = new TeiidEdmProvider("baseuri", schema, "x");
    ServiceMetadata serviceMetadata = odata.createServiceMetadata(edmProvider, Collections.<EdmxReference>emptyList());
    ODataHttpHandler handler = odata.createHandler(serviceMetadata);
    Hashtable<String, String> headers = new Hashtable<String, String>();
    headers.put("Content-Type", "application/json");
    Mockito.stub(client.getMetadataStore()).toReturn(store);
    Mockito.stub(client.executeCount(Mockito.any(Query.class), Mockito.anyListOf(SQLParameter.class))).toReturn(new CountResponse() {

        @Override
        public int getCount() {
            return 10;
        }
    });
    HttpServletRequest request = Mockito.mock(HttpServletRequest.class);
    Mockito.stub(request.getHeaderNames()).toReturn(headers.keys());
    Mockito.stub(request.getHeaders("Content-Type")).toReturn(headers.elements());
    Mockito.stub(request.getMethod()).toReturn(method);
    String requestURL = url;
    String queryString = "";
    int idx = url.indexOf("?");
    if (idx != -1) {
        requestURL = url.substring(0, idx);
        queryString = url.substring(idx + 1);
    }
    Mockito.stub(request.getRequestURL()).toReturn(new StringBuffer(requestURL));
    Mockito.stub(request.getQueryString()).toReturn(queryString);
    Mockito.stub(request.getServletPath()).toReturn("");
    Mockito.stub(request.getContextPath()).toReturn("/odata4/vdb/PM1");
    Mockito.stub(request.getInputStream()).toReturn(stream);
    final StringBuffer sb = new StringBuffer();
    ServletOutputStream out = new ServletOutputStream() {

        @Override
        public void write(int b) throws IOException {
            sb.append((char) b);
        }

        @Override
        public boolean isReady() {
            return true;
        }

        @Override
        public void setWriteListener(WriteListener writeListener) {
        }
    };
    HttpServletResponse response = Mockito.mock(HttpServletResponse.class);
    Mockito.stub(response.getOutputStream()).toReturn(out);
    try {
        TeiidServiceHandler tsh = new TeiidServiceHandler("PM1");
        tsh.setPrepared(false);
        TeiidServiceHandler.setClient(client);
        handler.register(tsh);
        handler.process(request, response);
    } finally {
        TeiidServiceHandler.setClient(null);
    }
    ArgumentCaptor<Integer> statusCapture = ArgumentCaptor.forClass(Integer.class);
    Mockito.verify(response).setStatus(statusCapture.capture());
    state.client = client;
    state.response = sb.toString();
    state.status = statusCapture.getValue();
    return state;
}
Also used : Query(org.teiid.query.sql.lang.Query) ServletOutputStream(javax.servlet.ServletOutputStream) CsdlSchema(org.apache.olingo.commons.api.edm.provider.CsdlSchema) HttpServletRequest(javax.servlet.http.HttpServletRequest) TeiidEdmProvider(org.teiid.olingo.service.TeiidEdmProvider) DDLHolder(org.teiid.query.unittest.RealMetadataFactory.DDLHolder) Client(org.teiid.odata.api.Client) WriteListener(javax.servlet.WriteListener) OData(org.apache.olingo.server.api.OData) TransformationMetadata(org.teiid.query.metadata.TransformationMetadata) Hashtable(java.util.Hashtable) SQLParameter(org.teiid.odata.api.SQLParameter) CountResponse(org.teiid.odata.api.CountResponse) HttpServletResponse(javax.servlet.http.HttpServletResponse) ODataHttpHandler(org.apache.olingo.server.api.ODataHttpHandler) MetadataStore(org.teiid.metadata.MetadataStore) TeiidServiceHandler(org.teiid.olingo.service.TeiidServiceHandler) ServiceMetadata(org.apache.olingo.server.api.ServiceMetadata)

Example 2 with Client

use of org.teiid.odata.api.Client 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)

Example 3 with Client

use of org.teiid.odata.api.Client in project teiid by teiid.

the class ODataServlet method service.

@Override
public void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
    ODataHttpHandler handler = (ODataHttpHandler) request.getAttribute(ODataHttpHandler.class.getName());
    Client client = (Client) request.getAttribute(Client.class.getName());
    try {
        TeiidServiceHandler.setClient(client);
        handler.process(request, response);
    } finally {
        TeiidServiceHandler.setClient(null);
    }
}
Also used : Client(org.teiid.odata.api.Client) ODataHttpHandler(org.apache.olingo.server.api.ODataHttpHandler)

Example 4 with Client

use of org.teiid.odata.api.Client in project teiid by teiid.

the class TestODataSQLBuilder method helpTest.

public QueryState helpTest(String url, String sqlExpected, Integer skip, Integer top, Boolean count) throws Exception {
    QueryState state = setup(url);
    Client client = state.client;
    ArgumentCaptor<Query> arg1 = ArgumentCaptor.forClass(Query.class);
    ArgumentCaptor<EntityCollectionResponse> arg6 = ArgumentCaptor.forClass(EntityCollectionResponse.class);
    List<SQLParameter> parameters = new ArrayList<SQLParameter>();
    if (sqlExpected != null) {
        Query actualCommand = (Query) QueryParser.getQueryParser().parseCommand(sqlExpected, new ParseInfo());
        Mockito.verify(client).executeSQL(arg1.capture(), Mockito.eq(parameters), Mockito.eq(count), (Integer) Mockito.eq(skip), (Integer) Mockito.eq(top), (String) Mockito.eq(null), Mockito.anyInt(), arg6.capture());
        Assert.assertEquals(actualCommand.toString(), arg1.getValue().toString());
    }
    state.parameters = parameters;
    state.arg1 = arg1;
    state.arg6 = arg6;
    return state;
}
Also used : Query(org.teiid.query.sql.lang.Query) EntityCollectionResponse(org.teiid.olingo.service.EntityCollectionResponse) SQLParameter(org.teiid.odata.api.SQLParameter) ArrayList(java.util.ArrayList) ParseInfo(org.teiid.query.parser.ParseInfo) Client(org.teiid.odata.api.Client)

Example 5 with Client

use of org.teiid.odata.api.Client in project teiid by teiid.

the class TestODataSQLBuilder method helpInsert.

private UpdateState helpInsert(String url, String sqlExpected, StringServletInputStream stream, String method) throws Exception {
    UpdateState state = (UpdateState) setup(DEFAULT_DDL, url, method, stream, new UpdateState());
    Client client = state.client;
    ArgumentCaptor<Command> arg1 = ArgumentCaptor.forClass(Command.class);
    ArgumentCaptor<List> arg2 = ArgumentCaptor.forClass(List.class);
    if (sqlExpected != null) {
        Command actualCommand = (Command) QueryParser.getQueryParser().parseCommand(sqlExpected, new ParseInfo());
        Mockito.verify(client).executeUpdate(arg1.capture(), arg2.capture());
        Assert.assertEquals(actualCommand.toString(), arg1.getValue().toString());
    }
    state.commandArg = arg1;
    return state;
}
Also used : Command(org.teiid.query.sql.lang.Command) ArrayList(java.util.ArrayList) List(java.util.List) ParseInfo(org.teiid.query.parser.ParseInfo) Client(org.teiid.odata.api.Client)

Aggregations

Client (org.teiid.odata.api.Client)5 ODataHttpHandler (org.apache.olingo.server.api.ODataHttpHandler)3 ArrayList (java.util.ArrayList)2 HttpServletRequest (javax.servlet.http.HttpServletRequest)2 SQLParameter (org.teiid.odata.api.SQLParameter)2 ParseInfo (org.teiid.query.parser.ParseInfo)2 Query (org.teiid.query.sql.lang.Query)2 Connection (java.sql.Connection)1 SQLException (java.sql.SQLException)1 Hashtable (java.util.Hashtable)1 List (java.util.List)1 ServletOutputStream (javax.servlet.ServletOutputStream)1 WriteListener (javax.servlet.WriteListener)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 CsdlSchema (org.apache.olingo.commons.api.edm.provider.CsdlSchema)1 OData (org.apache.olingo.server.api.OData)1 ServiceMetadata (org.apache.olingo.server.api.ServiceMetadata)1 TeiidProcessingException (org.teiid.core.TeiidProcessingException)1 MetadataStore (org.teiid.metadata.MetadataStore)1 CountResponse (org.teiid.odata.api.CountResponse)1