示例#1
0
  @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,
            this.nameGenerator);
    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);
    }
  }
示例#2
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();
   }
 }
示例#3
0
  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();
    }
  }
示例#4
0
 private void sendResults(
     final DataRequest request,
     final ODataSQLBuilder visitor,
     final BaseResponse queryResponse,
     EdmComplexResponse response)
     throws ODataApplicationException, SerializerException {
   if (request.getPreference(ODATA_MAXPAGESIZE) != null) {
     response.writeHeader(
         PREFERENCE_APPLIED,
         ODATA_MAXPAGESIZE + "=" + request.getPreference(ODATA_MAXPAGESIZE)); // $NON-NLS-1$
   }
   CrossJoinResult result = (CrossJoinResult) queryResponse;
   URI next = null;
   if (result.getNextToken() != null) {
     try {
       next =
           new URI(
               request.getODataRequest().getRawRequestUri()
                   + (request.getODataRequest().getRawQueryPath() == null
                       ? "?$skiptoken="
                       : "&$skiptoken=")
                   + result.getNextToken());
     } catch (URISyntaxException e) {
       throw new ODataApplicationException(e.getMessage(), 500, Locale.getDefault(), e);
     }
   }
   response.writeComplexType(result, next);
 }
示例#5
0
 private void sendResults(
     final DataRequest request,
     final ODataSQLBuilder visitor,
     final BaseResponse queryResponse,
     EntitySetResponse response)
     throws ODataApplicationException, SerializerException {
   if (request.getPreference(ODATA_MAXPAGESIZE) != null) {
     response.writeHeader(
         PREFERENCE_APPLIED,
         ODATA_MAXPAGESIZE + "=" + request.getPreference(ODATA_MAXPAGESIZE)); // $NON-NLS-1$
   }
   EntityCollectionResponse result = (EntityCollectionResponse) queryResponse;
   if (result.getNextToken() != null) {
     try {
       String nextUri =
           request.getODataRequest().getRawBaseUri()
               + request.getODataRequest().getRawODataPath()
               + "?"
               + buildNextToken(
                   request.getODataRequest().getRawQueryPath(), result.getNextToken());
       result.setNext(new URI(nextUri));
     } catch (URISyntaxException e) {
       throw new ODataApplicationException(e.getMessage(), 500, Locale.getDefault(), e);
     } catch (MalformedURLException e) {
       throw new ODataApplicationException(e.getMessage(), 500, Locale.getDefault(), e);
     }
   }
   response.writeReadEntitySet(visitor.getContext().getEdmEntityType(), result);
 }
示例#6
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();
    }
  }
示例#7
0
  @Override
  public void upsertEntity(
      DataRequest request, Entity entity, boolean merge, String entityETag, EntityResponse response)
      throws ODataLibraryException, ODataApplicationException {

    final ODataSQLBuilder visitor =
        new ODataSQLBuilder(
            this.odata,
            getClient().getMetadataStore(),
            this.prepared,
            true,
            request.getODataRequest().getRawBaseUri(),
            this.serviceMetadata,
            this.nameGenerator);
    visitor.visit(request.getUriInfo());

    final EntityCollectionResponse queryResponse;
    try {
      Query query = visitor.selectQuery();
      queryResponse =
          (EntityCollectionResponse)
              executeQuery(request, request.isCountRequest(), visitor, query);
    } catch (Exception e) {
      throw new ODataApplicationException(
          e.getMessage(),
          HttpStatusCode.INTERNAL_SERVER_ERROR.getStatusCode(),
          Locale.getDefault(),
          e);
    }

    if (!queryResponse.getEntities().isEmpty()) {
      updateEntity(request, entity, merge, entityETag, response);
    } else {
      createEntity(request, entity, response);
    }
  }
示例#8
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();
    }
  }
示例#9
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
        }
      }
    }
  }
示例#10
0
  @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,
            this.nameGenerator);
    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;
            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);
            }
            response.write(property.getValue());
          }

          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);
          }
        });
  }