マイペースなプログラミング日記

DTMやプログラミングにお熱なd-kamiがマイペースに書くブログ

Base64を使ってみる

WicketにはBase64を扱うクラスがあるため、使ってみることにした。というか何でJavaの標準の公開APIに含まれてないんだろうと疑問に思うクラスなわけだが。とりあえず以下ソース

まずはHTML、ファイル名はBase64Test.html

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<html>
  <head>
    <title></title>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
  </head>
  <body>
    <form wicket:id="form" style="text-align:center">
      <textarea wicket:id="src">入力</textarea><br />
      <select wicket:id="choice">
        <option>符号化</option>
        <option>復号化</option>
      </select>
      <input type="submit" wicket:id="submit" /><br />
      <textarea wicket:id="dist">結果</textarea>
    </form>
  </body>
</html>

次に対応するJavaのクラス、ファイル名はBase64Test.java

package base64;

import java.util.Arrays;
import java.io.UnsupportedEncodingException;

import org.apache.wicket.util.crypt.Base64;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextArea;
import org.apache.wicket.markup.html.form.DropDownChoice;
import org.apache.wicket.markup.html.form.Button;

import org.apache.wicket.model.Model;

public class Base64Test extends WebPage{
    public Base64Test(){
        Form form = new Form("form");
        final TextArea<String> src = new TextArea("src", new Model<String>());
        final DropDownChoice<String> choice
                = new DropDownChoice<String>("choice", new Model<String>(), Arrays.asList("符号化", "復号化"));
        final TextArea<String> dist = new TextArea("dist", new Model<String>());

        Button submit = new Button("submit"){
            public void onSubmit(){
                try{
                    byte[] srcData = src.getModelObject().getBytes("UTF-8");
                    byte[] distData = null;
                    String operation = choice.getModelObject();

                    if("符号化".equals(operation))
                        distData = Base64.encodeBase64(srcData);
                    else if("復号化".equals(operation))
                        distData = Base64.decodeBase64(srcData);
                    else
                        distData = new byte[0];

                    dist.setModelObject(new String(distData, "UTF-8"));
                }catch(UnsupportedEncodingException e){
                    throw new RuntimeException(e);
                }
            }
        };

        form.add(src);
        form.add(choice);
        form.add(submit);
        form.add(dist);
        add(form);
    }
}

次にアプリケーションクラス、ファイル名はBase64Application.java

package base64;

import org.apache.wicket.protocol.http.WebApplication;

public class Base64Application extends WebApplication{
    public Class<Base64Test> getHomePage(){
        return Base64Test.class;
    }

    public void init(){
        getRequestCycleSettings().setResponseRequestEncoding("UTF-8");
        getMarkupSettings().setDefaultMarkupEncoding("UTF-8");
    }
}

最後にフィルタマッピング

<?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <session-config>
        <session-timeout>
            30
        </session-timeout>
    </session-config>
    <filter>
        <filter-name>Base64Test</filter-name>
        <filter-class>org.apache.wicket.protocol.http.WicketFilter</filter-class>
        <init-param>
            <param-name>applicationClassName</param-name>
            <param-value>base64.Base64Application</param-value>
        </init-param>
    </filter>
    <filter-mapping>
        <filter-name>Base64Test</filter-name>
        <url-pattern>/base64/*</url-pattern>
    </filter-mapping>
</web-app>