Search in sources :

Example 46 with SocketException

use of java.net.SocketException in project camel by apache.

the class FailOverLoadBalanceWrappedExceptionTest method createRouteBuilder.

protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {

        public void configure() {
            from("direct:start").loadBalance().failover(IOException.class).to("direct:x", "direct:y", "direct:z");
            from("direct:x").to("mock:x").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    // socket exception is an io exception
                    throw new CamelExchangeException("foo", exchange, new SocketException("Forced"));
                }
            });
            from("direct:y").to("mock:y").process(new Processor() {

                public void process(Exchange exchange) throws Exception {
                    throw new IOException("Forced");
                }
            });
            from("direct:z").to("mock:z");
        }
    };
}
Also used : Exchange(org.apache.camel.Exchange) CamelExchangeException(org.apache.camel.CamelExchangeException) SocketException(java.net.SocketException) Processor(org.apache.camel.Processor) RouteBuilder(org.apache.camel.builder.RouteBuilder) IOException(java.io.IOException) SocketException(java.net.SocketException) IOException(java.io.IOException) CamelExchangeException(org.apache.camel.CamelExchangeException)

Example 47 with SocketException

use of java.net.SocketException in project camel by apache.

the class DefaultExceptionPolicyStrategyTest method testClosetMatch3.

public void testClosetMatch3() {
    setupPolicies();
    OnExceptionDefinition result = strategy.getExceptionPolicy(policies, null, new ConnectException(""));
    assertEquals(type3, result);
    result = strategy.getExceptionPolicy(policies, null, new SocketException(""));
    assertEquals(type3, result);
    result = strategy.getExceptionPolicy(policies, null, new FileNotFoundException());
    assertEquals(type3, result);
}
Also used : SocketException(java.net.SocketException) OnExceptionDefinition(org.apache.camel.model.OnExceptionDefinition) FileNotFoundException(java.io.FileNotFoundException) ConnectException(java.net.ConnectException)

Example 48 with SocketException

use of java.net.SocketException in project camel by apache.

the class HostUtils method getNetworkInterfaceAddresses.

/**
     * Returns a {@link Map} of {@link InetAddress} per {@link NetworkInterface}.
     */
public static Map<String, Set<InetAddress>> getNetworkInterfaceAddresses() {
    //JVM returns interfaces in a non-predictable order, so to make this more predictable
    //let's have them sort by interface name (by using a TreeMap).
    Map<String, Set<InetAddress>> interfaceAddressMap = new TreeMap<String, Set<InetAddress>>();
    try {
        Enumeration<NetworkInterface> ifaces = NetworkInterface.getNetworkInterfaces();
        while (ifaces.hasMoreElements()) {
            NetworkInterface iface = ifaces.nextElement();
            //We only care about usable non-loopback interfaces.
            if (iface.isUp() && !iface.isLoopback() && !iface.isPointToPoint()) {
                String name = iface.getName();
                Enumeration<InetAddress> ifaceAdresses = iface.getInetAddresses();
                while (ifaceAdresses.hasMoreElements()) {
                    InetAddress ia = ifaceAdresses.nextElement();
                    //We want to filter out mac addresses
                    if (!ia.isLoopbackAddress() && !ia.getHostAddress().contains(":")) {
                        Set<InetAddress> addresses = interfaceAddressMap.get(name);
                        if (addresses == null) {
                            addresses = new LinkedHashSet<InetAddress>();
                        }
                        addresses.add(ia);
                        interfaceAddressMap.put(name, addresses);
                    }
                }
            }
        }
    } catch (SocketException ex) {
    //noop
    }
    return interfaceAddressMap;
}
Also used : SocketException(java.net.SocketException) Set(java.util.Set) LinkedHashSet(java.util.LinkedHashSet) NetworkInterface(java.net.NetworkInterface) TreeMap(java.util.TreeMap) InetAddress(java.net.InetAddress)

Example 49 with SocketException

use of java.net.SocketException in project Genius-Android by qiujuer.

the class DnsResolve method resolve.

/**
     * This resolve domain to ips
     *
     * @param domain    Domain Name
     * @param dnsServer DNS Server
     * @return IPs
     */
private ArrayList<String> resolve(String domain, InetAddress dnsServer) {
    // Pointer
    int pos = 12;
    // Cmd buffer
    byte[] sendBuffer = new byte[100];
    // Message head
    sendBuffer[0] = ID[0];
    sendBuffer[1] = ID[1];
    sendBuffer[2] = 0x01;
    sendBuffer[3] = 0x00;
    sendBuffer[4] = 0x00;
    sendBuffer[5] = 0x01;
    sendBuffer[6] = 0x00;
    sendBuffer[7] = 0x00;
    sendBuffer[8] = 0x00;
    sendBuffer[9] = 0x00;
    sendBuffer[10] = 0x00;
    sendBuffer[11] = 0x00;
    // Add domain
    String[] part = domain.split("\\.");
    for (String s : part) {
        if (s == null || s.length() <= 0)
            continue;
        int sLength = s.length();
        sendBuffer[pos++] = (byte) sLength;
        int i = 0;
        char[] val = s.toCharArray();
        while (i < sLength) {
            sendBuffer[pos++] = (byte) val[i++];
        }
    }
    // 0 end
    sendBuffer[pos++] = 0x00;
    sendBuffer[pos++] = 0x00;
    // 1 A record query
    sendBuffer[pos++] = 0x01;
    sendBuffer[pos++] = 0x00;
    // Internet record query
    sendBuffer[pos++] = 0x01;
    /**
         * UDP Send
         */
    DatagramSocket ds = null;
    byte[] receiveBuffer = null;
    try {
        ds = new DatagramSocket();
        ds.setSoTimeout(TIME_OUT);
        // Send
        DatagramPacket dp = new DatagramPacket(sendBuffer, pos, dnsServer, 53);
        ds.send(dp);
        // Receive
        dp = new DatagramPacket(new byte[512], 512);
        ds.receive(dp);
        // Copy
        int len = dp.getLength();
        receiveBuffer = new byte[len];
        System.arraycopy(dp.getData(), 0, receiveBuffer, 0, len);
    } catch (UnknownHostException e) {
        mError = Cmd.UNKNOWN_HOST_ERROR;
    } catch (SocketException e) {
        mError = Cmd.NETWORK_SOCKET_ERROR;
        e.printStackTrace();
    } catch (IOException e) {
        mError = Cmd.NETWORK_IO_ERROR;
        e.printStackTrace();
    } finally {
        if (ds != null) {
            try {
                ds.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    // Check is return
    if (mError != Cmd.SUCCEED || receiveBuffer == null)
        return null;
    // ID
    if (receiveBuffer[0] != ID[0] || receiveBuffer[1] != ID[1] || (receiveBuffer[2] & 0x80) != 0x80)
        return null;
    // Count
    int queryCount = (receiveBuffer[4] << 8) | receiveBuffer[5];
    if (queryCount == 0)
        return null;
    int answerCount = (receiveBuffer[6] << 8) | receiveBuffer[7];
    if (answerCount == 0)
        return null;
    // Pointer restore
    pos = 12;
    // Skip the query part head
    for (int i = 0; i < queryCount; i++) {
        while (receiveBuffer[pos] != 0x00) {
            pos += receiveBuffer[pos] + 1;
        }
        pos += 5;
    }
    // Get ip form data
    ArrayList<String> iPs = new ArrayList<>();
    for (int i = 0; i < answerCount; i++) {
        if (receiveBuffer[pos] == (byte) 0xC0) {
            pos += 2;
        } else {
            while (receiveBuffer[pos] != (byte) 0x00) {
                pos += receiveBuffer[pos] + 1;
            }
            pos++;
        }
        byte queryType = (byte) (receiveBuffer[pos] << 8 | receiveBuffer[pos + 1]);
        pos += 8;
        int dataLength = (receiveBuffer[pos] << 8 | receiveBuffer[pos + 1]);
        pos += 2;
        // Add ip
        if (queryType == (byte) 0x01) {
            int[] address = new int[4];
            for (int n = 0; n < 4; n++) {
                address[n] = receiveBuffer[pos + n];
                if (address[n] < 0)
                    address[n] += 256;
            }
            iPs.add(String.format("%s.%s.%s.%S", address[0], address[1], address[2], address[3]));
        }
        pos += dataLength;
    }
    return iPs;
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) ArrayList(java.util.ArrayList) IOException(java.io.IOException) SocketException(java.net.SocketException) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) DatagramSocket(java.net.DatagramSocket) DatagramPacket(java.net.DatagramPacket)

Example 50 with SocketException

use of java.net.SocketException in project Genius-Android by qiujuer.

the class Telnet method start.

@Override
public void start() {
    Socket socket = null;
    try {
        Long startTime = System.currentTimeMillis();
        socket = new Socket();
        try {
            socket.setSoTimeout(TIME_OUT);
        } catch (SocketException e) {
            e.printStackTrace();
        }
        socket.connect(new InetSocketAddress(mHost, mPort), TIME_OUT);
        if (isConnected = socket.isConnected())
            mDelay = System.currentTimeMillis() - startTime;
        else
            mError = Cmd.TCP_LINK_ERROR;
    } catch (UnknownHostException e) {
        mError = Cmd.UNKNOWN_HOST_ERROR;
    } catch (IOException e) {
        mError = Cmd.TCP_LINK_ERROR;
    } finally {
        if (socket != null) {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : SocketException(java.net.SocketException) UnknownHostException(java.net.UnknownHostException) InetSocketAddress(java.net.InetSocketAddress) IOException(java.io.IOException) Socket(java.net.Socket)

Aggregations

SocketException (java.net.SocketException)523 IOException (java.io.IOException)189 InetAddress (java.net.InetAddress)127 NetworkInterface (java.net.NetworkInterface)110 Socket (java.net.Socket)100 UnknownHostException (java.net.UnknownHostException)93 ServerSocket (java.net.ServerSocket)82 DatagramSocket (java.net.DatagramSocket)78 SocketTimeoutException (java.net.SocketTimeoutException)77 InetSocketAddress (java.net.InetSocketAddress)62 Test (org.junit.Test)50 ConnectException (java.net.ConnectException)39 DatagramPacket (java.net.DatagramPacket)38 ArrayList (java.util.ArrayList)35 BindException (java.net.BindException)31 Inet4Address (java.net.Inet4Address)24 SocketAddress (java.net.SocketAddress)24 InterruptedIOException (java.io.InterruptedIOException)23 IllegalBlockingModeException (java.nio.channels.IllegalBlockingModeException)23 SmallTest (android.test.suitebuilder.annotation.SmallTest)20