Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Release #21

Merged
merged 1 commit into from
Jan 24, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
- More `Prettiers` to `Prettifiers` renaming and deprecation of `Prettiers`, by @HardNorth

## [5.2.6]
### Added
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import java.util.function.Function;

import static com.epam.reportportal.formatting.http.Constants.BODY_TYPE_MAP;
import static com.epam.reportportal.formatting.http.Constants.DEFAULT_PRETTIERS;
import static com.epam.reportportal.formatting.http.Constants.DEFAULT_PRETTIFIERS;
import static java.util.Optional.ofNullable;

/**
Expand All @@ -54,7 +54,7 @@ public abstract class AbstractHttpFormatter<SELF extends AbstractHttpFormatter<S
protected final Function<Cookie, String> cookieConverter;
protected final Function<String, String> uriConverter;

private Map<String, Function<String, String>> contentPrettifiers = DEFAULT_PRETTIERS;
private Map<String, Function<String, String>> contentPrettifiers = DEFAULT_PRETTIFIERS;

/**
* @deprecated Use {@link #getContentPrettifiers()} instead
Expand Down
71 changes: 38 additions & 33 deletions src/main/java/com/epam/reportportal/formatting/http/Constants.java
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@
package com.epam.reportportal.formatting.http;

import com.epam.reportportal.formatting.http.entities.BodyType;
import com.epam.reportportal.formatting.http.prettiers.HtmlPrettier;
import com.epam.reportportal.formatting.http.prettiers.JsonPrettier;
import com.epam.reportportal.formatting.http.prettiers.XmlPrettier;
import com.epam.reportportal.formatting.http.prettifiers.HtmlPrettifier;
import com.epam.reportportal.formatting.http.prettifiers.JsonPrettifier;
import com.epam.reportportal.formatting.http.prettifiers.XmlPrettifier;
import com.epam.reportportal.utils.http.ContentType;

import java.util.*;
Expand Down Expand Up @@ -47,43 +47,48 @@ public class Constants {
ContentType.MULTIPART_PARALLEL
)));

public static final Set<String> TEXT_TYPES =
Collections.unmodifiableSet(new HashSet<>(Arrays.asList(ContentType.APPLICATION_JSON,
ContentType.TEXT_PLAIN,
ContentType.TEXT_HTML,
ContentType.TEXT_XML,
ContentType.APPLICATION_XML,
ContentType.APPLICATION_SOAP_XML,
ContentType.APPLICATION_ATOM_XML,
// Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly
"text/json",
"application/x.reportportal.launch.v2+json",
"application/x.reportportal.test.v2+json"
)));
public static final Set<String> TEXT_TYPES = Collections.unmodifiableSet(new HashSet<>(Arrays.asList(
ContentType.APPLICATION_JSON,
ContentType.TEXT_PLAIN,
ContentType.TEXT_HTML,
ContentType.TEXT_XML,
ContentType.APPLICATION_XML,
ContentType.APPLICATION_SOAP_XML,
ContentType.APPLICATION_ATOM_XML,
// Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly
"text/json",
"application/x.reportportal.launch.v2+json",
"application/x.reportportal.test.v2+json"
)));

public static final Set<String> FORM_TYPES = Collections.singleton(ContentType.APPLICATION_FORM_URLENCODED);

public static final Map<String, BodyType> BODY_TYPE_MAP = Collections.unmodifiableMap(Stream.of(TEXT_TYPES.stream()
.collect(Collectors.toMap(k -> k, v -> BodyType.TEXT)),
public static final Map<String, BodyType> BODY_TYPE_MAP = Collections.unmodifiableMap(Stream.of(
TEXT_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.TEXT)),
FORM_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.FORM)),
MULTIPART_TYPES.stream().collect(Collectors.toMap(k -> k, v -> BodyType.MULTIPART))
).flatMap(m -> m.entrySet().stream()).collect(Collectors.toMap(Map.Entry::getKey, Map.Entry::getValue)));

public static final Map<String, Function<String, String>> DEFAULT_PRETTIERS =
Collections.unmodifiableMap(new HashMap<String, Function<String, String>>() {{
put(ContentType.APPLICATION_XML, XmlPrettier.INSTANCE);
put(ContentType.APPLICATION_SOAP_XML, XmlPrettier.INSTANCE);
put(ContentType.APPLICATION_ATOM_XML, XmlPrettier.INSTANCE);
put(ContentType.APPLICATION_SVG_XML, XmlPrettier.INSTANCE);
put(ContentType.APPLICATION_XHTML_XML, XmlPrettier.INSTANCE);
put(ContentType.TEXT_XML, XmlPrettier.INSTANCE);
put(ContentType.APPLICATION_JSON, JsonPrettier.INSTANCE);
// Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly
put("text/json", JsonPrettier.INSTANCE);
put("application/x.reportportal.launch.v2+json", JsonPrettier.INSTANCE);
put("application/x.reportportal.test.v2+json", JsonPrettier.INSTANCE);
put(ContentType.TEXT_HTML, HtmlPrettier.INSTANCE);
}});
public static final Map<String, Function<String, String>> DEFAULT_PRETTIFIERS = Collections.unmodifiableMap(new HashMap<String, Function<String, String>>() {{
put(ContentType.APPLICATION_XML, XmlPrettifier.INSTANCE);
put(ContentType.APPLICATION_SOAP_XML, XmlPrettifier.INSTANCE);
put(ContentType.APPLICATION_ATOM_XML, XmlPrettifier.INSTANCE);
put(ContentType.APPLICATION_SVG_XML, XmlPrettifier.INSTANCE);
put(ContentType.APPLICATION_XHTML_XML, XmlPrettifier.INSTANCE);
put(ContentType.TEXT_XML, XmlPrettifier.INSTANCE);
put(ContentType.APPLICATION_JSON, JsonPrettifier.INSTANCE);
// Can't use ContentType.TEXT_JSON, etc. because client-java dependency marked as compileOnly
put("text/json", JsonPrettifier.INSTANCE);
put("application/x.reportportal.launch.v2+json", JsonPrettifier.INSTANCE);
put("application/x.reportportal.test.v2+json", JsonPrettifier.INSTANCE);
put(ContentType.TEXT_HTML, HtmlPrettifier.INSTANCE);
}});

/**
* @deprecated Use {@link #DEFAULT_PRETTIFIERS} instead
*/
@Deprecated
public static final Map<String, Function<String, String>> DEFAULT_PRETTIERS = DEFAULT_PRETTIFIERS;

private Constants() {
throw new RuntimeException("No instances should exist for the class!");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,14 +61,11 @@ public static String joinParts(@Nonnull String delimiter, @Nullable String... pa
if (parts == null) {
return "";
}
return Arrays.stream(parts)
.filter(p -> Objects.nonNull(p) && !p.isEmpty())
.collect(Collectors.joining(delimiter));
return Arrays.stream(parts).filter(p -> Objects.nonNull(p) && !p.isEmpty()).collect(Collectors.joining(delimiter));
}

@Nonnull
public static <T> String format(@Nullable List<T> entities, @Nonnull Function<T, String> converter,
@Nullable String tag) {
public static <T> String format(@Nullable List<T> entities, @Nonnull Function<T, String> converter, @Nullable String tag) {
String prefix = tag == null ? "" : tag + LINE_DELIMITER;
if (entities == null || entities.isEmpty()) {
return "";
Expand All @@ -80,53 +77,42 @@ public static <T> String format(@Nullable List<T> entities, @Nonnull Function<T,
}

@Nonnull
public static String formatHeaders(@Nullable List<Header> headers,
@Nullable Function<Header, String> headerConverter) {
return format(headers,
headerConverter == null ? DefaultHttpHeaderConverter.INSTANCE : headerConverter,
HEADERS_TAG
);
public static String formatHeaders(@Nullable List<Header> headers, @Nullable Function<Header, String> headerConverter) {
return format(headers, headerConverter == null ? DefaultHttpHeaderConverter.INSTANCE : headerConverter, HEADERS_TAG);
}

@Nonnull
public static String formatCookies(@Nullable List<Cookie> cookies,
@Nullable Function<Cookie, String> cookieConverter) {
return format(cookies,
cookieConverter == null ? DefaultCookieConverter.INSTANCE : cookieConverter,
COOKIES_TAG
);
public static String formatCookies(@Nullable List<Cookie> cookies, @Nullable Function<Cookie, String> cookieConverter) {
return format(cookies, cookieConverter == null ? DefaultCookieConverter.INSTANCE : cookieConverter, COOKIES_TAG);
}

@Nonnull
public static String formatText(@Nullable String header, @Nullable List<Param> params, @Nullable String tag,
@Nullable Function<Param, String> paramConverter) {
@Nullable Function<Param, String> paramConverter) {
if (params == null || params.isEmpty()) {
return header == null ? "" : header;
}
String prefix = tag == null ? "" : tag + LINE_DELIMITER;
String body = format(params,
paramConverter == null ? DefaultFormParamConverter.INSTANCE : paramConverter,
null
);
String body = format(params, paramConverter == null ? DefaultFormParamConverter.INSTANCE : paramConverter, null);
return (header == null || header.isEmpty() ? "" : header + LINE_DELIMITER + LINE_DELIMITER) + (body.isEmpty() ?
body :
prefix + BODY_HIGHLIGHT + LINE_DELIMITER + body + LINE_DELIMITER + BODY_HIGHLIGHT);
}

@Nonnull
public static String formatText(@Nullable String header, @Nullable String body, @Nullable String tag,
@Nullable Map<String, Function<String, String>> contentPrettiers, String contentType) {
Map<String, Function<String, String>> prettiers = contentPrettiers;
if (contentPrettiers == null) {
prettiers = Collections.emptyMap();
@Nullable Map<String, Function<String, String>> contentPrettifiers, String contentType) {
Map<String, Function<String, String>> prettifiers = contentPrettifiers;
if (contentPrettifiers == null) {
prettifiers = Collections.emptyMap();
}
if (body == null || body.isEmpty()) {
return header == null ? "" : header;
}
return (header == null || header.isEmpty() ? "" : header + LINE_DELIMITER + LINE_DELIMITER) + (tag == null || tag.isEmpty() ?
"" :
tag + LINE_DELIMITER) + BODY_HIGHLIGHT + LINE_DELIMITER + (prettiers.containsKey(contentType) ?
prettiers.get(contentType).apply(body) :
tag + LINE_DELIMITER) + BODY_HIGHLIGHT + LINE_DELIMITER + (prettifiers.containsKey(contentType) ?
prettifiers.get(contentType).apply(body) :
body) + LINE_DELIMITER + BODY_HIGHLIGHT;
}

Expand Down Expand Up @@ -162,10 +148,9 @@ public static Stream<Pair<String, String>> toKeyValue(@Nonnull String headerValu
}

@Nonnull
public static Cookie toCookie(@Nonnull String name, @Nullable String value, @Nullable String comment,
@Nullable String path, @Nullable String domain, @Nullable Long maxAge, @Nullable Boolean secured,
@Nullable Boolean httpOnly, @Nullable Date expiryDate, @Nullable Integer version,
@Nullable String sameSite) {
public static Cookie toCookie(@Nonnull String name, @Nullable String value, @Nullable String comment, @Nullable String path,
@Nullable String domain, @Nullable Long maxAge, @Nullable Boolean secured, @Nullable Boolean httpOnly,
@Nullable Date expiryDate, @Nullable Integer version, @Nullable String sameSite) {
Cookie cookie = new Cookie(name);
cookie.setValue(value);
cookie.setComment(comment);
Expand Down Expand Up @@ -197,17 +182,16 @@ public static Cookie toCookie(@Nonnull String headerValue) {
// Wed, 06-Sep-2023 11:22:09 GMT
Date expiryDate = ofNullable(cookieMetadata.get("expires")).map(d -> {
try {
return new SimpleDateFormat(DefaultCookieConverter.DEFAULT_COOKIE_DATE_FORMAT).parse(d.replace('-',
' '
));
return new SimpleDateFormat(DefaultCookieConverter.DEFAULT_COOKIE_DATE_FORMAT).parse(d.replace('-', ' '));
} catch (ParseException e) {
return null;
}
}).orElse(null);
Integer version = cookieMetadata.get("version") == null ? null : Integer.valueOf(cookieMetadata.get("version"));
String sameSite = cookieMetadata.get("samesite");

return toCookie(nameValue.getKey(),
return toCookie(
nameValue.getKey(),
nameValue.getValue(),
comment,
path,
Expand All @@ -231,8 +215,10 @@ public static boolean isSetCookie(@Nullable String headerName) {

@Nonnull
private static Charset getCharset(@Nullable String contentType) {
return ofNullable(contentType).flatMap(h -> toKeyValue(h).filter(p -> "charset".equalsIgnoreCase(p.getKey()))
.findAny()).map(Pair::getValue).map(Charset::forName).orElse(StandardCharsets.UTF_8);
return ofNullable(contentType).flatMap(h -> toKeyValue(h).filter(p -> "charset".equalsIgnoreCase(p.getKey())).findAny())
.map(Pair::getValue)
.map(Charset::forName)
.orElse(StandardCharsets.UTF_8);
}

@Nonnull
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public class HttpPartFormatter {
private String fileName;

private Function<Header, String> headerConverter;
private Map<String, Function<String, String>> prettiers;
private Map<String, Function<String, String>> prettifiers;

public HttpPartFormatter(@Nonnull PartType type, @Nonnull String mimeType, @Nonnull Object payload) {
this.type = type;
Expand Down Expand Up @@ -71,7 +71,7 @@ public byte[] getBinaryPayload() {
}

public String formatAsText() {
return HttpFormatUtils.formatText(formatHeaders(), getTextPayload(), BODY_PART_TAG, prettiers, mimeType);
return HttpFormatUtils.formatText(formatHeaders(), getTextPayload(), BODY_PART_TAG, prettifiers, mimeType);
}

public String formatForBinaryDataPrefix() {
Expand Down Expand Up @@ -128,8 +128,17 @@ public void setHeaderConverter(Function<Header, String> headerConverter) {
this.headerConverter = headerConverter;
}

public void setPrettiers(Map<String, Function<String, String>> prettiers) {
this.prettiers = prettiers;
public void setPrettifiers(Map<String, Function<String, String>> prettifiers) {
this.prettifiers = prettifiers;
}

/**
* @param prettifiers a map with the content type as a key and the prettifier function as a value
* @deprecated Use {@link #setPrettifiers(Map)} instead
*/
@Deprecated
public void setPrettiers(Map<String, Function<String, String>> prettifiers) {
setPrettifiers(prettifiers);
}

public enum PartType {
Expand All @@ -148,7 +157,7 @@ public static class Builder {
private String fileName;

private Function<Header, String> headerConverter;
private Map<String, Function<String, String>> prettiers;
private Map<String, Function<String, String>> prettifiers;

/***
*
Expand Down Expand Up @@ -198,19 +207,29 @@ public Builder headerConverter(Function<Header, String> converter) {
return this;
}

public Builder prettiers(Map<String, Function<String, String>> formatPrettiers) {
this.prettiers = formatPrettiers;
public Builder prettifiers(Map<String, Function<String, String>> formatPrettifiers) {
this.prettifiers = formatPrettifiers;
return this;
}

/**
* @param formatPrettifiers a map with the content type as a key and the prettifier function as a value
* @return the builder instance
* @deprecated Use {@link #prettifiers(Map)} instead
*/
@Deprecated
public Builder prettiers(Map<String, Function<String, String>> formatPrettifiers) {
return prettifiers(formatPrettifiers);
}

public HttpPartFormatter build() {
HttpPartFormatter formatter = new HttpPartFormatter(type, mimeType, payload);
formatter.setControlName(controlName);
formatter.setHeaders(headers);
formatter.setCharset(charset);
formatter.setFileName(fileName);
formatter.setHeaderConverter(ofNullable(headerConverter).orElse(DefaultHttpHeaderConverter.INSTANCE));
formatter.setPrettiers(ofNullable(prettiers).orElse(Constants.DEFAULT_PRETTIERS));
formatter.setPrettifiers(ofNullable(prettifiers).orElse(Constants.DEFAULT_PRETTIFIERS));
return formatter;
}
}
Expand Down
Loading
Loading