Synthia
Generic and flexible data structure generator
LogLine.java
Go to the documentation of this file.
1 /*
2  Synthia, a data structure generator
3  Copyright (C) 2019-2020 Laboratoire d'informatique formelle
4  Université du Québec à Chicoutimi, Canada
5 
6  This program is free software: you can redistribute it and/or modify
7  it under the terms of the GNU Lesser General Public License as published
8  by the Free Software Foundation, either version 3 of the License, or
9  (at your option) any later version.
10 
11  This program is distributed in the hope that it will be useful,
12  but WITHOUT ANY WARRANTY; without even the implied warranty of
13  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  GNU Lesser General Public License for more details.
15 
16  You should have received a copy of the GNU Lesser General Public License
17  along with this program. If not, see <http://www.gnu.org/licenses/>.
18  */
19 package examples.apache;
20 
21 import java.text.DateFormat;
22 import java.text.SimpleDateFormat;
23 import java.util.Date;
24 
25 /**
26  * Representation of a line of Apache's access log. This class contains all
27  * the fields that Apache displays in its standard log format (IP address,
28  * URL, timestamp, return code, file size).
29  * @ingroup Examples
30  */
31 public class LogLine
32 {
33  /**
34  * The possible status codes associated to each request
35  */
36  public enum StatusCode {OK, NOT_FOUND, INTERNAL_SERVER_ERROR}
37 
38  /**
39  * A formatter for dates
40  */
41  protected static final transient DateFormat m_dateFormat = new SimpleDateFormat("d-MMM-yyyy HH:mm:ss z");
42 
43  /**
44  * The IP address of an HTTP request
45  */
46  protected String m_ipAddress;
47 
48  /**
49  * The date the request was made
50  */
51  protected long m_timestamp;
52 
53  /**
54  * The GMT offset of the timestamp
55  */
56  protected int m_timeZone;
57 
58  /**
59  * The HTTP request made
60  */
61  protected Request m_request;
62 
63  /**
64  * The size of the object requested
65  */
66  protected int m_size;
67 
68  /**
69  * The status code that the server sends back to the client
70  */
72 
73  public LogLine(String ip, long timestamp, int timezone, Request req, StatusCode code, int size)
74  {
75  super();
76  m_ipAddress = ip;
77  m_timestamp = timestamp;
78  m_timeZone = timezone;
79  m_request = req;
80  m_statusCode = code;
81  m_size = size;
82  }
83 
84  @Override
85  public String toString()
86  {
87  StringBuilder out = new StringBuilder();
88  out.append(m_ipAddress);
89  out.append(" - - ");
90  out.append("[").append(formatDate(m_timestamp, m_timeZone)).append("]");
91  out.append(" \"").append(m_request.toString()).append("\" ");
92  out.append(getCode(m_statusCode)).append(" ").append(m_size);
93  return out.toString();
94  }
95 
96  protected static String formatDate(long timestamp, long timezone)
97  {
98  Date d = new Date(timestamp);
99  return m_dateFormat.format(d) + " " + timezone;
100  }
101 
102  protected static int getCode(StatusCode code)
103  {
104  switch (code)
105  {
106  case OK:
107  return 200;
108  case NOT_FOUND:
109  return 404;
110  case INTERNAL_SERVER_ERROR:
111  return 500;
112  default:
113  return 500;
114  }
115  }
116 
117  /**
118  * Simple representation of an HTTP request
119  */
120  public static class Request
121  {
122  public enum Method {GET}
123 
124  protected String m_url;
125 
126  protected Method m_method;
127 
128  public Request(Method m, String url)
129  {
130  super();
131  m_method = m;
132  m_url = url;
133  }
134 
135  @Override
136  public String toString()
137  {
138  return m_method + " " + m_url + " HTTP/2";
139  }
140  }
141 }
examples.apache.LogLine.m_request
Request m_request
The HTTP request made.
Definition: LogLine.java:61
examples.apache.LogLine.StatusCode.OK
OK
Definition: LogLine.java:36
examples.apache.LogLine
Representation of a line of Apache's access log.
Definition: LogLine.java:31
examples.apache.LogLine.m_ipAddress
String m_ipAddress
The IP address of an HTTP request.
Definition: LogLine.java:46
examples.apache.LogLine.StatusCode
The possible status codes associated to each request.
Definition: LogLine.java:36
examples.apache.LogLine.toString
String toString()
Definition: LogLine.java:85
examples.apache.LogLine.m_statusCode
StatusCode m_statusCode
The status code that the server sends back to the client.
Definition: LogLine.java:71
examples.apache.LogLine.m_timestamp
long m_timestamp
The date the request was made.
Definition: LogLine.java:51
examples.apache.LogLine.m_dateFormat
static final transient DateFormat m_dateFormat
A formatter for dates.
Definition: LogLine.java:41
examples.apache.LogLine.m_size
int m_size
The size of the object requested.
Definition: LogLine.java:66
examples.apache.LogLine.formatDate
static String formatDate(long timestamp, long timezone)
Definition: LogLine.java:96
examples.apache.LogLine.m_timeZone
int m_timeZone
The GMT offset of the timestamp.
Definition: LogLine.java:56
examples.apache.LogLine.LogLine
LogLine(String ip, long timestamp, int timezone, Request req, StatusCode code, int size)
Definition: LogLine.java:73
examples.apache.LogLine.Request.Method
Definition: LogLine.java:122
examples.apache.LogLine.getCode
static int getCode(StatusCode code)
Definition: LogLine.java:102