Skip to content

Commit

Permalink
Merge pull request #233 from reportportal/develop
Browse files Browse the repository at this point in the history
Release
  • Loading branch information
HardNorth committed Feb 6, 2024
2 parents b520934 + bc9adaf commit e4838ec
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 49 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
# Changelog

## [Unreleased]
### Changed
- Improve MIME type detection in `MimeTypeDetector.detect(java.io.File)`, `ReportPortal.emitLog` and `ReportPortal.emitLaunchLog` methods, by @HardNorth

## [5.2.2]
### Added
Expand Down
75 changes: 31 additions & 44 deletions src/main/java/com/epam/reportportal/service/ReportPortal.java
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@
import com.epam.reportportal.service.launch.PrimaryLaunch;
import com.epam.reportportal.service.launch.SecondaryLaunch;
import com.epam.reportportal.utils.SslUtils;
import com.epam.reportportal.utils.files.Utils;
import com.epam.reportportal.utils.http.ClientUtils;
import com.epam.reportportal.utils.http.HttpRequestUtils;
import com.epam.reportportal.utils.properties.ListenerProperty;
Expand Down Expand Up @@ -59,9 +60,7 @@
import java.util.function.Function;

import static com.epam.reportportal.service.LaunchLoggingContext.DEFAULT_LAUNCH_KEY;
import static com.epam.reportportal.utils.MimeTypeDetector.detect;
import static com.epam.reportportal.utils.ObjectUtils.clonePojo;
import static com.epam.reportportal.utils.files.Utils.readFileToBytes;
import static java.util.Optional.ofNullable;
import static org.apache.commons.lang3.exception.ExceptionUtils.getStackTrace;

Expand All @@ -84,8 +83,8 @@ public class ReportPortal {
* @param rpClient ReportPortal client
* @param parameters Listener Parameters
*/
ReportPortal(@Nullable ReportPortalClient rpClient, @Nonnull ExecutorService executor,
@Nonnull ListenerParameters parameters, @Nullable LaunchIdLock launchIdLock) {
ReportPortal(@Nullable ReportPortalClient rpClient, @Nonnull ExecutorService executor, @Nonnull ListenerParameters parameters,
@Nullable LaunchIdLock launchIdLock) {
this.rpClient = rpClient;
this.executor = executor;
this.parameters = Objects.requireNonNull(parameters);
Expand Down Expand Up @@ -144,8 +143,7 @@ public Launch newLaunch(@Nonnull StartLaunchRQ rq) {
*/
@Nonnull
public Launch withLaunch(@Nonnull Maybe<String> launchUuid) {
return ofNullable(rpClient).map(c -> (Launch) new LaunchImpl(c, parameters, launchUuid, executor))
.orElse(Launch.NOOP_LAUNCH);
return ofNullable(rpClient).map(c -> (Launch) new LaunchImpl(c, parameters, launchUuid, executor)).orElse(Launch.NOOP_LAUNCH);
}

/**
Expand Down Expand Up @@ -199,8 +197,8 @@ public static ReportPortal create(ReportPortalClient client, ListenerParameters
* @return builder for {@link ReportPortal}
*/
@Nonnull
public static ReportPortal create(@Nonnull final ReportPortalClient client,
@Nonnull final ListenerParameters params, @Nonnull final ExecutorService executor) {
public static ReportPortal create(@Nonnull final ReportPortalClient client, @Nonnull final ListenerParameters params,
@Nonnull final ExecutorService executor) {
return new ReportPortal(client, executor, params, getLaunchLock(params));
}

Expand Down Expand Up @@ -289,21 +287,21 @@ public static boolean emitLaunchLog(final String message, final String level, fi
});
}

private static void fillSaveLogRQ(final SaveLogRQ rq, final String message, final String level, final Date time,
final File file) {
private static void fillSaveLogRQ(final SaveLogRQ rq, final String message, final String level, final Date time, final File file) {
rq.setMessage(message);
rq.setLevel(level);
rq.setLogTime(time);

try {
SaveLogRQ.File f = new SaveLogRQ.File();
f.setContentType(detect(file));
f.setContent(readFileToBytes(file));
TypeAwareByteSource typedSource = Utils.getFile(file);
f.setContentType(typedSource.getMediaType());
f.setContent(typedSource.read());

f.setName(UUID.randomUUID().toString());
rq.setFile(f);
} catch (IOException e) {
// seems like there is some problem. Do not report an file
// seems like there is some problem. Do not report a file
LOGGER.error("Cannot send file to ReportPortal", e);
}
}
Expand Down Expand Up @@ -344,8 +342,7 @@ public static boolean emitLaunchLog(final String message, final String level, fi
});
}

private static void fillSaveLogRQ(final SaveLogRQ rq, final String level, final Date time,
final ReportPortalMessage message) {
private static void fillSaveLogRQ(final SaveLogRQ rq, final String level, final Date time, final ReportPortalMessage message) {
rq.setLevel(level);
rq.setLogTime(time);
rq.setMessage(message.getMessage());
Expand Down Expand Up @@ -427,18 +424,12 @@ public Builder withExecutorService(ExecutorService executor) {
}

public ReportPortal build() {
ListenerParameters params = ofNullable(this.parameters).orElse(new ListenerParameters(
defaultPropertiesLoader()));
ListenerParameters params = ofNullable(this.parameters).orElse(new ListenerParameters(defaultPropertiesLoader()));
ExecutorService executorService = executor == null ? buildExecutorService(params) : executor;
Class<? extends ReportPortalClient> clientType = params.isAsyncReporting() ?
ReportPortalClientV2.class :
ReportPortalClient.class;
return new ReportPortal(
buildClient(clientType, params, executorService),
executorService,
params,
buildLaunchLock(params)
);
return new ReportPortal(buildClient(clientType, params, executorService), executorService, params, buildLaunchLock(params));
}

/**
Expand All @@ -447,8 +438,7 @@ public ReportPortal build() {
* @param <T> ReportPortal Client interface class
* @return a ReportPortal Client instance
*/
public <T extends ReportPortalClient> T buildClient(@Nonnull final Class<T> clientType,
@Nonnull final ListenerParameters params) {
public <T extends ReportPortalClient> T buildClient(@Nonnull final Class<T> clientType, @Nonnull final ListenerParameters params) {
return buildClient(clientType, params, buildExecutorService(params));
}

Expand All @@ -459,8 +449,8 @@ public <T extends ReportPortalClient> T buildClient(@Nonnull final Class<T> clie
* @param executor {@link ExecutorService} an Executor which will be used for internal request / response queue processing
* @return a ReportPortal Client instance
*/
public <T extends ReportPortalClient> T buildClient(@Nonnull final Class<T> clientType,
@Nonnull final ListenerParameters params, @Nonnull final ExecutorService executor) {
public <T extends ReportPortalClient> T buildClient(@Nonnull final Class<T> clientType, @Nonnull final ListenerParameters params,
@Nonnull final ExecutorService executor) {
OkHttpClient client = ofNullable(this.httpClient).map(c -> c.addInterceptor(new BearerAuthInterceptor(params.getApiKey()))
.build()).orElseGet(() -> defaultClient(params));

Expand All @@ -472,8 +462,7 @@ public <T extends ReportPortalClient> T buildClient(@Nonnull final Class<T> clie
* @param client {@link OkHttpClient} an HTTP client instance
* @return a ReportPortal endpoint description class
*/
protected Retrofit buildRestEndpoint(@Nonnull final ListenerParameters parameters,
@Nonnull final OkHttpClient client) {
protected Retrofit buildRestEndpoint(@Nonnull final ListenerParameters parameters, @Nonnull final OkHttpClient client) {
return buildRestEndpoint(parameters, client, buildExecutorService(parameters));
}

Expand All @@ -483,21 +472,19 @@ protected Retrofit buildRestEndpoint(@Nonnull final ListenerParameters parameter
* @param executor {@link ExecutorService} an Executor which will be used for internal request / response queue processing
* @return a ReportPortal endpoint description class
*/
protected Retrofit buildRestEndpoint(@Nonnull final ListenerParameters parameters,
@Nonnull final OkHttpClient client, @Nonnull final ExecutorService executor) {
String baseUrl =
(parameters.getBaseUrl().endsWith("/") ? parameters.getBaseUrl() : parameters.getBaseUrl() + "/")
+ API_PATH;
protected Retrofit buildRestEndpoint(@Nonnull final ListenerParameters parameters, @Nonnull final OkHttpClient client,
@Nonnull final ExecutorService executor) {
String baseUrl = (parameters.getBaseUrl().endsWith("/") ? parameters.getBaseUrl() : parameters.getBaseUrl() + "/") + API_PATH;
Retrofit.Builder builder = new Retrofit.Builder().client(client);
try {
builder.baseUrl(baseUrl);
} catch (NoSuchMethodError e) {
throw new InternalReportPortalClientException(
"Unable to initialize OkHttp client. "
+ "ReportPortal client supports OkHttp version 3.11.0 as minimum.\n"
+ "Please update OkHttp dependency.\n"
+ "Besides this usually happens due to old selenium-java version (it overrides our dependency), "
+ "please use selenium-java 3.141.0 as minimum.",
+ "ReportPortal client supports OkHttp version 3.11.0 as minimum.\n"
+ "Please update OkHttp dependency.\n"
+ "Besides this usually happens due to old selenium-java version (it overrides our dependency), "
+ "please use selenium-java 3.141.0 as minimum.",
e
);
}
Expand Down Expand Up @@ -529,9 +516,8 @@ protected OkHttpClient defaultClient(@Nonnull ListenerParameters parameters) {

if (HTTPS.equals(baseUrl.getProtocol()) && keyStore != null) {
if (null == keyStorePassword) {
String error =
"You should provide keystore password parameter [" + ListenerProperty.KEYSTORE_PASSWORD
+ "] if you use HTTPS protocol";
String error = "You should provide keystore password parameter [" + ListenerProperty.KEYSTORE_PASSWORD
+ "] if you use HTTPS protocol";
LOGGER.error(error);
throw new InternalReportPortalClientException(error);
}
Expand All @@ -540,13 +526,14 @@ protected OkHttpClient defaultClient(@Nonnull ListenerParameters parameters) {
TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
trustManagerFactory.init(SslUtils.loadKeyStore(keyStore, keyStorePassword));
TrustManager[] trustManagers = trustManagerFactory.getTrustManagers();
X509TrustManager trustManager = (X509TrustManager) ofNullable(trustManagers).flatMap(managers -> Arrays.stream(
managers).filter(m -> m instanceof X509TrustManager).findAny())
X509TrustManager trustManager = (X509TrustManager) ofNullable(trustManagers).flatMap(managers -> Arrays.stream(managers)
.filter(m -> m instanceof X509TrustManager)
.findAny())
.orElseThrow(() -> new InternalReportPortalClientException(
"Unable to find X509 trust manager, managers:" + Arrays.toString(trustManagers)));

SSLContext sslContext = SSLContext.getInstance("TLS");
sslContext.init(null, new TrustManager[]{trustManager}, null);
sslContext.init(null, new TrustManager[] { trustManager }, null);
SSLSocketFactory sslSocketFactory = sslContext.getSocketFactory();
builder.sslSocketFactory(sslSocketFactory, trustManager);
} catch (KeyStoreException | NoSuchAlgorithmException | KeyManagementException e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@
package com.epam.reportportal.utils;

import com.epam.reportportal.utils.files.ByteSource;
import com.epam.reportportal.utils.files.Utils;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.net.URLConnection;
import java.nio.file.Files;
Expand Down Expand Up @@ -55,7 +55,7 @@ private static String detectByExtensionInternal(String name) {

@Nonnull
public static String detect(@Nonnull final File file) throws IOException {
String type = URLConnection.guessContentTypeFromStream(new FileInputStream(file));
String type = URLConnection.guessContentTypeFromStream(Utils.getFileAsByteSource(file).openStream());
if (type == null) {
type = Files.probeContentType(file.toPath());
}
Expand Down
17 changes: 14 additions & 3 deletions src/main/java/com/epam/reportportal/utils/files/Utils.java
Original file line number Diff line number Diff line change
Expand Up @@ -146,18 +146,29 @@ public static URL getResource(@Nonnull String path) throws FileNotFoundException
* Locates and reads a file either by a direct path or by a relative path in classpath.
*
* @param file a file to locate and read
* @return file data and type
* @return file data
* @throws IOException in case of a read error, or a file not found
*/
public static TypeAwareByteSource getFile(@Nonnull File file) throws IOException {
public static ByteSource getFileAsByteSource(@Nonnull File file) throws IOException {
byte[] data;
if (file.exists() && file.isFile()) {
data = readFileToBytes(file);
} else {
data = readInputStreamToBytes(getResourceAsStream(file.getPath()));
}
return ByteSource.wrap(data);
}

/**
* Locates and reads a file either by a direct path or by a relative path in classpath.
*
* @param file a file to locate and read
* @return file data and type
* @throws IOException in case of a read error, or a file not found
*/
public static TypeAwareByteSource getFile(@Nonnull File file) throws IOException {
String name = file.getName();
ByteSource byteSource = ByteSource.wrap(data);
ByteSource byteSource = getFileAsByteSource(file);
return new TypeAwareByteSource(byteSource, MimeTypeDetector.detect(byteSource, name));
}
}

0 comments on commit e4838ec

Please sign in to comment.