List of usage examples for org.apache.commons.io IOUtils toInputStream
public static InputStream toInputStream(String input)
From source file:com.unicodecollective.oxml.OxmlParserTests.java
private XMLEventReader stringReader(String xml) throws XMLStreamException { return xmlInputFactory.createXMLEventReader(IOUtils.toInputStream(xml)); }
From source file:com.msopentech.odatajclient.testservice.utils.Commons.java
public static InputStream getLinksAsATOM(final Map.Entry<String, Collection<String>> link) throws IOException { final StringBuilder builder = new StringBuilder(); builder.append("<?xml version=\"1.0\" encoding=\"utf-8\"?>"); builder.append("<links xmlns=\"http://schemas.microsoft.com/ado/2007/08/dataservices\">"); for (String uri : link.getValue()) { builder.append("<uri>"); if (URI.create(uri).isAbsolute()) { builder.append(uri);//from ww w.j a va 2 s . c o m } else { builder.append(DEFAULT_SERVICE_URL).append(uri); } builder.append("</uri>"); } builder.append("</links>"); return IOUtils.toInputStream(builder.toString()); }
From source file:ddf.content.endpoint.rest.ContentEndpointCreateTest.java
/** * No Content-Type specified by client, so it should default to text/plain * and be refined by ContentEndpoint to application/json;id=geojson based on the filename's * extension of "json"./*w ww . j a v a 2 s . com*/ * * @throws Exception */ @Test public void testParseAttachmentContentTypeNotSpecified() throws Exception { InputStream is = IOUtils.toInputStream(TEST_JSON); MetadataMap<String, String> headers = new MetadataMap<String, String>(); headers.add(ContentEndpoint.CONTENT_DISPOSITION, "form-data; name=file; filename=C:\\DDF\\geojson_valid.json"); Attachment attachment = new Attachment(is, headers); ContentFramework framework = mock(ContentFramework.class); ContentEndpoint endpoint = new ContentEndpoint(framework, getMockMimeTypeMapper()); CreateInfo createInfo = endpoint.parseAttachment(attachment); Assert.assertNotNull(createInfo); Assert.assertEquals("application/json;id=geojson", createInfo.getContentType()); Assert.assertEquals("geojson_valid.json", createInfo.getFilename()); }
From source file:com.msopentech.odatajclient.engine.it.AsyncTestITCase.java
/** * @see MediaEntityTest#createMediaEntity(com.msopentech.odatajclient.engine.format.ODataPubFormat) *//* ww w.java 2 s . c o m*/ @Test public void createMediaEntity() throws InterruptedException, ExecutionException, IOException { URIBuilder builder = client.getURIBuilder(testDefaultServiceRootURL).appendEntitySetSegment("Car"); final String TO_BE_UPDATED = "async buffered stream sample"; final InputStream input = IOUtils.toInputStream(TO_BE_UPDATED); final ODataMediaEntityCreateRequest createReq = client.getStreamedRequestFactory() .getMediaEntityCreateRequest(builder.build(), input); final ODataMediaEntityCreateRequest.MediaEntityCreateStreamManager streamManager = createReq.execute(); final Future<ODataMediaEntityCreateResponse> futureCreateRes = streamManager.getAsyncResponse(); while (!futureCreateRes.isDone()) { } final ODataMediaEntityCreateResponse createRes = futureCreateRes.get(); assertEquals(201, createRes.getStatusCode()); final ODataEntity created = createRes.getBody(); assertNotNull(created); assertEquals(2, created.getProperties().size()); final int id = "VIN".equals(created.getProperties().get(0).getName()) ? created.getProperties().get(0).getPrimitiveValue().<Integer>toCastValue() : created.getProperties().get(1).getPrimitiveValue().<Integer>toCastValue(); builder = client.getURIBuilder(testDefaultServiceRootURL).appendEntityTypeSegment("Car") .appendKeySegment(id).appendValueSegment(); final ODataMediaRequest retrieveReq = client.getRetrieveRequestFactory().getMediaRequest(builder.build()); final ODataRetrieveResponse<InputStream> retrieveRes = retrieveReq.execute(); assertEquals(200, retrieveRes.getStatusCode()); assertEquals(TO_BE_UPDATED, IOUtils.toString(retrieveRes.getBody())); }
From source file:com.nike.cerberus.service.S3StoreServiceTest.java
@Test public void testGetNoSuchKey() { AmazonS3 client = mock(AmazonS3.class); S3StoreService service = new S3StoreService(client, S3_BUCKET, S3_PREFIX); String path = "path"; String value = "value"; ArgumentCaptor<GetObjectRequest> request = ArgumentCaptor.forClass(GetObjectRequest.class); S3Object s3Object = new S3Object(); s3Object.setObjectContent(/*from w w w . j a v a 2 s.co m*/ new S3ObjectInputStream(IOUtils.toInputStream(value), mock(HttpRequestBase.class))); AmazonServiceException error = new AmazonServiceException("fake expected exception"); error.setErrorCode("NoSuchKey"); when(client.getObject(request.capture())).thenThrow(error); // invoke method under test Optional<String> result = service.get(path); assertFalse(result.isPresent()); assertEquals(S3_BUCKET, request.getValue().getBucketName()); assertEquals(S3_PREFIX + "/" + path, request.getValue().getKey()); }
From source file:com.msopentech.odatajclient.engine.performance.BasicPerfTest.java
private void readViaODataJClient(final ODataPubFormat format) { final ODataEntity entity = getClient().getBinder().getODataEntity(getClient().getDeserializer() .toEntry(IOUtils.toInputStream(input.get(format)), ResourceFactory.entryClassForFormat(format))); assertNotNull(entity);//from w w w . j a va 2 s .c o m }
From source file:ee.ria.xroad.common.signature.SignatureVerifierTest.java
/** * Tests that verifying a valid signature succeeds. * @throws Exception if error occurs/*from ww w . ja v a 2 s . c o m*/ */ @Test public void verifyValidSignatureHashChain() throws Exception { Resolver resolver; resolver = new Resolver() { @Override public InputStream resolve(String uri) throws IOException { if ("/attachment1".equals(uri)) { // Returns the attachment content return IOUtils.toInputStream("blaah"); } else { return super.resolve(uri); } } }.withHashChain("src/test/signatures/hash-chain-1.xml").withMessage("src/test/signatures/message-1.xml"); createSignatureVerifier("src/test/signatures/batch-sig.xml", "src/test/signatures/hash-chain-result.xml", resolver).verify(CONSUMER_ID, CORRECT_VALIDATION_DATE); }
From source file:com.allogy.mime.MimeStreamingReaderTest.java
@Test(expected = IOException.class) public void getContentInputStream_should_throw_exception_if_header_part_ends_without_carriage_return_line_feed() throws IOException { StringBuilder mimeBodyPartStringBuilder = new StringBuilder(); String header = "header: value\r\n"; mimeBodyPartStringBuilder.append(header); mimeBodyPartStringBuilder.append("\r"); mimeBodyPartStringBuilder.append(contentString); mimeInputStream = IOUtils.toInputStream(mimeBodyPartStringBuilder.toString()); createObjectUnderTest().getContentInputStream(); }
From source file:com.nanocrawler.contentparser.HtmlContentParser.java
private Document parseHtml(Page page, HtmlContent c) throws SAXException, IOException { String html;//from w w w . ja va2 s . c o m // Handle char type conversions based on the byte stream, not based on what the server says UniversalDetector detector = new UniversalDetector(null); detector.handleData(page.getContentData(), 0, page.getContentData().length); detector.dataEnd(); String encoding = detector.getDetectedCharset(); if (encoding != null) { page.setContentCharset(encoding); try { html = new String(page.getContentData(), encoding); } catch (Exception ex) { html = new String(page.getContentData()); } } else { html = new String(page.getContentData()); } html = html.trim(); c.setHtml(html); HtmlDocumentBuilder builder = new HtmlDocumentBuilder(); builder.setCommentPolicy(XmlViolationPolicy.ALTER_INFOSET); builder.setContentNonXmlCharPolicy(XmlViolationPolicy.ALTER_INFOSET); builder.setContentSpacePolicy(XmlViolationPolicy.ALTER_INFOSET); builder.setNamePolicy(XmlViolationPolicy.ALTER_INFOSET); builder.setStreamabilityViolationPolicy(XmlViolationPolicy.ALTER_INFOSET); builder.setXmlnsPolicy(XmlViolationPolicy.ALTER_INFOSET); builder.setMappingLangToXmlLang(true); builder.setHtml4ModeCompatibleWithXhtml1Schemata(true); builder.setHeuristics(Heuristics.ALL); builder.setCheckingNormalization(false); builder.setDoctypeExpectation(DoctypeExpectation.NO_DOCTYPE_ERRORS); builder.setIgnoringComments(true); builder.setScriptingEnabled(true); builder.setXmlPolicy(XmlViolationPolicy.ALTER_INFOSET); Document doc = builder.parse(IOUtils.toInputStream(html)); if (doc.getElementsByTagName(BODY_ELEMENT).getLength() == 0) { throw new RuntimeException("Problem parsing document - invalid HTML, no body element found"); } return doc; }
From source file:com.temenos.useragent.generic.mediatype.HalJsonEntityHandler.java
@Override public InputStream getContent() { return IOUtils.toInputStream( representation.toString(RepresentationFactory.HAL_JSON, RepresentationFactory.PRETTY_PRINT)); }