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

The following examples show how to use org.apache.olingo.odata2.api.commons.HttpStatusCodes#CREATED . 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: ContentNegotiationPostRequestTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
public void testURI_1_ForIssue() throws Exception {
  // create test set
  UriType uriType = UriType.URI1;
  String httpMethod = "POST";
  String path = "/Employees";
  String queryOption = "";
  String acceptHeader = "*";
  String content = "IMAGE_;o)";
  String requestContentType = "image/jpeg";
  HttpStatusCodes expectedStatusCode = HttpStatusCodes.CREATED;
  String expectedContentType = "application/atom+xml; charset=utf-8; type=entry";

  FitTest test =
      FitTest.create(uriType, httpMethod, path, queryOption, acceptHeader, content, requestContentType,
          expectedStatusCode, expectedContentType);

  test.execute(getEndpoint());
}
 
Example 3
Source File: ContentNegotiationPostRequestTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
@Test
@Ignore("What is expected service behavior?")
public void testURI_2_ForIssue() throws Exception {
  // create test set
  UriType uriType = UriType.URI2;
  String httpMethod = "PUT";
  String path = "/Employees('1')";
  String queryOption = "";
  String acceptHeader = "*";
  String content = "IMAGE_;o)";
  String requestContentType = "image/jpeg";
  HttpStatusCodes expectedStatusCode = HttpStatusCodes.CREATED;
  String expectedContentType = "image/jpeg";

  FitTest test =
      FitTest.create(uriType, httpMethod, path, queryOption, acceptHeader, content, requestContentType,
          expectedStatusCode, expectedContentType);

  test.execute(getEndpoint());
}
 
Example 4
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 5
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 6
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 7
Source File: EntryXmlChangeTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void createWithAcceptHeaderEntry() throws Exception {
  // Create an entry for a type that has no media resource.
  String requestBody = getBody(callUri("Teams('1')"))
      .replace("'1'", "'9'")
      .replace("Id>1", "Id>9")
      .replace("Team 1", "Team X")
      .replaceAll("<link.+?/>", "");
  String requestContentType = HttpContentType.APPLICATION_ATOM_XML_ENTRY;
  HttpStatusCodes expectedStatusCode = HttpStatusCodes.CREATED;
  String headerName = "Accept";
  String headerValue = "application/atom+xml;type=entry";
  HttpResponse response =
      callUri(ODataHttpMethod.POST, "Teams()", headerName, headerValue, requestBody, requestContentType,
          expectedStatusCode);

  checkMediaType(response, HttpContentType.APPLICATION_ATOM_XML_UTF8 + ";type=entry");
  assertEquals(getEndpoint() + "Teams('4')", response.getFirstHeader(HttpHeaders.LOCATION).getValue());
  assertNull(response.getFirstHeader(HttpHeaders.ETAG));
  assertXpathEvaluatesTo("Team X", "/atom:entry/atom:content/m:properties/d:Name", getBody(response));

  // Create an entry for a type that has no media resource.
  // Add navigation to Employee('4') and Employee('5').
  requestBody = "<entry xmlns=\"" + Edm.NAMESPACE_ATOM_2005 + "\"" + "\n"
      + "       xmlns:d=\"" + Edm.NAMESPACE_D_2007_08 + "\"" + "\n"
      + "       xmlns:m=\"" + Edm.NAMESPACE_M_2007_08 + "\">" + "\n"
      + "  <author><name>no author</name></author>" + "\n"
      + "  <content type=\"application/xml\">" + "\n"
      + "    <m:properties>" + "\n"
      + "      <d:Id>109</d:Id>" + "\n"
      + "      <d:Name/>" + "\n"
      + "      <d:Seats>4</d:Seats>" + "\n"
      + "      <d:Version>2</d:Version>" + "\n"
      + "    </m:properties>" + "\n"
      + "  </content>" + "\n"
      + "  <id>Rooms('104')</id>" + "\n"
      + "  <title>Room 104</title>" + "\n"
      + "  <updated>2011-08-10T12:00:23Z</updated>" + "\n"
      + "  <link href=\"Employees('4')\"" + "\n"
      + "        rel=\"" + Edm.NAMESPACE_REL_2007_08 + "nr_Employees\"" + "\n"
      + "        type=\"" + HttpContentType.APPLICATION_ATOM_XML_FEED_UTF8 + "\"/>" + "\n"
      + "  <link href=\"Employees('5')\"" + "\n"
      + "        rel=\"" + Edm.NAMESPACE_REL_2007_08 + "nr_Employees\"" + "\n"
      + "        type=\"" + HttpContentType.APPLICATION_ATOM_XML_FEED_UTF8 + "\"/>" + "\n"
      + "</entry>";
  response = postUri("Rooms", requestBody, HttpContentType.APPLICATION_ATOM_XML_ENTRY, HttpStatusCodes.CREATED);
  checkMediaType(response, HttpContentType.APPLICATION_ATOM_XML_UTF8 + ";type=entry");
  assertEquals(getEndpoint() + "Rooms('104')", response.getFirstHeader(HttpHeaders.LOCATION).getValue());
  checkEtag(response, "W/\"2\"");
  assertXpathEvaluatesTo("4", "/atom:entry/atom:content/m:properties/d:Seats", getBody(response));
  checkUri("Rooms('104')/nr_Employees('4')");
  checkUri("Rooms('104')/nr_Employees('5')");
}