import de.dwerth.gntpserver.gntp.messages.SubscribeMessage;
public class MessageFactory {
public static GNTPMessage parseRawData(byte[] rawData) {
GNTPMessage retVal = null;
String firstLine = "";
int startPos = 0;
for (startPos = 0; startPos < rawData.length - 1; startPos++) {
// Search for CRLF
if (rawData[startPos] == 13 && rawData[startPos + 1] == 10) {
firstLine = new String(rawData, 0, startPos + 1);
break;
}
}
// Check what Messagetype we got here
StringTokenizer st = new StringTokenizer(firstLine, " ");
st.nextToken(); // GNTP/<version>
String messageType = st.nextToken(); // <messagetype>
if (messageType.equals(GNTPMessage.NOTIFY)) {
retVal = new NotifyMessage();
} else if (messageType.equals(GNTPMessage.REGISTER)) {
retVal = new RegisterMessage();
} else if (messageType.equals(GNTPMessage.SUBSCRIBE)) {
retVal = new SubscribeMessage();
} else {
throw new RuntimeException("Unknown Message Type: " + messageType);
}
String encryptionAlgorithmID = null;
String keyHashAlgorithmID = null;
if (st.hasMoreTokens()) {
encryptionAlgorithmID = st.nextToken().trim();
}
if (st.hasMoreTokens()) {
keyHashAlgorithmID = st.nextToken().trim();
}
if (encryptionAlgorithmID != null && !"NONE".equals(encryptionAlgorithmID)) {
throw new RuntimeException("Encryption not supported yet: " + encryptionAlgorithmID);
}
retVal.setEncryptionAlgorithmID(encryptionAlgorithmID);
retVal.setKeyHashAlgorithmID(keyHashAlgorithmID);
String aLine = null;
for (int i = startPos; i < rawData.length - 1; i++) {
// Search for CRLF
if (rawData[i] == 13 && rawData[i + 1] == 10) {