com.landawn.abacus.http.HARUtil Maven / Gradle / Ivy
/*
* Copyright (C) 2024 HaiYang Li
*
* 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.
*/
package com.landawn.abacus.http;
import java.io.File;
import java.util.List;
import java.util.Map;
import java.util.function.BiPredicate;
import java.util.function.Consumer;
import java.util.function.Predicate;
import com.landawn.abacus.logging.Logger;
import com.landawn.abacus.logging.LoggerFactory;
import com.landawn.abacus.util.Fn;
import com.landawn.abacus.util.IOUtil;
import com.landawn.abacus.util.Maps;
import com.landawn.abacus.util.N;
import com.landawn.abacus.util.Strings;
import com.landawn.abacus.util.Tuple;
import com.landawn.abacus.util.Tuple.Tuple2;
import com.landawn.abacus.util.Tuple.Tuple3;
import com.landawn.abacus.util.cs;
import com.landawn.abacus.util.u.Optional;
import com.landawn.abacus.util.stream.Stream;
@SuppressWarnings("JavadocLinkAsPlainText")
public final class HARUtil {
private static final Logger logger = LoggerFactory.getLogger(HARUtil.class);
private static final BiPredicate super String, String> defaultHttpHeaderFilterForHARRequest = HttpUtil::isValidHttpHeader;
private static final ThreadLocal> httpHeaderFilterForHARRequest_TL = ThreadLocal //NOSONAR
.withInitial(() -> defaultHttpHeaderFilterForHARRequest);
private static final Consumer defaultCurlLogHandler = curl -> {
if (logger.isInfoEnabled()) {
logger.info(curl);
}
};
private static final ThreadLocal>> logRequestCurlForHARRequest_TL = ThreadLocal //NOSONAR
.withInitial(() -> Tuple.of(false, '\'', defaultCurlLogHandler));
/**
*
* @param httpHeaderFilterForHARRequest
* @throws IllegalArgumentException
*/
public static void setHttpHeaderFilterForHARRequest(final BiPredicate super String, String> httpHeaderFilterForHARRequest)
throws IllegalArgumentException {
N.checkArgNotNull(httpHeaderFilterForHARRequest, cs.httpHeaderFilterForHARRequest);
httpHeaderFilterForHARRequest_TL.set(httpHeaderFilterForHARRequest);
}
public static void resetHttpHeaderFilterForHARRequest() {
httpHeaderFilterForHARRequest_TL.set(defaultHttpHeaderFilterForHARRequest);
}
/**
*
* @param logRequest
*/
public static void logRequestCurlForHARRequest(final boolean logRequest) {
logRequestCurlForHARRequest(logRequest, '\'');
}
/**
*
* @param logRequest
* @param quoteChar
*/
public static void logRequestCurlForHARRequest(final boolean logRequest, final char quoteChar) {
logRequestCurlForHARRequest_TL.set(Tuple.of(logRequest, quoteChar, defaultCurlLogHandler));
}
/**
*
* @param logRequest
* @param quoteChar
* @param logHandler
*/
public static void logRequestCurlForHARRequest(final boolean logRequest, final char quoteChar, final Consumer super String> logHandler) {
logRequestCurlForHARRequest_TL.set(Tuple.of(logRequest, quoteChar, logHandler));
}
/**
* http://www.softwareishard.com/har/viewer/
*
* https://confluence.atlassian.com/kb/generating-har-files-and-analyzing-web-requests-720420612.html
*
* @param har
* @param targetUrl
* @return
*/
public static String sendRequestByHAR(final File har, final String targetUrl) {
return sendRequestByHAR(har, Fn.equal(targetUrl));
}
/**
* http://www.softwareishard.com/har/viewer/
*
* https://confluence.atlassian.com/kb/generating-har-files-and-analyzing-web-requests-720420612.html
*
* @param har
* @param filterForTargetUrl
* @return
*/
public static String sendRequestByHAR(final File har, final Predicate super String> filterForTargetUrl) {
return sendRequestByHAR(IOUtil.readAllToString(har), filterForTargetUrl);
}
/**
* http://www.softwareishard.com/har/viewer/
*
* https://confluence.atlassian.com/kb/generating-har-files-and-analyzing-web-requests-720420612.html
*
* @param har
* @param targetUrl
* @return
*/
public static String sendRequestByHAR(final String har, final String targetUrl) {
return sendRequestByHAR(har, Fn.equal(targetUrl));
}
/**
* http://www.softwareishard.com/har/viewer/
*
* https://confluence.atlassian.com/kb/generating-har-files-and-analyzing-web-requests-720420612.html
*
* @param har
* @param filterForTargetUrl
* @return
*/
@SuppressWarnings("rawtypes")
public static String sendRequestByHAR(final String har, final Predicate super String> filterForTargetUrl) {
final Map map = N.fromJson(har, Map.class);
final List
© 2015 - 2025 Weber Informatics LLC | Privacy Policy