Thursday, March 22, 2012

Responsive Media Query Quick Reference


Just a reminder which helps me doing responsive design's Media Query.

@media (max-width: 767px)
@media (max-width: 480px) 
@media (min-width: 768px) and (max-width: 979px)
@media (min-width: 980px)
@media (min-width: 1200px)

注意 :上面的順序是有意義的 更改順序會導致CSS被蓋掉

Thursday, March 8, 2012

My Sublime Text 2 on Windows 7 64bit



Sublime, the awesome, amazing editor.
1. Get 64bit portable version
2. Install Package Control
This is the most complex step.
Type Ctrl + ` and enter following python code
import urllib2,os; pf='Package Control.sublime-package'; ipp=sublime.installed_packages_path(); os.makedirs(ipp) if not os.path.exists(ipp) else None; urllib2.install_opener(urllib2.build_opener(urllib2.ProxyHandler())); open(os.path.join(ipp,pf),'wb').write(urllib2.urlopen('http://sublime.wbond.net/'+pf.replace(' ','%20')).read()); print 'Please restart Sublime Text to finish installation'
3. Install Packages (All from package control)
  • Alignment
    • Format shortcut – Ctrl + Alt + a
  • BracketHighlighter
  • CoffeeScript
  • Display Functions (Java)
  • Edit History
    • Jump to next/previous edit location
    • Add Key Binding
      • { "keys": ["ctrl+alt+e"], "command": "previous_edit" },
        { "keys": ["ctrl+e"], "command": "next_edit" }
  • FileDiffs
    • Usage
      • Right click on tab (Or use command popup then search “Diff”)
        • Diff with clipboard – Text Diff
        • Diff with another file – Text Diff + Visual Diff Tool
          (KDiff3, WinMerge, DiffMerge…)
    • Install KDiff3
    • Modify Code (file_diffs.py)
      Under WINMERGE 
      if not WINMERGE:
          if os.path.exists("%s\KDiff3\KDiff3.exe" % os.environ['ProgramFiles(x86)']):
              WINMERGE = '"%s\KDiff3\KDiff3.exe"' % os.environ['ProgramFiles(x86)']
          else:
              WINMERGE = '"%s\KDiff3\KDiff3.exe"' % os.environ['ProgramFiles']
      ……
      End of run_diff function
      
      if from_file == 'from_file' or to_file == 'to_file':
                  cmd_line = '%s "%s" "%s"' % (WINMERGE, from_file, to_file)
                  print "KDiff3 command: " + cmd_line
                  Popen(cmd_line)
  • Git
  • HTML5
    • Snippets
  • IndentGuides
    • Indent line for editors
  • jQuery
    • Snippets
  • jQuery Snippets pack
    • Hint : Need enable jQuery support by command
    • Snippets
  • JS Minifier
    • Minify shortcut – Ctrl + Alt + m
  • JsFormat
    • Format shortcut – Ctrl + Alt + f
  • Pretty JSON
    • Format shortcut – Ctrl + Alt + j
  • Prefixr
    • Format shortcut – Ctrl + Alt + x (For CSS)
  • SideBarGit
  • SideBarEnhancement
  • SublimeCodeIntel (Must Have)
    • Autocomplete (Popup)
  • Tag
    • “/”to end tag
    • Format shortcut – Ctrl + Alt + f
    • Features : Edit –> Tag …
  • Terminal
    • Open file shortcut – Ctrl + Shift + t
    • Open folder shortcut – Ctrl + Shift + Alt + t
  • Tortoise
    • Need  TortoiseGit or TortoiseSVN
    • Update : 20120314 - error: [Error 2] The system cannot find the file specified
      • Because TortoiseGit use mysysgit as default Git under Windows 7 64bit version,
        we need to modify Tortoise.py
        class Git():
            def __init__(self, tortoise_proc_path, root_dir):
                self.git_path = os.path.abspath('C:\\PATH\\git.exe')
  • WinMerge
    • Compare shortcut – Ctrl + Alt + d (Current file with previous edit file)
    • Modify Code (WinMerge.py)
      WINMERGE = "F:\WinMerge-2.12.4\WinMergeU.exe"
  • ZenCoding (AWESOME!!)
    • Trigger – Tab
Editor Shortcut
  • Toggle sidebar (file explorer) – Ctrl + K then B
  • Command List - Ctrl + Shift + P
  • File Search – Ctrl + P
    • Search  Text in File – Ctrl + P then enter #searchText
    • Jump to specific paragraph(Function/Class/Tag) – Ctrl + P then enter @searchMethod
    • Go to specific line – Ctrl + P then enter :lineNumber
Right Click Menu Support



  • Modify the Path of following regestries
  • Save following text into a .reg file then double click on it to apply

Windows Registry Editor Version 5.00

[HKEY_CLASSES_ROOT\*\shell\SublimeText]
@="Edit With Sublime Text2"
"Icon"="D:\\WindowsApp\\Sublime\\sublime_text.exe"
"Position"="Bottom"

[HKEY_CLASSES_ROOT\*\shell\SublimeText\command]
@="D:\\WindowsApp\\Sublime\\sublime_text.exe %1"

Tuesday, March 6, 2012

《鐵娘子The Iron Lady》筆記

這篇文章我從Twitter 打到 Tumblr, 從Tumblr 打到 Blogger

昨天才看完電影,今天全憑記憶打,不知不覺就打了好多,部分英文可能有錯就不要太在意了

教育相關

  • 我們要不堅持立場,要不乾脆一事無成
    (We will stand on principle, or we will not stand at all)
  • 過去人們總是努力要完成某件事,現在卻是要努力成為某個人
    (It used to be about trying to do something, Now it’s about trying to be someone)
  • 如果父母總是要求孩子跟自己走一樣的路,那要怎麼要求孩子更傑出?
    > 教育是適性發展,不是要跟隨前人腳步

政治相關

  • 以前政治是關於如何做事,現在是關於如何出名
  • 我們想要一個人民可以自由的選擇、犯錯、持有寬容及同情心的社會。這就是我們所謂的道德社會;不是一個國家要負所有責任,而沒有人須要為國家負責的社會。
  • 假裝我們都是平等的沒有任何好處,我們並不是相同的,從來沒有過,也永遠不會是
  • 人們已經不思考了,他們全憑感覺。一下說我覺得不妥,一下子又說抱歉我們組織覺得如何如何。
    當代最重大的問題之一就是我們被這樣一群人所統治
  • > 一個領導人不能只靠"感覺",要理性地去思考每一步策略.
  • 那些人不是無能,就是懦弱
    > 這是鐵娘子教女兒開車時的話,其實正是暗指當時政客們的狀況
  • 總有人應該面對問題,說出難聽的真相! (對政客說) 你們這些人膽子太小了
  • In politics, If you want anything said, ask a man; If you want anything done, ask a woman

人生相關

  • 生活本來就是自由選擇
  • 思想會化成語言,語言會化成行動,行動會變成習慣,習慣會變成個性,而個性會決定命運!
    我們想甚麼,就會造就自己成為什麼樣的人
    (Watch your thoughts for they become words. Watch your words for they become actions. Watch your actions for they become… habits. Watch your habits, for they become your character. And watch your character, for it becomes your destiny! What we think we become.)
  • 小心你想些什麼,因為它們會從你口中溜出來
    (Watch your thoughts for they become words)
  • 你想改變這個國家(團體/公司),就去領導它
    (If you want to change the party, lead it. If you want to change the country, lead it.)
    > 要堅定立場,做出自己的風格. 還記得某個網路圈的名人曾在FB上說過,如果你不喜歡你的(公司)環境,不是該抱怨,而是該改造它(然後我就按讚)

 

 

最後一句看似簡單但卻意義非凡的話

One’s life must mean more than that.

正適合送給所有正在為自己理性/夢想打拼的人

加油!

Monday, March 5, 2012

Request Https resource (Pass Authentication + Trust All Certificates) in Java

 

A post to remember

  • Request resource from : https://a.b.c.d/test-metadata.xml
    • Trust all certificates (PKIX path building failed)
    • Pass SSL handshake verification (javax SSLHandshakeException)
    • Pass basic (username/password) authentication (401, Authentication Required)

Basic Java Way :

import java.io.*;
import java.net.*;
import java.security.cert.*;
import javax.net.ssl.*;

public class
HttpsRequestClient {
private final String USERNAME = "flyworld"
;
private final String PASSWORD = "mypwd"
;
private final String TESTURL = "https://a.b.c.d/test-metadata.xml"
;

public static void main(String
[] args) {
HttpsRequestClient client =
new
HttpsRequestClient();
switch
(args.length) {
case
0:
client.testURL();
break
;
case
1:
client.testURL(args[0]);
case
3:
client.testURL(args[0], args[1], args[2]);
default
:
client.testURL();
}

}

public void
testURL() {
testURL(TESTURL, USERNAME, PASSWORD);
}

public void testURL(String
url) {
testURL(url, USERNAME, PASSWORD);
}

public void testURL(String testURL, final String name, final String
pwd) {

HttpsURLConnection testConnection =
null
;
try
{
// Trust All HttpsCertificates
SSLContext sc = SSLContext.getInstance("SSL"
);
sc.init(
null, new TrustManager[] { new AllPassTrustManager() }, null
);

// Pass handshake verification
HostnameVerifier myHostnameVarifier = new HostnameVerifier() {
public boolean verify(String
urlHostName, SSLSession session) {
return true
;
}
};

// Configuration
HttpsURLConnection.setDefaultSSLSocketFactory(sc.getSocketFactory());
HttpsURLConnection.setDefaultHostnameVerifier(myHostnameVarifier);
Authenticator.setDefault(
new
Authenticator() {
protected
PasswordAuthentication getPasswordAuthentication() {
return new
PasswordAuthentication(name, pwd.toCharArray());
}
});

testConnection = (HttpsURLConnection)
new URL
(testURL).openConnection();

printHttpsCertificates(testConnection);
printResponseContent(testConnection);

}
catch (MalformedURLException
e) {
e.printStackTrace();
}
catch (IOException
e) {
e.printStackTrace();
}
catch (Exception
e) {
e.printStackTrace();
}
finally
{
if (testConnection != null
)
testConnection.disconnect();
}

}

private static void
printHttpsCertificates(HttpsURLConnection con) {
try
{
System.out.println("-----Certificates-----"
);
System.out.println("Response Code : "
+ con.getResponseCode());
System.out.println("Cipher Suite : "
+ con.getCipherSuite());
System.out.println(System.getProperty("line.separator"
));

Certificate
[] certs = con.getServerCertificates();
System.out.println("Certificate Number : "
+ certs.length);
for (int
i = 0; i < certs.length; i++) {
Certificate
cert = certs[i];
System.out.println("No : "
+ i);
System.out.println("Cert Type : "
+ cert.getType());
System.out.println("Cert Hash Code : "
+ cert.hashCode());
System.out.println("Cert Public Key Algorithm : "
+ cert.getPublicKey().getAlgorithm());
System.out.println("Cert Public Key Format : "
+ cert.getPublicKey().getFormat());
System.out.println(System.getProperty("line.separator"
));
}
}
catch
(SSLPeerUnverifiedException e) {
e.printStackTrace();
}
catch (IOException
e) {
e.printStackTrace();
}
}

private static void
printResponseContent(HttpsURLConnection con) {
try
{
System.out.println("-----Content-----"
);
BufferedReader br = new BufferedReader(
new InputStreamReader
(con.getInputStream()));
String
input;
while ((input = br.readLine()) != null
) {
System
.out.println(input);
}
br.close();
}
catch (IOException
e) {
e.printStackTrace();
}
}

/**
     * A TrustManager which passes all certificates
     *
     * @see TrustManager
     */
private class AllPassTrustManager implements
TrustManager, X509TrustManager {
public
X509Certificate[] getAcceptedIssuers() {
return null
;
}

public void checkServerTrusted(X509Certificate[] certs, String authType) throws
CertificateException {
return
;
}

public void checkClientTrusted(X509Certificate[] certs, String authType) throws
CertificateException {
return
;
}
}

}

With HttpClient, the testURL method become easier





public void testURL(String testURL, String name, String pwd) {
DefaultHttpClient httpclient = new DefaultHttpClient();
try {
// Trust all https certificates
SSLContext sc = SSLContext.getInstance("SSL");
sc.init(null, new TrustManager[] { new AllPassTrustManager() }, null);
// Use apache SSLSocketFactory
httpclient.getConnectionManager().getSchemeRegistry()
.register(new Scheme("https", 443, new SSLSocketFactory(sc)));
httpclient.getCredentialsProvider().setCredentials(
new AuthScope(AuthScope.ANY_HOST, 443),
new UsernamePasswordCredentials(name, pwd));
HttpGet httpget = new HttpGet(testURL);
ResponseHandler<String> handler = new ResponseHandler<String>() {
public String handleResponse(HttpResponse response)
throws ClientProtocolException, IOException {
System.out.println("Response Status : " + response.getStatusLine());
System.out.println("-----Content-----");
HttpEntity entity = response.getEntity();
if (entity != null) {
return EntityUtils.toString(entity);
} else {
return null;
}
}
};
System.out.println(httpclient.execute(httpget, handler));
} catch (Exception e) {
e.printStackTrace();
} finally {
httpclient.getConnectionManager().shutdown();
}
}

Reference :


http://www.mkyong.com/java/java-https-client-httpsurlconnection-example/
http://hc.apache.org/httpcomponents-client-ga/examples.html