Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance. Project price only 1 $
You can buy this project and download/modify it how often you want.
/*-
* #%L
* Coffee
* %%
* Copyright (C) 2020 - 2023 i-Cell Mobilsoft Zrt.
* %%
* Licensed 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.
* #L%
*/
package hu.icellmobilsoft.coffee.rest.log.optimized;
import java.io.IOException;
import java.io.OutputStream;
import hu.icellmobilsoft.coffee.tool.utils.date.DateUtil;
import jakarta.inject.Inject;
import jakarta.inject.Named;
import jakarta.servlet.http.HttpServletResponse;
import jakarta.ws.rs.container.ContainerRequestContext;
import jakarta.ws.rs.container.ContainerRequestFilter;
import jakarta.ws.rs.core.Context;
import jakarta.ws.rs.core.MediaType;
import jakarta.ws.rs.core.MultivaluedMap;
import jakarta.ws.rs.core.Response.Status;
import jakarta.ws.rs.core.UriInfo;
import jakarta.ws.rs.ext.WriterInterceptor;
import jakarta.ws.rs.ext.WriterInterceptorContext;
import org.apache.commons.lang3.StringUtils;
import hu.icellmobilsoft.coffee.cdi.logger.AppLogger;
import hu.icellmobilsoft.coffee.cdi.logger.ThisLogger;
import hu.icellmobilsoft.coffee.dto.common.LogConstants;
import hu.icellmobilsoft.coffee.rest.cdi.BaseApplicationContainer;
import hu.icellmobilsoft.coffee.rest.log.annotation.LogSpecifier;
import hu.icellmobilsoft.coffee.rest.log.annotation.enumeration.LogSpecifierTarget;
import hu.icellmobilsoft.coffee.rest.utils.RestLoggerUtil;
import hu.icellmobilsoft.coffee.se.logging.mdc.MDC;
import hu.icellmobilsoft.coffee.tool.utils.stream.RequestLoggerInputStream;
import hu.icellmobilsoft.coffee.tool.utils.stream.ResponseEntityCollectorOutputStream;
import hu.icellmobilsoft.coffee.tool.utils.string.RandomUtil;
/**
* Base class for REST logging
*
* @author ischeffer
* @author mate.biro
* @since 2.4.0
*/
public abstract class BaseRestLogger implements ContainerRequestFilter, WriterInterceptor {
@Inject
@ThisLogger
private AppLogger log;
@Inject
private BaseApplicationContainer baseApplicationContainer;
@Inject
private @Named("optimized_RequestResponseLogger") RequestResponseLogger requestResponseLogger;
@Context
private UriInfo uriInfo;
@Context
private HttpServletResponse httpServletResponse;
/**
* Default constructor, constructs a new object.
*/
public BaseRestLogger() {
super();
}
/** {@inheritDoc} */
@Override
public void filter(ContainerRequestContext requestContext) throws IOException {
MDC.clear();
MDC.put(LogConstants.LOG_SERVICE_NAME, baseApplicationContainer.getCoffeeAppName());
processRequest(requestContext);
}
/** {@inheritDoc} */
@Override
public void aroundWriteTo(WriterInterceptorContext context) throws IOException {
processResponse(context);
}
/**
* Processes HTTP request.
*
* @param requestContext
* context
*/
protected void processRequest(ContainerRequestContext requestContext) {
if (RestLoggerUtil.logDisabled(requestContext, LogSpecifierTarget.REQUEST)) {
return;
}
var logMessage = new StringBuilder();
appendRequestLine(logMessage, requestContext);
appendRequestHeaders(logMessage, requestContext);
if (!requestContext.hasEntity() || requestContext.getLength() <= 0) {
log.info(logMessage.toString());
}
int maxRequestEntityLogSize = requestResponseLogger.getMaxRequestEntityLogSize(requestContext);
var requestLoggerInputStream = new RequestLoggerInputStream(
requestContext.getEntityStream(),
maxRequestEntityLogSize,
RequestResponseLogger.REQUEST_PREFIX,
logMessage);
// We set our own InputStream in the context, which will log the request when reading the entity stream
requestContext.setEntityStream(requestLoggerInputStream);
}
/**
* Processes HTTP response.
*
* @param context
* context
* @throws IOException
* if response cannot be processed.
*/
@SuppressWarnings("Var")
protected void processResponse(WriterInterceptorContext context) throws IOException {
if (RestLoggerUtil.logDisabled(context, LogSpecifierTarget.RESPONSE)) {
context.proceed();
return;
}
StringBuilder message = new StringBuilder();
try {
printResponseLine(message, context);
printResponseHeaders(message, context);
OutputStream originalResponseStream = context.getOutputStream();
byte[] entity = new byte[0];
int maxResponseEntityLogSize = requestResponseLogger.getMaxResponseEntityLogSize(context);
if (maxResponseEntityLogSize != LogSpecifier.NO_LOG) {
var responseEntityCollectorOutputStream = new ResponseEntityCollectorOutputStream(originalResponseStream, maxResponseEntityLogSize);
// We set our own OutputStream in the context, which will collect the entity stream when writing to it for logging purposes
context.setOutputStream(responseEntityCollectorOutputStream);
context.proceed();
entity = responseEntityCollectorOutputStream.getEntity();
} else {
context.proceed();
}
printResponseEntity(message, context, entity);
} finally {
log.info(message.toString());
}
}
/**
* The name of the session key appearing in the HTTP headers.
* The logger will look for this key in the HTTP headers and use its value in the MDC.put(LogConstants.LOG_SESSION_ID, value) section.
*
* Process identification makes a lot of sense in Graylog logging
*
* @return session key
*/
public abstract String sessionKey();
/**
* Prints request headers from {@link ContainerRequestContext} and appends given {@link StringBuilder} with the print result.
*
* @param b
* request message
* @param requestContext
* context
* @see RequestResponseLogger#printRequestHeaders(java.util.Map)
*/
protected void appendRequestHeaders(StringBuilder b, ContainerRequestContext requestContext) {
MultivaluedMap headers = requestContext.getHeaders();
b.append(requestResponseLogger.printRequestHeaders(headers));
String sessionId = null;
if (headers != null && headers.containsKey(sessionKey())) {
sessionId = headers.get(sessionKey()).get(0);
}
MDC.put(LogConstants.LOG_SESSION_ID, StringUtils.defaultIfBlank(sessionId, RandomUtil.generateId()));
}
/**
* Prints http path info from {@link ContainerRequestContext} and appends given {@link StringBuilder} with the print result.
*
* @param b
* request message
* @param requestContext
* context
* @see RequestResponseLogger#printRequestLine(ContainerRequestContext)
*/
protected void appendRequestLine(StringBuilder b, ContainerRequestContext requestContext) {
b.append(requestResponseLogger.printRequestLine(requestContext));
// Since logging occurs only at the end of the stream, the timestamp does not reflect when the request was received
b.append(RequestResponseLogger.REQUEST_PREFIX).append("-- Request timestamp:").append(DateUtil.nowUTC()).append('\n');
}
/**
* Prints response URL line and appends given {@link StringBuilder} with the print result.
*
* @param b
* response message
* @param context
* context
* @see RequestResponseLogger#printResponseLine(String, int, String, String)
*/
protected void printResponseLine(StringBuilder b, WriterInterceptorContext context) {
String fullPath = uriInfo.getAbsolutePath().toASCIIString();
int status = httpServletResponse.getStatus();
Status statusEnum = Status.fromStatusCode(status);
String statusInfo = statusEnum != null ? statusEnum.getReasonPhrase() : null;
MediaType mediaType = context.getMediaType();
b.append(requestResponseLogger.printResponseLine(fullPath, status, String.valueOf(statusInfo), String.valueOf(mediaType)));
}
/**
* Prints response header values and appends given {@link StringBuilder} with the print result.
*
* @param b
* response message
* @param context
* context
* @see RequestResponseLogger#printResponseHeaders(java.util.Map)
*/
protected void printResponseHeaders(StringBuilder b, WriterInterceptorContext context) {
b.append(requestResponseLogger.printResponseHeaders(context.getHeaders()));
}
/**
* Prints response from {@link WriterInterceptorContext} and appends given {@link StringBuilder} with the print result.
*
* @param b
* response message
* @param context
* context
* @param entityCopy
* entity
* @see RequestResponseLogger#printResponseEntity(String, WriterInterceptorContext, byte[])
*/
protected void printResponseEntity(StringBuilder b, WriterInterceptorContext context, byte[] entityCopy) {
b.append(requestResponseLogger.printResponseEntity(uriInfo.getAbsolutePath().toASCIIString(), context, entityCopy));
}
}