1
0
Fork 0
mirror of https://github.com/vanitasvitae/Smack.git synced 2025-09-10 17:49:38 +02:00

Enable LeftCurly checkstyle check

This commit is contained in:
Florian Schmaus 2018-04-06 10:21:46 +02:00
parent 5a841ff0a8
commit 9b5dafe541
93 changed files with 549 additions and 1087 deletions

View file

@ -193,8 +193,7 @@ public final class ProviderManager {
*/
@SuppressWarnings("unchecked")
public static void addIQProvider(String elementName, String namespace,
Object provider)
{
Object provider) {
validate(elementName, namespace);
// First remove existing providers
String key = removeIQProvider(elementName, namespace);
@ -255,8 +254,7 @@ public final class ProviderManager {
*/
@SuppressWarnings("unchecked")
public static void addExtensionProvider(String elementName, String namespace,
Object provider)
{
Object provider) {
validate(elementName, namespace);
// First remove existing providers
String key = removeExtensionProvider(elementName, namespace);

View file

@ -37,8 +37,7 @@ class HTTPProxySocketConnection implements ProxySocketConnection {
private final ProxyInfo proxy;
HTTPProxySocketConnection(ProxyInfo proxy)
{
HTTPProxySocketConnection(ProxyInfo proxy) {
this.proxy = proxy;
}
@ -51,12 +50,10 @@ class HTTPProxySocketConnection implements ProxySocketConnection {
String hostport = "CONNECT " + host + ":" + port;
String proxyLine;
String username = proxy.getProxyUsername();
if (username == null)
{
if (username == null) {
proxyLine = "";
}
else
{
else {
String password = proxy.getProxyPassword();
proxyLine = "\r\nProxy-Authorization: Basic " + Base64.encode(username + ":" + password);
}
@ -67,41 +64,33 @@ class HTTPProxySocketConnection implements ProxySocketConnection {
StringBuilder got = new StringBuilder(100);
int nlchars = 0;
while (true)
{
while (true) {
int inByte = in.read();
if (inByte == -1)
{
if (inByte == -1) {
throw new ProxyException(ProxyInfo.ProxyType.HTTP);
}
char c = (char) inByte;
got.append(c);
if (got.length() > 1024)
{
if (got.length() > 1024) {
throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Received " +
"header of >1024 characters from "
+ proxyhost + ", cancelling connection");
}
if ((nlchars == 0 || nlchars == 2) && c == '\r')
{
if ((nlchars == 0 || nlchars == 2) && c == '\r') {
nlchars++;
}
else if ((nlchars == 1 || nlchars == 3) && c == '\n')
{
else if ((nlchars == 1 || nlchars == 3) && c == '\n') {
nlchars++;
}
else
{
else {
nlchars = 0;
}
if (nlchars == 4)
{
if (nlchars == 4) {
break;
}
}
if (nlchars != 4)
{
if (nlchars != 4) {
throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Never " +
"received blank line from "
+ proxyhost + ", cancelling connection");
@ -112,23 +101,20 @@ class HTTPProxySocketConnection implements ProxySocketConnection {
BufferedReader br = new BufferedReader(new StringReader(gotstr));
String response = br.readLine();
if (response == null)
{
if (response == null) {
throw new ProxyException(ProxyInfo.ProxyType.HTTP, "Empty proxy " +
"response from " + proxyhost + ", cancelling");
}
Matcher m = RESPONSE_PATTERN.matcher(response);
if (!m.matches())
{
if (!m.matches()) {
throw new ProxyException(ProxyInfo.ProxyType.HTTP , "Unexpected " +
"proxy response from " + proxyhost + ": " + response);
}
int code = Integer.parseInt(m.group(1));
if (code != HttpURLConnection.HTTP_OK)
{
if (code != HttpURLConnection.HTTP_OK) {
throw new ProxyException(ProxyInfo.ProxyType.HTTP);
}
}

View file

@ -29,13 +29,11 @@ public class ProxyException extends IOException {
*/
private static final long serialVersionUID = 1L;
public ProxyException(ProxyInfo.ProxyType type, String ex)
{
public ProxyException(ProxyInfo.ProxyType type, String ex) {
super("Proxy Exception " + type.toString() + " : " + ex);
}
public ProxyException(ProxyInfo.ProxyType type)
{
public ProxyException(ProxyInfo.ProxyType type) {
super("Proxy Exception " + type.toString() + " : " + "Unknown Error");
}
}

View file

@ -23,10 +23,8 @@ package org.jivesoftware.smack.proxy;
* @author Atul Aggarwal
*/
public class ProxyInfo
{
public enum ProxyType
{
public class ProxyInfo {
public enum ProxyType {
HTTP,
SOCKS4,
SOCKS5
@ -40,8 +38,7 @@ public class ProxyInfo
private final ProxySocketConnection proxySocketConnection;
public ProxyInfo(ProxyType pType, String pHost, int pPort, String pUser,
String pPass)
{
String pPass) {
this.proxyType = pType;
this.proxyAddress = pHost;
this.proxyPort = pPort;
@ -63,45 +60,37 @@ public class ProxyInfo
}
public static ProxyInfo forHttpProxy(String pHost, int pPort, String pUser,
String pPass)
{
String pPass) {
return new ProxyInfo(ProxyType.HTTP, pHost, pPort, pUser, pPass);
}
public static ProxyInfo forSocks4Proxy(String pHost, int pPort, String pUser,
String pPass)
{
String pPass) {
return new ProxyInfo(ProxyType.SOCKS4, pHost, pPort, pUser, pPass);
}
public static ProxyInfo forSocks5Proxy(String pHost, int pPort, String pUser,
String pPass)
{
String pPass) {
return new ProxyInfo(ProxyType.SOCKS5, pHost, pPort, pUser, pPass);
}
public ProxyType getProxyType()
{
public ProxyType getProxyType() {
return proxyType;
}
public String getProxyAddress()
{
public String getProxyAddress() {
return proxyAddress;
}
public int getProxyPort()
{
public int getProxyPort() {
return proxyPort;
}
public String getProxyUsername()
{
public String getProxyUsername() {
return proxyUsername;
}
public String getProxyPassword()
{
public String getProxyPassword() {
return proxyPassword;
}

View file

@ -33,8 +33,7 @@ import org.jivesoftware.smack.util.StringUtils;
public class Socks4ProxySocketConnection implements ProxySocketConnection {
private final ProxyInfo proxy;
Socks4ProxySocketConnection(ProxyInfo proxy)
{
Socks4ProxySocketConnection(ProxyInfo proxy) {
this.proxy = proxy;
}
@ -47,8 +46,7 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
int proxy_port = proxy.getProxyPort();
String user = proxy.getProxyUsername();
try
{
try {
socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout);
in = socket.getInputStream();
out = socket.getOutputStream();
@ -84,13 +82,11 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
InetAddress inetAddress = InetAddress.getByName(proxy_host);
byte[] byteAddress = inetAddress.getAddress();
for (int i = 0; i < byteAddress.length; i++)
{
for (int i = 0; i < byteAddress.length; i++) {
buf[index++] = byteAddress[i];
}
if (user != null)
{
if (user != null) {
byte[] userBytes = user.getBytes(StringUtils.UTF8);
System.arraycopy(userBytes, 0, buf, index, user.length());
index += user.length();
@ -127,29 +123,23 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
int len = 6;
int s = 0;
while (s < len)
{
while (s < len) {
int i = in.read(buf, s, len - s);
if (i <= 0)
{
if (i <= 0) {
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4,
"stream is closed");
}
s += i;
}
if (buf[0] != 0)
{
if (buf[0] != 0) {
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4,
"server returns VN " + buf[0]);
}
if (buf[1] != 90)
{
try
{
if (buf[1] != 90) {
try {
socket.close();
}
catch (Exception eee)
{
catch (Exception eee) {
}
String message = "ProxySOCKS4: server returns CD " + buf[1];
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, message);
@ -157,18 +147,14 @@ public class Socks4ProxySocketConnection implements ProxySocketConnection {
byte[] temp = new byte[2];
in.read(temp, 0, 2);
}
catch (RuntimeException e)
{
catch (RuntimeException e) {
throw e;
}
catch (Exception e)
{
try
{
catch (Exception e) {
try {
socket.close();
}
catch (Exception eee)
{
catch (Exception eee) {
}
throw new ProxyException(ProxyInfo.ProxyType.SOCKS4, e.toString());
}

View file

@ -32,8 +32,7 @@ import org.jivesoftware.smack.util.StringUtils;
public class Socks5ProxySocketConnection implements ProxySocketConnection {
private final ProxyInfo proxy;
Socks5ProxySocketConnection(ProxyInfo proxy)
{
Socks5ProxySocketConnection(ProxyInfo proxy) {
this.proxy = proxy;
}
@ -47,8 +46,7 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
String user = proxy.getProxyUsername();
String passwd = proxy.getProxyPassword();
try
{
try {
socket.connect(new InetSocketAddress(proxy_host, proxy_port), timeout);
in = socket.getInputStream();
out = socket.getOutputStream();
@ -100,14 +98,12 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
fill(in, buf, 2);
boolean check = false;
switch ((buf[1]) & 0xff)
{
switch ((buf[1]) & 0xff) {
case 0: // NO AUTHENTICATION REQUIRED
check = true;
break;
case 2: // USERNAME/PASSWORD
if (user == null || passwd == null)
{
if (user == null || passwd == null) {
break;
}
@ -160,22 +156,18 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
connection.
*/
fill(in, buf, 2);
if (buf[1] == 0)
{
if (buf[1] == 0) {
check = true;
}
break;
default:
}
if (!check)
{
try
{
if (!check) {
try {
socket.close();
}
catch (Exception eee)
{
catch (Exception eee) {
}
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
"fail in SOCKS5 proxy");
@ -260,21 +252,17 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
fill(in, buf, 4);
if (buf[1] != 0)
{
try
{
if (buf[1] != 0) {
try {
socket.close();
}
catch (Exception eee)
{
catch (Exception eee) {
}
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5,
"server returns " + buf[1]);
}
switch (buf[3] & 0xff)
{
switch (buf[3] & 0xff) {
case 1:
fill(in, buf, 6);
break;
@ -288,18 +276,14 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
default:
}
}
catch (RuntimeException e)
{
catch (RuntimeException e) {
throw e;
}
catch (Exception e)
{
try
{
catch (Exception e) {
try {
socket.close();
}
catch (Exception eee)
{
catch (Exception eee) {
}
// TODO convert to IOException(e) when minimum Android API level is 9 or higher
throw new IOException(e.getLocalizedMessage());
@ -307,14 +291,11 @@ public class Socks5ProxySocketConnection implements ProxySocketConnection {
}
private static void fill(InputStream in, byte[] buf, int len)
throws IOException
{
throws IOException {
int s = 0;
while (s < len)
{
while (s < len) {
int i = in.read(buf, s, len - s);
if (i <= 0)
{
if (i <= 0) {
throw new ProxyException(ProxyInfo.ProxyType.SOCKS5, "stream " +
"is closed");
}

View file

@ -24,16 +24,13 @@ import org.jivesoftware.smack.packet.Stanza;
import org.junit.Test;
public class StanzaCollectorTest
{
public class StanzaCollectorTest {
@Test
public void verifyRollover() throws InterruptedException
{
public void verifyRollover() throws InterruptedException {
TestStanzaCollector collector = new TestStanzaCollector(null, new OKEverything(), 5);
for (int i = 0; i < 6; i++)
{
for (int i = 0; i < 6; i++) {
Stanza testPacket = new TestPacket(i);
collector.processStanza(testPacket);
}
@ -46,8 +43,7 @@ public class StanzaCollectorTest
assertEquals("5", collector.pollResult().getStanzaId());
assertNull(collector.pollResult());
for (int i = 10; i < 15; i++)
{
for (int i = 10; i < 15; i++) {
Stanza testPacket = new TestPacket(i);
collector.processStanza(testPacket);
}
@ -67,26 +63,18 @@ public class StanzaCollectorTest
* catch problems.
*/
@Test
public void verifyThreadSafety()
{
public void verifyThreadSafety() {
int insertCount = 500;
final TestStanzaCollector collector = new TestStanzaCollector(null, new OKEverything(), insertCount);
Thread consumer1 = new Thread(new Runnable()
{
Thread consumer1 = new Thread(new Runnable() {
@Override
public void run()
{
try
{
while (true)
{
try
{
public void run() {
try {
while (true) {
try {
Thread.sleep(3);
}
catch (InterruptedException e)
{
} catch (InterruptedException e) {
}
@SuppressWarnings("unused")
Stanza packet = collector.nextResultBlockForever();
@ -100,55 +88,41 @@ public class StanzaCollectorTest
});
consumer1.setName("consumer 1");
Thread consumer2 = new Thread(new Runnable()
{
Thread consumer2 = new Thread(new Runnable() {
@Override
public void run()
{
public void run() {
Stanza p;
do
{
try
{
do {
try {
Thread.sleep(3);
}
catch (InterruptedException e)
{
} catch (InterruptedException e) {
}
try {
p = collector.nextResult(1);
}
catch (InterruptedException e) {
} catch (InterruptedException e) {
throw new RuntimeException(e);
}
// System.out.println(Thread.currentThread().getName() + " packet: " + p);
// System.out.println(Thread.currentThread().getName() + " packet: " + p);
}
while (p != null);
}
});
consumer2.setName("consumer 2");
Thread consumer3 = new Thread(new Runnable()
{
Thread consumer3 = new Thread(new Runnable() {
@Override
public void run()
{
public void run() {
Stanza p;
do
{
try
{
do {
try {
Thread.sleep(3);
}
catch (InterruptedException e)
{
} catch (InterruptedException e) {
}
p = collector.pollResult();
// System.out.println(Thread.currentThread().getName() + " packet: " + p);
}
while (p != null);
// System.out.println(Thread.currentThread().getName() + " packet: " + p);
} while (p != null);
}
});
consumer3.setName("consumer 3");
@ -157,54 +131,43 @@ public class StanzaCollectorTest
consumer2.start();
consumer3.start();
for (int i = 0; i < insertCount; i++)
{
for (int i = 0; i < insertCount; i++) {
collector.processStanza(new TestPacket(i));
}
try
{
try {
Thread.sleep(5000);
consumer3.join();
consumer2.join();
consumer1.interrupt();
}
catch (InterruptedException e)
{
} catch (InterruptedException e) {
}
// We cannot guarantee that this is going to pass due to the possible issue of timing between consumer 1
// and main, but the probability is extremely remote.
assertNull(collector.pollResult());
}
static class OKEverything implements StanzaFilter
{
static class OKEverything implements StanzaFilter {
@Override
public boolean accept(Stanza packet)
{
public boolean accept(Stanza packet) {
return true;
}
}
static class TestStanzaCollector extends StanzaCollector
{
protected TestStanzaCollector(XMPPConnection conection, StanzaFilter packetFilter, int size)
{
static class TestStanzaCollector extends StanzaCollector {
protected TestStanzaCollector(XMPPConnection conection, StanzaFilter packetFilter, int size) {
super(conection, StanzaCollector.newConfiguration().setStanzaFilter(packetFilter).setSize(size));
}
}
static class TestPacket extends Stanza
{
TestPacket(int i)
{
static class TestPacket extends Stanza {
TestPacket(int i) {
setStanzaId(String.valueOf(i));
}
@Override
public String toXML()
{
public String toXML() {
return "<packetId>" + getStanzaId() + "</packetId>";
}

View file

@ -45,8 +45,7 @@ public class FromMatchesFilterTest {
private static final Jid SERVICE_JID2 = JidTestUtil.PUBSUB_EXAMPLE_ORG;
@Test
public void autoCompareMatchingEntityFullJid()
{
public void autoCompareMatchingEntityFullJid() {
FromMatchesFilter filter = FromMatchesFilter.create(FULL_JID1_R1);
Stanza packet = new Message();
@ -70,8 +69,7 @@ public class FromMatchesFilterTest {
}
@Test
public void autoCompareMatchingBaseJid()
{
public void autoCompareMatchingBaseJid() {
FromMatchesFilter filter = FromMatchesFilter.create(BASE_JID1);
Stanza packet = new Message();
@ -95,8 +93,7 @@ public class FromMatchesFilterTest {
}
@Test
public void autoCompareMatchingServiceJid()
{
public void autoCompareMatchingServiceJid() {
FromMatchesFilter filter = FromMatchesFilter.create(SERVICE_JID1);
Stanza packet = new Message();
@ -117,8 +114,7 @@ public class FromMatchesFilterTest {
}
@Test
public void bareCompareMatchingEntityFullJid()
{
public void bareCompareMatchingEntityFullJid() {
FromMatchesFilter filter = FromMatchesFilter.createBare(FULL_JID1_R1);
Stanza packet = new Message();
@ -142,8 +138,7 @@ public class FromMatchesFilterTest {
}
@Test
public void bareCompareMatchingBaseJid()
{
public void bareCompareMatchingBaseJid() {
FromMatchesFilter filter = FromMatchesFilter.createBare(BASE_JID1);
Stanza packet = new Message();
@ -167,8 +162,7 @@ public class FromMatchesFilterTest {
}
@Test
public void bareCompareMatchingServiceJid()
{
public void bareCompareMatchingServiceJid() {
FromMatchesFilter filter = FromMatchesFilter.createBare(SERVICE_JID1);
Stanza packet = new Message();
@ -189,8 +183,7 @@ public class FromMatchesFilterTest {
}
@Test
public void fullCompareMatchingEntityFullJid()
{
public void fullCompareMatchingEntityFullJid() {
FromMatchesFilter filter = FromMatchesFilter.createFull(FULL_JID1_R1);
Stanza packet = new Message();
@ -214,8 +207,7 @@ public class FromMatchesFilterTest {
}
@Test
public void fullCompareMatchingBaseJid()
{
public void fullCompareMatchingBaseJid() {
FromMatchesFilter filter = FromMatchesFilter.createFull(BASE_JID1);
Stanza packet = new Message();
@ -239,8 +231,7 @@ public class FromMatchesFilterTest {
}
@Test
public void fullCompareMatchingServiceJid()
{
public void fullCompareMatchingServiceJid() {
FromMatchesFilter filter = FromMatchesFilter.createFull(SERVICE_JID1);
Stanza packet = new Message();