View Javadoc
1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one or more
3    * contributor license agreements. See the NOTICE file distributed with
4    * this work for additional information regarding copyright ownership.
5    * The ASF licenses this file to You under the Apache license, Version 2.0
6    * (the "License"); you may not use this file except in compliance with
7    * the License. You may obtain a copy of the License at
8    *
9    *      http://www.apache.org/licenses/LICENSE-2.0
10   *
11   * Unless required by applicable law or agreed to in writing, software
12   * distributed under the License is distributed on an "AS IS" BASIS,
13   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14   * See the license for the specific language governing permissions and
15   * limitations under the license.
16   */
17  package org.apache.logging.log4j.core.appender;
18  
19  import java.nio.charset.StandardCharsets;
20  
21  import org.apache.logging.log4j.util.Chars;
22  
23  /**
24   * Wraps messages that are formatted according to RFC 5425.
25   *
26   * @see <a href="https://tools.ietf.org/html/rfc5425">RFC 5425</a>
27   */
28  public class TlsSyslogFrame {
29      private final String message;
30      private final int byteLength;
31  
32      public TlsSyslogFrame(final String message) {
33          this.message = message;
34          final byte[] messageBytes = message.getBytes(StandardCharsets.UTF_8);
35          byteLength = messageBytes.length;
36      }
37  
38      public String getMessage() {
39          return this.message;
40      }
41  
42      @Override
43      public String toString() {
44          return Integer.toString(byteLength) + Chars.SPACE + message;
45      }
46  
47      @Override
48      public int hashCode() {
49          final int prime = 31;
50          int result = 1;
51          result = prime * result + ((message == null) ? 0 : message.hashCode());
52          return result;
53      }
54  
55      @Override
56      public boolean equals(final Object obj) {
57          if (this == obj) {
58              return true;
59          }
60          if (obj == null) {
61              return false;
62          }
63          if (!(obj instanceof TlsSyslogFrame)) {
64              return false;
65          }
66          final TlsSyslogFrame other = (TlsSyslogFrame) obj;
67          if (message == null) {
68              if (other.message != null) {
69                  return false;
70              }
71          } else if (!message.equals(other.message)) {
72              return false;
73          }
74          return true;
75      }
76  
77  }