Search in sources :

Example 6 with LogConfiguration

use of com.dtflys.forest.logging.LogConfiguration in project forest by dromara.

the class DownloadLifeCycle method onSuccess.

@Override
public void onSuccess(Object data, ForestRequest request, ForestResponse response) {
    String dirPath = getAttributeAsString(request, "dir");
    String filename = getAttributeAsString(request, "filename");
    Type resultType = getAttribute(request, "__resultType", Type.class);
    if (StringUtils.isBlank(filename)) {
        filename = response.getFilename();
    }
    LogConfiguration logConfiguration = request.getLogConfiguration();
    ForestLogHandler logHandler = logConfiguration.getLogHandler();
    File dir = new File(dirPath);
    if (!dir.exists()) {
        try {
            dir.mkdirs();
            if (logConfiguration.isLogEnabled()) {
                logHandler.logContent("Created directory '" + dirPath + "' successful.");
            }
        } catch (Throwable th) {
            throw new ForestRuntimeException(th);
        }
    }
    InputStream in = null;
    if (data != null && data instanceof byte[]) {
        in = new ByteArrayInputStream((byte[]) data);
    } else {
        try {
            in = response.getInputStream();
        } catch (Exception e) {
            throw new ForestRuntimeException(e);
        }
    }
    String path = dir.getAbsolutePath() + File.separator + filename;
    File file = new File(path);
    try {
        FileUtils.copyInputStreamToFile(in, file);
        FileUtils.waitFor(file, 10);
        if (logConfiguration.isLogEnabled() || !file.exists()) {
            logHandler.logContent("Saved file '" + path + "' successful.");
        }
        request.addAttachment(ATTACHMENT_NAME_FILE, file);
        if (resultType != null) {
            ForestConverter converter = request.getConfiguration().getConverterMap().get(ForestDataType.AUTO);
            data = converter.convertToJavaObject(file, resultType);
            response.setResult(data);
        }
    } catch (IOException e) {
        throw new ForestRuntimeException(e);
    }
}
Also used : ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) Type(java.lang.reflect.Type) ForestDataType(com.dtflys.forest.utils.ForestDataType) ForestConverter(com.dtflys.forest.converter.ForestConverter) DownloadFile(com.dtflys.forest.extensions.DownloadFile)

Example 7 with LogConfiguration

use of com.dtflys.forest.logging.LogConfiguration in project forest by dromara.

the class BaseLogEnabledLifeCycle method onProxyHandlerInitialized.

@Override
public void onProxyHandlerInitialized(InterfaceProxyHandler interfaceProxyHandler, LogEnabled annotation) {
    LogConfiguration logConfiguration = interfaceProxyHandler.getBaseLogConfiguration();
    if (logConfiguration == null) {
        logConfiguration = new LogConfiguration();
        interfaceProxyHandler.setBaseLogConfiguration(logConfiguration);
    }
    boolean logEnabled = annotation.value();
    boolean logRequest = annotation.logRequest();
    boolean logResponseStatus = annotation.logResponseStatus();
    boolean logResponseContent = annotation.logResponseContent();
    logConfiguration.setLogEnabled(logEnabled);
    logConfiguration.setLogRequest(logRequest);
    logConfiguration.setLogResponseStatus(logResponseStatus);
    logConfiguration.setLogResponseContent(logResponseContent);
}
Also used : LogConfiguration(com.dtflys.forest.logging.LogConfiguration)

Example 8 with LogConfiguration

use of com.dtflys.forest.logging.LogConfiguration in project forest by dromara.

the class OkHttp3Executor method logResponse.

public void logResponse(ForestResponse response) {
    LogConfiguration logConfiguration = request.getLogConfiguration();
    if (!logConfiguration.isLogEnabled() || response.isLogged()) {
        return;
    }
    response.setLogged(true);
    ResponseLogMessage logMessage = new ResponseLogMessage(response, response.getStatusCode());
    ForestLogHandler logHandler = logConfiguration.getLogHandler();
    if (logHandler != null) {
        if (logConfiguration.isLogResponseStatus()) {
            logHandler.logResponseStatus(logMessage);
        }
        if (logConfiguration.isLogResponseContent()) {
            logHandler.logResponseContent(logMessage);
        }
    }
}
Also used : ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) ResponseLogMessage(com.dtflys.forest.logging.ResponseLogMessage)

Example 9 with LogConfiguration

use of com.dtflys.forest.logging.LogConfiguration in project forest by dromara.

the class LogHandlerLifeCycle method onMethodInitialized.

@Override
public void onMethodInitialized(ForestMethod method, LogHandler annotation) {
    MetaRequest metaRequest = method.getMetaRequest();
    if (metaRequest == null) {
        return;
    }
    ForestConfiguration configuration = method.getConfiguration();
    LogConfiguration logConfiguration = metaRequest.getLogConfiguration();
    if (logConfiguration == null) {
        logConfiguration = new LogConfiguration();
        logConfiguration.setLogEnabled(configuration.isLogEnabled());
        logConfiguration.setLogRequest(configuration.isLogRequest());
        logConfiguration.setLogResponseStatus(configuration.isLogResponseStatus());
        logConfiguration.setLogResponseContent(configuration.isLogResponseContent());
        metaRequest.setLogConfiguration(logConfiguration);
    }
    Class<? extends ForestLogHandler> logHandlerClass = annotation.value();
    ForestLogHandler logHandler = null;
    try {
        logHandler = logHandlerClass.newInstance();
    } catch (InstantiationException e) {
        throw new ForestRuntimeException(e);
    } catch (IllegalAccessException e) {
        throw new ForestRuntimeException(e);
    }
    if (logHandler != null) {
        logConfiguration.setLogHandler(logHandler);
    } else {
        logConfiguration.setLogHandler(configuration.getLogHandler());
    }
}
Also used : ForestConfiguration(com.dtflys.forest.config.ForestConfiguration) ForestLogHandler(com.dtflys.forest.logging.ForestLogHandler) MetaRequest(com.dtflys.forest.reflection.MetaRequest) ForestRuntimeException(com.dtflys.forest.exceptions.ForestRuntimeException) LogConfiguration(com.dtflys.forest.logging.LogConfiguration)

Example 10 with LogConfiguration

use of com.dtflys.forest.logging.LogConfiguration in project forest by dromara.

the class ForestMethod method processMetaRequest.

private void processMetaRequest(MetaRequest metaRequest) {
    Class[] paramTypes = method.getParameterTypes();
    Type[] genericParamTypes = method.getGenericParameterTypes();
    Annotation[][] paramAnns = method.getParameterAnnotations();
    Parameter[] parameters = method.getParameters();
    Class<? extends Annotation> reqAnnType = metaRequest.getRequestAnnotation().annotationType();
    parameterTemplateArray = new MappingParameter[paramTypes.length];
    processParameters(parameters, genericParamTypes, paramAnns);
    bodyTypeTemplate = makeTemplate(reqAnnType, "type", metaRequest.getBodyType());
    urlTemplate = makeURLTemplate(reqAnnType, "url", metaRequest.getUrl());
    typeTemplate = makeTemplate(reqAnnType, "type", metaRequest.getType());
    dataTypeTemplate = makeTemplate(reqAnnType, "dataType", metaRequest.getDataType());
    if (StringUtils.isNotEmpty(metaRequest.getContentType())) {
        contentTypeTemplate = makeTemplate(reqAnnType, "contentType", metaRequest.getContentType());
    }
    if (StringUtils.isNotEmpty(metaRequest.getUserAgent())) {
        userAgentTemplate = makeTemplate(reqAnnType, "userAgent", metaRequest.getUserAgent());
    }
    sslKeyStoreId = makeTemplate(reqAnnType, "keyStore", metaRequest.getKeyStore());
    if (StringUtils.isNotEmpty(metaRequest.getContentEncoding())) {
        encodeTemplate = makeTemplate(reqAnnType, "contentEncoding", metaRequest.getContentEncoding());
    }
    charsetTemplate = makeTemplate(reqAnnType, "charset", metaRequest.getCharset());
    if (metaRequest.getResponseEncoding() != null) {
        responseEncodingTemplate = makeTemplate(reqAnnType, "responseEncoding", metaRequest.getResponseEncoding());
    }
    sslProtocolTemplate = makeTemplate(reqAnnType, "sslProtocol", metaRequest.getSslProtocol());
    progressStep = metaRequest.getProgressStep();
    async = metaRequest.isAsync();
    retryerClass = metaRequest.getRetryer();
    Class<? extends ForestEncoder> encoderClass = metaRequest.getEncoder();
    Class<? extends ForestConverter> decoderClass = metaRequest.getDecoder();
    String[] dataArray = metaRequest.getData();
    String[] headerArray = metaRequest.getHeaders();
    Integer tout = metaRequest.getTimeout();
    if (tout != null && tout >= 0) {
        timeout = tout;
    }
    Integer ctout = metaRequest.getConnectTimeout();
    if (ctout != null && ctout >= 0) {
        connectTimeout = ctout;
    }
    Integer rtout = metaRequest.getReadTimeout();
    if (rtout != null && rtout >= 0) {
        readTimeout = rtout;
    }
    Integer rtnum = metaRequest.getRetryCount();
    if (rtnum != null && rtnum >= 0) {
        retryCount = rtnum;
    }
    maxRetryInterval = metaRequest.getMaxRetryInterval();
    logEnabled = configuration.isLogEnabled();
    if (!logEnabled) {
        logEnabled = metaRequest.isLogEnabled();
    }
    logRequest = configuration.isLogRequest();
    logResponseStatus = configuration.isLogResponseStatus();
    logResponseContent = configuration.isLogResponseContent();
    LogConfiguration metaLogConfiguration = metaRequest.getLogConfiguration();
    if (metaLogConfiguration == null && baseLogConfiguration != null) {
        metaLogConfiguration = baseLogConfiguration;
    }
    if (metaLogConfiguration != null) {
        logEnabled = metaLogConfiguration.isLogEnabled();
        logRequest = metaLogConfiguration.isLogRequest();
        logResponseStatus = metaLogConfiguration.isLogResponseStatus();
        logResponseContent = metaLogConfiguration.isLogResponseContent();
        logHandler = metaLogConfiguration.getLogHandler();
        if (logHandler == null && baseLogConfiguration != null) {
            logHandler = baseLogConfiguration.getLogHandler();
        }
    }
    if (logHandler == null && configuration.getLogHandler() != null) {
        logHandler = configuration.getLogHandler();
    }
    if (logHandler == null) {
        logHandler = new DefaultLogHandler();
    }
    logConfiguration = new LogConfiguration();
    logConfiguration.setLogEnabled(logEnabled);
    logConfiguration.setLogRequest(logRequest);
    logConfiguration.setLogResponseStatus(logResponseStatus);
    logConfiguration.setLogResponseContent(logResponseContent);
    logConfiguration.setLogHandler(logHandler);
    dataTemplateArray = new MappingTemplate[dataArray.length];
    for (int j = 0; j < dataArray.length; j++) {
        String data = dataArray[j];
        MappingTemplate dataTemplate = makeTemplate(reqAnnType, "data", data);
        dataTemplateArray[j] = dataTemplate;
    }
    headerTemplateArray = new MappingTemplate[headerArray.length];
    for (int j = 0; j < headerArray.length; j++) {
        String header = headerArray[j];
        MappingTemplate headerTemplate = makeTemplate(reqAnnType, "header", header);
        headerTemplateArray[j] = headerTemplate;
    }
    Class[] interceptorClasses = metaRequest.getInterceptor();
    if (interceptorClasses != null && interceptorClasses.length > 0) {
        for (int cidx = 0, len = interceptorClasses.length; cidx < len; cidx++) {
            Class interceptorClass = interceptorClasses[cidx];
            addInterceptor(interceptorClass);
        }
    }
    if (encoderClass != null && !encoderClass.isInterface() && ForestEncoder.class.isAssignableFrom(encoderClass)) {
        this.encoder = configuration.getForestObjectFactory().getObject(encoderClass);
    }
    if (decoderClass != null && !encoderClass.isInterface() && ForestConverter.class.isAssignableFrom(decoderClass)) {
        this.decoder = configuration.getForestObjectFactory().getObject(decoderClass);
    }
}
Also used : ForestEncoder(com.dtflys.forest.converter.ForestEncoder) MappingTemplate(com.dtflys.forest.mapping.MappingTemplate) DefaultLogHandler(com.dtflys.forest.logging.DefaultLogHandler) LogConfiguration(com.dtflys.forest.logging.LogConfiguration) ForestRequestType(com.dtflys.forest.http.ForestRequestType) ContentType(com.dtflys.forest.backend.ContentType) ForestDataType(com.dtflys.forest.utils.ForestDataType) ForestConverter(com.dtflys.forest.converter.ForestConverter) MappingParameter(com.dtflys.forest.mapping.MappingParameter) ForestQueryParameter(com.dtflys.forest.http.ForestQueryParameter)

Aggregations

LogConfiguration (com.dtflys.forest.logging.LogConfiguration)11 ForestLogHandler (com.dtflys.forest.logging.ForestLogHandler)6 ForestConfiguration (com.dtflys.forest.config.ForestConfiguration)3 ForestRuntimeException (com.dtflys.forest.exceptions.ForestRuntimeException)3 ForestConverter (com.dtflys.forest.converter.ForestConverter)2 RequestLogMessage (com.dtflys.forest.logging.RequestLogMessage)2 ResponseLogMessage (com.dtflys.forest.logging.ResponseLogMessage)2 MetaRequest (com.dtflys.forest.reflection.MetaRequest)2 ForestDataType (com.dtflys.forest.utils.ForestDataType)2 ContentType (com.dtflys.forest.backend.ContentType)1 ForestEncoder (com.dtflys.forest.converter.ForestEncoder)1 DownloadFile (com.dtflys.forest.extensions.DownloadFile)1 ForestQueryParameter (com.dtflys.forest.http.ForestQueryParameter)1 ForestRequest (com.dtflys.forest.http.ForestRequest)1 ForestRequestType (com.dtflys.forest.http.ForestRequestType)1 ForestResponse (com.dtflys.forest.http.ForestResponse)1 DefaultLogHandler (com.dtflys.forest.logging.DefaultLogHandler)1 RequestProxyLogMessage (com.dtflys.forest.logging.RequestProxyLogMessage)1 MappingParameter (com.dtflys.forest.mapping.MappingParameter)1 MappingTemplate (com.dtflys.forest.mapping.MappingTemplate)1