Skip to content

Instantly share code, notes, and snippets.

@ufuk
ufuk / HtmlToTextUtils.java
Created Oct 6, 2021
Utility method to convert HTML text to plain text while preserving newlines (using jsoup as main dependency)
View HtmlToTextUtils.java
import org.apache.commons.lang3.StringUtils;
import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.nodes.Node;
import org.jsoup.nodes.TextNode;
import java.util.List;
import java.util.stream.Collectors;
@ufuk
ufuk / XmlUtils.java
Last active Apr 6, 2021
Unmarshall and consume XML input stream by target element name (and -optional- target element depth)
View XmlUtils.java
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Unmarshaller;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.InputStream;
import java.util.Objects;
import java.util.function.Consumer;
public final class XmlUtils {
View jaxb-on-java-11-dependencies.xml
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jaxb.version>2.3.3</jaxb.version>
...
</properties>
View soap-client-on-java-11-dependencies.xml
<properties>
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<jakarta-activation-api.version>2.0.0</jakarta-activation-api.version>
<jakarta.xml.soap-api.version>2.0.0</jakarta.xml.soap-api.version>
...
</properties>
@ufuk
ufuk / TextUtils.java
Last active Apr 8, 2021
Curated text utils for specific use cases
View TextUtils.java
import org.apache.commons.lang3.StringUtils;
import java.text.Normalizer;
import java.util.Arrays;
import java.util.List;
import java.util.Locale;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
import java.util.stream.Collectors;
View rabbitmq-management-ui-fetched-print-payloads.js
// Print line by line
document.querySelectorAll('pre.msg-payload').forEach(it => console.log(it.innerText));
// Pretty-printed JSON array (If payloads are JSON)
JSON.stringify(Array.from(document.querySelectorAll('pre.msg-payload')).map(it => JSON.parse(it.innerText)), null, 2);
@ufuk
ufuk / logback-spring.xml
Last active Jan 5, 2021
Spring Boot & AWS Elastic Beanstalk & AWS Cloudwatch Log Stream friendly logging with newline replacing support to convert root cause's stack trace into single line message. And does these within Spring Boot's default pattern layout.
View logback-spring.xml
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
<include resource="org/springframework/boot/logging/logback/console-appender.xml"/>
<appender name="CLOUD_WATCH_FRIENDLY" class="ch.qos.logback.core.ConsoleAppender">
<encoder>
<pattern>%clr(%d{${LOG_DATEFORMAT_PATTERN:-yyyy-MM-dd HH:mm:ss.SSS}}){faint} %clr(${LOG_LEVEL_PATTERN:-%5p}) %clr(${PID:- }){magenta} %clr(---){faint} %clr([%15.15t]){faint} %clr(%-40.40logger{39}){cyan} %clr(:){faint} %m %replace(%rEx{short}){'[\r\n\t]+', '\\n'}%nopex%n</pattern>
</encoder>
@ufuk
ufuk / ExampleConstraint.java
Last active Nov 13, 2020
How to initialize an instance of the constraint annotation while writing unit tests for a javax.validation.ConstraintValidator
View ExampleConstraint.java
import javax.validation.Constraint;
import javax.validation.Payload;
import java.lang.annotation.*;
@Documented
@Constraint(validatedBy = ExampleConstraintValidator.class)
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface ExampleConstraint {
@ufuk
ufuk / SpringDataPageUtils.java
Last active Feb 8, 2021
SpringDataPageUtils to convert paged entities to paged model objects
View SpringDataPageUtils.java
import org.springframework.data.domain.Page;
import org.springframework.data.support.PageableExecutionUtils;
import java.util.function.Function;
import java.util.stream.Collectors;
public final class SpringDataPageUtils {
public static <T, C> Page<C> convertPage(Page<T> page, Function<T, C> converter) {
return PageableExecutionUtils.getPage(
@ufuk
ufuk / HttpServletRequestResponsePrinter.java
Created Sep 5, 2019
Utility to generate logging-purpose text for HttpServletRequest/Response
View HttpServletRequestResponsePrinter.java
package ...;
import ...JsonUtils;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.util.ContentCachingRequestWrapper;
import org.springframework.web.util.WebUtils;