View Javadoc

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