-
Notifications
You must be signed in to change notification settings - Fork 85
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Implement AccessLog support for HTTP/2.0
- Loading branch information
1 parent
1725057
commit a8d6a34
Showing
16 changed files
with
653 additions
and
18 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
native/src/main/java/io/ballerina/stdlib/http/api/logging/accesslog/HttpAccessLogConfig.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
package io.ballerina.stdlib.http.api.logging.accesslog; | ||
|
||
import io.ballerina.runtime.api.values.BArray; | ||
import io.ballerina.runtime.api.values.BMap; | ||
import io.ballerina.runtime.api.values.BString; | ||
|
||
import java.util.Arrays; | ||
import java.util.Collections; | ||
import java.util.HashSet; | ||
import java.util.List; | ||
import java.util.Set; | ||
import java.util.stream.Collectors; | ||
|
||
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_ATTRIBUTES; | ||
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_FORMAT; | ||
import static io.ballerina.stdlib.http.api.HttpConstants.HTTP_LOG_FORMAT_JSON; | ||
|
||
public class HttpAccessLogConfig { | ||
private static final Set<String> EXCLUDED_ATTRIBUTES = new HashSet<>(List.of( | ||
"http_referrer", "http_user_agent", "http_x_forwarded_for" | ||
)); | ||
private static BMap accessLogConfig = null; | ||
|
||
public static void initializeHttpAccessLogConfig(BMap accessLogConfigFromBallerina) { | ||
accessLogConfig = accessLogConfigFromBallerina; | ||
} | ||
|
||
public static List<String> getCustomHeaders() { | ||
List<String> attributes = getAccessLogAttributes(); | ||
if (attributes == null) { | ||
return Collections.emptyList(); | ||
} | ||
|
||
return attributes.stream() | ||
.filter(attr -> attr.startsWith("http_") && !EXCLUDED_ATTRIBUTES.contains(attr)) | ||
.map(attr -> attr.substring(5)) | ||
.collect(Collectors.toList()); | ||
} | ||
|
||
public static HttpAccessLogFormat getAccessLogFormat() { | ||
BString logFormat = accessLogConfig.getStringValue(HTTP_LOG_FORMAT); | ||
if (logFormat.getValue().equals(HTTP_LOG_FORMAT_JSON)) { | ||
return HttpAccessLogFormat.JSON; | ||
} | ||
return HttpAccessLogFormat.FLAT; | ||
} | ||
|
||
public static List<String> getAccessLogAttributes() { | ||
BArray logAttributes = accessLogConfig.getArrayValue(HTTP_LOG_ATTRIBUTES); | ||
if (logAttributes != null) { | ||
return Arrays.stream(logAttributes.getStringArray()) | ||
.collect(Collectors.toList()); | ||
} | ||
return null; | ||
} | ||
} |
23 changes: 23 additions & 0 deletions
23
native/src/main/java/io/ballerina/stdlib/http/api/logging/accesslog/HttpAccessLogFormat.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.ballerina.stdlib.http.api.logging.accesslog; | ||
|
||
public enum HttpAccessLogFormat { | ||
FLAT, JSON | ||
} |
183 changes: 183 additions & 0 deletions
183
...ve/src/main/java/io/ballerina/stdlib/http/api/logging/accesslog/HttpAccessLogMessage.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,183 @@ | ||
/* | ||
* Copyright (c) 2024, WSO2 LLC. (http://www.wso2.org) All Rights Reserved. | ||
* | ||
* WSO2 LLC. licenses this file to you under the Apache License, | ||
* Version 2.0 (the "License"); you may not use this file except | ||
* in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
package io.ballerina.stdlib.http.api.logging.accesslog; | ||
|
||
import java.util.Calendar; | ||
import java.util.HashMap; | ||
import java.util.Map; | ||
|
||
public class HttpAccessLogMessage { | ||
private String ip; | ||
private Calendar dateTime; | ||
private String requestMethod; | ||
private String requestUri; | ||
private String scheme; | ||
private int status; | ||
private long requestBodySize; | ||
private long responseBodySize; | ||
private long requestTime; | ||
private String httpReferrer; | ||
private String httpUserAgent; | ||
private String httpXForwardedFor; | ||
private String host; | ||
private int port; | ||
private Map<String, String> customHeaders; | ||
|
||
public HttpAccessLogMessage() { | ||
this.customHeaders = new HashMap<>(); | ||
} | ||
|
||
public HttpAccessLogMessage(String ip, Calendar dateTime, String requestMethod, String requestUri, String scheme, | ||
int status, long responseBodySize, String httpReferrer, String httpUserAgent) { | ||
this.ip = ip; | ||
this.dateTime = dateTime; | ||
this.requestMethod = requestMethod; | ||
this.requestUri = requestUri; | ||
this.scheme = scheme; | ||
this.status = status; | ||
this.responseBodySize = responseBodySize; | ||
this.httpReferrer = httpReferrer; | ||
this.httpUserAgent = httpUserAgent; | ||
this.customHeaders = new HashMap<>(); | ||
} | ||
|
||
public Calendar getDateTime() { | ||
return dateTime; | ||
} | ||
|
||
public void setDateTime(Calendar dateTime) { | ||
this.dateTime = dateTime; | ||
} | ||
|
||
public String getIp() { | ||
return ip; | ||
} | ||
|
||
public void setIp(String ip) { | ||
this.ip = ip; | ||
} | ||
|
||
public String getRequestMethod() { | ||
return requestMethod; | ||
} | ||
|
||
public void setRequestMethod(String requestMethod) { | ||
this.requestMethod = requestMethod; | ||
} | ||
|
||
public String getRequestUri() { | ||
return requestUri; | ||
} | ||
|
||
public void setRequestUri(String requestUri) { | ||
this.requestUri = requestUri; | ||
} | ||
|
||
public String getScheme() { | ||
return scheme; | ||
} | ||
|
||
public void setScheme(String scheme) { | ||
this.scheme = scheme; | ||
} | ||
|
||
public int getStatus() { | ||
return status; | ||
} | ||
|
||
public void setStatus(int status) { | ||
this.status = status; | ||
} | ||
|
||
public long getRequestBodySize() { | ||
return requestBodySize; | ||
} | ||
|
||
public void setRequestBodySize(Long requestBodySize) { | ||
this.requestBodySize = requestBodySize; | ||
} | ||
|
||
public long getResponseBodySize() { | ||
return responseBodySize; | ||
} | ||
|
||
public void setResponseBodySize(Long responseBodySize) { | ||
this.responseBodySize = responseBodySize; | ||
} | ||
|
||
public long getRequestTime() { | ||
return requestTime; | ||
} | ||
|
||
public void setRequestTime(Long requestTime) { | ||
this.requestTime = requestTime; | ||
} | ||
|
||
public String getHttpUserAgent() { | ||
return httpUserAgent; | ||
} | ||
|
||
public String getHttpReferrer() { | ||
return httpReferrer; | ||
} | ||
|
||
public void setHttpReferrer(String httpReferrer) { | ||
this.httpReferrer = httpReferrer; | ||
} | ||
|
||
public void setHttpUserAgent(String httpUserAgent) { | ||
this.httpUserAgent = httpUserAgent; | ||
} | ||
|
||
public String getHttpXForwardedFor() { | ||
return httpXForwardedFor; | ||
} | ||
|
||
public void setHttpXForwardedFor(String httpXForwardedFor) { | ||
this.httpXForwardedFor = httpXForwardedFor; | ||
} | ||
|
||
public String getHost() { | ||
return this.host; | ||
} | ||
|
||
public void setHost(String host) { | ||
this.host = host; | ||
} | ||
|
||
public int getPort() { | ||
return this.port; | ||
} | ||
|
||
public void setPort(int port) { | ||
this.port = port; | ||
} | ||
|
||
public Map<String, String> getCustomHeaders() { | ||
return customHeaders; | ||
} | ||
|
||
public void setCustomHeaders(Map<String, String> customHeaders) { | ||
this.customHeaders = customHeaders; | ||
} | ||
|
||
public void putCustomHeader(String headerKey, String headerValue) { | ||
this.customHeaders.put(headerKey, headerValue); | ||
} | ||
} |
Oops, something went wrong.