Skip to content

Instantly share code, notes, and snippets.

View loinguyenduc101's full-sized avatar

Loi Nguyen loinguyenduc101

View GitHub Profile
@loinguyenduc101
loinguyenduc101 / SimpleCORSFilter.java
Created October 13, 2015 07:28
Spring simple CORS Filter
@Component
public class SimpleCORSFilter implements Filter {
public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain) throws IOException, ServletException {
HttpServletResponse response = (HttpServletResponse) res;
response.setHeader("Access-Control-Allow-Origin", "*");
response.setHeader("Access-Control-Allow-Methods", "POST, GET, OPTIONS, DELETE");
response.setHeader("Access-Control-Max-Age", "3600");
response.setHeader("Access-Control-Allow-Headers", "Authorization");
chain.doFilter(req, res);
/**
*
* @author loind
*
*/
@Provider
public class JerseyDefaultExceptionMapper implements ExceptionMapper<Throwable> {
private static final Logger logger = LoggerFactory.getLogger(DefaultExceptionMapper.class);
/**
* Created by loind on 11/25/2015.
* tested with application/* . not test with multi-part
*/
public class JerseyRawBodyRequestFilter implements ContainerRequestFilter {
private static final Logger logger = LoggerFactory.getLogger(RawBodyRequestFilter.class);
//GEOIP
// http://maxmind.github.io/GeoIP2-java/
//https://github.com/maxmind/GeoIP2-java
//http://dev.maxmind.com/geoip/geoip2/geoip2-csv-databases/
File database = new File("C:/Users/loind/Desktop/GeoIP/GeoLite2-City.mmdb");
// This creates the DatabaseReader object, which should be reused across
// lookups.
DatabaseReader reader = new DatabaseReader.Builder(database).fileMode(Reader.FileMode.MEMORY_MAPPED).build();
InetAddress ipAddress = InetAddress.getByName("14.162.178.45");
@loinguyenduc101
loinguyenduc101 / ClientIpFilter.java
Created December 9, 2015 16:21
Real client ip - jersey request filter
package com.monitoringvn.utils.rest.filter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import javax.servlet.http.HttpServletRequest;
import javax.ws.rs.container.ContainerRequestContext;
import javax.ws.rs.container.ContainerRequestFilter;
import javax.ws.rs.container.PreMatching;
import javax.ws.rs.core.Context;
## Generated by install_server.sh ##
# Redis configuration file example.
#
# Note that in order to read the configuration file, Redis must be
# started with the file path as first argument:
#
# ./redis-server /path/to/redis.conf
# Note on units: when memory size is needed, it is possible to specify
# it in the usual form of 1k 5GB 4M and so forth:
public void captureScreen(String fileName) throws Exception {
Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
Rectangle screenRectangle = new Rectangle(screenSize);
Robot robot = new Robot();
BufferedImage image = robot.createScreenCapture(screenRectangle);
ImageIO.write(image, "png", new File(fileName));
}
@loinguyenduc101
loinguyenduc101 / SQLServerUnicodeDialect.java
Created January 29, 2016 08:55
SQLServerUnicodeDialect 2008 tested
// http://stackoverflow.com/questions/30627583/hibernate-with-sql-server-fail-for-nvarchar-field-with-no-dialect-mapping
public class SQLServerUnicodeDialect extends org.hibernate.dialect.SQLServerDialect {
public SQLServerUnicodeDialect() {
super();
registerColumnType(Types.CHAR, "nchar(1)");
registerColumnType(Types.LONGVARCHAR, "nvarchar(max)" );
registerColumnType(Types.VARCHAR, 4000, "nvarchar($l)");
registerColumnType(Types.VARCHAR, "nvarchar(max)");
registerColumnType(Types.CLOB, "nvarchar(max)" );
@loinguyenduc101
loinguyenduc101 / latin1_to_utf8.sql
Created March 26, 2016 08:22
How to convert latin1_swedish_ci data into utf8_general_ci?
SELECT convert(cast(convert(LATIN1_FIELD using latin1) as binary) using utf8)
FROM LATIN1_TABLE;
-- http://stackoverflow.com/questions/12756877/how-to-convert-latin1-swedish-ci-data-into-utf8-general-ci
@loinguyenduc101
loinguyenduc101 / 30 Regex Code Snippets All Web Developers Should Know.txt
Created March 30, 2016 05:41
30 Regex Code Snippets All Web Developers Should Know
##http://www.hongkiat.com/blog/regex-web-developers/
Regular expressions are a powerful tool that should be in every developer’s tool belt. They can match against a string of characters based on very complex parameters, which can save you a lot of time when building dynamic websites.
Web developers face different tasks than software developers but many of the same code fundamentals remain. Regular expressions (or regex) do have a steep initial learning curve, but they can be tremendously powerful when used correctly.
The trickiest part is learning the syntax and learning how to write your own regex code from scratch. To save time I’ve organized 30 different regex code snippets that you can incorporate into development projects. And since regex isn’t limited to a single language, you can apply these snippets to anything from JavaScript to PHP or Python.
1. Password Strength
1