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

The following examples show how to use org.apache.olingo.odata2.api.processor.ODataResponse#getEntity() . 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: ODataResponseImpl.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Override
protected ODataResponseBuilder fromResponse(final ODataResponse response) {
  status = response.getStatus();
  entity = response.getEntity();

  headers = new HashMap<String, String>();
  for (String key : response.getHeaderNames()) {
    headers.put(key, response.getHeader(key));
  }

  return this;
}
 
Example 2
Source File: ProducerConsumerIntegrationTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Map<String, Object> execute(final Map<String, Object> localRoomData, final EdmEntitySet roomSet,
    final String contentType)
    throws EntityProviderException {
  ODataResponse response = EntityProvider.writeEntry(contentType, roomSet, localRoomData, DEFAULT_WRITE_PROPERTIES);
  InputStream content = (InputStream) response.getEntity();

  ODataEntry entry = EntityProvider.readEntry(contentType, roomSet, content, DEFAULT_READ_PROPERTIES);
  Map<String, Object> properties = entry.getProperties();
  return properties;
}
 
Example 3
Source File: EdmxProviderTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
private Edm createEdm() throws Exception {
  EdmProvider testProvider = new EdmTestProvider();
  ODataResponse response = EntityProvider.writeMetadata(testProvider.getSchemas(), null);
  InputStream in = (InputStream) response.getEntity();
  return EntityProvider.readMetadata(in, true);

}
 
Example 4
Source File: ODataExceptionWrapperTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testCallbackWithLocales1() throws Exception {
  UriInfo uriInfo = getMockedUriInfo();
  HttpHeaders httpHeaders = Mockito.mock(HttpHeaders.class);
  List<Locale> locales = new ArrayList<Locale>();
  locales.add(Locale.GERMANY);
  locales.add(Locale.FRANCE);
  when(httpHeaders.getAcceptableLanguages()).thenReturn(locales);
  
  ODataErrorCallback errorCallback = new ODataErrorCallback() {
    @Override
    public ODataResponse handleError(final ODataErrorContext context) throws ODataApplicationException {
      assertEquals("de", context.getLocale().getLanguage());
      assertEquals("DE", context.getLocale().getCountry());
      return ODataResponse.entity("bla").status(HttpStatusCodes.BAD_REQUEST).contentHeader("text/html").build();
    }
  };

  ODataExceptionWrapper exceptionWrapper = createWrapper1(uriInfo, httpHeaders, errorCallback);
  ODataResponse response = exceptionWrapper.wrapInExceptionResponse(
      new ODataApplicationException("Error",Locale.GERMANY));

  // verify
  assertNotNull(response);
  assertEquals(HttpStatusCodes.BAD_REQUEST.getStatusCode(), response.getStatus().getStatusCode());
  String errorMessage = (String) response.getEntity();
  assertEquals("bla", errorMessage);
  String contentTypeHeader = response.getContentHeader();
  assertEquals("text/html", contentTypeHeader);
}
 
Example 5
Source File: BatchResponseWriterITTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testSimpleRequest() throws Exception {
  // Create batch response
  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();
  // Get boundary
  int lineEndingIndex = body.indexOf("\r\n");
  String boundary = body.substring(2, lineEndingIndex);

  // Parse response and test outputs
  final BatchParser parser = new BatchParser("multipart/mixed;boundary=" + boundary, true);
  List<BatchSingleResponse> parserResponses = parser.parseBatchResponse(new ByteArrayInputStream(body.getBytes()));
  for (BatchSingleResponse parserResponse : parserResponses) {
    assertEquals("200", parserResponse.getStatusCode());
    assertEquals("OK", parserResponse.getStatusInfo());
    assertEquals("application/json", parserResponse.getHeaders().get(HttpHeaders.CONTENT_TYPE));
    assertEquals("13", parserResponse.getHeaders().get(HttpHeaders.CONTENT_LENGTH));
    assertEquals("Walter Winter", parserResponse.getBody());
  }
}
 
Example 6
Source File: BatchResponseWriterITTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoContent() throws Exception {
  // Create batch response
  List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
  ODataResponse response =
      ODataResponse.status(HttpStatusCodes.NO_CONTENT).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();
  // Get boundary
  int lineEndingIndex = body.indexOf("\r\n");
  String boundary = body.substring(2, lineEndingIndex);

  // Parse response and test outputs
  final BatchParser parser = new BatchParser("multipart/mixed;boundary=" + boundary, true);
  List<BatchSingleResponse> parserResponses = parser.parseBatchResponse(new ByteArrayInputStream(body.getBytes()));
  for (BatchSingleResponse parserResponse : parserResponses) {
    assertEquals("204", parserResponse.getStatusCode());
    assertEquals("No Content", parserResponse.getStatusInfo());
  }
}
 
Example 7
Source File: BatchResponseTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testChangeSetResponse() throws BatchException, IOException {
  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());
  String body = (String) batchResponse.getEntity();
  assertTrue(body.contains("--batch"));
  assertTrue(body.contains("--changeset"));
  assertTrue(body.indexOf("--changeset") != body.lastIndexOf("--changeset"));
  assertFalse(body.contains("HTTP/1.1 200 OK" + "\r\n"));
  assertTrue(body.contains("Content-Type: application/http" + "\r\n"));
  assertTrue(body.contains("Content-Transfer-Encoding: binary" + "\r\n"));
  assertTrue(body.contains("HTTP/1.1 204 No Content" + "\r\n"));
  assertTrue(body.contains("Content-Type: multipart/mixed; boundary=changeset"));

  String contentHeader = batchResponse.getContentHeader();
  BatchParser parser = new BatchParser(contentHeader, true);
  List<BatchSingleResponse> result = parser.parseBatchResponse(new ByteArrayInputStream(body.getBytes()));
  assertEquals(1, result.size());
}
 
Example 8
Source File: BatchResponseTest.java    From olingo-odata2 with Apache License 2.0 5 votes vote down vote up
@Test
public void testTwoChangeSetResponse() throws BatchException, IOException {
  List<BatchResponsePart> parts = new ArrayList<BatchResponsePart>();
  ODataResponse changeSetResponse = ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
  ODataResponse changeSetResponseTwo = ODataResponse.status(HttpStatusCodes.NO_CONTENT).build();
  List<ODataResponse> responses = new ArrayList<ODataResponse>(1);
  responses.add(changeSetResponse);
  responses.add(changeSetResponseTwo);
  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());
  String body = (String) batchResponse.getEntity();
  assertTrue(body.contains("--batch"));
  assertTrue(body.contains("--changeset"));
  assertTrue(body.indexOf("--changeset") != body.lastIndexOf("--changeset"));
  assertFalse(body.contains("HTTP/1.1 200 OK" + "\r\n"));
  assertTrue(body.contains("Content-Type: application/http" + "\r\n"));
  assertTrue(body.contains("Content-Transfer-Encoding: binary" + "\r\n"));
  assertTrue(body.contains("HTTP/1.1 204 No Content" + "\r\n"));
  assertTrue(body.contains("Content-Type: multipart/mixed; boundary=changeset"));

  String contentHeader = batchResponse.getContentHeader();
  BatchParser parser = new BatchParser(contentHeader, true);
  StringHelper.Stream content = StringHelper.toStream(body);
  List<BatchSingleResponse> result = parser.parseBatchResponse(content.asStream());
  assertEquals(2, result.size());
  assertEquals("Failing content:\n" + content.asString(), 19, content.linesCount());
}
 
Example 9
Source File: ODataServlet.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
protected void createResponse(final HttpServletResponse resp, final ODataResponse response,
                              final boolean omitResponseBody)
    throws IOException {

  resp.setStatus(response.getStatus().getStatusCode());
  resp.setContentType(response.getContentHeader());
  for (String headerName : response.getHeaderNames()) {
    resp.setHeader(headerName, response.getHeader(headerName));
  }

  if(omitResponseBody) {
    return;
  }

  Object entity = response.getEntity();
  if (entity != null) {
    ServletOutputStream out = resp.getOutputStream();
    int contentLength;

    if (entity instanceof InputStream) {
      contentLength = handleStream((InputStream) entity, out);
    } else if (entity instanceof String) {
      String body = (String) entity;
      final byte[] entityBytes = body.getBytes(DEFAULT_READ_CHARSET);
      out.write(entityBytes);
      contentLength = entityBytes.length;
    } else {
      throw new IOException("Illegal entity object in ODataResponse of type '" + entity.getClass() + "'.");
    }

    if (response.getHeader(HttpHeaders.CONTENT_LENGTH) != null) {
      // Override content length
      try {
        contentLength = Integer.parseInt(response.getHeader(HttpHeaders.CONTENT_LENGTH));
      } catch (NumberFormatException e) {
        // Ignore
      }
    }

    resp.setContentLength(contentLength);
    out.flush();
    out.close();
  }
}
 
Example 10
Source File: BatchResponseWriterITTest.java    From olingo-odata2 with Apache License 2.0 4 votes vote down vote up
@Test
public void testChangeSet() 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());

  ODataResponse changeSetResponse =
      ODataResponse.status(HttpStatusCodes.NO_CONTENT).header(BatchHelper.MIME_HEADER_CONTENT_ID, "1").build();
  responses = new ArrayList<ODataResponse>(2);
  ODataResponse changeSetResponseEntity =
      ODataResponse.status(HttpStatusCodes.OK).contentHeader(ContentType.APPLICATION_JSON.toContentTypeString())
          .header(BatchHelper.MIME_HEADER_CONTENT_ID, "2")
          .entity("Test\r\n").build();
  ODataResponse changeSetResponseEntity2 =
      ODataResponse.status(HttpStatusCodes.OK).contentHeader(ContentType.APPLICATION_JSON.toContentTypeString())
          .header(BatchHelper.MIME_HEADER_CONTENT_ID, "2")
          .entity("Test\n").build();
  ODataResponse changeSetResponseEntity3 =
      ODataResponse.status(HttpStatusCodes.OK).contentHeader(ContentType.APPLICATION_JSON.toContentTypeString())
          .header(BatchHelper.MIME_HEADER_CONTENT_ID, "2")
          .entity("Test").build();
  responses.add(changeSetResponse);
  responses.add(changeSetResponseEntity);
  responses.add(changeSetResponseEntity2);
  responses.add(changeSetResponseEntity3);

  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());
  String body = (String) batchResponse.getEntity();

  // Get boundary
  int lineEndingIndex = body.indexOf("\r\n");
  String boundary = body.substring(2, lineEndingIndex);

  // Parse response and test outputs
  final BatchParser parser = new BatchParser("multipart/mixed;boundary=" + boundary, true);
  List<BatchSingleResponse> parserResponses = parser.parseBatchResponse(new ByteArrayInputStream(body.getBytes()));
  assertEquals(5, parserResponses.size());

  BatchSingleResponse parserResponse = parserResponses.get(0);
  assertEquals("200", parserResponse.getStatusCode());
  assertEquals("OK", parserResponse.getStatusInfo());
  assertEquals("application/json", parserResponse.getHeaders().get(HttpHeaders.CONTENT_TYPE));
  assertEquals("13", parserResponse.getHeaders().get(HttpHeaders.CONTENT_LENGTH));
  assertEquals("Walter Winter", parserResponse.getBody());

  parserResponse = parserResponses.get(1);
  assertEquals("204", parserResponse.getStatusCode());
  assertEquals("1", parserResponse.getContentId());
  assertEquals("No Content", parserResponse.getStatusInfo());

  parserResponse = parserResponses.get(2);
  assertEquals("200", parserResponse.getStatusCode());
  assertEquals("OK", parserResponse.getStatusInfo());
  assertEquals("application/json", parserResponse.getHeaders().get(HttpHeaders.CONTENT_TYPE));
  assertEquals("6", parserResponse.getHeaders().get(HttpHeaders.CONTENT_LENGTH));
  assertEquals("Test\r\n", parserResponse.getBody());

  parserResponse = parserResponses.get(3);
  assertEquals("200", parserResponse.getStatusCode());
  assertEquals("OK", parserResponse.getStatusInfo());
  assertEquals("application/json", parserResponse.getHeaders().get(HttpHeaders.CONTENT_TYPE));
  assertEquals("5", parserResponse.getHeaders().get(HttpHeaders.CONTENT_LENGTH));
  assertEquals("Test\n", parserResponse.getBody());

  parserResponse = parserResponses.get(4);
  assertEquals("200", parserResponse.getStatusCode());
  assertEquals("OK", parserResponse.getStatusInfo());
  assertEquals("application/json", parserResponse.getHeaders().get(HttpHeaders.CONTENT_TYPE));
  assertEquals("4", parserResponse.getHeaders().get(HttpHeaders.CONTENT_LENGTH));
  assertEquals("Test", parserResponse.getBody());
}