예제 #1
0
 @Override
 public void upsertStreamProperty(
     DataRequest request, String entityETag, InputStream streamContent, NoContentResponse response)
     throws ODataLibraryException, ODataApplicationException {
   UpdateResponse updateResponse = null;
   EdmProperty edmProperty = request.getUriResourceProperty().getProperty();
   try {
     ODataSQLBuilder visitor =
         new ODataSQLBuilder(
             this.odata,
             getClient().getMetadataStore(),
             this.prepared,
             false,
             request.getODataRequest().getRawBaseUri(),
             this.serviceMetadata,
             this.nameGenerator);
     visitor.visit(request.getUriInfo());
     Update update = visitor.updateStreamProperty(edmProperty, streamContent);
     updateResponse = getClient().executeUpdate(update, visitor.getParameters());
   } catch (SQLException | TeiidException 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();
   }
 }
예제 #2
0
  private BaseResponse executeQuery(
      final ServiceRequest request,
      boolean countRequest,
      final ODataSQLBuilder visitor,
      Query query)
      throws SQLException {
    if (countRequest) {
      return getClient().executeCount(query, visitor.getParameters());
    } else {
      String pageSize = getPageSize(request);

      QueryResponse result =
          new EntityCollectionResponse(
              request.getODataRequest().getRawBaseUri(), visitor.getContext());

      if (visitor.getContext() instanceof CrossJoinNode) {
        result =
            new CrossJoinResult(
                request.getODataRequest().getRawBaseUri(), (CrossJoinNode) visitor.getContext());
      } else if (visitor.getContext() instanceof ComplexDocumentNode) {
        ComplexDocumentNode cdn = (ComplexDocumentNode) visitor.getContext();
        result = new OperationResponseImpl(cdn.getProcedureReturn());
      }

      getClient()
          .executeSQL(
              query,
              visitor.getParameters(),
              visitor.includeTotalSize(),
              visitor.getSkip(),
              visitor.getTop(),
              visitor.getNextToken(),
              Integer.parseInt(pageSize),
              result);

      return result;
    }
  }
예제 #3
0
 private UpdateResponse performInsert(
     String rawURI, UriInfo uriInfo, EdmEntityType entityType, Entity entity)
     throws SQLException, TeiidException {
   ODataSQLBuilder visitor =
       new ODataSQLBuilder(
           this.odata,
           getClient().getMetadataStore(),
           this.prepared,
           false,
           rawURI,
           this.serviceMetadata,
           this.nameGenerator);
   visitor.visit(uriInfo);
   Insert command = visitor.insert(entityType, entity, null, this.prepared);
   return getClient().executeUpdate(command, visitor.getParameters());
 }
예제 #4
0
  @Override
  public void deleteEntity(DataRequest request, String entityETag, EntityResponse response)
      throws ODataLibraryException, ODataApplicationException {

    // TODO: need to match entityETag.
    checkETag(entityETag);

    UpdateResponse updateResponse = null;
    try {
      ODataSQLBuilder visitor =
          new ODataSQLBuilder(
              this.odata,
              getClient().getMetadataStore(),
              this.prepared,
              false,
              request.getODataRequest().getRawBaseUri(),
              this.serviceMetadata,
              this.nameGenerator);
      visitor.visit(request.getUriInfo());
      Delete delete = visitor.delete();
      updateResponse = getClient().executeUpdate(delete, 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.writeDeletedEntityOrReference();
    } else {
      // since DELETE is idempotent same response as otherwise success operation.
      response.writeDeletedEntityOrReference();
    }
  }
예제 #5
0
  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,
            this.nameGenerator);
    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);
            }
          }
        });
  }
예제 #6
0
  @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,
                this.nameGenerator);
        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,
                this.nameGenerator);
        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,
                this.nameGenerator);
        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();
    }
  }
예제 #7
0
  @Override
  public void createEntity(DataRequest request, Entity entity, EntityResponse response)
      throws ODataLibraryException, ODataApplicationException {

    EdmEntityType entityType = request.getEntitySet().getEntityType();

    String txn;
    try {
      txn = getClient().startTransaction();
    } catch (SQLException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    }
    boolean success = false;

    try {
      List<ExpandNode> expands = new ArrayList<TeiidServiceHandler.ExpandNode>();
      UpdateResponse updateResponse =
          performDeepInsert(
              request.getODataRequest().getRawBaseUri(),
              request.getUriInfo(),
              entityType,
              entity,
              expands);

      if (updateResponse != null && updateResponse.getUpdateCount() == 1) {
        ODataSQLBuilder visitor =
            new ODataSQLBuilder(
                this.odata,
                getClient().getMetadataStore(),
                true,
                false,
                request.getODataRequest().getRawBaseUri(),
                this.serviceMetadata,
                this.nameGenerator);

        Query query =
            visitor.selectWithEntityKey(
                entityType, entity, updateResponse.getGeneratedKeys(), expands);
        LogManager.logDetail(
            LogConstants.CTX_ODATA,
            null,
            "created entity = ",
            entityType.getName(),
            " with key=",
            query.getCriteria().toString()); // $NON-NLS-1$ //$NON-NLS-2$

        EntityCollectionResponse result =
            new EntityCollectionResponse(
                request.getODataRequest().getRawBaseUri(), visitor.getContext());

        getClient().executeSQL(query, visitor.getParameters(), false, null, null, null, 1, result);

        if (!result.getEntities().isEmpty()) {
          entity = result.getEntities().get(0);
          String location =
              EntityResponse.buildLocation(
                  request.getODataRequest().getRawBaseUri(),
                  entity,
                  request.getEntitySet().getName(),
                  entityType);
          entity.setId(new URI(location));
        }
        response.writeCreatedEntity(request.getEntitySet(), entity);
      } else {
        response.writeNotModified();
      }
      getClient().commit(txn);
      success = true;
    } catch (SQLException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    } catch (URISyntaxException 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);
    } catch (EdmPrimitiveTypeException e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    } finally {
      if (!success) {
        try {
          getClient().rollback(txn);
        } catch (SQLException e1) {
          // ignore
        }
      }
    }
  }