Java Code Examples for org.apache.olingo.odata2.api.commons.HttpStatusCodes#NO_CONTENT

The following examples show how to use org.apache.olingo.odata2.api.commons.HttpStatusCodes#NO_CONTENT . 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: ODataRequestHandler.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private HttpStatusCodes getStatusCode(final ODataResponse odataResponse, final ODataHttpMethod method,
    final UriType uriType) {
  if (odataResponse.getStatus() == null) {
    if (method == ODataHttpMethod.POST) {
      if (uriType == UriType.URI9) {
        return HttpStatusCodes.OK;
      } else if (uriType == UriType.URI7B) {
        return HttpStatusCodes.NO_CONTENT;
      }
      return HttpStatusCodes.CREATED;
    } else if (method == ODataHttpMethod.PUT
        || method == ODataHttpMethod.PATCH
        || method == ODataHttpMethod.MERGE
        || method == ODataHttpMethod.DELETE) {
      return HttpStatusCodes.NO_CONTENT;
    }
    return HttpStatusCodes.OK;
  }
  return odataResponse.getStatus();
}
 
Example 2
Source File: AnnotationSampleDataGenerator.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private void writeEntity(String absoluteUri, String content, String contentType, String httpMethod)
    throws IOException, URISyntaxException {

  print(httpMethod + " request on uri: " + absoluteUri + ":\n  " + content + "\n");
  //
  HttpURLConnection connection = initializeConnection(absoluteUri, contentType, httpMethod);
  byte[] buffer = content.getBytes("UTF-8");
  connection.getOutputStream().write(buffer);

  // if a entity is created (via POST request) the response body contains the new created entity
  HttpStatusCodes statusCode = HttpStatusCodes.fromStatusCode(connection.getResponseCode());
  if(statusCode == HttpStatusCodes.CREATED) {
    // get the content as InputStream and de-serialize it into an ODataEntry object
    InputStream responseContent = connection.getInputStream();
    logRawContent(httpMethod + " response:\n  ", responseContent, "\n");
  } else if(statusCode == HttpStatusCodes.NO_CONTENT) {
    print("No content.");
  } else {
    checkStatus(connection);
  }

  //
  connection.disconnect();
}
 
Example 3
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected HttpResponse callUri(
    final ODataHttpMethod httpMethod, final String uri,
    final String additionalHeader, final String additionalHeaderValue,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {

  HttpRequestBase request =
      httpMethod == ODataHttpMethod.GET ? new HttpGet() :
          httpMethod == ODataHttpMethod.DELETE ? new HttpDelete() :
              httpMethod == ODataHttpMethod.POST ? new HttpPost() :
                  httpMethod == ODataHttpMethod.PUT ? new HttpPut() : new HttpPatch();
  request.setURI(URI.create(getEndpoint() + uri));
  if (additionalHeader != null) {
    request.addHeader(additionalHeader, additionalHeaderValue);
  }
  if (requestBody != null) {
    ((HttpEntityEnclosingRequest) request).setEntity(new StringEntity(requestBody));
    request.setHeader(HttpHeaders.CONTENT_TYPE, requestContentType);
  }

  final HttpResponse response = getHttpClient().execute(request);

  assertNotNull(response);
  assertEquals(expectedStatusCode.getStatusCode(), response.getStatusLine().getStatusCode());

  if (expectedStatusCode == HttpStatusCodes.OK) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
  } else if (expectedStatusCode == HttpStatusCodes.CREATED) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
    assertNotNull(response.getFirstHeader(HttpHeaders.LOCATION));
  } else if (expectedStatusCode == HttpStatusCodes.NO_CONTENT) {
    assertTrue(response.getEntity() == null || response.getEntity().getContent() == null);
  }

  return response;
}
 
Example 4
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void deleteUri(final String uri, final HttpStatusCodes expectedStatusCode)
    throws Exception, AssertionError {
  final HttpResponse response = callUri(ODataHttpMethod.DELETE, uri, null, null, null, null, expectedStatusCode);
  if (expectedStatusCode != HttpStatusCodes.NO_CONTENT) {
    response.getEntity().getContent().close();
  }
}
 
Example 5
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void putUri(final String uri,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {
  final HttpResponse response =
      callUri(ODataHttpMethod.PUT, uri, null, null, requestBody, requestContentType, expectedStatusCode);
  if (expectedStatusCode != HttpStatusCodes.NO_CONTENT) {
    response.getEntity().getContent().close();
  }
}
 
Example 6
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void putUri(final String uri, final String acceptHeader,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {
  final HttpResponse response =
      callUri(ODataHttpMethod.PUT, uri,
          org.apache.olingo.odata2.api.commons.HttpHeaders.ACCEPT, acceptHeader, requestBody, requestContentType,
          expectedStatusCode);
  if (expectedStatusCode != HttpStatusCodes.NO_CONTENT) {
    response.getEntity().getContent().close();
  }
}
 
Example 7
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected HttpResponse callUri(
    final ODataHttpMethod httpMethod, final String uri,
    final String additionalHeader, final String additionalHeaderValue,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {

  HttpRequestBase request =
      httpMethod == ODataHttpMethod.GET ? new HttpGet() :
          httpMethod == ODataHttpMethod.DELETE ? new HttpDelete() :
              httpMethod == ODataHttpMethod.POST ? new HttpPost() :
                  httpMethod == ODataHttpMethod.PUT ? new HttpPut() : new HttpPatch();
  request.setURI(URI.create(getEndpoint() + uri));
  if (additionalHeader != null) {
    request.addHeader(additionalHeader, additionalHeaderValue);
  }
  if (requestBody != null) {
    ((HttpEntityEnclosingRequest) request).setEntity(new StringEntity(requestBody));
    request.setHeader(HttpHeaders.CONTENT_TYPE, requestContentType);
  }

  final HttpResponse response = getHttpClient().execute(request);

  assertNotNull(response);
  assertEquals(expectedStatusCode.getStatusCode(), response.getStatusLine().getStatusCode());

  if (expectedStatusCode == HttpStatusCodes.OK) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
  } else if (expectedStatusCode == HttpStatusCodes.CREATED) {
    assertNotNull(response.getEntity());
    assertNotNull(response.getEntity().getContent());
    assertNotNull(response.getFirstHeader(HttpHeaders.LOCATION));
  } else if (expectedStatusCode == HttpStatusCodes.NO_CONTENT) {
    assertTrue(response.getEntity() == null || response.getEntity().getContent() == null);
  }

  return response;
}
 
Example 8
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void deleteUri(final String uri, final HttpStatusCodes expectedStatusCode)
    throws Exception, AssertionError {
  final HttpResponse response = callUri(ODataHttpMethod.DELETE, uri, null, null, null, null, expectedStatusCode);
  if (expectedStatusCode != HttpStatusCodes.NO_CONTENT) {
    response.getEntity().getContent().close();
  }
}
 
Example 9
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void putUri(final String uri,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {
  final HttpResponse response =
      callUri(ODataHttpMethod.PUT, uri, null, null, requestBody, requestContentType, expectedStatusCode);
  if (expectedStatusCode != HttpStatusCodes.NO_CONTENT) {
    response.getEntity().getContent().close();
  }
}
 
Example 10
Source File: AbstractRefTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
protected void putUri(final String uri, final String acceptHeader,
    final String requestBody, final String requestContentType,
    final HttpStatusCodes expectedStatusCode) throws Exception {
  final HttpResponse response =
      callUri(ODataHttpMethod.PUT, uri,
          org.apache.olingo.odata2.api.commons.HttpHeaders.ACCEPT, acceptHeader, requestBody, requestContentType,
          expectedStatusCode);
  if (expectedStatusCode != HttpStatusCodes.NO_CONTENT) {
    response.getEntity().getContent().close();
  }
}