001/* 002 * The contents of this file are subject to the terms of the Common Development and 003 * Distribution License (the License). You may not use this file except in compliance with the 004 * License. 005 * 006 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the 007 * specific language governing permission and limitations under the License. 008 * 009 * When distributing Covered Software, include this CDDL Header Notice in each file and include 010 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL 011 * Header, with the fields enclosed by brackets [] replaced by your own identifying 012 * information: "Portions Copyright [year] [name of copyright owner]". 013 * 014 * Copyright 2016 ForgeRock AS. 015 */ 016package org.opends.server.protocols.http; 017 018import static org.opends.server.loggers.CommonAudit.DEFAULT_TRANSACTION_ID; 019 020import java.net.URI; 021 022import org.forgerock.http.header.MalformedHeaderException; 023import org.forgerock.http.header.TransactionIdHeader; 024import org.forgerock.http.protocol.Request; 025import org.forgerock.services.context.AbstractContext; 026import org.forgerock.services.context.ClientContext; 027import org.forgerock.services.context.Context; 028import org.opends.server.core.ServerContext; 029import org.opends.server.loggers.HTTPAccessLogger; 030import org.opends.server.loggers.HTTPRequestInfo; 031 032/** This context contains the logging informations related to the request processing. */ 033public final class HttpLogContext extends AbstractContext implements HTTPRequestInfo 034{ 035 /** The client (remote) address. */ 036 private final String clientAddress; 037 /** The client (remote) port. */ 038 private final int clientPort; 039 /** The server (local) address. */ 040 private final String serverAddress; 041 /** The server (local) port. */ 042 private final int serverPort; 043 /** The protocol in use for this client connection. */ 044 private final String protocol; 045 /** The HTTP method/verb used for this request. */ 046 private final String method; 047 /** The user agent used by the client. */ 048 private final String userAgent; 049 /** TransactionId for tracking of ForgeRock stack transactions. */ 050 private final String transactionId; 051 /** The URI issued by the client. */ 052 private final URI uri; 053 054 private final long startTime; 055 private long totalProcessingTime; 056 private long connectionId; 057 private int statusCode; 058 private String authUser; 059 060 HttpLogContext(Context parent, ServerContext serverContext, Request request) 061 { 062 super(parent, "Http Log Context"); 063 064 final ClientContext clientContext = parent.asContext(ClientContext.class); 065 this.uri = request.getUri().asURI(); 066 this.serverAddress = uri.getHost(); 067 this.serverPort = uri.getPort(); 068 this.method = request.getMethod(); 069 this.protocol = request.getVersion(); 070 this.clientAddress = clientContext.getRemoteAddress(); 071 this.clientPort = clientContext.getRemotePort(); 072 this.userAgent = clientContext.getUserAgent(); 073 this.startTime = System.currentTimeMillis(); 074 this.transactionId = getTransactionId(serverContext, request); 075 } 076 077 private String getTransactionId(ServerContext serverContext, Request request) 078 { 079 if (serverContext.getCommonAudit().shouldTrustTransactionIds()) 080 { 081 try 082 { 083 TransactionIdHeader txHeader = request.getHeaders().get(TransactionIdHeader.class); 084 return txHeader == null ? DEFAULT_TRANSACTION_ID : txHeader.getTransactionId().getValue(); 085 } 086 catch (MalformedHeaderException e) 087 { 088 // ignore it 089 } 090 } 091 return DEFAULT_TRANSACTION_ID; 092 } 093 094 void setConnectionID(long connectionID) 095 { 096 this.connectionId = connectionID; 097 } 098 099 @Override 100 public void log(int statusCode) 101 { 102 this.statusCode = statusCode; 103 totalProcessingTime = System.currentTimeMillis() - startTime; 104 HTTPAccessLogger.logRequestInfo(this); 105 } 106 107 @Override 108 public String getAuthUser() 109 { 110 return authUser; 111 } 112 113 @Override 114 public void setAuthUser(String authUser) 115 { 116 this.authUser = authUser; 117 } 118 119 @Override 120 public int getStatusCode() 121 { 122 return statusCode; 123 } 124 125 @Override 126 public String getServerAddress() 127 { 128 return serverAddress; 129 } 130 131 @Override 132 public String getServerHost() 133 { 134 return serverAddress; 135 } 136 137 @Override 138 public int getServerPort() 139 { 140 return serverPort; 141 } 142 143 @Override 144 public String getClientAddress() 145 { 146 return clientAddress; 147 } 148 149 @Override 150 public String getClientHost() 151 { 152 return clientAddress; 153 } 154 155 @Override 156 public int getClientPort() 157 { 158 return clientPort; 159 } 160 161 @Override 162 public String getProtocol() 163 { 164 return protocol; 165 } 166 167 @Override 168 public String getMethod() 169 { 170 return method; 171 } 172 173 @Override 174 public URI getUri() 175 { 176 return uri; 177 } 178 179 @Override 180 public String getUserAgent() 181 { 182 return userAgent; 183 } 184 185 @Override 186 public long getConnectionID() 187 { 188 return connectionId; 189 } 190 191 @Override 192 public long getTotalProcessingTime() 193 { 194 return totalProcessingTime; 195 } 196 197 @Override 198 public String getTransactionId() 199 { 200 return transactionId; 201 } 202}