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

The following examples show how to use org.apache.olingo.odata2.api.processor.ODataResponse#getEntityAsStream() . 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: ProducerConsumerIntegrationTest.java    From olingo-odata2 with Apache License 2.0 6 votes vote down vote up
private List<Map<String, Object>> execute1(final EntityCollection localRoomData, final EdmEntitySet roomSet,
    final String contentType)
    throws ODataException {
  List<Map<String, Object>> propertiesList = new ArrayList<Map<String,Object>>();
  localRoomData.setCollectionProperties(EntityCollectionSerializerProperties.serviceRoot(BASE_URI).build());
  ODataResponse response = ODataClient.newInstance().createSerializer(contentType)
      .writeFeed(roomSet, localRoomData);
  InputStream content = response.getEntityAsStream();
  EntityStream entityContent = new EntityStream();
  entityContent.setReadProperties(DEFAULT_READ_PROPERTIES);
  entityContent.setContent(content);
  ODataFeed feed = ODataClient.newInstance()
      .createDeserializer(contentType).readFeed(roomSet, entityContent);
  List<ODataEntry> entries = feed.getEntries();
  for (ODataEntry entry : entries) {
    propertiesList.add(entry.getProperties());
  }
  return propertiesList;
}
 
Example 2
Source File: ODataResponseTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void buildEntityAsStreamResponseTest() throws Exception {
  ODataResponse response = ODataResponse.entity("abc").build();
  assertNull(response.getStatus());
  InputStream entityAsStream = response.getEntityAsStream();
  assertNotNull(entityAsStream);
  assertEquals("abc", StringHelper.inputStreamToString(entityAsStream));
}
 
Example 3
Source File: ODataResponseTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void buildEntityAsStreamResponseTestCharset() throws Exception {
  ODataResponse response = ODataResponse.entity("äbc")
      .contentHeader("app/json; charset=utf-8").build();
  assertNull(response.getStatus());
  InputStream entityAsStream = response.getEntityAsStream();
  assertNotNull(entityAsStream);
  assertEquals("äbc", StringHelper.inputStreamToString(entityAsStream));
}
 
Example 4
Source File: ODataResponseTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void buildEntityAsStreamResponseTestCharsetIso() throws Exception {
  ODataResponse response = ODataResponse.entity("äbc")
      .contentHeader("app/json; charset=iso-8859-1").build();
  assertNull(response.getStatus());
  InputStream entityAsStream = response.getEntityAsStream();
  assertNotNull(entityAsStream);
  StringHelper.Stream s = StringHelper.toStream(entityAsStream);
  assertEquals("äbc", s.asString("iso-8859-1"));
}
 
Example 5
Source File: BatchResponseWriterTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
  public void testResponse() throws Exception {
    List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
    ODataResponse response =
        ODataResponse.entity("Walter Winter").status(HttpStatusCodes.OK).contentHeader("application/json").build();
    List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
    responses.add(response);
    parts.add(BatchResponsePart.responses(responses).changeSet(false).build());
    BatchResponseWriter writer = new BatchResponseWriter();
    ODataResponse batchResponse = writer.writeResponse(parts);

    assertEquals(202, batchResponse.getStatus().getStatusCode());
    assertNotNull(batchResponse.getEntity());
//    String body = (String) batchResponse.getEntity();
    
    BatchLineReader reader =
        new BatchLineReader(batchResponse.getEntityAsStream());
    List<Line> lines = reader.toLineList();
    reader.close();
    int index = 0;
    
    assertTrue(lines.get(index++).toString().startsWith("--batch"));
    assertEquals("Content-Type: application/http" + CRLF, lines.get(index++).toString());
    assertEquals("Content-Transfer-Encoding: binary" + CRLF, lines.get(index++).toString());
    assertEquals(CRLF, lines.get(index++).toString());
    assertEquals("HTTP/1.1 200 OK" + CRLF, lines.get(index++).toString());
    assertEquals("Content-Type: application/json" + CRLF, lines.get(index++).toString());
    assertEquals("Content-Length: 13" + CRLF, lines.get(index++).toString());
    assertEquals(CRLF, lines.get(index++).toString());
    assertEquals("Walter Winter" + CRLF, lines.get(index++).toString());
    assertTrue(lines.get(index).toString().startsWith("--batch"));
  }
 
Example 6
Source File: BatchResponseWriterTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeSetResponse() throws Exception {
  List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
  ODataResponse changeSetResponse = ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
  List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
  responses.add(changeSetResponse);
  parts.add(BatchResponsePart.responses(responses).changeSet(true).build());

  BatchResponseWriter writer = new BatchResponseWriter();
  ODataResponse batchResponse = writer.writeResponse(parts);

  assertEquals(202, batchResponse.getStatus().getStatusCode());
  assertNotNull(batchResponse.getEntity());

  BatchLineReader reader =
      new BatchLineReader(batchResponse.getEntityAsStream());
  List<Line> lines = reader.toLineList();
  reader.close();
  int index = 0;
  
  assertTrue(lines.get(index++).toString().startsWith("--batch"));
  assertTrue(lines.get(index++).toString().startsWith("Content-Type: multipart/mixed; boundary=changeset_"));
  assertEquals(CRLF, lines.get(index++).toString());
  assertTrue(lines.get(index++).toString().startsWith("--changeset"));
  assertEquals("Content-Type: application/http" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Transfer-Encoding: binary" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("HTTP/1.1 204 No Content" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertTrue(lines.get(index++).toString().startsWith("--changeset"));
  assertTrue(lines.get(index).toString().startsWith("--batch"));
}
 
Example 7
Source File: BatchResponseWriterTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testContentIdEchoing() throws Exception {
  List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
  ODataResponse response = ODataResponse.entity("Walter Winter")
      .status(HttpStatusCodes.OK)
      .contentHeader("application/json")
      .header(BatchHelper.MIME_HEADER_CONTENT_ID, "mimeHeaderContentId123")
      .header(BatchHelper.REQUEST_HEADER_CONTENT_ID, "requestHeaderContentId123")
      .build();
  List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
  responses.add(response);
  parts.add(BatchResponsePart.responses(responses).changeSet(false).build());
  BatchResponseWriter writer = new BatchResponseWriter();
  ODataResponse batchResponse = writer.writeResponse(parts);

  assertEquals(202, batchResponse.getStatus().getStatusCode());
  assertNotNull(batchResponse.getEntity());

  BatchLineReader reader =
      new BatchLineReader(batchResponse.getEntityAsStream());
  List<Line> lines = reader.toLineList();
  reader.close();
  int index = 0;

  assertTrue(lines.get(index++).toString().startsWith("--batch"));
  assertEquals("Content-Type: application/http" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Transfer-Encoding: binary" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Id: mimeHeaderContentId123" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("HTTP/1.1 200 OK" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Id: requestHeaderContentId123" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Type: application/json" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Length: 13" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("Walter Winter" + CRLF, lines.get(index++).toString());
  assertTrue(lines.get(index).toString().startsWith("--batch"));
}
 
Example 8
Source File: BatchResponseWriterTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testResponseIso() throws Exception {
  List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
  StringHelper.Stream stream = StringHelper.toStream("Wälter Winter", "iso-8859-1");
  ODataResponse response =
      ODataResponse.entity(stream.asStream())
          .contentHeader("application/json; charset=iso-8859-1")
          .status(HttpStatusCodes.OK)
          .build();
  List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
  responses.add(response);
  parts.add(BatchResponsePart.responses(responses).changeSet(false).build());
  BatchResponseWriter writer = new BatchResponseWriter(true);
  ODataResponse batchResponse = writer.writeResponse(parts);

  assertEquals(202, batchResponse.getStatus().getStatusCode());
  assertNotNull(batchResponse.getEntity());

  BatchLineReader reader =
      new BatchLineReader(batchResponse.getEntityAsStream());
  List<Line> lines = reader.toLineList();
  reader.close();
  int index = 0;

  assertTrue(lines.get(index++).toString().startsWith("--batch"));
  assertEquals("Content-Type: application/http" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Transfer-Encoding: binary" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("HTTP/1.1 200 OK" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Type: application/json; charset=iso-8859-1" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Length: 13" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("Wälter Winter" + CRLF, lines.get(index++).toString());
  assertTrue(lines.get(index).toString().startsWith("--batch"));
}
 
Example 9
Source File: BatchResponseWriterTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
  public void testResponseUtf() throws Exception {
    List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
    String charset = "utf-8";
    StringHelper.Stream stream = StringHelper.toStream("Wälter Winter", charset);
    ODataResponse response =
        ODataResponse.entity(stream.asString(charset))
            .contentHeader("application/json; charset=" + charset)
            .status(HttpStatusCodes.OK)
            .build();
    List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
    responses.add(response);
    parts.add(BatchResponsePart.responses(responses).changeSet(false).build());
    BatchResponseWriter writer = new BatchResponseWriter();
    ODataResponse batchResponse = writer.writeResponse(parts);

    assertEquals(202, batchResponse.getStatus().getStatusCode());
    assertNotNull(batchResponse.getEntity());
//    String body = (String) batchResponse.getEntity();

    BatchLineReader reader =
        new BatchLineReader(batchResponse.getEntityAsStream());
    List<Line> lines = reader.toLineList();
    reader.close();
    int index = 0;

    assertTrue(lines.get(index++).toString().startsWith("--batch"));
    assertEquals("Content-Type: application/http" + CRLF, lines.get(index++).toString());
    assertEquals("Content-Transfer-Encoding: binary" + CRLF, lines.get(index++).toString());
    assertEquals(CRLF, lines.get(index++).toString());
    assertEquals("HTTP/1.1 200 OK" + CRLF, lines.get(index++).toString());
    assertEquals("Content-Type: application/json; charset=" + charset + CRLF, lines.get(index++).toString());
    assertEquals("Content-Length: 14" + CRLF, lines.get(index++).toString());
    assertEquals(CRLF, lines.get(index++).toString());
    assertEquals("Wälter Winter" + CRLF, lines.get(index++).toString());
    assertTrue(lines.get(index).toString().startsWith("--batch"));
  }
 
Example 10
Source File: ProducerConsumerIntegrationTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> execute(final Entity localRoomData, final EdmEntitySet roomSet,
    final String contentType)
    throws ODataException {
  localRoomData.setWriteProperties(DEFAULT_WRITE_PROPERTIES);
  ODataResponse response = ODataClient.newInstance().createSerializer(contentType)
      .writeEntry(roomSet, localRoomData);
  InputStream content = response.getEntityAsStream();
  EntityStream entityContent = new EntityStream();
  entityContent.setReadProperties(DEFAULT_READ_PROPERTIES);
  entityContent.setContent(content);
  ODataEntry entry = ODataClient.newInstance()
      .createDeserializer(contentType).readEntry(roomSet, entityContent);
  Map<String, Object> properties = entry.getProperties();
  return properties;
}
 
Example 11
Source File: ProducerConsumerIntegrationTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> executeFail(final Entity localRoomData, final EdmEntitySet roomSet,
    final String contentType)
    throws ODataException {
  localRoomData.setWriteProperties(DEFAULT_WRITE_PROPERTIES);
  ODataResponse response = ODataClient.newInstance().createSerializer(XML)
      .writeEntry(roomSet, localRoomData);
  InputStream content = response.getEntityAsStream();
  EntityStream entityContent = new EntityStream();
  entityContent.setReadProperties(DEFAULT_READ_PROPERTIES);
  entityContent.setContent(content);
  ODataEntry entry = ODataClient.newInstance()
      .createDeserializer(contentType).readEntry(roomSet, entityContent);
  Map<String, Object> properties = entry.getProperties();
  return properties;
}
 
Example 12
Source File: BatchResponseWriterTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testBatchResponse() throws ODataException, IOException {
  List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
  ODataResponse response = ODataResponse.entity("Walter Winter")
      .status(HttpStatusCodes.OK)
      .contentHeader("application/json")
      .build();
  List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
  responses.add(response);
  parts.add(BatchResponsePart.responses(responses).changeSet(false).build());

  ODataResponse changeSetResponse = ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
  responses = new ArrayList<ODataResponse>(1);
  responses.add(changeSetResponse);
  parts.add(BatchResponsePart.responses(responses).changeSet(true).build());

  BatchResponseWriter writer = new BatchResponseWriter();
  ODataResponse batchResponse = writer.writeResponse(parts);

  assertEquals(202, batchResponse.getStatus().getStatusCode());
  assertNotNull(batchResponse.getEntity());

  BatchLineReader reader =
      new BatchLineReader(batchResponse.getEntityAsStream());
  List<Line> lines = reader.toLineList();
  reader.close();
  int index = 0;

  assertTrue(lines.get(index++).toString().startsWith("--batch"));
  assertEquals("Content-Type: application/http" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Transfer-Encoding: binary" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("HTTP/1.1 200 OK" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Type: application/json" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Length: 13" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("Walter Winter" + CRLF, lines.get(index++).toString());
  
  assertTrue(lines.get(index++).toString().startsWith("--batch"));
  assertTrue(lines.get(index++).toString().startsWith("Content-Type: multipart/mixed; boundary=changeset_"));
  assertEquals(CRLF, lines.get(index++).toString());
  assertTrue(lines.get(index++).toString().startsWith("--changeset"));
  assertEquals("Content-Type: application/http" + CRLF, lines.get(index++).toString());
  assertEquals("Content-Transfer-Encoding: binary" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals("HTTP/1.1 204 No Content" + CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertEquals(CRLF, lines.get(index++).toString());
  assertTrue(lines.get(index++).toString().startsWith("--changeset"));
  assertTrue(lines.get(index).toString().startsWith("--batch"));
}