Skip to content

Instantly share code, notes, and snippets.

import org.springframework.stereotype.Component;
import javax.crypto.Cipher;
import java.io.ByteArrayInputStream;
import java.io.InputStream;
import java.security.KeyPair;
import java.util.Base64;
import static java.nio.charset.StandardCharsets.ISO_8859_1;
@Cepr0
Cepr0 / gist:86fb282a5697c5ba57623b51ade3d271
Created October 5, 2020 18:51
Confugure Logback with Logstash appender in Spring Boot application
pom.xml
-------
<!-- Used for conditional processing of Logback config -->
<!-- See: http://logback.qos.ch/manual/configuration.html#conditional-->
<dependency>
<groupId>org.codehaus.janino</groupId>
<artifactId>janino</artifactId>
<version>3.0.6</version>
</dependency>
@Cepr0
Cepr0 / ResultCaptor.java
Last active July 9, 2020 19:55
Example of Mockito ResultCaptor
@SpyBean private IdGenerator idGenerator;
@Autowired private SomeService service;
@Test
void createSubscription() {
SomeRequest request = new SomeRequest("name");
ResultCaptor<String> idCaptor = new ResultCaptor<>();
doAnswer(idCaptor).when(idGenerator).generate();
@Cepr0
Cepr0 / pom.xml
Last active August 28, 2019 13:01
Settig up pom.xml for using Elastic APM agent for Java
<!--
EAK Docker Compose environment
==============================
https://github.com/yidinghan/eak
Dockerfile example
==================
FROM openjdk:11-jre-slim
VOLUME /tmp
WORKDIR /service
@Cepr0
Cepr0 / HttpHeadersTest.java
Last active January 6, 2022 15:37
How to get all the headers from HttpServletRequest? - https://stackoverflow.com/a/49771639
package io.github.cepr0.demo;
import org.junit.Before;
import org.junit.Test;
import org.springframework.http.HttpHeaders;
import javax.servlet.http.HttpServletRequest;
import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;
@Cepr0
Cepr0 / AssertResponseBodyWithAssertJ.java
Last active August 16, 2022 16:05
A variant of how to assert the response body in Spring MVC tests with AssertJ
@Autowired private ObjectMapper objectMapper;
@Autowired private MockMvc mockMvc;
@Test
public void create_when_all_is_correct_then_201_Created() throws Exception {
ResultActions result = mockMvc.perform(post(USERS)
.contentType(APPLICATION_JSON_UTF8)
.content(toJson(new UserRequest("user")))
.accept(APPLICATION_JSON)
@Cepr0
Cepr0 / DataIntegrityViolationExceptionHandler.java
Created February 21, 2019 14:43
DataIntegrityViolationException handling
@Order(HIGHEST_PRECEDENCE)
@ExceptionHandler(DataIntegrityViolationException.class)
ResponseEntity<?> handleException(DataIntegrityViolationException ex, ServletWebRequest request) {
String message = NestedExceptionUtils.getMostSpecificCause(ex).getMessage();
String messageCode = message.replaceFirst(".*\"(.*)\"\\s(.|\\s)*", "$1"); // replace "bla-bla\"code\"\nbla-bla" to "code"
var errorMessage = ApiErrorMessage.badRequest(messageProvider.getLocalizedMessage(messageCode));
return super.handleExceptionInternal(ex, errorMessage, null, errorMessage.getHttpStatus(), request);
}
@Cepr0
Cepr0 / build-push-docker-image-to-aws.md
Last active October 26, 2021 21:03
Build and push docker image to Amazon from maven
  1. Install amazon-ecr-credential-helper
go get -u github.com/awslabs/amazon-ecr-credential-helper/ecr-login/cli/docker-credential-ecr-login 
  1. Move it to a some folder already in the execution PATH:
mv ~/go/bin/docker-credential-ecr-login ~/bin/
  1. Add credHelpers section to ~/.docker/config.json file for our Amazon ECR docker repo ID:
@Cepr0
Cepr0 / git-submodules.md
Created January 29, 2019 12:27
Working with git submodules

Add submodule to the project

  1. Create subdir for submodule in the project dir
  2. Add necessary files to submodule dir
  3. In the submodule dir type the commands:
git init 
git add . 
git commit -m "Initial commit"
  1. Back to project dir
@Cepr0
Cepr0 / SofDeleteRepo.java
Created December 2, 2016 15:22
Soft delete sample implementatioon
// From here htps://github.com/olivergierke/repositories-deepdive/issues/1
public interface SofDeleteRepo extends JpaRepository<Entity, Long> {
@Override
@Query("select e from #{#entityName} e where e.deleteFlag=false")
public List findAll();
//Check entity in cycle bin.
@Query("select e from #{#entityName} e where e.deleteFlag=true")
public List recycleBin();