mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Matthew Swift
22.18.2016 9bdba2b5de74b9cdb94b809bebbda7465243ecb9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2016 ForgeRock AS.
 */
package org.opends.server.protocols.http;
 
import static org.opends.server.loggers.CommonAudit.DEFAULT_TRANSACTION_ID;
 
import java.net.URI;
 
import org.forgerock.http.header.MalformedHeaderException;
import org.forgerock.http.header.TransactionIdHeader;
import org.forgerock.http.protocol.Request;
import org.forgerock.services.context.AbstractContext;
import org.forgerock.services.context.ClientContext;
import org.forgerock.services.context.Context;
import org.opends.server.core.ServerContext;
import org.opends.server.loggers.HTTPAccessLogger;
import org.opends.server.loggers.HTTPRequestInfo;
 
/** This context contains the logging informations related to the request processing. */
public final class HttpLogContext extends AbstractContext implements HTTPRequestInfo
{
  /** The client (remote) address. */
  private final String clientAddress;
  /** The client (remote) port. */
  private final int clientPort;
  /** The server (local) address. */
  private final String serverAddress;
  /** The server (local) port. */
  private final int serverPort;
  /** The protocol in use for this client connection. */
  private final String protocol;
  /** The HTTP method/verb used for this request. */
  private final String method;
  /** The user agent used by the client. */
  private final String userAgent;
  /** TransactionId for tracking of ForgeRock stack transactions. */
  private final String transactionId;
  /** The URI issued by the client. */
  private final URI uri;
 
  private final long startTime;
  private long totalProcessingTime;
  private long connectionId;
  private int statusCode;
  private String authUser;
 
  HttpLogContext(Context parent, ServerContext serverContext, Request request)
  {
    super(parent, "Http Log Context");
 
    final ClientContext clientContext = parent.asContext(ClientContext.class);
    this.uri = request.getUri().asURI();
    this.serverAddress = uri.getHost();
    this.serverPort = uri.getPort();
    this.method = request.getMethod();
    this.protocol = request.getVersion();
    this.clientAddress = clientContext.getRemoteAddress();
    this.clientPort = clientContext.getRemotePort();
    this.userAgent = clientContext.getUserAgent();
    this.startTime = System.currentTimeMillis();
    this.transactionId = getTransactionId(serverContext, request);
  }
 
  private String getTransactionId(ServerContext serverContext, Request request)
  {
    if (serverContext.getCommonAudit().shouldTrustTransactionIds())
    {
      try
      {
        TransactionIdHeader txHeader = request.getHeaders().get(TransactionIdHeader.class);
        return txHeader == null ? DEFAULT_TRANSACTION_ID :  txHeader.getTransactionId().getValue();
      }
      catch (MalformedHeaderException e)
      {
        // ignore it
      }
    }
    return DEFAULT_TRANSACTION_ID;
  }
 
  void setConnectionID(long connectionID)
  {
    this.connectionId = connectionID;
  }
 
  @Override
  public void log(int statusCode)
  {
    this.statusCode = statusCode;
    totalProcessingTime = System.currentTimeMillis() - startTime;
    HTTPAccessLogger.logRequestInfo(this);
  }
 
  @Override
  public String getAuthUser()
  {
    return authUser;
  }
 
  @Override
  public void setAuthUser(String authUser)
  {
    this.authUser = authUser;
  }
 
  @Override
  public int getStatusCode()
  {
    return statusCode;
  }
 
  @Override
  public String getServerAddress()
  {
    return serverAddress;
  }
 
  @Override
  public String getServerHost()
  {
    return serverAddress;
  }
 
  @Override
  public int getServerPort()
  {
    return serverPort;
  }
 
  @Override
  public String getClientAddress()
  {
    return clientAddress;
  }
 
  @Override
  public String getClientHost()
  {
    return clientAddress;
  }
 
  @Override
  public int getClientPort()
  {
    return clientPort;
  }
 
  @Override
  public String getProtocol()
  {
    return protocol;
  }
 
  @Override
  public String getMethod()
  {
    return method;
  }
 
  @Override
  public URI getUri()
  {
    return uri;
  }
 
  @Override
  public String getUserAgent()
  {
    return userAgent;
  }
 
  @Override
  public long getConnectionID()
  {
    return connectionId;
  }
 
  @Override
  public long getTotalProcessingTime()
  {
    return totalProcessingTime;
  }
 
  @Override
  public String getTransactionId()
  {
    return transactionId;
  }
}