2020年12月16日 星期三

Datatable增加Export/Copy Button一併顯示筆數下拉的設定調整

JS Datatable dom設定成Bfrtip會顯示Button但是造成無法切換筆數,改成Blfrtip即可解決,至於排版可參考下列說網址說明

https://datatables.net/reference/option/dom

2020年12月4日 星期五

Get method has XSS的處理

 弱點掃描反映有高風險問題,處理方法註記下

原寫法

<%
String TEST = "";
if ((request.getParameter("test")!=null)) TEST = request.getParameter("test");
%>

<input type="TEXT" id="TEST" name="TEST"  value="<%=TEST %> />

在這方式下就GG惹
http://localhost/home?test=%22onmouseover%3D%22alert%28140%29%22

改寫法搞定

<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
<input type="TEXT" id="TEST" name="TEST"  value="${fn:escapeXml(param.test)}" />

轉跑道寫JSP 一些PHP跟C#處理的方式會有所不同,恩恩...記錄下

2020年6月18日 星期四

XMLWorkerHelper加PDF浮水印



File file = new File(outFilePath + "filename.pdf");
String DEST = outFilePath + "filename_water.pdf";
FileInputStream input1 = new FileInputStream(file);
FileOutputStream output2 = new FileOutputStream(new File(DEST));
stringWaterMark(input1, output2, "浮水印測試", 2, 3, 0.15f,45, 84,BaseColor.BLACK);

/* 斜角排列、多個重複的花式文字浮水印
     * @param input             需要加水印的PDF讀取輸入流
     * @param output            輸出生成PDF的輸出流
     * @param waterMarkString   浮水印字
     * @param xAmout            x軸重複數量
     * @param yAmout            y軸重複數量
     * @param opacity           水印透明度
     * @param rotation          水印文字旋轉角度,一般為45度角
     * @param waterMarkFontSize 水印字型大小
     * @param color             水印字型顏色
     */
public static void stringWaterMark(InputStream input, OutputStream output, String waterMarkString, int xAmout, int yAmout, float opacity, float rotation, int waterMarkFontSize, BaseColor color) {
        try {

            PdfReader reader = new PdfReader(input);
            PdfStamper stamper = new PdfStamper(reader, output);

            // 新增中文字型
            String chMingliu1FontPath = "c:\\Windows\\Fonts\\mingliu.ttc,0";//windows內建的新細明體         
            BaseFont baseFont = BaseFont.createFont(chMingliu1FontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);

            int total = reader.getNumberOfPages() + 1;

            PdfContentByte over;
            // 給每一頁面加浮水印
            for (int i = 1; i < total; i++) {
                Rectangle pageRect = stamper.getReader().getPageSizeWithRotation(i);
                // 計算水印每個單位距離X,Y
                float x = pageRect.getWidth() / xAmout;
                float y = pageRect.getHeight() / yAmout;

                over = stamper.getOverContent(i);
                PdfGState gs = new PdfGState();
                // 設定透明度為
                gs.setFillOpacity(opacity);

                over.setGState(gs);
                over.saveState();

                over.beginText();
                over.setColorFill(color);
                over.setFontAndSize(baseFont, waterMarkFontSize);

                for (int n = 0; n < xAmout + 1; n++) {
                    for (int m = 0; m < yAmout + 1; m++) {
                        over.showTextAligned(Element.ALIGN_CENTER, waterMarkString, x * n, y * m, rotation);
                    }
                }

                over.endText();
            }
            stamper.close();
        } catch (Exception e) {
        System.out.println("PDF error "+e.getMessage());
        }
}

2020年6月15日 星期一

GRADLE 安裝備忘

最近碰到Spring Batch的專案需求,編譯環境的設定記錄下...以後可以參考用。

1. 下載: https://gradle.org/releases/

2. 解壓縮後,將bin path設定加入到系統環境變數的path裡

3. 自命令提示字元中(終端機) 輸入 Gradle –v 進行測試,若設定OK會回版本號回來

4. gradle build 進行專案編譯


使用Enter鍵來移動焦點至下個輸入元素

用AJAX做功能習慣了,碰到習慣submit的使用者,對於封鎖在輸入框按Enter做提交的設計,給了許多的負評,沒辦法改變人那就改變寫法吧,把ENTER的封鎖改成切換焦點方式,當切到按鈕處在執行提交,那頁面功能就不會異常了,寫法如下~~

 var allInputSelector = ":input:visible:enabled"; 
 $("body").on("keydown", allInputSelector + ":not(textarea, :button)", function(event){
    //排除 focus 在 textarea 或 button 時觸發 
    if (event.keyCode == 13) { 
         $(allInputSelector + ":eq(" + ($(allInputSelector).index($(this)) + 1) + ")").focus();
         event.preventDefault(); 
     } 
 });


2020年4月10日 星期五

Maven create PDF file By WebGet Html content

<dependency>
<groupId>com.itextpdf</groupId>
<artifactId>itextpdf</artifactId>
<version>5.5.13</version>
</dependency>

public static void createPdf(String url, String id, String outPath) throws Exception {
  //Pagesize, marginLeft, marginRight, marginTop, marginBottom
Document document = new Document(PageSize.A4, 20, 45, 20, 45);
PdfWriter pdfWriter = PdfWriter.getInstance(document,new FileOutputStream(outPath + id + ".pdf"));
document.open();
document.addAuthor("***");
document.addTitle("***");
document.addCreator("***");
document.addHeader("Domain", "www.***.gov.tw");
document.addKeywords("**");
document.addSubject("**");
                
                //Referenct by https://seed0111.blogspot.com/2020/04/maven-get-html-content-use-httppost.html
String _html = getHtmlContent(url, caseno);
try {
//InputStream cssInput =new URL("http://xxx/xx.css").openConnection().getInputStream();
InputStream cssInput = null;
//HTML To PDF
XMLWorkerHelper.getInstance().parseXHtml(pdfWriter, document,new ByteArrayInputStream(_html.getBytes("UTF-8")),cssInput,Charset.forName("UTF-8"), new AsianFontProvider());
}catch (IOException e) {
                        log.error("createPdf has exception " + e.getMessage(), e);
               }finally {
                        document.close();
               }
}
※ 部份環境中文字會有亂碼產生,故_html.getBytes() =>  _html.getBytes("UTF-8") 排除

public class AsianFontProvider extends XMLWorkerFontProvider {
   @Override
    public Font getFont(final String fontname, final String encoding,
            final boolean embedded, final float size, final int style,
            final BaseColor color) {
            BaseFont bf = null;
        try {
        /*
        // 設定中文字體
String chMingliu0FontPath = "c:\\Windows\\Fonts\\mingliu.ttc,0";//windows內建的細明體                           
String chMingliu1FontPath = "c:\\Windows\\Fonts\\mingliu.ttc,1";//windows內建的新細明體                         
        */
        String chKaiuFontPath = "c:\\Windows\\Fonts\\KAIU.TTF";//windows內建的標楷體
bf = BaseFont.createFont(chKaiuFontPath,BaseFont.IDENTITY_H,BaseFont.NOT_EMBEDDED);
        } catch (Exception e) {
           e.printStackTrace();
        }
        Font font = new Font(bf, size, style, color);
        font.setColor(color);
        return font;
    }

}
※ 部份環境無法讀取TTC 選TTF字型檔可以排除

// PDF設定縮放
PdfDestination pdfDest = new PdfDestination(PdfDestination.XYZ, 0, document.getPageSize().getHeight(), 0.8f);
PdfAction action = PdfAction.gotoLocalPage(1, pdfDest, pdfWriter);
pdfWriter.setOpenAction(action);

Maven Get Html Content use HttpPost

//url: target url address
//id: post include params 
private static String getHtmlContent(String url, String id) throws IOException {
System.setProperty("javax.net.debug","ssl,handshake");
System.setProperty("sun.security.ssl.allowUnsafeRenegotiation","true");
String content = "";
try {   
                   //if use https or call https url addr than show handshake failed return, can add in pass check
SSLContext sslcontext = SSLContexts.createDefault();
SSLConnectionSocketFactory sslsf = new 
              SSLConnectionSocketFactory(sslcontext, new String[] { "TLSv1.2"}, null, new NoopHostnameVerifier());
CloseableHttpClient httpClient = HttpClients.custom().setSSLSocketFactory(sslsf).build();
        
        try {        
    //Use HttpGet method 
    //HttpGet webGet = new HttpGet(url);
                            //Use HttpPost method 
    HttpPost webGet = new HttpPost(url); 
    ArrayList<BasicNameValuePair> params = new ArrayList<BasicNameValuePair>( );
    params.add(new BasicNameValuePair("id" ,id )) ;
    webGet.setEntity(new UrlEncodedFormEntity(params));
    
    CloseableHttpResponse response = httpClient.execute(webGet);
    HttpEntity entity = response.getEntity();
    content = EntityUtils.toString(entity, "UTF-8");
    content = content.replace("&emsp;", "<font color='white'>_</font>");
    //System.out.println(content);
    EntityUtils.consume(entity);
} catch (Exception ex) {
log.error(ex.getMessage(), ex);
} finally {
httpClient.close();
}         
} catch (Exception ex) {
log.error("createIgnoreVerifySSL has exception " + ex.getMessage(), ex);
}
    return content;
}  


2020年3月26日 星期四

Jenkins curl 出現 unable to get local issuer certificate

平常在跑的排程因為SSL簽章過期更新後出現unable to get local issuer certificate這樣的錯誤訊息 參考這篇blog無法解決,不過也是個研究方向,應急不行。

這篇 curl 略過檢查自簽 SSL 憑證有效性 就不錯用了,加上-k 或 --insecure 去重跑排程就不會再報錯!! 當然一般寫code加上CURLOPT_SSL_VERIFYPEER去排除狀態,不過這是透過Shell Script來執行的初次碰到就註記下。


#!/bin/sh
system=('www1' 'www2' 'www3')
for i in "${system[@]}"
do
   curl -k --request GET 'https://'$i'.domain.net/method.php'
done


<?php
$url = "https://www1.domain.net/method.php/";
$post_data['var'] = "xxx";
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($post_data));
$result = curl_exec($ch);
curl_close($ch);
?>

jQuery autocomplete example memo

最近用到又忘了怎麼做,在Model顯示卡了些關~寫個文章記錄下!!

//Head 需要Include的Resource
<script src="https://code.jquery.com/ui/1.12.1/jquery-ui.js"></script>
<link rel="stylesheet" href="https://code.jquery.com/ui/1.12.1/themes/base/jquery-ui.css">

//補充顯示的CSS設定
<style type="text/css">
.ui-autocomplete { 
position: absolute; 
cursor: default;
z-index:99999 !important;   //有時候(使用Model)會顯示不在最前面,故設定大些
display:block;
}  
</style>

//Body
<form action="#" role="form" method="post" id="ListForm" >  
  <input type="text" class="form-control inputFieldClass" id="inValue" name="inValue" ></input>
  <input type="hidden" id="tx_value" name="tx_value" />  
   <input type="hidden" id="tx_lable" name="tx_lable" /> 
</form>

//如果回傳是一個Class,其中一個變數才是要用的jQuery.parseJSON(data)這樣處理
<script>
$(function (){
    $(".inputFieldClass").on("focus", function(){
        $(this).autocomplete({
            source: function (request, response) {
                $.ajax({
                    url: "API_URL", 
                    dataType: "json",
                    type: "POST",
                    data: {
                        key: request.term     
                      },
                    success: function (data) {
                    if (data.isSuccess){
                    response($.map(jQuery.parseJSON(data.jsondata), function (item) {
                            return {
                            value: item.in_value,      
                            lable: item.in_lable
                            }
                        }));
                    }
                    }
                });
            },
            minLength: 2,   //輸入多少字元觸發
            select : function(event, ui) {  
                //候選項選擇帶入
    $("#tx_value").val(ui.item.value);  
                $("#tx_lable").val(ui.item.lable);  
                return false;
            }
        });
    });
});
</script>

2020年3月6日 星期五

Maven Call SOAP sample

At pos.xml add

<dependency>
    <groupId>com.mashape.unirest</groupId>
    <artifactId>unirest-java</artifactId>
    <version>1.4.9</version>
</dependency>

At controller add method

import com.mashape.unirest.http.Unirest;

String soap_postdata = "{\"DATA1\":\"XXX\",\"DATA2\":\"XXX\"}";
HttpResponse<JsonNode> jsonResponse = Unirest.post(soap_url)
  .header("cache-control", "no-cache")
  .header("accept", "application/json")
  .body(soap_postdata)
  .asJson();
if (jsonResponse != null) {
 if (jsonResponse.getStatus() == 200) {
    JSONObject json = jsonResponse.getBody().getObject();
       if (json.has("error")) {
   //SOAP Fail. json.has error
} else {
           //do it
        }
  }
}

補充說明:
呼叫後會有一堆這類的紀錄,貌似關不掉可以透過log關閉方式排除
[Thread-46] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing expired connections
[Thread-46] DEBUG org.apache.http.impl.conn.PoolingHttpClientConnectionManager - Closing connections idle longer than 30 SECONDS

At logback.xml add
<logger name="com.mashape.unirest" level="ERROR" />

2020年2月7日 星期五

Spring java.io.CharConversionException DB2中文異常排除

錯誤訊息
error code [-4220]; [jcc][t4][1065][12306][4.19.72] 捕捉到 java.io.CharConversionException

經查是在處理有中文字欄位時出現的Exception訊息,排除紀錄如下:

1. SERVERS處點滑鼠右鍵兩下,開啟Overview頁面。
務必確定SERVERSTOP狀態






2. 點選Open Launch configuration 開啟Edit Configuration頁面。而後切換至Arguments  。



















3. 加上 -Ddb2.jcc.charsetDecoderEncoder=3 點選Apply後離開,啟動SERVER即可。