Skip to content

Instantly share code, notes, and snippets.

Avatar

BJ Dela Cruz bjpeterdelacruz

View GitHub Profile
@bjpeterdelacruz
bjpeterdelacruz / BillValidator.java
Last active August 27, 2021 18:46
Validator that contains an HttpServletRequest object used to get the session
View BillValidator.java
import java.math.BigDecimal;
import javax.servlet.http.HttpServletRequest;
import org.springframework.validation.Errors;
import org.springframework.validation.Validator;
public class BillValidator implements Validator {
private HttpServletRequest request;
public void setRequest(HttpServletRequest request) {
@bjpeterdelacruz
bjpeterdelacruz / HowToLogMethod.java
Created June 19, 2019 21:08
How to log a method in Java
View HowToLogMethod.java
public void doSomething(String recordId) {
log.debug("Deficiency list for recordId: {} - START", recordId);
Instant start = Instant.now();
try {
// do something
} finally {
Instant end = Instant.now();
Duration duration = Duration.between(start, end);
log.debug("Deficiency list for recordId: {} - END, millis: {}", recordId, duration.toMillis());
@bjpeterdelacruz
bjpeterdelacruz / amp.js
Created December 14, 2018 23:44
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
bjpeterdelacruz / SecurityUtils.java
Created December 14, 2018 23:01
A method for stripping out HTML code that could cause XSS
View SecurityUtils.java
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
bjpeterdelacruz / insert_php5_vs_php7.php
Last active November 13, 2018 06:26
Inserting a document into MongoDB database (PHP 5 vs. PHP 7)
View insert_php5_vs_php7.php
// PHP 5
$m = new MongoClient();
$db = $m->test;
$collection = $db->todos;
$collection->insert(array("name" => $name));
// PHP 7
$m = new MongoDB\Driver\Manager();
$bulk = new MongoDB\Driver\BulkWrite;
$bulk->insert(array("name" => $name));
@bjpeterdelacruz
bjpeterdelacruz / post_office_regex.js
Last active October 27, 2018 02:54
Regular Express for P.O. Box Addresses
View post_office_regex.js
/* Credit: https://github.com/EpicVoyage/pobox-regex */
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>";
$(text).insertBefore($("#li_2"));
}
@bjpeterdelacruz
bjpeterdelacruz / logout.js
Last active May 23, 2018 07:17
How to warn user that their session will timeout soon and how to log them out once it expires
View logout.js
var logoutUrl = "https://test-swhv.ehawaii.gov/swhv-wikiflow/logout.php";
var refreshUrl = "https://test-swhv.ehawaii.gov/swhv-wikiflow/refresh.html";
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
bjpeterdelacruz / date_range.sql
Created May 8, 2018 20:46
Find data for one day only in MySQL
View date_range.sql
-- Find data for May 8, 2018 only.
-- WRONG
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
bjpeterdelacruz / drop_table.sql
Created April 26, 2018 00:02
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_"
SELECT CONCAT('DROP TABLE `', TABLE_NAME, '`;') AS queries
FROM INFORMATION_SCHEMA.TABLES
WHERE TABLE_NAME LIKE 'ap_form_%'
-- Sample output:
-- DROP TABLE `ap_form_10792`;
-- DROP TABLE `ap_form_10792_review`;
-- DROP TABLE `ap_form_11763`;
@bjpeterdelacruz
bjpeterdelacruz / WebUtilsTest.java
Last active April 25, 2018 23:33
How to mock static methods using PowerMockito
View WebUtilsTest.java
package com.bjpeter.sampleapp.utils;
import com.bjpeter.sampleapp.services.ServiceLoader;
import com.bjpeter.sampleapp.services.TextService;
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;