Java Code Examples for org.apache.cxf.helpers.IOUtils#readStringFromStream()

The following examples show how to use org.apache.cxf.helpers.IOUtils#readStringFromStream() . 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: ClientCacheTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testGetTimeStringAsInputStreamAndString() throws Exception {
    CacheControlFeature feature = new CacheControlFeature();
    try {
        feature.setCacheResponseInputStream(true);
        final WebTarget base = ClientBuilder.newBuilder().register(feature).build().target(ADDRESS);
        final Invocation.Builder cached = base.request("text/plain").header(HttpHeaders.CACHE_CONTROL, "public");
        final Response r = cached.get();
        assertEquals(Response.Status.OK.getStatusCode(), r.getStatus());
        InputStream is = r.readEntity(InputStream.class);
        final String r1 = IOUtils.readStringFromStream(is);
        waitABit();
        // CassCastException would occur without a cached stream support
        final String r2 = cached.get().readEntity(String.class);
        assertEquals(r1, r2);
    } finally {
        feature.close();
    }
}
 
Example 2
Source File: CodeGenBugTest.java    From cxf with Apache License 2.0 6 votes vote down vote up
@Test
public void testCXF3105() throws Exception {
    String[] args = new String[] {"-d", output.getCanonicalPath(), "-impl", "-server", "-client", "-b",
                                  getLocation("/wsdl2java_wsdl/cxf3105/ws-binding.xml"),
                                  getLocation("/wsdl2java_wsdl/cxf3105/cxf3105.wsdl")};
    CommandInterfaceUtils.commandCommonMain();
    WSDLToJava w2j = new WSDLToJava(args);
    w2j.run(new ToolContext());

    assertNotNull(output);
    File f = new File(output, "org/apache/cxf/testcase/cxf3105/Login.java");
    assertTrue(f.exists());
    String contents = IOUtils.readStringFromStream(new FileInputStream(f));
    assertTrue(contents.contains("Loginrequesttype loginRequest"));
    assertTrue(contents.contains("<Loginresponsetype> loginResponse"));
}
 
Example 3
Source File: JavaDocProvider.java    From cxf with Apache License 2.0 6 votes vote down vote up
private ClassDocs getClassDocInternal(Class<?> cls) throws Exception {
    Class<?> annotatedClass = getPathAnnotatedClass(cls);
    String resource = annotatedClass.getName().replace(".", "/") + ".html";
    ClassDocs classDocs = docs.get(resource);
    if (classDocs == null) {
        ClassLoader loader = javaDocLoader != null ? javaDocLoader : annotatedClass.getClassLoader();
        InputStream resourceStream = loader.getResourceAsStream(resource);
        if (resourceStream != null) {
            String doc = IOUtils.readStringFromStream(resourceStream);

            String qualifier = annotatedClass.isInterface() ? "Interface" : "Class";
            String classMarker = qualifier + " " + annotatedClass.getSimpleName();
            int index = doc.indexOf(classMarker);
            if (index != -1) {
                String classInfoTag = getClassInfoTag();
                String classInfo = getJavaDocText(doc, classInfoTag,
                                                  "Method Summary", index + classMarker.length());
                classDocs = new ClassDocs(doc, classInfo);
                docs.putIfAbsent(resource, classDocs);
            }
        }
    }
    return classDocs;
}
 
Example 4
Source File: AccessMtomServiceResource.java    From dropwizard-jaxws with Apache License 2.0 6 votes vote down vote up
@GET
@Timed
public String getFoo() {

    ObjectFactory of = new ObjectFactory();
    Hello h = of.createHello();
    h.setTitle("Hello");
    h.setBinary(new DataHandler(new ByteArrayDataSource("test".getBytes(), "text/plain")));

    HelloResponse hr = mtomServiceClient.hello(h);

    try {
        return "Hello response: " + hr.getTitle() + ", " +
                IOUtils.readStringFromStream(hr.getBinary().getInputStream());
    }
    catch (IOException e) {
        throw new RuntimeException(e);
    }
}
 
Example 5
Source File: MultipartStore.java    From cxf with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/books/istream2")
@Produces("text/xml")
public Book addBookFromInputStreamReadItself(InputStream is) throws Exception {

    String body = IOUtils.readStringFromStream(is);
    if (!body.trim().startsWith("--")) {
        throw new RuntimeException();
    }

    return new Book("432", 432L);
}
 
Example 6
Source File: JAXRSServicesListingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapeHTMLUnformatted() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/service_listing/services/<script>alert(1)</script>/../../?formatted=false";

    URL url = new URL(endpointAddress);
    try (InputStream input = url.openStream()) {
        String result = IOUtils.readStringFromStream(input);
        assertFalse(result.contains("<script>"));
    }
}
 
Example 7
Source File: JAXRSServicesListingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testServiceListing() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/service_listing/services";
    String expectedResult =
        "http://localhost:" + PORT + "/service_listing/services/resources";

    URL url = new URL(endpointAddress);
    try (InputStream input = url.openStream()) {
        String result = IOUtils.readStringFromStream(input);
        assertTrue(result.contains(expectedResult));
    }
}
 
Example 8
Source File: JAXRSSoapBookTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testNoBook357WebClient() throws Exception {

    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    Map<String, Object> properties = new HashMap<>();
    properties.put("org.apache.cxf.http.throw_io_exceptions", Boolean.TRUE);
    bean.setProperties(properties);
    bean.setAddress("http://localhost:" + PORT + "/test/services/rest/bookstore/356");
    WebClient wc = bean.createWebClient();
    Response response = wc.get();
    assertEquals(404, response.getStatus());
    String msg = IOUtils.readStringFromStream((InputStream)response.getEntity());
    assertEquals("No Book with id 356 is available", msg);

}
 
Example 9
Source File: BookStoreSpring.java    From cxf with Apache License 2.0 5 votes vote down vote up
@POST
@Path("/bookform")
@Consumes("application/xml")
@Produces("application/xml")
public String echoBookFormXml(@Context HttpServletRequest req) throws IOException {
    InputStream is = req.getInputStream();
    return IOUtils.readStringFromStream(is);
}
 
Example 10
Source File: JwkUtils.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static JsonWebKeys loadJwkSet(Properties props, Bus bus, JweDecryptionProvider jwe) {
    String keyContent = null;
    String keyStoreLoc = props.getProperty(JoseConstants.RSSEC_KEY_STORE_FILE);
    if (keyStoreLoc != null) {
        try (InputStream isResource = JoseUtils.getResourceStream(keyStoreLoc, bus)) {
            if (isResource == null) {
                throw new JwkException("Error in loading keystore location: " + keyStoreLoc);
            }
            keyContent = IOUtils.readStringFromStream(isResource);
        } catch (IOException ex) {
            throw new JwkException(ex);
        }
    } else {
        keyContent = props.getProperty(JoseConstants.RSSEC_KEY_STORE_JWKSET);
        if (keyContent == null) {
            keyContent = props.getProperty(JoseConstants.RSSEC_KEY_STORE_JWKKEY);
        }
    }
    if (jwe != null) {
        keyContent = jwe.decrypt(keyContent).getContentText();
    }
    JwkReaderWriter reader = new JwkReaderWriter();
    if (props.getProperty(JoseConstants.RSSEC_KEY_STORE_JWKKEY) == null) {
        return reader.jsonToJwkSet(keyContent);
    }
    JsonWebKey jwk = reader.jsonToJwk(keyContent);
    return new JsonWebKeys(jwk);
}
 
Example 11
Source File: JAXRSMultithreadedClientTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
private void verifyResponse(Response response, String actualBookName, String actualHeaderName)
    throws Exception {
    List<Object> customHeaders = response.getMetadata().get("CustomHeader");
    assertEquals(customHeaders.size(), 1);
    assertEquals(actualHeaderName, customHeaders.get(0).toString());
    String responseValue = IOUtils.readStringFromStream((InputStream)response.getEntity());
    assertEquals(actualBookName, responseValue);
}
 
Example 12
Source File: JAXWSServicesListingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapeHTMLUnformatted() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/service_listing/services/<script>alert(1)</script>/../../?formatted=false";

    URL url = new URL(endpointAddress);
    try (InputStream input = url.openStream()) {
        String result = IOUtils.readStringFromStream(input);
        assertFalse(result.contains("<script>"));
    }
}
 
Example 13
Source File: JAXWSServicesListingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testEscapeHTML() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/service_listing/services/<script>alert(1)</script>/../../";

    URL url = new URL(endpointAddress);
    try (InputStream input = url.openStream()) {
        String result = IOUtils.readStringFromStream(input);
        assertFalse(result.contains("<script>"));
    }
}
 
Example 14
Source File: JsonMapObjectProvider.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Override
public JsonMapObject readFrom(Class<JsonMapObject> cls, Type t, Annotation[] anns, MediaType mt,
                              MultivaluedMap<String, String> headers, InputStream is) throws IOException,
    WebApplicationException {
    String s = IOUtils.readStringFromStream(is);
    try {
        JsonMapObject obj = cls == JsonMapObject.class ? new JsonMapObject() : cls.newInstance();
        handler.fromJson(obj, s);
        return obj;
    } catch (Exception ex) {
        throw new IOException(ex);
    }

}
 
Example 15
Source File: JAXWSServicesListingTest.java    From cxf with Apache License 2.0 5 votes vote down vote up
@Test
public void testServiceListing() throws Exception {

    String endpointAddress =
        "http://localhost:" + PORT + "/service_listing/services";
    String expectedResult =
        "http://localhost:" + PORT + "/service_listing/services/SoapContext/GreeterPort";

    URL url = new URL(endpointAddress);
    try (InputStream input = url.openStream()) {
        String result = IOUtils.readStringFromStream(input);
        assertTrue(result.contains(expectedResult));
    }
}
 
Example 16
Source File: PolicyTestHelper.java    From cxf with Apache License 2.0 5 votes vote down vote up
public static void updatePolicyRef(String file, String oldData,
                                   String newData) throws IOException {
    File f = FileUtils.getDefaultTempDir();
    InputStream in = PolicyTestHelper.class.getResourceAsStream(file);
    String s = IOUtils.readStringFromStream(in);
    s = s.replaceAll(oldData, newData);
    File newFile = new File(f, file);
    FileWriter fw = new FileWriter(newFile);
    fw.write(s);
    fw.close();
}
 
Example 17
Source File: ResourceHostHistoricalMetricsTest.java    From peer-os with Apache License 2.0 5 votes vote down vote up
@Before
public void setUp() throws Exception
{
    InputStream is =
            ResourceHostHistoricalMetricsTest.class.getClassLoader().getResourceAsStream( "rh-metric.dat" );

    String metricString = IOUtils.readStringFromStream( is );

    metric = mapper.readValue( metricString, HistoricalMetrics.class );
}
 
Example 18
Source File: BookStore.java    From cxf with Apache License 2.0 4 votes vote down vote up
public String[] readFrom(Class<String[]> arg0, Type arg1,
    Annotation[] arg2, MediaType arg3, MultivaluedMap<String, String> arg4, InputStream arg5)
    throws IOException, WebApplicationException {
    return new String[] {IOUtils.readStringFromStream(arg5)};
}
 
Example 19
Source File: JAXRSMultipartTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
@Test
public void testXopWebClient() throws Exception {
    String address = "http://localhost:" + PORT + "/bookstore/xop";
    JAXRSClientFactoryBean bean = new JAXRSClientFactoryBean();
    bean.setAddress(address);
    bean.setProperties(Collections.singletonMap(org.apache.cxf.message.Message.MTOM_ENABLED,
                                                (Object)"true"));
    WebClient client = bean.createWebClient();
    WebClient.getConfig(client).getInInterceptors().add(new LoggingInInterceptor());
    WebClient.getConfig(client).getOutInterceptors().add(new LoggingOutInterceptor());
    WebClient.getConfig(client).getRequestContext().put("support.type.as.multipart",
        "true");
    client.type("multipart/related").accept("multipart/related");

    XopType xop = new XopType();
    xop.setName("xopName");
    InputStream is =
        getClass().getResourceAsStream("/org/apache/cxf/systest/jaxrs/resources/book.xsd");
    byte[] data = IOUtils.readBytesFromStream(is);
    xop.setAttachinfo(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));
    xop.setAttachInfoRef(new DataHandler(new ByteArrayDataSource(data, "application/octet-stream")));

    String bookXsd = IOUtils.readStringFromStream(getClass().getResourceAsStream(
        "/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
    xop.setAttachinfo2(bookXsd.getBytes());

    xop.setImage(getImage("/org/apache/cxf/systest/jaxrs/resources/java.jpg"));

    XopType xop2 = client.post(xop, XopType.class);

    String bookXsdOriginal = IOUtils.readStringFromStream(getClass().getResourceAsStream(
            "/org/apache/cxf/systest/jaxrs/resources/book.xsd"));
    String bookXsd2 = IOUtils.readStringFromStream(xop2.getAttachinfo().getInputStream());
    assertEquals(bookXsdOriginal, bookXsd2);
    String bookXsdRef = IOUtils.readStringFromStream(xop2.getAttachInfoRef().getInputStream());
    assertEquals(bookXsdOriginal, bookXsdRef);

    String ctString =
        client.getResponse().getMetadata().getFirst("Content-Type").toString();
    MediaType mt = MediaType.valueOf(ctString);
    Map<String, String> params = mt.getParameters();
    assertEquals(4, params.size());
    assertNotNull(params.get("boundary"));
    assertNotNull(params.get("type"));
    assertNotNull(params.get("start"));
    assertNotNull(params.get("start-info"));
}
 
Example 20
Source File: JwkJoseCookBookTest.java    From cxf with Apache License 2.0 4 votes vote down vote up
public JsonWebKeys readKeySet(String fileName) throws Exception {
    InputStream is = JwkJoseCookBookTest.class.getResourceAsStream(fileName);
    String s = IOUtils.readStringFromStream(is);
    return JwkUtils.readJwkSet(s);
}