Search in sources :

Example 16 with BufferedReader

use of java.io.BufferedReader 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 17 with BufferedReader

use of java.io.BufferedReader 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 18 with BufferedReader

use of java.io.BufferedReader 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 19 with BufferedReader

use of java.io.BufferedReader 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 20 with BufferedReader

use of java.io.BufferedReader 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

BufferedReader (java.io.BufferedReader)4790 InputStreamReader (java.io.InputStreamReader)2944 IOException (java.io.IOException)2260 FileReader (java.io.FileReader)1112 File (java.io.File)818 InputStream (java.io.InputStream)730 ArrayList (java.util.ArrayList)676 FileInputStream (java.io.FileInputStream)626 URL (java.net.URL)390 Test (org.junit.Test)367 FileNotFoundException (java.io.FileNotFoundException)329 StringReader (java.io.StringReader)282 BufferedWriter (java.io.BufferedWriter)205 HashMap (java.util.HashMap)199 Matcher (java.util.regex.Matcher)196 OutputStreamWriter (java.io.OutputStreamWriter)190 HttpURLConnection (java.net.HttpURLConnection)190 PrintWriter (java.io.PrintWriter)187 Reader (java.io.Reader)169 Socket (java.net.Socket)151