Search in sources :

Example 21 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class OSGiCacheManager method getClassLoader.

private ClassLoader getClassLoader(String providerName) throws Exception {
    if (providerName == null || !getConfiguration().isLookupProviders()) {
        return null;
    }
    final BundleContext bc = FrameworkUtil.getBundle(JCacheHelper.class).getBundleContext();
    final ClassLoader bcl = bc.getBundle().adapt(BundleWiring.class).getClassLoader();
    final ClassLoader acl = getConfiguration().getApplicationContextClassLoader();
    for (final Bundle bundle : bc.getBundles()) {
        URL spi = bundle.getResource("META-INF/services/javax.cache.spi.CachingProvider");
        if (spi != null) {
            try (BufferedReader in = new BufferedReader(new InputStreamReader(spi.openStream()))) {
                if (ObjectHelper.equal(providerName, in.readLine())) {
                    return new ClassLoader(bcl) {

                        @Override
                        protected Class<?> findClass(String name) throws ClassNotFoundException {
                            try {
                                return acl.loadClass(name);
                            } catch (ClassNotFoundException e) {
                                return bundle.loadClass(name);
                            }
                        }

                        @Override
                        protected URL findResource(String name) {
                            URL resource = acl.getResource(name);
                            if (resource == null) {
                                resource = bundle.getResource(name);
                            }
                            return resource;
                        }

                        @Override
                        protected Enumeration findResources(String name) throws IOException {
                            try {
                                return acl.getResources(name);
                            } catch (IOException e) {
                                return bundle.getResources(name);
                            }
                        }
                    };
                }
            }
        }
    }
    return null;
}
Also used : InputStreamReader(java.io.InputStreamReader) Bundle(org.osgi.framework.Bundle) BundleWiring(org.osgi.framework.wiring.BundleWiring) BufferedReader(java.io.BufferedReader) JCacheHelper(org.apache.camel.component.jcache.JCacheHelper) IOException(java.io.IOException) URL(java.net.URL) BundleContext(org.osgi.framework.BundleContext)

Example 22 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class HandlerTest method testWithTwoHandlersTwoEndpointsOnSamePort.

@Test
public void testWithTwoHandlersTwoEndpointsOnSamePort() throws Exception {
    // First test the situation where one should invoke the handler once
    assertEquals(0, statisticsHandler1.getRequests());
    assertEquals(0, statisticsHandler2.getRequests());
    assertEquals(0, statisticsHandler3.getRequests());
    InputStream html1 = (InputStream) template.requestBody("http://localhost:" + port2, "");
    BufferedReader br1 = IOHelper.buffered(new InputStreamReader(html1));
    assertEquals(htmlResponse, br1.readLine());
    InputStream html2 = (InputStream) template.requestBody("http://localhost:" + port2 + "/endpoint2", "");
    BufferedReader br2 = IOHelper.buffered(new InputStreamReader(html2));
    assertEquals(htmlResponse, br2.readLine());
    assertEquals(0, statisticsHandler1.getRequests());
    assertEquals(2, statisticsHandler2.getRequests());
    assertEquals(2, statisticsHandler3.getRequests());
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 23 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class HandlerTest method testWithTwoHandlers.

@Test
public void testWithTwoHandlers() throws Exception {
    // First test the situation where one should invoke the handler once
    assertEquals(0, statisticsHandler1.getRequests());
    assertEquals(0, statisticsHandler2.getRequests());
    assertEquals(0, statisticsHandler3.getRequests());
    InputStream html = (InputStream) template.requestBody("http://localhost:" + port2, "");
    BufferedReader br = IOHelper.buffered(new InputStreamReader(html));
    assertEquals(htmlResponse, br.readLine());
    assertEquals(0, statisticsHandler1.getRequests());
    assertEquals(1, statisticsHandler2.getRequests());
    assertEquals(1, statisticsHandler3.getRequests());
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) Test(org.junit.Test)

Example 24 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class RestJettyBindingModeJsonWithContractTest method testBindingModeJsonWithContract.

@Test
public void testBindingModeJsonWithContract() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:input");
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(UserPojoEx.class);
    String body = "{\"id\": 123, \"name\": \"Donald Duck\"}";
    Object answer = template.requestBody("http://localhost:" + getPort() + "/users/new", body);
    assertNotNull(answer);
    BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) answer));
    String line;
    String answerString = "";
    while ((line = reader.readLine()) != null) {
        answerString += line;
    }
    assertTrue("Unexpected response: " + answerString, answerString.contains("\"active\":true"));
    assertMockEndpointsSatisfied();
    Object obj = mock.getReceivedExchanges().get(0).getIn().getBody();
    assertEquals(UserPojoEx.class, obj.getClass());
    UserPojoEx user = (UserPojoEx) obj;
    assertNotNull(user);
    assertEquals(123, user.getId());
    assertEquals("Donald Duck", user.getName());
    assertEquals(true, user.isActive());
}
Also used : InputStreamReader(java.io.InputStreamReader) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Example 25 with InputStreamReader

use of java.io.InputStreamReader in project camel by apache.

the class RestJettyBindingModeOffWithContractTest method testBindingModeOffWithContract.

@Test
public void testBindingModeOffWithContract() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:input");
    mock.expectedMessageCount(1);
    mock.message(0).body().isInstanceOf(UserPojoEx.class);
    String body = "{\"id\": 123, \"name\": \"Donald Duck\"}";
    Object answer = template.requestBodyAndHeader("http://localhost:" + getPort() + "/users/new", body, Exchange.CONTENT_TYPE, "application/json");
    assertNotNull(answer);
    BufferedReader reader = new BufferedReader(new InputStreamReader((InputStream) answer));
    String line;
    String answerString = "";
    while ((line = reader.readLine()) != null) {
        answerString += line;
    }
    assertTrue("Unexpected response: " + answerString, answerString.contains("\"active\":true"));
    assertMockEndpointsSatisfied();
    Object obj = mock.getReceivedExchanges().get(0).getIn().getBody();
    assertEquals(UserPojoEx.class, obj.getClass());
    UserPojoEx user = (UserPojoEx) obj;
    assertNotNull(user);
    assertEquals(123, user.getId());
    assertEquals("Donald Duck", user.getName());
    assertEquals(true, user.isActive());
}
Also used : InputStreamReader(java.io.InputStreamReader) MockEndpoint(org.apache.camel.component.mock.MockEndpoint) InputStream(java.io.InputStream) BufferedReader(java.io.BufferedReader) BaseJettyTest(org.apache.camel.component.jetty.BaseJettyTest) Test(org.junit.Test)

Aggregations

InputStreamReader (java.io.InputStreamReader)4181 BufferedReader (java.io.BufferedReader)2938 IOException (java.io.IOException)1843 InputStream (java.io.InputStream)1083 FileInputStream (java.io.FileInputStream)767 ArrayList (java.util.ArrayList)494 URL (java.net.URL)471 File (java.io.File)448 Reader (java.io.Reader)446 Test (org.junit.Test)336 HttpURLConnection (java.net.HttpURLConnection)253 ByteArrayInputStream (java.io.ByteArrayInputStream)231 OutputStreamWriter (java.io.OutputStreamWriter)218 FileNotFoundException (java.io.FileNotFoundException)210 HashMap (java.util.HashMap)171 Socket (java.net.Socket)170 OutputStream (java.io.OutputStream)150 URLConnection (java.net.URLConnection)148 StringWriter (java.io.StringWriter)135 Path (org.apache.hadoop.fs.Path)123