Skip to content

Commit 2510ccb

Browse files
committed
Extract method.
1 parent 84d089b commit 2510ccb

File tree

1 file changed

+38
-69
lines changed

1 file changed

+38
-69
lines changed

src/main/java/ch/ethz/ssh2/Connection.java

Lines changed: 38 additions & 69 deletions
Original file line numberDiff line numberDiff line change
@@ -838,11 +838,7 @@ public void run()
838838
public synchronized LocalPortForwarder createLocalPortForwarder(int local_port, String host_to_connect,
839839
int port_to_connect) throws IOException
840840
{
841-
if (tm == null)
842-
throw new IllegalStateException("Cannot forward ports, you need to establish a connection first.");
843-
844-
if (!authenticated)
845-
throw new IllegalStateException("Cannot forward ports, connection is not authenticated.");
841+
this.checkConnection();
846842

847843
return new LocalPortForwarder(cm, local_port, host_to_connect, port_to_connect);
848844
}
@@ -865,11 +861,7 @@ public synchronized LocalPortForwarder createLocalPortForwarder(int local_port,
865861
public synchronized LocalPortForwarder createLocalPortForwarder(InetSocketAddress addr, String host_to_connect,
866862
int port_to_connect) throws IOException
867863
{
868-
if (tm == null)
869-
throw new IllegalStateException("Cannot forward ports, you need to establish a connection first.");
870-
871-
if (!authenticated)
872-
throw new IllegalStateException("Cannot forward ports, connection is not authenticated.");
864+
this.checkConnection();
873865

874866
return new LocalPortForwarder(cm, addr, host_to_connect, port_to_connect);
875867
}
@@ -888,11 +880,7 @@ public synchronized LocalPortForwarder createLocalPortForwarder(InetSocketAddres
888880
public synchronized LocalStreamForwarder createLocalStreamForwarder(String host_to_connect, int port_to_connect)
889881
throws IOException
890882
{
891-
if (tm == null)
892-
throw new IllegalStateException("Cannot forward, you need to establish a connection first.");
893-
894-
if (!authenticated)
895-
throw new IllegalStateException("Cannot forward, connection is not authenticated.");
883+
this.checkConnection();
896884

897885
return new LocalStreamForwarder(cm, host_to_connect, port_to_connect);
898886
}
@@ -911,11 +899,7 @@ public synchronized LocalStreamForwarder createLocalStreamForwarder(String host_
911899
*/
912900
public synchronized SCPClient createSCPClient() throws IOException
913901
{
914-
if (tm == null)
915-
throw new IllegalStateException("Cannot create SCP client, you need to establish a connection first.");
916-
917-
if (!authenticated)
918-
throw new IllegalStateException("Cannot create SCP client, connection is not authenticated.");
902+
this.checkConnection();
919903

920904
return new SCPClient(this);
921905
}
@@ -935,8 +919,7 @@ public synchronized SCPClient createSCPClient() throws IOException
935919
*/
936920
public synchronized void forceKeyExchange() throws IOException
937921
{
938-
if (tm == null)
939-
throw new IllegalStateException("You need to establish a connection first.");
922+
this.checkConnection();
940923

941924
tm.forceKeyExchange(cryptoWishList, dhgexpara, null, null);
942925
}
@@ -972,9 +955,8 @@ public synchronized int getPort()
972955
*/
973956
public synchronized ConnectionInfo getConnectionInfo() throws IOException
974957
{
975-
if (tm == null)
976-
throw new IllegalStateException(
977-
"Cannot get details of connection, you need to establish a connection first.");
958+
this.checkConnection();
959+
978960
return tm.getConnectionInfo(1);
979961
}
980962

@@ -1074,11 +1056,11 @@ public synchronized boolean isAuthMethodAvailable(String user, String method) th
10741056

10751057
String methods[] = getRemainingAuthMethods(user);
10761058

1077-
for (int i = 0; i < methods.length; i++)
1078-
{
1079-
if (methods[i].compareTo(method) == 0)
1080-
return true;
1081-
}
1059+
for(final String m : methods) {
1060+
if(m.compareTo(method) == 0) {
1061+
return true;
1062+
}
1063+
}
10821064

10831065
return false;
10841066
}
@@ -1101,11 +1083,7 @@ private SecureRandom getOrCreateSecureRND()
11011083
*/
11021084
public synchronized Session openSession() throws IOException
11031085
{
1104-
if (tm == null)
1105-
throw new IllegalStateException("Cannot open session, you need to establish a connection first.");
1106-
1107-
if (!authenticated)
1108-
throw new IllegalStateException("Cannot open session, connection is not authenticated.");
1086+
this.checkConnection();
11091087

11101088
return new Session(cm, getOrCreateSecureRND());
11111089
}
@@ -1137,12 +1115,7 @@ public synchronized void sendIgnorePacket() throws IOException
11371115
*/
11381116
public synchronized void sendIgnorePacket(byte[] data) throws IOException
11391117
{
1140-
if (data == null)
1141-
throw new IllegalArgumentException("data argument must not be null.");
1142-
1143-
if (tm == null)
1144-
throw new IllegalStateException(
1145-
"Cannot send SSH_MSG_IGNORE packet, you need to establish a connection first.");
1118+
this.checkConnection();
11461119

11471120
PacketIgnore pi = new PacketIgnore();
11481121
pi.setData(data);
@@ -1167,26 +1140,21 @@ private String[] removeDuplicates(String[] list)
11671140

11681141
int count = 0;
11691142

1170-
for (int i = 0; i < list.length; i++)
1171-
{
1172-
boolean duplicate = false;
1173-
1174-
String element = list[i];
1143+
for(final String element : list) {
1144+
boolean duplicate = false;
1145+
for(int j = 0; j < count; j++) {
1146+
if(((element == null) && (list2[j] == null)) || ((element != null) && (element.equals(list2[j])))) {
1147+
duplicate = true;
1148+
break;
1149+
}
1150+
}
11751151

1176-
for (int j = 0; j < count; j++)
1177-
{
1178-
if (((element == null) && (list2[j] == null)) || ((element != null) && (element.equals(list2[j]))))
1179-
{
1180-
duplicate = true;
1181-
break;
1182-
}
1183-
}
1152+
if(duplicate) {
1153+
continue;
1154+
}
11841155

1185-
if (duplicate)
1186-
continue;
1187-
1188-
list2[count++] = list[i];
1189-
}
1156+
list2[count++] = element;
1157+
}
11901158

11911159
if (count == list2.length)
11921160
return list2;
@@ -1370,11 +1338,7 @@ public synchronized void setProxyData(ProxyData proxyData)
13701338
public synchronized void requestRemotePortForwarding(String bindAddress, int bindPort, String targetAddress,
13711339
int targetPort) throws IOException
13721340
{
1373-
if (tm == null)
1374-
throw new IllegalStateException("You need to establish a connection first.");
1375-
1376-
if (!authenticated)
1377-
throw new IllegalStateException("The connection is not authenticated.");
1341+
this.checkConnection();
13781342

13791343
if ((bindAddress == null) || (targetAddress == null) || (bindPort <= 0) || (targetPort <= 0))
13801344
throw new IllegalArgumentException();
@@ -1394,11 +1358,7 @@ public synchronized void requestRemotePortForwarding(String bindAddress, int bin
13941358
*/
13951359
public synchronized void cancelRemotePortForwarding(int bindPort) throws IOException
13961360
{
1397-
if (tm == null)
1398-
throw new IllegalStateException("You need to establish a connection first.");
1399-
1400-
if (!authenticated)
1401-
throw new IllegalStateException("The connection is not authenticated.");
1361+
this.checkConnection();
14021362

14031363
cm.requestCancelGlobalForward(bindPort);
14041364
}
@@ -1419,4 +1379,13 @@ public synchronized void setSecureRandom(SecureRandom rnd)
14191379

14201380
this.generator = rnd;
14211381
}
1382+
1383+
private void checkConnection() throws IllegalStateException {
1384+
if (tm == null) {
1385+
throw new IllegalStateException("You need to establish a connection first.");
1386+
}
1387+
if (!authenticated) {
1388+
throw new IllegalStateException("The connection is not authenticated.");
1389+
}
1390+
}
14221391
}

0 commit comments

Comments
 (0)