Skip to content

Instantly share code, notes, and snippets.

View rponte's full-sized avatar
🏠
Working from home

Rafael Ponte rponte

🏠
Working from home
View GitHub Profile
@rponte
rponte / build.gradle
Last active February 20, 2024 06:44
Configuring Gradle compiler encoding
apply plugin: 'java'
apply plugin: 'eclipse'
sourceCompatibility = JavaVersion.VERSION_1_8
targetCompatibility = JavaVersion.VERSION_1_8
eclipseJdt << {
ant.propertyfile(file: ".settings/org.eclipse.core.resources.prefs") {
ant.entry(key: "eclipse.preferences.version", value: "1")
ant.entry(key: "encoding/<project>", value: "utf-8")
@rponte
rponte / ResourceServerConfig.java
Last active February 7, 2024 02:39
Spring Security: example of OAuth2 Resource Server configuration (Spring Boot v2.6.7)
package br.com.zup.edu.minhasfigurinhas;
import org.springframework.context.annotation.Configuration;
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
import static org.springframework.http.HttpMethod.GET;
import static org.springframework.http.HttpMethod.POST;
import static org.springframework.security.config.http.SessionCreationPolicy.STATELESS;
@rponte
rponte / AccountRepository.java
Last active February 7, 2024 02:36
JPA and Hibernate: Simple and Smart way of using PostgreSQL Advisory Locks with JPQL to prevent Lost Update anomaly
package br.com.stackspot.nullbank.withdrawal;
import org.hibernate.LockOptions;
import org.springframework.data.jpa.repository.*;
import org.springframework.stereotype.Repository;
import javax.persistence.LockModeType;
import javax.persistence.QueryHint;
import javax.transaction.Transactional;
import java.util.Optional;
@rponte
rponte / MockEnvironmentRule.java
Last active February 7, 2024 02:25
Mocking Spring Boot Environment during integration tests
package br.com.rponte.base.spring.config.env;
import org.junit.rules.ExternalResource;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.env.ConfigurableEnvironment;
import org.springframework.mock.env.MockPropertySource;
import org.springframework.stereotype.Component;
/**
* https://docs.spring.io/spring-framework/docs/current/javadoc-api/org/springframework/core/env/ConfigurableEnvironment.html
@rponte
rponte / SignedXml.java
Last active February 2, 2024 11:35
How to sign a XML with private key certificate in Java (using Java Key Store)
package br.com.mdias.rponte.signature;
import java.util.Base64;
/**
* Represents a signed XML
*/
public class SignedXml {
private String content;
@rponte
rponte / keep_messages_after_redirect.java
Created October 12, 2011 00:45
Keeping faces message after redirect (JSF)
facesContext.getExternalContext().getFlash().setKeepMessages(true);
// more informations
// http://stackoverflow.com/questions/5137601/preserving-facesmessage-after-redirect-for-presentation-through-hmessage-in-js
@rponte
rponte / ORA-12519-error.sql
Created September 5, 2010 22:00
Increasing Processes, Sessions and Transactions in Oracle XE
-- Tendo problemas com "ORA-12519, TNS:no appropriate service handler found"?
-- dica: http://en.newinstance.it/2007/06/01/ora-12519-tnsno-appropriate-service-handler-found/
-- Basta rodar este comando e reiniciar o listener (OracleXETNSListener):
ALTER SYSTEM SET PROCESSES=150 SCOPE=SPFILE;
@rponte
rponte / BookRepositoryTest.java
Last active January 17, 2024 19:57
Spring Boot: example of base test class for testing Repositories
package br.com.zup.edu.ifoodwebapp.samples.books;
import base.SpringDataJpaIntegrationTest;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataIntegrityViolationException;
import org.springframework.transaction.TransactionSystemException;
@rponte
rponte / CreateNewUserController.java
Last active January 9, 2024 14:37
Designing fault-tolerant and idempotent APIs with HTTP requests mapped to database's transactions at 1:1 model
/**
* This Spring Boot controller was implemented as an example of a simple but robust idempotent REST API that
* leverages the ACID properties of a relational database.
*/
@RestController
public class CreateNewUserController {
@Autowired
private UserRepository repository;
@Autowired