Search in sources :

Example 6 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method crossJoin.

@Override
public void crossJoin(DataRequest request, List<String> entitySetNames, ODataResponse response) throws ODataLibraryException, ODataApplicationException {
    final ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.visit(request.getUriInfo());
    try {
        Query query = visitor.selectQuery();
        BaseResponse queryResponse = executeQuery(request, request.isCountRequest(), visitor, query);
        ContextURL.Builder builder = new ContextURL.Builder().asCollection().entitySetOrSingletonOrType("Edm.ComplexType");
        EdmComplexResponse complexResponse = EdmComplexResponse.getInstance(request, builder.build(), false, response);
        sendResults(request, visitor, queryResponse, complexResponse);
    } catch (Exception e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
}
Also used : BaseResponse(org.teiid.odata.api.BaseResponse) ContextURL(org.apache.olingo.commons.api.data.ContextURL) Query(org.teiid.query.sql.lang.Query) URISyntaxException(java.net.URISyntaxException) TransformationException(org.teiid.core.types.TransformationException) TeiidProcessingException(org.teiid.core.TeiidProcessingException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) TeiidException(org.teiid.core.TeiidException) EdmPrimitiveTypeException(org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException) SerializerException(org.apache.olingo.server.api.serializer.SerializerException) SQLException(java.sql.SQLException) MalformedURLException(java.net.MalformedURLException) TeiidRuntimeException(org.teiid.core.TeiidRuntimeException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) EdmComplexResponse(org.teiid.olingo.EdmComplexResponse)

Example 7 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method read.

@Override
public <T extends ServiceResponse> void read(final DataRequest request, T response) throws ODataLibraryException, ODataApplicationException {
    final ODataSQLBuilder visitor = new ODataSQLBuilder(odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.visit(request.getUriInfo());
    final BaseResponse queryResponse;
    try {
        Query query = visitor.selectQuery();
        queryResponse = executeQuery(request, request.isCountRequest(), visitor, query);
    } catch (Throwable e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    response.accepts(new ServiceResponseVisior() {

        public void visit(CountResponse response) throws ODataLibraryException, ODataApplicationException {
            org.teiid.odata.api.CountResponse cr = (org.teiid.odata.api.CountResponse) queryResponse;
            response.writeCount(cr.getCount());
        }

        public void visit(PrimitiveValueResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (!entitySet.getEntities().isEmpty()) {
                Entity entity = entitySet.getEntities().get(0);
                EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
                Property property = entity.getProperty(edmProperty.getName());
                if (property == null) {
                    response.writeNotFound(true);
                } else if (property.getValue() == null) {
                    response.writeNoContent(true);
                } else {
                    response.write(property.getValue());
                }
            } else {
                response.writeNotFound(true);
            }
        }

        public void visit(PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (!entitySet.getEntities().isEmpty()) {
                Entity entity = entitySet.getEntities().get(0);
                EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
                Property property = entity.getProperty(edmProperty.getName());
                response.writeProperty(edmProperty.getType(), property);
            } else {
                response.writeNotFound(true);
            }
        }

        public void visit(StreamResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollectionResponse entitySet = (EntityCollectionResponse) queryResponse;
            EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
            Object value = entitySet.getStream(edmProperty.getName());
            if (value == null) {
                response.writeNoContent(true);
            } else {
                try {
                    handleLobResult(getClient().getProperty(Client.CHARSET), value, response);
                } catch (SQLException e) {
                    LogManager.logDetail(LogConstants.CTX_ODATA, e);
                    response.writeServerError(true);
                }
            }
        }

        public void visit(EntityResponse response) throws ODataLibraryException, ODataApplicationException {
            EntityCollection entitySet = (EntityCollection) queryResponse;
            if (entitySet.getEntities().isEmpty()) {
                if (visitor.hasNavigation()) {
                    response.writeNoContent(true);
                } else {
                    response.writeNotFound(true);
                }
            } else {
                response.writeReadEntity(visitor.getContext().getEdmEntityType(), entitySet.getEntities().get(0));
            }
        }

        public void visit(EntitySetResponse response) throws ODataLibraryException, ODataApplicationException {
            sendResults(request, visitor, queryResponse, response);
        }
    });
}
Also used : Entity(org.apache.olingo.commons.api.data.Entity) Query(org.teiid.query.sql.lang.Query) SQLException(java.sql.SQLException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) BaseResponse(org.teiid.odata.api.BaseResponse) EntityCollection(org.apache.olingo.commons.api.data.EntityCollection) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty) Property(org.apache.olingo.commons.api.data.Property) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

Example 8 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method updateEntity.

@Override
public void updateEntity(DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response) throws ODataLibraryException, ODataApplicationException {
    // TODO: need to match entityETag.
    checkETag(entityETag);
    UpdateResponse updateResponse = null;
    if (merge) {
        try {
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            Update update = visitor.update(entityType, entity, this.prepared);
            updateResponse = getClient().executeUpdate(update, visitor.getParameters());
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        }
    } else {
        // delete, then insert
        String txn = startTransaction();
        boolean success = false;
        try {
            // build insert first as it could fail to validate
            ODataSQLBuilder visitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            visitor.visit(request.getUriInfo());
            EdmEntityType entityType = request.getEntitySet().getEntityType();
            List<UriParameter> keys = request.getKeyPredicates();
            Insert command = visitor.insert(entityType, entity, keys, this.prepared);
            // run delete
            ODataSQLBuilder deleteVisitor = new ODataSQLBuilder(this.odata, getClient().getMetadataStore(), this.prepared, false, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
            deleteVisitor.visit(request.getUriInfo());
            Delete delete = deleteVisitor.delete();
            updateResponse = getClient().executeUpdate(delete, deleteVisitor.getParameters());
            // run insert
            updateResponse = getClient().executeUpdate(command, visitor.getParameters());
            commit(txn);
            success = true;
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
        } finally {
            if (!success) {
                rollback(txn);
            }
        }
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writeUpdatedEntity();
    } else {
        response.writeNotModified();
    }
}
Also used : Delete(org.teiid.query.sql.lang.Delete) SQLException(java.sql.SQLException) EdmEntityType(org.apache.olingo.commons.api.edm.EdmEntityType) Update(org.teiid.query.sql.lang.Update) Insert(org.teiid.query.sql.lang.Insert) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) TeiidException(org.teiid.core.TeiidException) UpdateResponse(org.teiid.odata.api.UpdateResponse) UriParameter(org.apache.olingo.server.api.uri.UriParameter)

Example 9 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method invokeOperation.

private <T extends ServiceResponse> void invokeOperation(final OperationRequest request, OperationParameterValueProvider parameters, T response) throws ODataApplicationException, ODataLibraryException {
    checkExpand(request.getUriInfo().asUriInfoResource());
    final ODataSQLBuilder visitor = new ODataSQLBuilder(odata, getClient().getMetadataStore(), this.prepared, true, request.getODataRequest().getRawBaseUri(), this.serviceMetadata);
    visitor.setOperationParameterValueProvider(parameters);
    visitor.visit(request.getUriInfo());
    final OperationResponseImpl queryResponse;
    try {
        if (visitor.getContext() instanceof NoDocumentNode) {
            NoDocumentNode cdn = (NoDocumentNode) visitor.getContext();
            ProcedureReturn procReturn = cdn.getProcedureReturn();
            queryResponse = new OperationResponseImpl(procReturn);
            getClient().executeCall(cdn.getQuery(), visitor.getParameters(), procReturn, queryResponse);
        } else {
            Query query = visitor.selectQuery();
            queryResponse = (OperationResponseImpl) executeQuery(request, request.isCountRequest(), visitor, query);
        }
    } catch (Throwable e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    /*
        try {
            MetadataStore store = getClient().getMetadataStore();
            ProcedureSQLBuilder builder = new ProcedureSQLBuilder(store.getSchema(schemaName), request);
            ProcedureReturn procedureReturn = builder.getReturn();
            result = new OperationResponseImpl(procedureReturn);
            
            getClient().executeCall(builder.buildProcedureSQL(), builder.getSqlParameters(), procedureReturn, result);
        } catch (SQLException e) {
            throw new ODataApplicationException(e.getMessage(),
                    HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                    Locale.getDefault(), e);
        } catch (TeiidException e) {
            throw new ODataApplicationException(e.getMessage(),
                    HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
                    Locale.getDefault(), e);
        } 
        */
    final OperationResponseImpl operationResult = queryResponse;
    response.accepts(new ServiceResponseVisior() {

        @Override
        public void visit(PropertyResponse response) throws ODataLibraryException, ODataApplicationException {
            Property property = (Property) operationResult.getResult();
            Object value = property.getValue();
            if (value instanceof SQLXML || value instanceof Blob || value instanceof Clob) {
                try {
                    handleLobResult(getClient().getProperty(Client.CHARSET), value, response);
                } catch (SQLException e) {
                    LogManager.logDetail(LogConstants.CTX_ODATA, e);
                    response.writeServerError(true);
                }
            } else {
                response.writeProperty(request.getReturnType().getType(), property);
            }
        }
    });
}
Also used : Blob(java.sql.Blob) Query(org.teiid.query.sql.lang.Query) SQLException(java.sql.SQLException) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException) ODataLibraryException(org.apache.olingo.server.api.ODataLibraryException) SQLXML(java.sql.SQLXML) ProcedureReturn(org.teiid.olingo.service.ProcedureSQLBuilder.ProcedureReturn) Clob(java.sql.Clob) Property(org.apache.olingo.commons.api.data.Property) EdmNavigationProperty(org.apache.olingo.commons.api.edm.EdmNavigationProperty) EdmProperty(org.apache.olingo.commons.api.edm.EdmProperty)

Example 10 with ODataApplicationException

use of org.apache.olingo.server.api.ODataApplicationException in project teiid by teiid.

the class TeiidServiceHandler method manageReference.

private void manageReference(DataRequest request, URI referenceId, NoContentResponse response, boolean delete) throws ODataApplicationException {
    UpdateResponse updateResponse = null;
    try {
        ReferenceUpdateSQLBuilder visitor = new ReferenceUpdateSQLBuilder(getClient().getMetadataStore(), request.getODataRequest().getRawBaseUri(), this.serviceMetadata, this.odata);
        visitor.visit(request.getUriInfo());
        Update update = visitor.updateReference(referenceId, this.prepared, delete);
        updateResponse = getClient().executeUpdate(update, visitor.getParameters());
    } catch (SQLException e) {
        throw new ODataApplicationException(e.getMessage(), HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(), Locale.getDefault(), e);
    }
    if (updateResponse != null && updateResponse.getUpdateCount() > 0) {
        response.writeNoContent();
    } else {
        response.writeNotModified();
    }
}
Also used : UpdateResponse(org.teiid.odata.api.UpdateResponse) SQLException(java.sql.SQLException) Update(org.teiid.query.sql.lang.Update) ODataApplicationException(org.apache.olingo.server.api.ODataApplicationException)

Aggregations

ODataApplicationException (org.apache.olingo.server.api.ODataApplicationException)12 SQLException (java.sql.SQLException)10 TeiidException (org.teiid.core.TeiidException)6 UpdateResponse (org.teiid.odata.api.UpdateResponse)6 URISyntaxException (java.net.URISyntaxException)5 Query (org.teiid.query.sql.lang.Query)5 EdmProperty (org.apache.olingo.commons.api.edm.EdmProperty)4 ODataLibraryException (org.apache.olingo.server.api.ODataLibraryException)4 Update (org.teiid.query.sql.lang.Update)4 MalformedURLException (java.net.MalformedURLException)3 URI (java.net.URI)3 EdmPrimitiveTypeException (org.apache.olingo.commons.api.edm.EdmPrimitiveTypeException)3 Property (org.apache.olingo.commons.api.data.Property)2 EdmEntityType (org.apache.olingo.commons.api.edm.EdmEntityType)2 EdmNavigationProperty (org.apache.olingo.commons.api.edm.EdmNavigationProperty)2 SerializerException (org.apache.olingo.server.api.serializer.SerializerException)2 TeiidProcessingException (org.teiid.core.TeiidProcessingException)2 TeiidRuntimeException (org.teiid.core.TeiidRuntimeException)2 TransformationException (org.teiid.core.types.TransformationException)2 BaseResponse (org.teiid.odata.api.BaseResponse)2