Total Pageviews

2018/03/01

[閱讀筆記] Seriously... I'm Kidding


  1. 你要了解你自己,並且接受這樣的你。
  2. 每個人都有缺點,因為只要是人類都會有缺點。如果我們能接受缺點是我們的一部份,這些缺點就不會是個問題。當你學會接受自己,別人就會學著接受你
  3. 年紀是我們的一部份,也是一個不可改變的事實。當你 7 歲半時,若人家說你7 歲,你還會生氣的說我已經 7 歲半。但是當你 42 歲時,還會向別人強調你是 42 歲半嗎? 只要是人就會天天變老,但是在變老的過程中,記得從每一天的錯誤中學習,讓自己更加睿智。
  4. 如何讓自己在伸展台上變成 supermodel
    1. The Look: 總是讓自己看起來很生氣的樣子
    2. The Walk: 有侵略性的快步走,彷彿一匹試圖避開水坑的馬
    3. The Squint: 瞇著眼看人,彷如剛起床,看到陽光般的眼睛
    4. The Pout: 噘起你的嘴唇,猶如吸著吸管般
    5. The Pose: 動作要神秘,總是將一隻手放在口袋裡,讓人永遠不知道口袋裡的那隻手有可能是隻 hook hand
    6. The Breeze: 總是帶著一個巨大的電扇
  5. 當你住得越久,買的東西會越來越多,家裡的東西就會越來越多,你可能衣櫥塞滿了過季、穿不到的衣服,抽屜塞滿了再也用不到的雜物。記住,保持整潔乾淨的環境很重要,我不喜歡凌亂的家裡,我堅信每個東西都有一個歸屬的地方。將不需要、用不到的東西丟掉、送人或放到拍賣網站上面賣掉它吧
  6. 世界上最重要的事情就是幸福 (happiness),絕對不是金錢,當你找到幸福的時候,你就會發現 the secret of life。不要一直看著背後,也不要花太多時間憂心未來,好好地享受當下。在每天走過的地方停下來聞聞花香,起床後聞聞咖啡的味道,享受成功當下甜美的味道,當你聞越多,你就會感覺得越快樂與幸福
  7. 我喜歡獨特,我喜歡有自己的風格、自己的意見。能夠從廣大的群眾中凸顯出自己的與眾不同會是一件很棒的一件事情,若一眼望去發現一堆跟自己很像的人,這世界將會變得很乏味無趣。常有人說哪個比較好或比較差?甚至是什麼比較正常或一般?記住,我們都是不同的人,也允許別人與你不同
  8. 人生的道路猶如在道路上開車,道路有時筆直、有時彎曲,有時你會加速、有時會減速,有時候你會短暫的停車加油或維修,加油、維修結束後,勇敢、努力地繼續往前走
  9. 人們總是很喜歡幫別人貼標籤,貼標籤導致刻板印象 (stereotypes),刻板印象導致以偏概全 (generalizations),以偏概全導致臆測 (assumptions),臆測又回到刻板印象,這是一個惡性循環 (vicious cycle)。若你認定刻板印象是精確的話,那恐怕是非常危險的一件事,不是所有的紐約客都是粗魯的,也不是加州人都是嬉皮。不是所有有錢人都是勢利眼的,也不是所有名人都是以自我為中心的。在你做出貼標籤、刻板印象、以偏概全、臆測前,不如走過去跟他們聊聊天、問問題
  10. 你永遠不會太晚學習新事物,你只會太晚穿低腰牛仔褲 (low-rise jeans)
  11. 有些人很擅長做決策,因為他們知道他們要什麼,所以可以很快地做出決策,我稱這些人為 Quick Decision Makers;有些人則是非常拙於做決策,常常猶豫不決,花了好幾天、或好幾周甚至好幾年還決定不了,我稱這些人為 Annoying。你會在星巴克發現一個有趣的事實:Quick Decision Makers 常常在星巴克排隊隊伍中被 Annoying 卡住
  12. 當在做重大的決策時,我會把決策的正面與反面的清單列出來,如此一來我可以看到所有正面與負面的清單,如此一來可以決定哪個方案對我是最好的
  13. 我覺得世界上有兩種人,deep thinkers 與 not so deep thinkers。deep thinkers 會問很多問題,對於他們的行動是有意識的,對於每天所做、所見、所聞,都會尋找理由與合理的解釋;not so deep thinkers 就像人們去丟垃圾一樣毫無所感。記得要當個 deep thinkers!
  14. 讓自己保持正面,會讓自己的生活很不一樣。這會讓你周遭都是正面的人,沒有人喜歡負面、陰沉的人。當你保持正面與快樂,你的體內會充滿 endorphins (腦內嗎啡) ,endorphins 可以提升你的免疫系統、健康,讓你感到更幸福
  15. 人生就是在維持平衡,人生總是有好有壞,有高有低,充滿著酸甜苦辣。幸福緣自於內心,想辦法避免負面、不好的想法入侵你的心理,將其取代為愉快、正面的想法吧
  16. 幸福是一段旅程,而非終點 (Happiness is a journey, not a destination)
  17. 這個以消費為主的社會,不斷鼓勵人們消費,超出實際所需,甚至到了不健康的地步。如果你覺得儲蓄很難,那你的問題就很大。最簡單的致富方式就是盡可能少花錢
  18. 你要有自我的主見,不要憂心別人怎麼說你,怎麼看待你,就讓那些唱反調的人去說吧


2018/02/28

2018/02/27

[Travel] 2018/02 雲林

雲林布袋戲館.
DSC03010

DSC03019

DSC03022

DSC03032

DSC03028


石龜車站
DSC03070

DSC03058

DSC03056

DSC03053

2018/02/14

[Fortify] Fix Cross-Site Scripting: Persistent

Problem


Before
Code snippet in JSP file:
1
2
    String content = report.getContent();
    out.print(content);


After

Add dependency in pom.xml
1
2
3
4
5
    <dependency>
        <groupId>org.owasp.encoder</groupId>
        <artifactId>encoder</artifactId>
        <version>1.2.1</version>
    </dependency>

Updated code snippet in JSP file:
1
2
3
4
5
    <%@ page import="org.owasp.encoder.Encode"%>


    String content = report.getContent(acct_no,schema+"://"+server+":"+port);
    out.print(Encode.forHtml(content));    


Reference
[1] https://github.com/OWASP/owasp-java-encoder/wiki/2)-Use-the-OWASP-Java-Encoder

2018/02/13

[HP Laptop] Function Keys 問題

Problem
剛拿到 HP Laptop 時,發現當我要按 F1 ~ F12 按鈕,都需要先按下 fn key 才能使用,若否則會啟動音量調整、螢幕亮度調整的功能。該如何設定,讓與系統預設的設定對調?


How-To
須進入BIOS Setup 畫面進行設定,處理流程如下



Screenshot for Step 2


Screenshot for Step 3 and Step 4


Screenshot for Step 5


2018/02/12

[Jackson] How to convert JSON string to YAML?

Problem
Assume I have a JSON string:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
{
 "id": 1,
 "name": "Albert",
 "email": "albert@gmail.com",
 "phone": "0900123456",
 "address": {
  "streetAddress": "信義區信義路五段7號",
  "city": "台北市",
  "zipCode": "110"
 }
}


I would like to convert JSON string to YAML:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
 ---
id: 1
name: "Albert"
email: "albert@gmail.com"
phone: "0900123456"
address:
  streetAddress: "信義區信義路五段7號"
  city: "台北市"
  zipCode: "110"
 



How to do it?

How-To
Take advantage of libraries can fulfill this requirement.

Add the following dependencies in pom.xml
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-core</artifactId>
  <version>2.9.3</version>
 </dependency>

 <dependency>
  <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-databind</artifactId>
  <version>2.9.3</version>
 </dependency>

 <dependency>
        <groupId>com.fasterxml.jackson.core</groupId>
  <artifactId>jackson-annotations</artifactId>
  <version>2.9.3</version>
 </dependency>

 <dependency>
  <groupId>com.fasterxml.jackson.dataformat</groupId>
  <artifactId>jackson-dataformat-yaml</artifactId>
  <version>2.9.3</version>
 </dependency>

Here has sample code:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
  package test.albert.jackson;
  
  import java.io.IOException;
  
  import com.fasterxml.jackson.databind.JsonNode;
  import com.fasterxml.jackson.databind.ObjectMapper;
  import com.fasterxml.jackson.dataformat.yaml.YAMLMapper;
  
  import lombok.extern.slf4j.Slf4j;
  
  @Slf4j
  public class JacksonTest {
  
      public static void main(String[] args) throws IOException {
          Address address = Address.builder().zipCode("110").city("台北市").streetAddress("信義區信義路五段7號").build();
          Employee albert = Employee.builder().id(1).name("Albert").email("albert@gmail.com").phone("0900123456")
                  .address(address).build();
  
          
          ObjectMapper mapper = new ObjectMapper();
          
          // convert object to JSON
          String json = mapper.writeValueAsString(albert);
          log.debug(json);
          
          // convert JSON to YAML
          JsonNode jsonNode = mapper.readTree(json);
          String yaml = new YAMLMapper().writeValueAsString(jsonNode);
          log.debug("yaml = \n " + yaml);
      }
  
  }



2018/02/11

[Liquibase] How to fix checksum validation error

Problem
I am editing changelog-master.yaml, I got error message as I start up Spring boot:
org.springframework.beans.factory.BeanCreationException: Error creating bean with name 'liquibase' defined in class path resource [org/springframework/boot/autoconfigure/liquibase/LiquibaseAutoConfiguration$LiquibaseConfiguration.class]: 
Invocation of init method failed; nested exception is liquibase.exception.ValidationFailedException: Validation Failed:
     1 change sets check sum
          classpath:/db/changelog/db.changelog-master.yaml::1::albert was: 7:6e574692a29b35c6788f06c99fe2eecc but is now: 7:c5994a0c5ad9c2ba211a4f8e3ced2ec9


How-To
Here has the process to solve this problem:
1. drop all tables which had already created in liquibase
2. clear all data in databasechangelog table
delete from databasechangelog


2018/02/10

[Ant] java.lang.UnsupportedClassVersionError: com/sun/tools/javac/Main : Unsupported major.minor version 52.0

Problem
When I use Ant 1.10.1 to build my Java project, it occurs error as bellows:
java.lang.UnsupportedClassVersionError: org/apache/tools/ant/launch/Launcher : Unsupported major.minor version 52.0

How-To

The problem is 1.10.x releases require Java 8 at run time, but I am using Java 7 in my project.
Therefore, I need to use Ant 1.9.x releases instead of 1.10.x releases



2018/02/09

[Maven] How to stop Maven to check for updates for artifacts in offline mode ?

Problem
I am using offline mode to build my project, the command is as bellows:
mvn -o -Dmaven.repo.local="C:/Users/albert/.m2" clean install -Dmaven.test.skip=true

During the build process, Maven still check for updated for certain artifacts even in offline mode. How to avoid it?


How-To

Go to your local repository (ex. C:/Users/albert/.m2), and delete all *.lastupdated and _remote.repositories files.
Then Maven won't check updates any more.




2018/02/08

[PostgreSQL] How to assign customized numbering rule in primary key

Problem
Assume I have a table, loan_main, which have a primary key, loan_id, with numbering rule : LN_YYYYMMDD_9999999999.
How to insert data into loan_main automatically without caring how to build loan_id with this numbering rule.

How-to


In this case, we need to know
1. how to concatenate string, i.e. ||
2. how to get current date with specific format:
select to_char(now(), 'YYYYMMDD')

3. how to get value from sequence and pad zero to the left:

select LPAD(nextval('loan_sequence')::text, 10, '0')


The syntax looks like:
ALTER TABLE loan_main ALTER COLUMN loan_id SET DEFAULT ('LN_' || (to_char(now(), 'YYYYMMDD')) || '_' || LPAD(nextval('loan_sequence')::text, 10, '0'));





2018/02/07

Java Bean Validation example

Add dependencies to your pom.xml
    <!-- validation start  -->
    <dependency>
        <groupId>javax.validation</groupId>
        <artifactId>validation-api</artifactId>
        <version>2.0.0.Final</version>
    </dependency>

    <dependency>
        <groupId>org.hibernate</groupId>
        <artifactId>hibernate-validator</artifactId>
        <version>6.0.2.Final</version>
    </dependency>

    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.1-b08</version>
    </dependency>
    <!-- validation end -->
        
    <!-- DI start -->
    <dependency>
        <groupId>com.google.inject</groupId>
        <artifactId>guice</artifactId>
        <version>4.1.0</version>
    </dependency>
    <!-- DI end -->
        
    <!-- lombok -->
    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <version>$1.16.18</version>
        <scope>provided</scope>
    </dependency>
    
    <dependency>
        <groupId>com.google.guava</groupId>
        <artifactId>guava</artifactId>
        <version>21.0</version>
    </dependency>


Scenario
Assume I have a Customer bean, it have 6 attributes which include id, name, gender, age, email and dateOfBirth.
These attributes have its validation rules:

  1. id should not be null
  2. name should not by null or empty
  3. gender should not be null
  4. age should be between 18 and 60
  5. email should not be null or empty and should have valid format
  6. dateOfBirth should be before today


The Java bean with bean validation annotation looks like:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package albert.practice.validation;

import java.util.Date;

import javax.validation.constraints.Email;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;
import javax.validation.constraints.Past;

import org.hibernate.validator.constraints.Range;

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.ToString;

@AllArgsConstructor
@NoArgsConstructor
@ToString
@Data
public class Customer {
    public static enum Gender {
        MALE, FEMALE;
    }

    @NotNull(message = "顧客編號不可為 null")
    private Long id;

    @NotEmpty(message = "姓名不可為空")
    private String name;

    @NotNull(message = "性別不可為空")
    private Gender gender;

    @Range(min = 18, max = 60, message = "年齡必須介於 18 到 60 歲之間")
    private Integer age;

    @NotEmpty(message = "Email 不可為空")
    @Email(message = "Email 格式不合法")
    private String email;
    
    @Past(message = "生日必須要今天以前")
    private Date dateOfBirth;
}


Assume I have a CustomerService class to create Customer, it should validate Customer Java Bean before do insert. The main validate logic will be in validate method (line 25 ~ 38).
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
package albert.practice.validation;

import java.util.ArrayList;
import java.util.List;
import java.util.Set;

import javax.validation.ConstraintViolation;
import javax.validation.Validation;
import javax.validation.Validator;

import com.google.common.base.Joiner;
import com.google.common.base.Strings;
import com.google.inject.Singleton;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Singleton
public class CustomerService {

    public void create(Customer customer) {
        validate(customer);
    }

    public static void validate(Object obj) {
        List<String> errors = new ArrayList<>();

        Validator validator = Validation.buildDefaultValidatorFactory().getValidator();
        Set<ConstraintViolation<Object>> violations = validator.validate(obj);
        for (ConstraintViolation<Object> violation : violations) {
            errors.add(violation.getMessage());
        }

        String completeErrorMsg = Joiner.on("\n").join(errors);
        if (!Strings.isNullOrEmpty(completeErrorMsg)) {
            throw new IllegalArgumentException(completeErrorMsg);
        }
    }

}



The resource class will prepare Customer Object and invoke create method which provide by CustomerService
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
package albert.practice.validation;

import com.google.inject.Guice;
import com.google.inject.Inject;

public class CustomerResource {

 @Inject
 private CustomerService service;
 
 public static void main(String[] args) {
    Customer customer = new Customer();
    customer.setId(null);
    customer.setAge(12);
    customer.setEmail("test@gmail.");
  
    new CustomerResource().createCustomer(customer);
 }
 
 public void createCustomer(Customer customer){
    service = Guice.createInjector().getInstance(CustomerService.class);
    service.create(customer);
 }
 
}


Execution message in console:
1
2
3
4
5
6
7
8
9
Exception in thread "main" java.lang.IllegalArgumentException: 性別不可為空
年齡必須介於 18  60 歲之間
Email 格式不合法
顧客編號不可為 null
姓名不可為空
 at albert.practice.validation.CustomerService.validate(CustomerService.java:35)
 at albert.practice.validation.CustomerService.create(CustomerService.java:20)
 at albert.practice.validation.CustomerResource.createCustomer(CustomerResource.java:22)
 at albert.practice.validation.CustomerResource.main(CustomerResource.java:17)



2018/02/06

[JSR 303 - Bean Validation] HV000183: Unable to initialize 'javax.el.ExpressionFactory'.

Problem
I am using JSR 303 to do bean validation.

When I try to initialize 
Validator instance ..... 
Validator validator = Validation.buildDefaultValidatorFactory().getValidator(); 

But I get the following error message: 

Exception in thread "main" javax.validation.ValidationException: HV000183: Unable to initialize 'javax.el.ExpressionFactory'. Check that you have the EL dependencies on the classpath, or use ParameterMessageInterpolator instead
 at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.buildExpressionFactory(ResourceBundleMessageInterpolator.java:115)
 at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.<init>(ResourceBundleMessageInterpolator.java:46)
 at org.hibernate.validator.internal.engine.ConfigurationImpl.getDefaultMessageInterpolator(ConfigurationImpl.java:420)
 at org.hibernate.validator.internal.engine.ConfigurationImpl.getDefaultMessageInterpolatorConfiguredWithClassLoader(ConfigurationImpl.java:596)
 at org.hibernate.validator.internal.engine.ConfigurationImpl.getMessageInterpolator(ConfigurationImpl.java:355)
 at org.hibernate.validator.internal.engine.ValidatorFactoryImpl.<init>(ValidatorFactoryImpl.java:149)
 at org.hibernate.validator.HibernateValidator.buildValidatorFactory(HibernateValidator.java:38)
 at org.hibernate.validator.internal.engine.ConfigurationImpl.buildValidatorFactory(ConfigurationImpl.java:322)
 at javax.validation.Validation.buildDefaultValidatorFactory(Validation.java:103)
 at albert.practice.validation.CustomerService.validate(CustomerService.java:38)
 at albert.practice.validation.CustomerService.create(CustomerService.java:21)
 at albert.practice.validation.CustomerService.main(CustomerService.java:32)
Caused by: java.lang.NoClassDefFoundError: javax/el/ELManager
 at org.hibernate.validator.messageinterpolation.ResourceBundleMessageInterpolator.buildExpressionFactory(ResourceBundleMessageInterpolator.java:107)
 ... 11 more
Caused by: java.lang.ClassNotFoundException: javax.el.ELManager
 at java.net.URLClassLoader.findClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 at sun.misc.Launcher$AppClassLoader.loadClass(Unknown Source)
 at java.lang.ClassLoader.loadClass(Unknown Source)
 ... 12 more
    



How-to
Add this dependency to your pom.xml
    <dependency>
        <groupId>org.glassfish</groupId>
        <artifactId>javax.el</artifactId>
        <version>3.0.1-b08</version>
    </dependency>



Reference
[1] https://stackoverflow.com/questions/42718869/hibernate-validation-unable-to-initialize-javax-el-expressionfactory-error