Search in sources :

Example 91 with IOException

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

the class CamelSSLIRCConnection method connect.

@Override
public void connect() throws IOException {
    if (sslContextParameters == null) {
        super.connect();
    } else {
        if (level != 0) {
            throw new SocketException("Socket closed or already open (" + level + ")");
        }
        IOException exception = null;
        final SSLContext sslContext;
        try {
            sslContext = sslContextParameters.createSSLContext(camelContext);
        } catch (GeneralSecurityException e) {
            throw new RuntimeCamelException("Error in SSLContextParameters configuration or instantiation.", e);
        }
        final SSLSocketFactory sf = sslContext.getSocketFactory();
        SSLSocket s = null;
        for (int i = 0; i < ports.length && s == null; i++) {
            try {
                s = (SSLSocket) sf.createSocket(host, ports[i]);
                s.startHandshake();
                exception = null;
            } catch (SSLNotSupportedException exc) {
                if (s != null) {
                    s.close();
                }
                s = null;
                throw exc;
            } catch (IOException exc) {
                if (s != null) {
                    s.close();
                }
                s = null;
                exception = exc;
            }
        }
        if (exception != null) {
            // connection wasn't successful at any port
            throw exception;
        }
        prepare(s);
    }
}
Also used : SocketException(java.net.SocketException) GeneralSecurityException(java.security.GeneralSecurityException) SSLSocket(javax.net.ssl.SSLSocket) SSLNotSupportedException(org.schwering.irc.lib.ssl.SSLNotSupportedException) RuntimeCamelException(org.apache.camel.RuntimeCamelException) IOException(java.io.IOException) SSLContext(javax.net.ssl.SSLContext) SSLSocketFactory(javax.net.ssl.SSLSocketFactory)

Example 92 with IOException

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

the class CodehausIrcChat method main.

public static void main(String[] args) throws InterruptedException {
    List<IrcChannel> channels = new ArrayList<IrcChannel>();
    channels.add(new IrcChannel("camel-test", null));
    final IrcConfiguration config = new IrcConfiguration("irc.codehaus.org", "camel-rc", "Camel IRC Component", channels);
    final IRCConnection conn = new IRCConnection(config.getHostname(), config.getPorts(), config.getPassword(), config.getNickname(), config.getUsername(), config.getRealname());
    conn.addIRCEventListener(new CodehausIRCEventAdapter());
    conn.setEncoding("UTF-8");
    // conn.setDaemon(true);
    conn.setColors(false);
    conn.setPong(true);
    try {
        conn.connect();
    } catch (IOException e) {
        e.printStackTrace();
    }
    // while (!conn.isConnected()) {
    // Thread.sleep(1000);
    // LOG.info("Sleeping");
    // }
    LOG.info("Connected");
    for (IrcChannel channel : config.getChannels()) {
        conn.doJoin(channel.getName());
    }
    conn.doPrivmsg("#camel-test", "hi!");
    Thread.sleep(Integer.MAX_VALUE);
}
Also used : ArrayList(java.util.ArrayList) IOException(java.io.IOException) IRCConnection(org.schwering.irc.lib.IRCConnection)

Example 93 with IOException

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

the class Utility method setSecurityPolicy.

public static synchronized void setSecurityPolicy(String policyResourceName, String tmpFileName) throws IOException {
    InputStream in = Thread.currentThread().getContextClassLoader().getResourceAsStream(policyResourceName);
    if (in == null) {
        throw new IOException("Unable to find the resource policy.all on classpath");
    }
    File outfile = new File(tmpFileName);
    OutputStream out = new FileOutputStream(outfile);
    byte[] tmp = new byte[8192];
    int len = 0;
    while (true) {
        len = in.read(tmp);
        if (len <= 0) {
            break;
        }
        out.write(tmp, 0, len);
    }
    out.close();
    in.close();
    System.setProperty("java.security.policy", outfile.getAbsolutePath());
}
Also used : InputStream(java.io.InputStream) OutputStream(java.io.OutputStream) FileOutputStream(java.io.FileOutputStream) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) File(java.io.File)

Example 94 with IOException

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

the class JaxbDataFormat method unmarshal.

public Object unmarshal(Exchange exchange, InputStream stream) throws IOException, SAXException {
    try {
        Object answer;
        XMLStreamReader xmlReader;
        if (needFiltering(exchange)) {
            xmlReader = typeConverter.convertTo(XMLStreamReader.class, createNonXmlFilterReader(exchange, stream));
        } else {
            xmlReader = typeConverter.convertTo(XMLStreamReader.class, stream);
        }
        if (partialClass != null) {
            // partial unmarshalling
            answer = createUnmarshaller().unmarshal(xmlReader, partialClass);
        } else {
            answer = createUnmarshaller().unmarshal(xmlReader);
        }
        if (answer instanceof JAXBElement && isIgnoreJAXBElement()) {
            answer = ((JAXBElement<?>) answer).getValue();
        }
        return answer;
    } catch (JAXBException e) {
        throw new IOException(e);
    }
}
Also used : XMLStreamReader(javax.xml.stream.XMLStreamReader) JAXBException(javax.xml.bind.JAXBException) JAXBElement(javax.xml.bind.JAXBElement) IOException(java.io.IOException)

Example 95 with IOException

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

the class JaxbDataFormat method marshal.

public void marshal(Exchange exchange, Object graph, OutputStream stream) throws IOException, SAXException {
    try {
        // must create a new instance of marshaller as its not thread safe
        Marshaller marshaller = createMarshaller();
        if (isPrettyPrint()) {
            marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
        }
        // exchange take precedence over encoding option
        String charset = exchange.getProperty(Exchange.CHARSET_NAME, String.class);
        if (charset == null) {
            charset = encoding;
        }
        if (charset != null) {
            marshaller.setProperty(Marshaller.JAXB_ENCODING, charset);
        }
        if (isFragment()) {
            marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        }
        if (ObjectHelper.isNotEmpty(schemaLocation)) {
            marshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION, schemaLocation);
        }
        if (ObjectHelper.isNotEmpty(noNamespaceSchemaLocation)) {
            marshaller.setProperty(Marshaller.JAXB_NO_NAMESPACE_SCHEMA_LOCATION, noNamespaceSchemaLocation);
        }
        if (namespacePrefixMapper != null) {
            marshaller.setProperty(namespacePrefixMapper.getRegistrationKey(), namespacePrefixMapper);
        }
        // Inject any JAX-RI custom properties from the exchange or from the instance into the marshaller
        Map<String, Object> customProperties = exchange.getProperty(JaxbConstants.JAXB_PROVIDER_PROPERTIES, Map.class);
        if (customProperties == null) {
            customProperties = getJaxbProviderProperties();
        }
        if (customProperties != null) {
            for (Entry<String, Object> property : customProperties.entrySet()) {
                if (LOG.isDebugEnabled()) {
                    LOG.debug("Using JAXB Provider Property {}={}", property.getKey(), property.getValue());
                }
                marshaller.setProperty(property.getKey(), property.getValue());
            }
        }
        marshal(exchange, graph, stream, marshaller);
        if (contentTypeHeader) {
            if (exchange.hasOut()) {
                exchange.getOut().setHeader(Exchange.CONTENT_TYPE, "application/xml");
            } else {
                exchange.getIn().setHeader(Exchange.CONTENT_TYPE, "application/xml");
            }
        }
    } catch (Exception e) {
        throw new IOException(e);
    }
}
Also used : Marshaller(javax.xml.bind.Marshaller) IOException(java.io.IOException) NoTypeConversionAvailableException(org.apache.camel.NoTypeConversionAvailableException) XMLStreamException(javax.xml.stream.XMLStreamException) MalformedURLException(java.net.MalformedURLException) IOException(java.io.IOException) JAXBException(javax.xml.bind.JAXBException) FileNotFoundException(java.io.FileNotFoundException) SAXException(org.xml.sax.SAXException) InvalidPayloadException(org.apache.camel.InvalidPayloadException) UnsupportedEncodingException(java.io.UnsupportedEncodingException)

Aggregations

IOException (java.io.IOException)41104 File (java.io.File)7663 InputStream (java.io.InputStream)4105 Test (org.junit.Test)3557 ArrayList (java.util.ArrayList)3023 FileInputStream (java.io.FileInputStream)2674 FileOutputStream (java.io.FileOutputStream)2358 FileNotFoundException (java.io.FileNotFoundException)2146 BufferedReader (java.io.BufferedReader)2028 ByteArrayOutputStream (java.io.ByteArrayOutputStream)1761 InputStreamReader (java.io.InputStreamReader)1677 URL (java.net.URL)1608 HashMap (java.util.HashMap)1552 ByteArrayInputStream (java.io.ByteArrayInputStream)1534 OutputStream (java.io.OutputStream)1349 Path (org.apache.hadoop.fs.Path)1316 Map (java.util.Map)1212 List (java.util.List)1042 Properties (java.util.Properties)994 ServletException (javax.servlet.ServletException)916