Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  /*
   * JBoss, Home of Professional Open Source
   * Copyright 2014, JBoss Inc., and individual contributors as indicated
   * by the @authors tag.
   *
   * Licensed under the Apache License, Version 2.0 (the "License");
   * you may not use this file except in compliance with the License.
   * You may obtain a copy of the License at
   *
  * http://www.apache.org/licenses/LICENSE-2.0
  *
  * Unless required by applicable law or agreed to in writing, software
  * distributed under the License is distributed on an "AS IS" BASIS,
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  * See the License for the specific language governing permissions and
  * limitations under the License.
  */
 package org.jboss.as.test.syslogserver;
 
 import java.util.List;
 import java.util.Set;
 
Socket handler for TCP and TLS syslog server implementations. It handles automatically Octet Counting/Non-Transparent-Framing switch.

Author(s):
Josef Cacek
 
 public class TCPSyslogSocketHandler implements Runnable {
 
     private static final Logger LOGGER = Logger.getLogger(TCPSyslogSocketHandler.class);
 
     protected SyslogServerIF server = null;
     protected Socket socket = null;
     protected Set<Socketsockets = null;

    
Constructor.

Parameters:
sockets Set of all registered handlers.
server Syslog server instance
socket socket returned from the serverSocket.accept()
 
     public TCPSyslogSocketHandler(Set<SocketsocketsSyslogServerIF serverSocket socket) {
         this. = sockets;
         this. = server;
         this. = socket;
 
         synchronized (this.) {
             this..add(this.);
         }
     }
 
     public void run() {
         try {
             final BufferedInputStream bis = new BufferedInputStream(.getInputStream());
             int b = -1;
             final ByteArrayOutputStream baos = new ByteArrayOutputStream();
             boolean firstByte = true;
             boolean octetCounting = false;
             StringBuilder octetLenStr = new StringBuilder();
             do {
                 b = bis.read();
                 if (firstByte && b >= '1' && b <= '9') {
                     // handle Octet Counting messages (cf. rfc-6587)
                     octetCounting = true;
                 }
                 firstByte = false;
                 if (octetCounting) {
                     if (b != ' ') {
                         octetLenStr.append((charb);
                     } else {
                         int len = Integer.parseInt(octetLenStr.toString());
                         handleSyslogMessage(IOUtils.toByteArray(bislen));
                         // reset the stuff
                         octetLenStr = new StringBuilder();
                         firstByte = true;
                         octetCounting = false;
                     }
                 } else {
                     // handle Non-Transparent-Framing messages (cf. rfc-6587)
                     switch (b) {
                         case -1:
                         case '\r':
                         case '\n':
                             if (baos.size() > 0) {
                                 handleSyslogMessage(baos.toByteArray());
                                 baos.reset();
                                 firstByte = true;
                            }
                            break;
                        default:
                            baos.write(b);
                            break;
                    }
                }
            } while (b != -1);
        } catch (IOException e) {
            .warn("IOException occurred"e);
        } finally {
            IOUtils.closeQuietly();
            .remove();
        }
    }

    
Parses Rfc5424SyslogEvent instance from given raw message bytes and sends it to event handlers.

Parameters:
rawMsg
    private void handleSyslogMessage(final byte[] rawMsg) {
        final SyslogServerEventIF event = new Rfc5424SyslogEvent(rawMsg, 0, rawMsg.length);
        if (.isDebugEnabled()) {
            .debug("Firing Syslog event: " + event);
        }
        final List eventHandlers = this..getConfig().getEventHandlers();
        for (int i = 0; i < eventHandlers.size(); i++) {
            final SyslogServerEventHandlerIF eventHandler = (SyslogServerEventHandlerIFeventHandlers.get(i);
            eventHandler.event(this.event);
        }
    }
New to GrepCode? Check out our FAQ X