Java Code Examples for org.apache.olingo.server.api.ODataResponse#getContent()

The following examples show how to use org.apache.olingo.server.api.ODataResponse#getContent() . 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: ODataNettyHandlerImpl.java    From olingo-odata4 with Apache License 2.0 6 votes vote down vote up
/**
 * Convert the OData Response to Netty Response
 * @param response
 * @param odResponse
 */
static void convertToHttp(final HttpResponse response, final ODataResponse odResponse) {
   response.setStatus(HttpResponseStatus.valueOf(odResponse.getStatusCode()));

   for (Entry<String, List<String>> entry : odResponse.getAllHeaders().entrySet()) {
     for (String headerValue : entry.getValue()) {
       ((HttpMessage)response).headers().add(entry.getKey(), headerValue);
     }
   }

   if (odResponse.getContent() != null) {
     copyContent(odResponse.getContent(), response);
   } else if (odResponse.getODataContent() != null) {
     writeContent(odResponse, response);
   }
 }
 
Example 2
Source File: ODataHandlerImplTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void applicationExceptionInProcessorMessage() throws Exception {
  final String ODATA_ERRORCODE = "425";
  final String ORIGINAL_MESSAGE = "original message";
  final String LOCALIZED_MESSAGE = "localized message";
  MetadataProcessor processor = mock(MetadataProcessor.class);

  ODataApplicationException oDataApplicationException =
      new ODataApplicationException(ORIGINAL_MESSAGE, 425, Locale.ENGLISH, ODATA_ERRORCODE) {
        private static final long serialVersionUID = 1L;

        @Override
        public String getLocalizedMessage() {
          return LOCALIZED_MESSAGE;
        }
      };

  doThrow(oDataApplicationException).when(processor).readMetadata(
      any(ODataRequest.class), any(ODataResponse.class), any(UriInfo.class), any(ContentType.class));

  final ODataResponse response = dispatch(HttpMethod.GET, "$metadata", processor);
  InputStream contentStream = response.getContent();
  String responseContent = IOUtils.toString(contentStream, Charset.forName("UTF-8"));
  // does the response contain the localized message and the status code?
  boolean isMessage = responseContent.contains(LOCALIZED_MESSAGE) && responseContent.contains(ODATA_ERRORCODE);
  // test if message is localized
  assertEquals(true, isMessage);
  // test if the original is hold
  assertEquals(ORIGINAL_MESSAGE, oDataApplicationException.getMessage());
}
 
Example 3
Source File: ODataHttpHandlerImpl.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
static void convertToHttp(final HttpServletResponse response, final ODataResponse odResponse) {
  response.setStatus(odResponse.getStatusCode());

  for (Entry<String, List<String>> entry : odResponse.getAllHeaders().entrySet()) {
    for (String headerValue : entry.getValue()) {
      response.addHeader(entry.getKey(), headerValue);
    }
  }

  if (odResponse.getContent() != null) {
    copyContent(odResponse.getContent(), response);
  } else if (odResponse.getODataContent() != null) {
    writeContent(odResponse, response);
  }
}
 
Example 4
Source File: AsyncResponseSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private void appendBody(final ODataResponse response, final ByteArrayOutputStream buffer) throws IOException {
  InputStream input = response.getContent();
  if (input != null) {
    ByteBuffer inBuffer = ByteBuffer.allocate(BUFFER_SIZE);
    try (ReadableByteChannel ic = Channels.newChannel(input)) {
      try (WritableByteChannel oc = Channels.newChannel(buffer)) {
        while (ic.read(inBuffer) > 0) {
          inBuffer.flip();
          oc.write(inBuffer);
          inBuffer.rewind();
        }
      }
    }
  }
}
 
Example 5
Source File: BatchResponseSerializer.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
private byte[] getBody(final ODataResponse response) {
  if (response == null || (response.getContent() == null && 
      response.getODataContent() == null)) {
    return new byte[0];
  }

  try {
    ByteArrayOutputStream output = new ByteArrayOutputStream();
    ByteBuffer inBuffer = ByteBuffer.allocate(BUFFER_SIZE);
    if (response.getContent() == null) {
      if (response.getODataContent() != null) {
        ODataContent res = response.getODataContent();
        res.write(Channels.newChannel(output));
        }
    } else {
      try (WritableByteChannel oc = Channels.newChannel(output)) {
        try (ReadableByteChannel ic = Channels.newChannel(response.getContent())) {
          while (ic.read(inBuffer) > 0) {
            inBuffer.flip();
            oc.write(inBuffer);
            inBuffer.rewind();
          }
        }
      }
    }
    return output.toByteArray();
  } catch (IOException e) {
    throw new ODataRuntimeException("Error on reading request content", e);
  }
}
 
Example 6
Source File: MockedBatchHandlerTest.java    From olingo-odata4 with Apache License 2.0 5 votes vote down vote up
@Test
public void testGetRequest() throws Exception {
  final String content = ""
      + "--batch_12345" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + CRLF
      + "GET ESAllPrim(0) HTTP/1.1" + CRLF
      + CRLF
      + CRLF
      + "--batch_12345--";

  final Map<String, List<String>> header = getMimeHeader();
  final ODataResponse response = new ODataResponse();
  final ODataRequest request = buildODataRequest(content, header);

  batchHandler.process(request, response, true);

  BatchLineReader reader =
      new BatchLineReader(response.getContent());

  final List<String> responseContent = reader.toList();
  int line = 0;

  assertEquals(9, responseContent.size());
  assertTrue(responseContent.get(line++).contains("--batch_"));
  assertEquals("Content-Type: application/http" + CRLF, responseContent.get(line++));
  assertEquals("Content-Transfer-Encoding: binary" + CRLF, responseContent.get(line++));
  assertEquals(CRLF, responseContent.get(line++));
  assertEquals("HTTP/1.1 200 OK" + CRLF, responseContent.get(line++));
  assertEquals("Content-Length: 0" + CRLF, responseContent.get(line++));
  assertEquals(CRLF, responseContent.get(line++));
  assertEquals(CRLF, responseContent.get(line++));
  assertTrue(responseContent.get(line++).contains("--batch_"));

  assertEquals(9, line);

  reader.close();
}
 
Example 7
Source File: MockedBatchHandlerTest.java    From olingo-odata4 with Apache License 2.0 4 votes vote down vote up
@Test
public void mimeBodyPartTransitive() throws Exception {
  final String content = ""
      + "--batch_12345" + CRLF
      + "Content-Type: multipart/mixed; boundary=changeset_12345" + CRLF
      + CRLF
      + "--changeset_12345" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + "Content-Id: 1" + CRLF
      + CRLF
      + "PUT ESAllPrim(1) HTTP/1.1" + CRLF
      + "Content-Type: application/json;odata=verbose" + CRLF
      + CRLF
      + CRLF
      + "--changeset_12345" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + "Content-Id: 2" + CRLF
      + CRLF
      + "POST $1/NavPropertyETTwoPrimMany HTTP/1.1" + CRLF
      + "Content-Type: application/json;odata=verbose" + CRLF
      + CRLF
      + CRLF
      + "--changeset_12345" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + "Content-Id: 3" + CRLF
      + CRLF
      + "POST $2/NavPropertyETAllPrimMany HTTP/1.1" + CRLF
      + "Content-Type: application/json;odata=verbose" + CRLF
      + CRLF
      + CRLF
      + "--changeset_12345" + CRLF
      + "Content-Type: application/http" + CRLF
      + "Content-Transfer-Encoding: binary" + CRLF
      + "Content-Id: 4" + CRLF
      + CRLF
      + "POST $3/NavPropertyETTwoPrimOne HTTP/1.1" + CRLF
      + "Content-Type: application/json;odata=verbose" + CRLF
      + CRLF
      + CRLF
      + "--changeset_12345--" + CRLF

      + CRLF
      + "--batch_12345--";

  final Map<String, List<String>> header = getMimeHeader();
  final ODataResponse response = new ODataResponse();
  final ODataRequest request = buildODataRequest(content, header);

  batchHandler.process(request, response, true);

  BatchLineReader reader =
      new BatchLineReader(response.getContent());

  final List<String> responseContent = reader.toList();
  reader.close();

  int line = 0;
  assertEquals(44, responseContent.size());

  // Check change set
  assertTrue(responseContent.get(line++).contains("--batch_"));
  assertTrue(responseContent.get(line++).contains("Content-Type: multipart/mixed; boundary=changeset_"));

  for (int i = 0; i < 4; i++) {
    String contentId = checkChangeSetPartHeader(responseContent, line);
    line += 6;

    if ("1".equals(contentId)) {
      assertEquals("HTTP/1.1 200 OK" + CRLF, responseContent.get(line++));
      assertEquals("Content-Length: 0" + CRLF, responseContent.get(line++));
    } else if ("2".equals(contentId)) {
      assertEquals("HTTP/1.1 201 Created" + CRLF, responseContent.get(line++));
      assertEquals("Location: " + BASE_URI + "/ESTwoPrim(1)" + CRLF, responseContent.get(line++));
      assertEquals("Content-Length: 0" + CRLF, responseContent.get(line++));
    } else if ("3".equals(contentId)) {
      assertEquals("HTTP/1.1 201 Created" + CRLF, responseContent.get(line++));
      assertEquals("Location: " + BASE_URI + "/ESAllPrim(2)" + CRLF, responseContent.get(line++));
      assertEquals("Content-Length: 0" + CRLF, responseContent.get(line++));
    } else if ("4".equals(contentId)) {
      assertEquals("HTTP/1.1 201 Created" + CRLF, responseContent.get(line++));
      assertEquals("Location: " + BASE_URI + "/ESTwoPrim(3)" + CRLF, responseContent.get(line++));
      assertEquals("Content-Length: 0" + CRLF, responseContent.get(line++));
    } else {
      fail();
    }

    assertEquals(CRLF, responseContent.get(line++));
  }

  // Close body part (change set)
  assertEquals(CRLF, responseContent.get(line++));
  assertTrue(responseContent.get(line++).contains("--changeset_"));

  // Close batch
  assertTrue(responseContent.get(line++).contains("--batch_"));
  assertEquals(44, line);
}