Search in sources :

Example 6 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class Olingo2Producer method process.

@Override
public boolean process(final Exchange exchange, final AsyncCallback callback) {
    // properties for method arguments
    final Map<String, Object> properties = new HashMap<String, Object>();
    properties.putAll(endpoint.getEndpointProperties());
    propertiesHelper.getExchangeProperties(exchange, properties);
    // let the endpoint and the Producer intercept properties
    endpoint.interceptProperties(properties);
    interceptProperties(properties);
    // create response handler
    properties.put(Olingo2Endpoint.RESPONSE_HANDLER_PROPERTY, new Olingo2ResponseHandler<Object>() {

        @Override
        public void onResponse(Object response) {
            // producer returns a single response, even for methods with List return types
            exchange.getOut().setBody(response);
            // copy headers
            exchange.getOut().setHeaders(exchange.getIn().getHeaders());
            interceptResult(response, exchange);
            callback.done(false);
        }

        @Override
        public void onException(Exception ex) {
            exchange.setException(ex);
            callback.done(false);
        }

        @Override
        public void onCanceled() {
            exchange.setException(new RuntimeCamelException("OData HTTP Request cancelled!"));
            callback.done(false);
        }
    });
    // decide which method to invoke
    final ApiMethod method = findMethod(exchange, properties);
    if (method == null) {
        // synchronous failure
        callback.done(true);
        return true;
    }
    if (LOG.isDebugEnabled()) {
        LOG.debug("Invoking operation {} with {}", method.getName(), properties.keySet());
    }
    try {
        doInvokeMethod(method, properties);
    } catch (Throwable t) {
        exchange.setException(ObjectHelper.wrapRuntimeCamelException(t));
        callback.done(true);
        return true;
    }
    return false;
}
Also used : HashMap(java.util.HashMap) ApiMethod(org.apache.camel.util.component.ApiMethod) RuntimeCamelException(org.apache.camel.RuntimeCamelException) RuntimeCamelException(org.apache.camel.RuntimeCamelException)

Example 7 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class AttachmentHttpBinding method populateAttachments.

@Override
protected void populateAttachments(HttpServletRequest request, HttpMessage message) {
    try {
        Collection<Part> parts = request.getParts();
        for (Part part : parts) {
            DataSource ds = new PartDataSource(part);
            Attachment attachment = new DefaultAttachment(ds);
            for (String headerName : part.getHeaderNames()) {
                for (String headerValue : part.getHeaders(headerName)) {
                    attachment.addHeader(headerName, headerValue);
                }
            }
            message.addAttachmentObject(part.getName(), attachment);
        }
    } catch (Exception e) {
        throw new RuntimeCamelException("Cannot populate attachments", e);
    }
}
Also used : Part(javax.servlet.http.Part) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) Attachment(org.apache.camel.Attachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) DefaultAttachment(org.apache.camel.impl.DefaultAttachment) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) DataSource(javax.activation.DataSource)

Example 8 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class SmppConnectionFactory method connectProxy.

private void connectProxy(String host, int port, Socket socket) throws IOException {
    try {
        OutputStream out = socket.getOutputStream();
        InputStream in = socket.getInputStream();
        String connectString = "CONNECT " + host + ":" + port + " HTTP/1.0\r\n";
        out.write(connectString.getBytes());
        String username = config.getHttpProxyUsername();
        String password = config.getHttpProxyPassword();
        if (username != null && password != null) {
            String usernamePassword = username + ":" + password;
            byte[] code = Base64.encodeBase64(usernamePassword.getBytes());
            out.write("Proxy-Authorization: Basic ".getBytes());
            out.write(code);
            out.write("\r\n".getBytes());
        }
        Map<String, String> proxyHeaders = config.getProxyHeaders();
        if (proxyHeaders != null) {
            for (Map.Entry<String, String> entry : proxyHeaders.entrySet()) {
                out.write((entry.getKey() + ": " + entry.getValue()).getBytes());
                out.write("\r\n".getBytes());
            }
        }
        out.write("\r\n".getBytes());
        out.flush();
        int ch = 0;
        BufferedReader reader = IOHelper.buffered(new InputStreamReader(in));
        String response = reader.readLine();
        if (response == null) {
            throw new RuntimeCamelException("Empty response to CONNECT request to host " + host + ":" + port);
        }
        String reason = "Unknown reason";
        int code = -1;
        try {
            ch = response.indexOf(' ');
            int bar = response.indexOf(' ', ch + 1);
            code = Integer.parseInt(response.substring(ch + 1, bar));
            reason = response.substring(bar + 1);
        } catch (NumberFormatException e) {
            throw new RuntimeCamelException("Invalid response to CONNECT request to host " + host + ":" + port + " - cannot parse code from response string: " + response);
        }
        if (code != 200) {
            throw new RuntimeCamelException("Proxy error: " + reason);
        }
        // read until empty line
        for (; response.length() > 0; ) {
            response = reader.readLine();
            if (response == null) {
                throw new RuntimeCamelException("Proxy error: reached end of stream");
            }
        }
    } catch (RuntimeException re) {
        closeSocket(socket);
        throw re;
    } catch (Exception e) {
        closeSocket(socket);
        throw new RuntimeException("SmppConnectionFactory: " + e.getMessage());
    }
}
Also used : InputStreamReader(java.io.InputStreamReader) InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) BufferedReader(java.io.BufferedReader) RuntimeCamelException(org.apache.camel.RuntimeCamelException) Map(java.util.Map)

Example 9 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class TransactionalClientDataSourceWithOnExceptionTest method testTransactionRollback.

public void testTransactionRollback() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:error");
    mock.expectedMessageCount(1);
    try {
        template.sendBody("direct:fail", "Hello World");
        fail("Should have thrown exception");
    } catch (RuntimeCamelException e) {
        // expected as we fail
        assertIsInstanceOf(RuntimeCamelException.class, e.getCause());
        assertTrue(e.getCause().getCause() instanceof IllegalArgumentException);
        assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage());
    }
    assertMockEndpointsSatisfied();
    int count = jdbc.queryForObject("select count(*) from books", Integer.class);
    assertEquals("Number of books", 1, count);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Example 10 with RuntimeCamelException

use of org.apache.camel.RuntimeCamelException in project camel by apache.

the class TransactionalClientDataSourceAsyncTest method testTransactionRollback.

public void testTransactionRollback() throws Exception {
    MockEndpoint mock = getMockEndpoint("mock:error");
    mock.expectedMessageCount(1);
    try {
        template.sendBody("direct:fail", "Hello World");
        fail("Should have thrown exception");
    } catch (RuntimeCamelException e) {
        // expected as we fail
        assertIsInstanceOf(RuntimeCamelException.class, e.getCause());
        assertTrue(e.getCause().getCause() instanceof IllegalArgumentException);
        assertEquals("We don't have Donkeys, only Camels", e.getCause().getCause().getMessage());
    }
    assertMockEndpointsSatisfied();
    int count = jdbc.queryForObject("select count(*) from books", Integer.class);
    assertEquals("Number of books", 1, count);
}
Also used : MockEndpoint(org.apache.camel.component.mock.MockEndpoint) RuntimeCamelException(org.apache.camel.RuntimeCamelException) MockEndpoint(org.apache.camel.component.mock.MockEndpoint)

Aggregations

RuntimeCamelException (org.apache.camel.RuntimeCamelException)196 HashMap (java.util.HashMap)52 CamelContextAware (org.apache.camel.CamelContextAware)45 DataFormatFactory (org.apache.camel.spi.DataFormatFactory)45 ConditionalOnBean (org.springframework.boot.autoconfigure.condition.ConditionalOnBean)45 ConditionalOnClass (org.springframework.boot.autoconfigure.condition.ConditionalOnClass)45 ConditionalOnMissingBean (org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean)45 Bean (org.springframework.context.annotation.Bean)45 IOException (java.io.IOException)36 MockEndpoint (org.apache.camel.component.mock.MockEndpoint)32 Test (org.junit.Test)16 ArrayList (java.util.ArrayList)11 Exchange (org.apache.camel.Exchange)11 InputStream (java.io.InputStream)9 GeneralSecurityException (java.security.GeneralSecurityException)9 ByteArrayInputStream (java.io.ByteArrayInputStream)8 TimeoutException (java.util.concurrent.TimeoutException)7 QName (javax.xml.namespace.QName)7 Message (org.apache.camel.Message)7 Method (java.lang.reflect.Method)6