Skip to content

Instantly share code, notes, and snippets.

BJ Peter DeLaCruz bjpeterdelacruz

Block or report user

Report or block bjpeterdelacruz

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
bjpeterdelacruz /
Created Jun 19, 2019
How to log a method in Java
public void doSomething(String recordId) {
log.debug("Deficiency list for recordId: {} - START", recordId);
Instant start =;
try {
// do something
} finally {
Instant end =;
Duration duration = Duration.between(start, end);
log.debug("Deficiency list for recordId: {} - END, millis: {}", recordId, duration.toMillis());
bjpeterdelacruz / amp.js
Created Dec 14, 2018
How to remove ampersands and other encoded characters using JavaScript
View amp.js
var input = "A%20&%20B%20Company";
var output = $('<textarea/>').html(unescape(input).trim()).text();
// output = "A & B Company"
bjpeterdelacruz /
Created Dec 14, 2018
A method for stripping out HTML code that could cause XSS
package gov.ehawaii.swhv.utils;
import java.util.ArrayList;
import java.util.List;
import java.util.regex.Pattern;
import lombok.NonNull;
public final class SecurityUtils {
private final static List<String> MALICIOUS_STRING_LIST = new ArrayList<>();
bjpeterdelacruz / post_office_regex.js
Last active Oct 27, 2018
Regular Express for P.O. Box Addresses
View post_office_regex.js
/* Credit: */
var pos = $(this).val().search(/(?:P(?:ost(?:al)?)?[\W\s]*(?:(?:O(?:ffice)?[\W\s]*)?B(?:ox|in|\b|\d)|o(?:ffice|\b)(?:[\W\s]*\d)|code)|b(?:o*x|i*n)[\W\s\b]*\d)|(?:P+[\W\s]*O+[\W\s]*)/i);
if (pos >= 0) { // P.O. box address was entered, so disable Submit button and display warning message
$("#submit_primary").attr("disabled", true);
if ($("#element_2_warning").length == 0) {
var text = "<div class='alert alert-warning' style='font-size: 12pt' id='element_2_warning'><strong style='color: #FF0000'>";
text = text + "Street Address only, no P.O. Box.</strong></div>";
bjpeterdelacruz / logout.js
Last active May 23, 2018
How to warn user that their session will timeout soon and how to log them out once it expires
View logout.js
var logoutUrl = "";
var refreshUrl = "";
function displayTimeOutWarningMessage() {
var thirtyMinutes = 1000 * 60 * 30;
var fifteenMinutes = thirtyMinutes / 2;
var lastActivity = new Date().getTime();
var checkTimeOut = function() {
var currentTime = new Date().getTime();
if (currentTime >= lastActivity + fifteenMinutes) {
bjpeterdelacruz / date_range.sql
Created May 8, 2018
Find data for one day only in MySQL
View date_range.sql
-- Find data for May 8, 2018 only.
SELECT * FROM inbox WHERE datetime >= date('2018-05-08') and datetime < date('2018-05-08');
-- Correct
SELECT * FROM inbox WHERE datetime >= date('2018-05-08') and datetime < date('2018-05-08') + interval 1 day;
bjpeterdelacruz / drop_table.sql
Created Apr 26, 2018
How to generate DROP TABLE statements for tables that begin with a certain prefix
View drop_table.sql
-- Generate DROP TABLE statements for tables that begin with "ap_form_"
-- Sample output:
-- DROP TABLE `ap_form_10792`;
-- DROP TABLE `ap_form_10792_review`;
-- DROP TABLE `ap_form_11763`;
bjpeterdelacruz /
Last active Apr 25, 2018
How to sanitize a URL to prevent open redirection
package com.bjpeter.sampleapp.utils;
import org.apache.commons.lang3.StringUtils;
public WebUtils() {
private WebUtils() {
bjpeterdelacruz /
Last active Apr 25, 2018
How to mock static methods using PowerMockito
package com.bjpeter.sampleapp.utils;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mockito;
import org.powermock.api.mockito.PowerMockito;
import org.powermock.core.classloader.annotations.PrepareForTest;
bjpeterdelacruz /
Last active Apr 11, 2018
What happens when a singleton object is pointing to a new reference?
public class MyObject {
public static MyObject myObject = null; // Singleton object
private MyObject() {
// Only one instance of MyObject will ever exist, and this method must be called to grab it.
public synchronized static MyObject getInstance() {
if (myObject == null) {
You can’t perform that action at this time.