Java Code Examples for org.apache.olingo.odata2.api.processor.ODataResponse#close()

The following examples show how to use org.apache.olingo.odata2.api.processor.ODataResponse#close() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: CMODataAbapClient.java    From devops-cm-client with Apache License 2.0 7 votes vote down vote up
public void updateTransport(Transport transport) throws IOException, URISyntaxException, ODataException, UnexpectedHttpResponseException
{
  Edm edm = getEntityDataModel();
  try (CloseableHttpClient client = clientFactory.createClient()) {
    
    HttpPut put = requestBuilder.updateTransport(transport.getTransportID());
    put.setHeader("x-csrf-token", getCSRFToken());
    EdmEntityContainer entityContainer = edm.getDefaultEntityContainer();
    EdmEntitySet entitySet = entityContainer.getEntitySet(TransportRequestBuilder.getEntityKey());
    URI rootUri = new URI(endpoint.toASCIIString() + "/");
    EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(rootUri).build();
    ODataResponse response = null;
    try {
        response = EntityProvider.writeEntry(put.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue(), entitySet,transport.getValueMap(), properties);
        put.setEntity(EntityBuilder.create().setStream(response.getEntityAsStream()).build());
        try (CloseableHttpResponse httpResponse = client.execute(put)) {
            hasStatusOrFail(httpResponse, SC_OK, HttpStatus.SC_NO_CONTENT);
        }
    }  finally {
        if(response != null) response.close();
    }
  }
}
 
Example 2
Source File: CMODataAbapClient.java    From devops-cm-client with Apache License 2.0 6 votes vote down vote up
public Transport createTransport(Map<String, Object> transport) throws IOException, URISyntaxException, ODataException, UnexpectedHttpResponseException
{
  try (CloseableHttpClient client = clientFactory.createClient()) {
    HttpPost post = requestBuilder.createTransport();
    post.setHeader("x-csrf-token", getCSRFToken());
    EdmEntityContainer entityContainer = getEntityDataModel().getDefaultEntityContainer();
    EdmEntitySet entitySet = entityContainer.getEntitySet(TransportRequestBuilder.getEntityKey());
    URI rootUri = new URI(endpoint.toASCIIString() + "/");
    EntityProviderWriteProperties properties = EntityProviderWriteProperties.serviceRoot(rootUri).build();
    ODataResponse marshaller = null;

    try {
      marshaller = EntityProvider.writeEntry(post.getHeaders(HttpHeaders.CONTENT_TYPE)[0].getValue(), entitySet, transport, properties);
      post.setEntity(EntityBuilder.create().setStream(marshaller.getEntityAsStream()).build());

      try(CloseableHttpResponse httpResponse = client.execute(post)) {
        hasStatusOrFail(httpResponse, SC_CREATED);
        return getTransport(httpResponse);
      }
    } finally {
        if(marshaller != null) marshaller.close();
    }
  }
}
 
Example 3
Source File: RestUtil.java    From olingo-odata2 with Apache License 2.0 3 votes vote down vote up
public static Response convertResponse(final ODataResponse odataResponse, final boolean omitResponseBody) {
  try {
    ResponseBuilder responseBuilder =
        Response.noContent().status(odataResponse.getStatus().getStatusCode());
    if(!omitResponseBody) {
      responseBuilder.entity(odataResponse.getEntity());
    }

    for (final String name : odataResponse.getHeaderNames()) {
      responseBuilder = responseBuilder.header(name, odataResponse.getHeader(name));
    }

    return responseBuilder.build();
  } catch (RuntimeException e) {
    if (odataResponse != null) {
      try {
        odataResponse.close();
      } catch (IOException inner) {
        // if close throw an exception we ignore these and re-throw our exception
        throw e;
      }
    }
    throw e;
  }
}