Skip to content

Instantly share code, notes, and snippets.

@pavelfomin
pavelfomin / liferay-web-content-request-parameters.ftl
Last active January 22, 2022 14:35
Get the http request parameters in Liferay from the freemarker web content template
<#--
The request here is not the original request and doesn't contain the url query parameters
hence the usage of the serviceContext from ServiceContextThreadLocal.
-->
<#assign serviceContext = staticUtil["com.liferay.portal.service.ServiceContextThreadLocal"].getServiceContext()>
<#assign httpServletRequest = serviceContext.getRequest()>
myparam="${(httpServletRequest.getParameter('myparam')?html)!''}"
@pavelfomin
pavelfomin / DateFormatterTest.java
Last active January 8, 2020 21:38
JUnit 5 Parameterized Test example
import org.junit.jupiter.params.ParameterizedTest;
import org.junit.jupiter.params.aggregator.ArgumentsAccessor;
import org.junit.jupiter.params.converter.ConvertWith;
import org.junit.jupiter.params.provider.CsvSource;
import java.time.LocalDate;
import java.time.format.DateTimeFormatter;
import static org.junit.jupiter.api.Assertions.assertEquals;
@pavelfomin
pavelfomin / DateCellEditor.java
Last active January 27, 2020 21:33
Java Swing JTable date cell editor based on JSpinner and JFormattedTextField
import javax.swing.*;
import javax.swing.table.TableCellEditor;
import java.awt.*;
import java.util.Calendar;
import java.util.Date;
/**
* DateCellEditor that is based on {@link JSpinner} and {@link JFormattedTextField}.
*/
public class DateCellEditor extends AbstractCellEditor implements TableCellEditor {
@pavelfomin
pavelfomin / using-multiple-github-accounts-with-ssh-keys.md
Created August 3, 2023 18:27
Using multiple github accounts with ssh keys

From https://gist.github.com/oanhnn/80a89405ab9023894df7

Problem

I have two Github accounts: defaul for work and personal. I want to use both accounts on same computer via git ssh (without typing password everytime, when doing git push or pull).

Solution

Use ssh keys and define host aliases in ssh config file (an alias per account).

How to?

@pavelfomin
pavelfomin / ClassUtil.java
Last active August 30, 2023 21:30
Get jar locations of all classes extending a given class
import org.reflections.Reflections;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import java.net.URL;
import java.util.Set;
public class ClassUtil {
public <T> MultiValueMap<String, Class<? extends T>> getSubTypeLocationsOf(final String prefix, final Class<T> type) {
@pavelfomin
pavelfomin / Confluent-Schema-Registry-Metadata.md
Last active September 7, 2023 15:10
Confluent Schema Registry Metadata

Confluent Schema Registry Metadata

Prior to v7.4, schema registry did not support any custom metadata associated with a schema uploaded to the schema registry.

So the following mapping between

  • the version of a jar artifact containing a schema content
  • and the id and version of the schema in the schema registry can only be derived by comparing the content of the schemas in different schema registry environments (using POST /subjects/(string: subject)).
@pavelfomin
pavelfomin / cors-and-xss.md
Last active February 6, 2024 19:34
CORS and XSS

I did some reading on CORS and I think I understand how they can restrict which origins the requests are coming from. However, allowing the cross origin calls from the browser increases a possibility of XSS:

a person with malicious intent injects some JavaScript into a page to steal users' cookies and send them to a URL he controls, all he has to do is add the following header Access-Control-Allow-Origin: * on the server side to make the request work. https://security.stackexchange.com/questions/108835/how-does-cors-prevent-xss

The scenario that CORS is preventing is different:

For example, the victim logged into their bank's application. Then they were tricked into loading an external website on a new browser tab. The external website then used the victim's cookie credentials and relayed data to the bank application

@pavelfomin
pavelfomin / kafka-metrics.md
Last active December 4, 2023 15:01
Useful Kafka Metrics

Kafka metrics:

  • kafka.consumer.fetch.manager.records.consumed.rate
  • kafka.consumer.fetch.manager.records.lag

Spring Kafka Listener metrics

  • spring.kafka.listener.count
  • spring.kafka.listener.max

Metrics queries:

  • sum(align(1m, ts(kafka.consumer.fetch.manager.records.consumed.rate, space=${env} and service=${service} and dc=${dc})), service, dc)
@pavelfomin
pavelfomin / metrics.md
Last active January 16, 2024 23:12
How to use @timed annotation
@pavelfomin
pavelfomin / AvroDataConsumer.java
Last active December 8, 2023 15:50
Spring Kafka configuration for consuming JSON and Avro formats using @KafkaListener.
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.apache.kafka.clients.consumer.ConsumerRecord;
import org.springframework.kafka.annotation.KafkaListener;
import org.springframework.messaging.MessageHeaders;
@RequiredArgsConstructor
@Slf4j
public class AvroDataConsumer {