@Test public void spacesAtBeginningAndDifferentNewlinesTest() throws WebApplicationException, IOException { for (String newLineToUse : new String[] { "\n", "\r", "\r\n" }) { PlainTextExamplesMessageBodyReader toTest = new PlainTextExamplesMessageBodyReader(); MediaType mediaType = MediaType.TEXT_PLAIN_TYPE; StringBuilder theExamples = new StringBuilder(); theExamples.append("Example 1"); theExamples.append(newLineToUse); theExamples.append("Example 2"); theExamples.append(newLineToUse); theExamples.append("Example3 and 4 and 5"); theExamples.append(newLineToUse); theExamples.append(newLineToUse); ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream( theExamples.toString().getBytes( ReaderWriter.getCharset(mediaType))); Iterable<Example> theIterable = toTest.readFrom(null, null, null, mediaType, null, byteArrayInputStream); int x = 0; for (Example example : theIterable) { switch (x++) { case 0: Assert.assertEquals("Example 1", example.getVWStringRepresentation()); break; case 1: Assert.assertEquals("Example 2", example.getVWStringRepresentation()); break; case 2: Assert.assertEquals("Example3 and 4 and 5", example.getVWStringRepresentation()); break; case 3: Assert.assertEquals("", example.getVWStringRepresentation()); break; default: Assert.fail(); } } Assert.assertEquals(4, x); } }
@Override public ExamplesIterable readFrom(Class<ExamplesIterable> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { if (LOGGER.isDebugEnabled()) if (httpHeaders != null && httpHeaders.size() > 0) { LOGGER.debug("Rec'd HTTP headers: "); for (Entry<String, List<String>> entry : httpHeaders.entrySet()) { LOGGER.debug("{}:{}", entry.getKey(), StringUtils.join(entry.getValue(), ',')); } } Charset charset = ReaderWriter.getCharset(mediaType); LOGGER.debug("Reading examples using charset: {}", charset.displayName()); StringExampleIterator theIterator = new StringExampleIterator(entityStream, charset); return new ExamplesIterableImpl(Integer.MAX_VALUE, null, theIterator); }
PlainTextExamplesMessageBodyReader implements MessageBodyReader<ExamplesIterable> { @Override public ExamplesIterable readFrom(Class<ExamplesIterable> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { if (LOGGER.isDebugEnabled()) if (httpHeaders != null && httpHeaders.size() > 0) { LOGGER.debug("Rec'd HTTP headers: "); for (Entry<String, List<String>> entry : httpHeaders.entrySet()) { LOGGER.debug("{}:{}", entry.getKey(), StringUtils.join(entry.getValue(), ',')); } } Charset charset = ReaderWriter.getCharset(mediaType); LOGGER.debug("Reading examples using charset: {}", charset.displayName()); StringExampleIterator theIterator = new StringExampleIterator(entityStream, charset); return new ExamplesIterableImpl(Integer.MAX_VALUE, null, theIterator); } }
PlainTextExamplesMessageBodyReader implements MessageBodyReader<ExamplesIterable> { @Override public ExamplesIterable readFrom(Class<ExamplesIterable> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { if (LOGGER.isDebugEnabled()) if (httpHeaders != null && httpHeaders.size() > 0) { LOGGER.debug("Rec'd HTTP headers: "); for (Entry<String, List<String>> entry : httpHeaders.entrySet()) { LOGGER.debug("{}:{}", entry.getKey(), StringUtils.join(entry.getValue(), ',')); } } Charset charset = ReaderWriter.getCharset(mediaType); LOGGER.debug("Reading examples using charset: {}", charset.displayName()); StringExampleIterator theIterator = new StringExampleIterator(entityStream, charset); return new ExamplesIterableImpl(Integer.MAX_VALUE, null, theIterator); } PlainTextExamplesMessageBodyReader(); }
PlainTextExamplesMessageBodyReader implements MessageBodyReader<ExamplesIterable> { @Override public ExamplesIterable readFrom(Class<ExamplesIterable> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { if (LOGGER.isDebugEnabled()) if (httpHeaders != null && httpHeaders.size() > 0) { LOGGER.debug("Rec'd HTTP headers: "); for (Entry<String, List<String>> entry : httpHeaders.entrySet()) { LOGGER.debug("{}:{}", entry.getKey(), StringUtils.join(entry.getValue(), ',')); } } Charset charset = ReaderWriter.getCharset(mediaType); LOGGER.debug("Reading examples using charset: {}", charset.displayName()); StringExampleIterator theIterator = new StringExampleIterator(entityStream, charset); return new ExamplesIterableImpl(Integer.MAX_VALUE, null, theIterator); } PlainTextExamplesMessageBodyReader(); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override ExamplesIterable readFrom(Class<ExamplesIterable> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream); }
PlainTextExamplesMessageBodyReader implements MessageBodyReader<ExamplesIterable> { @Override public ExamplesIterable readFrom(Class<ExamplesIterable> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream) throws IOException, WebApplicationException { if (LOGGER.isDebugEnabled()) if (httpHeaders != null && httpHeaders.size() > 0) { LOGGER.debug("Rec'd HTTP headers: "); for (Entry<String, List<String>> entry : httpHeaders.entrySet()) { LOGGER.debug("{}:{}", entry.getKey(), StringUtils.join(entry.getValue(), ',')); } } Charset charset = ReaderWriter.getCharset(mediaType); LOGGER.debug("Reading examples using charset: {}", charset.displayName()); StringExampleIterator theIterator = new StringExampleIterator(entityStream, charset); return new ExamplesIterableImpl(Integer.MAX_VALUE, null, theIterator); } PlainTextExamplesMessageBodyReader(); @Override boolean isReadable(Class<?> type, Type genericType, Annotation[] annotations, MediaType mediaType); @Override ExamplesIterable readFrom(Class<ExamplesIterable> type, Type genericType, Annotation[] annotations, MediaType mediaType, MultivaluedMap<String, String> httpHeaders, InputStream entityStream);  }