Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/main/java/com/notnoop/apns/EnhancedApnsNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,16 @@
import java.util.concurrent.atomic.AtomicInteger;
import com.notnoop.apns.internal.Utilities;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;

/**
* Represents an APNS notification to be sent to Apple service.
*/
public class EnhancedApnsNotification implements ApnsNotification {


private static final Logger LOGGER = LoggerFactory.getLogger(EnhancedApnsNotification.class);
private final static byte COMMAND = 1;
private static AtomicInteger nextId = new AtomicInteger(0);
private final int identifier;
Expand Down Expand Up @@ -168,7 +172,8 @@ public String toString() {
String payloadString;
try {
payloadString = new String(payload, "UTF-8");
} catch (Exception ex) {
} catch (UnsupportedEncodingException ex) {
LOGGER.debug("UTF-8 charset not found on the JRE", ex);
payloadString = "???";
}
return "Message(Id="+identifier+"; Token="+Utilities.encodeHex(deviceToken)+"; Payload="+payloadString+")";
Expand Down
10 changes: 7 additions & 3 deletions src/main/java/com/notnoop/apns/SimpleApnsNotification.java
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,9 @@

import com.notnoop.apns.internal.Utilities;
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.io.UnsupportedEncodingException;

/**
* Represents an APNS notification to be sent to Apple service. This is for legacy use only
Expand All @@ -52,7 +54,8 @@
@SuppressWarnings("deprecation")
@Deprecated
public class SimpleApnsNotification implements ApnsNotification {


private static final Logger LOGGER = LoggerFactory.getLogger(SimpleApnsNotification.class);
private final static byte COMMAND = 0;
private final byte[] deviceToken;
private final byte[] payload;
Expand Down Expand Up @@ -155,7 +158,8 @@ public String toString() {
String payloadString;
try {
payloadString = new String(payload, "UTF-8");
} catch (Exception ex) {
} catch (UnsupportedEncodingException ex) {
LOGGER.debug("UTF-8 charset not found on the JRE", ex);
payloadString = "???";
}
return "Message(Token="+Utilities.encodeHex(deviceToken)+"; Payload="+payloadString+")";
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ public void run() {
try {
in = socket.getInputStream();
} catch (IOException ioe) {
logger.warn("The value of socket is null", ioe);
in = null;
}

Expand Down
15 changes: 9 additions & 6 deletions src/test/java/com/notnoop/apns/utils/ApnsServerStub.java
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@
import java.nio.ByteBuffer;
import java.util.concurrent.Semaphore;
import java.util.concurrent.atomic.AtomicInteger;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class ApnsServerStub {

Expand Down Expand Up @@ -70,7 +72,8 @@ public static ApnsServerStub prepareAndStartServer() {
server.start();
return server;
}


private static final Logger LOGGER = LoggerFactory.getLogger(ApnsServerStub.class);
private final AtomicInteger toWaitBeforeSend = new AtomicInteger(0);
private final ByteArrayOutputStream received;
private final ByteArrayOutputStream toSend;
Expand Down Expand Up @@ -118,14 +121,14 @@ public void stop() {
gatewaySocket.close();
}
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Can not close gatewaySocket properly", e);
}
try {
if (feedbackSocket != null) {
feedbackSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Can not close feedbackSocket properly", e);
}

if (gatewayThread != null) {
Expand All @@ -146,7 +149,7 @@ public void sendError(int err, int id) {
gatewayOutputStream.write(buf.array());
gatewayOutputStream.flush();
} catch (Exception ex) {
ex.printStackTrace();
LOGGER.warn("An error occured with accessing gateway", ex);
}
}

Expand Down Expand Up @@ -227,14 +230,14 @@ public void run() {
in.close();
}
} catch (IOException ioex) {
System.err.println(ioex.toString());
LOGGER.warn("Can not close socket properly", ioex);
}
try {
if (gatewayOutputStream != null) {
gatewayOutputStream.close();
}
} catch (IOException ioex) {
System.err.println(ioex.toString());
LOGGER.warn("Can not close gatewayOutputStream properly", ioex);
}
messages.release();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -85,14 +85,14 @@ public void stop() {
gatewaySocket.close();
}
} catch (IOException e) {
e.printStackTrace();
logger.warn("Can not close gatewaySocket properly", e);
}
try {
if (feedbackSocket != null) {
feedbackSocket.close();
}
} catch (IOException e) {
e.printStackTrace();
logger.warn("Can not close feedbackSocket properly", e);
}

if (gatewayThread != null) {
Expand Down Expand Up @@ -141,9 +141,10 @@ public void run() {
try {
handleGatewayConnection(new InputOutputSocket(gatewaySocket.accept()));
} catch (SocketException ex) {
logger.warn("Interruption while handling gateway connection", ex);
interrupt();
} catch (IOException ioe) {
ioe.printStackTrace();
logger.warn("An error occured while handling gateway connection", ioe);
}
}
} finally {
Expand Down Expand Up @@ -186,6 +187,7 @@ private void parseNotifications(final InputOutputSocket inputOutputSocket) {
break;
}
} catch (IOException ioe) {
logger.warn("An error occured while reading notification", ioe);
Thread.currentThread().interrupt();
}
}
Expand Down Expand Up @@ -255,7 +257,7 @@ public void interrupt() {
try {
gatewaySocket.close();
} catch (IOException e) {
e.printStackTrace();
logger.warn("Can not close gatewaySocket properly", e);
}
}
}
Expand Down Expand Up @@ -310,9 +312,10 @@ public void run() {
try {
handleFeedbackConnection(new InputOutputSocket(feedbackSocket.accept()));
} catch (SocketException ex) {
logger.warn("Interruption while handling feedback connection", ex);
interrupt();
} catch (IOException ioe) {
ioe.printStackTrace();
logger.warn("An error occured while handling feedback connection", ioe);
}
}
} finally {
Expand All @@ -331,7 +334,7 @@ public void run() {
sendFeedback(inputOutputSocket);
} catch (IOException ioe) {
// An exception is unexpected here. Close the current connection and bail out.
ioe.printStackTrace();
logger.warn("An error occured while sending feedback", ioe);
} finally {
inputOutputSocket.close();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,14 @@
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.Socket;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
* Wrap some of the boilerplate code using socket, enable passing around a socket together with its streams.
*/
public class InputOutputSocket {
private static final Logger LOGGER = LoggerFactory.getLogger(InputOutputSocket.class);
private final Socket socket;
private final ApnsInputStream inputStream;
private final DataOutputStream outputStream;
Expand Down Expand Up @@ -75,19 +78,19 @@ public synchronized void close() {
try {
inputStream.close();
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Can not close inputStream properly", e);
}

try {
outputStream.close();
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Can not close outputStream properly", e);
}

try {
socket.close();
} catch (IOException e) {
e.printStackTrace();
LOGGER.warn("Can not close socket properly", e);
}
}

Expand Down