Skip to content

Instantly share code, notes, and snippets.

@dehidehidehi
dehidehidehi / merge-blocked-hosts.sh
Created December 28, 2021 23:20
/etc/hosts : Block adservers, facebook products, and cryptominers urls
# Block advertisers, facebook products, and cryptominer scripts
# into a new file which can replace /etc/hosts.
# Make a dir to store the to-be downloaded files.
mkdir /etc/hosts.d/blocked
cp /etc/hosts /etc/hosts.d/blocked/previous-hosts-contents.txt
cd /etc/hosts.d/blocked
# Download block lists directly from officially maintained repositories.
curl -o adservers.txt https://raw.githubusercontent.com/anudeepND/blacklist/master/adservers.txt
@dehidehidehi
dehidehidehi / AbstractSimpleJWTValidator.java
Created July 27, 2022 07:53
Java - JWT Validator (RSA) Implementation
import io.jsonwebtoken.MalformedJwtException;
import java.time.Instant;
@AllArgsConstructor
@Getter(AccessLevel.PROTECTED)
@Slf4j
public abstract class AbstractSimpleJWTValidator implements JWTValidator {
private String jwtToken;
private String localKeyId;
@dehidehidehi
dehidehidehi / PageImplInstanciationExample.java
Last active July 28, 2022 09:21
Sample instantiation of a PageImpl instance.
/** Correct usage of a PageImpl which will return proper Pageable information. **/
private <T> PageImpl<T> asPageable(List<T> theList, Pageable page) {
return new PageImpl<>(theList, page, theList.size());
}
@dehidehidehi
dehidehidehi / .gitconfig
Last active July 27, 2022 15:51 — forked from milanpanchal/.gitconfig
Git config properties
[user]
name = K
email = DGEM@GM.CM
[alias]
cp = cherry-pick
co = checkout
s = status
sb = status -sb
@dehidehidehi
dehidehidehi / .gitconfig
Created July 27, 2022 15:54
Pretty GitLog One Line
[alias]
l = log --oneline --abbrev-commit --reverse --date=short --pretty="%C(Yellow)%h %C(reset)%ad (%C(Green)%cr%C(reset))%x09 %C(Cyan)%an: %C(reset)%s"'
@dehidehidehi
dehidehidehi / uriBuilder.java
Created July 28, 2022 08:44
Builds a basic url: path params and query params from Maps.
static URI buildUri(String baseUrl, LinkedHashMap<String, String> pathParams, LinkedHashMap<String, String> queryParams) {
StringBuilder sb = new StringBuilder(baseUrl);
if (Objects.nonNull(pathParams))
sb.append("/").append(String.join("/", pathParams.values()));
if (Objects.nonNull(queryParams)) {
List<String> keyValueStrings = queryParams.entrySet().stream().map(
e -> e.getKey() + "=" + e.getValue()).collect(Collectors.toUnmodifiableList());
sb.append("?").append(String.join("&", keyValueStrings));
}
return URI.create(sb.toString());
@dehidehidehi
dehidehidehi / JWTDecoder.java
Created July 28, 2022 08:47
Decodes JWT from Base64 to a StringArray
static byte[] decodeBase64(String encoded) {
return Base64.getDecoder().decode(encoded);
}
static String[] decodeJwt(String encodedIdToken) {
String[] splitIdToken = encodedIdToken.split("\\.");
String header = new String(decodeBase64(splitIdToken[0]));
String payload = new String(decodeBase64(splitIdToken[1]));
String signature = splitIdToken[2]; // decode this for later, kept on having decode errors
return {header, payload, signature};
@dehidehidehi
dehidehidehi / GenerateCommonLangRandomString.java
Created July 28, 2022 08:49
Generates random string non case-sensitive
/** Generates random string non-case-sensitive alphanumeric, and special chars **/
public static String generateCommonLangRandomString() {
String upperCaseLetters = RandomStringUtils.random(4, 65, 90, true, true);
String lowerCaseLetters = RandomStringUtils.random(4, 97, 122, true, true);
String numbers = RandomStringUtils.randomNumeric(4);
String specialChar = RandomStringUtils.random(2, 33, 47, false, false);
String totalChars = RandomStringUtils.randomAlphanumeric(2);
String combinedChars = upperCaseLetters.concat(lowerCaseLetters).concat(numbers)
.concat(specialChar)
.concat(totalChars);
@dehidehidehi
dehidehidehi / RetrofitHttpClientProvider.java
Last active July 28, 2022 09:12
Boilerplate code to generate a general usage Retrofit2 http model client
import lombok.Getter;
import lombok.Setter;
import lombok.extern.slf4j.Slf4j;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.jackson.JacksonConverterFactory;
import java.net.Authenticator;
import java.net.InetSocketAddress;
import java.net.PasswordAuthentication;
@dehidehidehi
dehidehidehi / EntityMapper.java
Created July 28, 2022 09:17
Contract for a generic dto to entity mapper from JHipster.
import java.util.List;
import org.mapstruct.BeanMapping;
import org.mapstruct.MappingTarget;
import org.mapstruct.Named;
import org.mapstruct.NullValuePropertyMappingStrategy;
/**
* Contract for a generic dto to entity mapper.
*
* @param <D> - DTO type parameter.