View Javadoc

1   /*
2    * Copyright [2007] [University Corporation for Advanced Internet Development, Inc.]
3    *
4    * Licensed under the Apache License, Version 2.0 (the "License");
5    * you may not use this file except in compliance with the License.
6    * You may obtain a copy of the License at
7    *
8    * http://www.apache.org/licenses/LICENSE-2.0
9    *
10   * Unless required by applicable law or agreed to in writing, software
11   * distributed under the License is distributed on an "AS IS" BASIS,
12   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13   * See the License for the specific language governing permissions and
14   * limitations under the License.
15   */
16  
17  package edu.internet2.middleware.shibboleth.common.log;
18  
19  import javax.servlet.http.HttpServletRequest;
20  
21  import org.joda.time.DateTime;
22  import org.joda.time.DateTimeZone;
23  import org.joda.time.format.DateTimeFormatter;
24  import org.joda.time.format.ISODateTimeFormat;
25  import org.opensaml.xml.util.DatatypeHelper;
26  
27  import edu.internet2.middleware.shibboleth.common.util.HttpHelper;
28  
29  /**
30   * Data object for generating server access logs.
31   */
32  public class AccessLogEntry {
33      
34      /** Name of the Shibboleth Access logging category. */
35      public static final String ACCESS_LOGGER_NAME = "Shibboleth-Access";
36      
37      /** Formatter used to convert timestamps to strings. */
38      private static DateTimeFormatter dateFormatter = ISODateTimeFormat.basicDateTimeNoMillis();
39      
40      /** Request timestamp. */
41      private DateTime requestTime;
42  
43      /** Hostname or IP address of the remote host. */
44      private String remoteHost;
45  
46      /** Hostname or IP address of the server. */
47      private String serverHost;
48  
49      /** Port the request came in on. */
50      private int serverPort;
51  
52      /** Path of the request. */
53      private String requestPath;
54  
55      /**
56       * Constructor.
57       * 
58       * @param request the request
59       */
60      public AccessLogEntry(HttpServletRequest request) {
61          requestTime = new DateTime();
62          remoteHost = request.getRemoteHost();
63          serverHost = request.getServerName();
64          serverPort = request.getServerPort();
65          requestPath = HttpHelper.getRequestUriWithoutContext(request);
66      }
67  
68      /**
69       * Constructor.
70       * 
71       * @param remote the remote client host name or IP
72       * @param host the servers host name or IP
73       * @param port the servers port number
74       * @param path the request path informatio minus the servlet context information
75       */
76      public AccessLogEntry(String remote, String host, int port, String path) {
77          requestTime = new DateTime();
78          remoteHost = DatatypeHelper.safeTrimOrNullString(remote);
79          serverHost = DatatypeHelper.safeTrimOrNullString(host);
80          serverPort = port;
81          requestPath = DatatypeHelper.safeTrimOrNullString(path);
82      }
83  
84      /**
85       * Gets the remote client host or IP address.
86       * 
87       * @return remote client host or IP address
88       */
89      public String getRemoteHost() {
90          return remoteHost;
91      }
92  
93      /**
94       * Gets the request path without servlet context information.
95       * 
96       * @return request path without servlet context information
97       */
98      public String getRequestPath() {
99          return requestPath;
100     }
101     
102     /**
103      * Gets the time the request was made.
104      * 
105      * @return time the request was made
106      */
107     public DateTime getRequestTime(){
108         return requestTime;
109     }
110 
111     /**
112      * Gets the server's host name or IP address.
113      * 
114      * @return server's host name or IP address
115      */
116     public String getServerHost() {
117         return serverHost;
118     }
119 
120     /**
121      * Gets the server's port number.
122      * 
123      * @return server's port number
124      */
125     public int getServerPort() {
126         return serverPort;
127     }
128     
129     /** {@inheritDoc} */
130     public String toString() {
131         StringBuilder entryString = new StringBuilder();
132 
133         entryString.append(getRequestTime().toString(dateFormatter.withZone(DateTimeZone.UTC)));
134         entryString.append("|");
135 
136         entryString.append(getRemoteHost());
137         entryString.append("|");
138 
139         entryString.append(getServerHost());
140         entryString.append(":");
141         entryString.append(getServerPort());
142         entryString.append("|");
143 
144         entryString.append(getRequestPath());
145         entryString.append("|");
146 
147         return entryString.toString();
148     }
149 }