View Javadoc

1   /*
2    *  XNap Commons
3    *
4    *  Copyright (C) 2005  Steffen Pingel
5    *
6    *  This library is free software; you can redistribute it and/or
7    *  modify it under the terms of the GNU Lesser General Public
8    *  License as published by the Free Software Foundation; either
9    *  version 2.1 of the License, or (at your option) any later version.
10   *
11   *  This library is distributed in the hope that it will be useful,
12   *  but WITHOUT ANY WARRANTY; without even the implied warranty of
13   *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
14   *  Lesser General Public License for more details.
15   *
16   *  You should have received a copy of the GNU Lesser General Public
17   *  License along with this library; if not, write to the Free Software
18   *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
19   */
20  package org.xnap.commons.util;
21  
22  import java.io.IOException;
23  import java.net.ConnectException;
24  import java.net.UnknownHostException;
25  import org.apache.commons.logging.Log;
26  import org.apache.commons.logging.LogFactory;
27  import org.xnap.commons.i18n.I18n;
28  import org.xnap.commons.i18n.I18nFactory;
29  
30  /***
31   * Provides a set of static methods for common network tasks.
32   */
33  public class NetHelper {
34  
35  	private static final I18n i18n = I18nFactory.getI18n(NetHelper.class);
36      private static final Log logger = LogFactory.getLog(NetHelper.class);
37  
38      /***
39       * Enables the use of a socks proxy at <code>host:port</code>. 
40       *
41       * @param host hostname of the proxy
42       * @param port port of the proxy
43       */
44      public static void enableSocksProxy(String host, int port) 
45      {
46  		logger.debug("Enabling socks proxy: " + host + ":" + port);
47  
48          System.getProperties().put("socksProxySet", "true");
49          System.getProperties().put("socksProxyHost", host);
50          System.getProperties().put("socksProxyPort", port + "");
51      }
52  
53      /***
54       * Disables the use of a socks proxy.
55       */ 
56      public static void disableSocksProxy() 
57      {
58  		logger.debug("Disabling socks proxy");
59  
60          System.getProperties().remove("socksProxySet");
61          System.getProperties().remove("socksProxyHost");
62          System.getProperties().remove("socksProxyPort");
63      }
64  
65      /***
66       * Enables the use of a http proxy at <code>host:port</code>. 
67       *
68       * @param host hostname of the proxy
69       * @param port port of the proxy
70       */
71      public static void enableHttpProxy(String host, int port) 
72      {
73  		logger.debug("Enabling http proxy: " + host + ":" + port);
74  
75          System.getProperties().put("proxySet", "true");
76          System.getProperties().put("proxyHost", host);
77          System.getProperties().put("proxyPort", port + "");
78      }
79  
80      /***
81       * Disables the use of a http proxy.
82       */ 
83      public static void disableHttpProxy() 
84      {
85  		logger.debug("Disabling http proxy");
86  
87          System.getProperties().remove("proxySet");
88          System.getProperties().remove("proxyHost");
89          System.getProperties().remove("proxyPort");
90      }
91  
92      /***
93       * Returns a sensible error message.
94       */
95      public static String getErrorMessage(IOException e)
96      {
97  		String message = e.getLocalizedMessage();
98  		if (e instanceof ConnectException) {
99  			return i18n.tr("Connection refused");
100 		}
101 		else if (e instanceof UnknownHostException) {
102 			return i18n.tr("Unknown host {0}", 
103 						   (message != null) ? message : "");
104 		}
105 		else {
106 			return i18n.tr("Error ({0})", 
107 						   (message != null) ? message : e.toString());
108 		}
109     }
110 
111 	public static long ipToLongHiFirst(byte[] address)
112 	{
113         if (address.length != 4) {
114             throw new IllegalArgumentException("byte array must be of length 4");
115         }
116 
117         long ipNum = 0;
118         long multiplier = 1;
119         for (int i = 3; i >= 0; i--) {
120             int byteVal = (address[i] + 256) % 256;
121             ipNum += byteVal * multiplier;
122             multiplier *= 256;
123         }
124         return ipNum;
125 	}
126 
127     /***
128      * Converts <code>ip</code> from an integer value to a dotted string
129      * representation.
130      */
131     public static String toIPAddressLittleEndian(long ip)
132     {
133 		StringBuilder sb = new StringBuilder(4 * 3 + 3);
134 	
135 		sb.append(ip & 0xFF);
136 		sb.append(".");
137 		sb.append((ip >> 8) & 0xFF);
138 		sb.append(".");
139 		sb.append((ip >> 16) & 0xFF);
140 		sb.append(".");
141 		sb.append((ip >> 24) & 0xFF);
142 
143 		return sb.toString();
144     }
145 
146     /***
147      * Converts <code>ip</code> from an integer value to a dotted string
148      * representation.
149      */
150     public static String toIPAddressBigEndian(long ip)
151     {
152 		StringBuilder sb = new StringBuilder(4 * 3 + 3);
153 	
154 		sb.append((ip >> 24) & 0xFF);
155 		sb.append(".");
156 		sb.append((ip >> 16) & 0xFF);
157 		sb.append(".");
158 		sb.append((ip >> 8) & 0xFF);
159 		sb.append(".");
160 		sb.append(ip & 0xFF);
161 
162 		return sb.toString();
163     }
164 
165 }