Yuta's Weblog

All | Personal | Sun

20070131 2007年 1月 31日 水曜日

Java Pet Store 2.0 EA 4.5 released

We just released a new version of Pet Store 2.0.
Although the UI looks pretty much same, the codes were refactored and we fixed a lot of bugs. Also this version of Pet Store includes a new news.jsp which can be found when you click the left side of the RSS bar.

newsbar

and the news.jsp looks like:--

newsbar

In the previous version of Pet Store 2.0, when you click this link it pops up another window to show the content of RSS feed, but now it guides you to the news.jsp which is one of the jsp page included in the package. There were a few problems in popping up the window, such as not working well with IE.

We also changed the logic to cache the RSS feed information in the server. Now it's caches data in an application scope so that all requests within the Pet Store context can share the same RSS feed information and both RSS bar and news.jsp use the same cached data.

The code in news.jsp(and the associated JavaScript) is very simple. Since the data is already constructed by ROME library on the server, it just consumes and rearrange the contents within the page.

Handling the strings in the RSS feed is a little tricky. Because usually those strings in the feed contain characters which are not usable in JSON, such as double quote, we need to 'escape' them somehow. We use URL encoding for this purpose. We encode all strings on the server side and decode them on the client's JavaScript.
However, as there's a slight difference between Java URL encoding and JavaScript's URLencode() and URLdecode(), we needed to write our own URLdecode() function for matching Java URL encoding mechanism.

Please check this version of Pet Store 2.0 and let us know what you think on the forum.

Posted by yuta ( 1月 31日 2007年, 03:06:27 午後 PST ) Permalink 投稿されたコメント [0]

20061218 2006年 12月 18日 月曜日

Java EE SDK 日本語ダウンロードページ
お気づきでしょうか、Java EE 5 SDK の日本語ダウンロードページが できてます。
→→ こちら←←
訳がちょっと変なところもありますが、まぁこんなもんでしょう。 ただ「ダウンロード」ボタンをクリックすると、その先は残念ながら 英語です。でも「accept」のラジオボタンにチェックいれて 任意のプラットホームの SDK を選べばOKです。
その先も日本語版作れ?おっしゃるとおりです。ごめんなさい。。。 Posted by yuta ( 12月 18日 2006年, 01:16:43 午後 PST ) Permalink 投稿されたコメント [0]

20061002 2006年 10月 02日 月曜日

New RSS Bar in Pet Store 2.0 EA3
-- This blog was revised on 10/06 as there might be some confusions --

The latest version(early access 3.0) of Pet Store 2.0 was just released. You can download from here. If you have ever tried Pet Store 2.0, you know it utilizes the library jar from BluePrints Solutions Catalog which contains various JavaServer Faces component with AJAX capabilities. In this version, we rewrote the RSS component in the library jar to use ROME to process the RSS feed xml instead of XSLT. Also we are using ROME Fetcher for retrieving the XML itself. For those who don't know Fetcher, it fetches XML via http(and https of course) and it supports HTTP conditional gets (ie: last modified and ETag handling) and GZip encoded feeds. All changes were about the logic of retrieving and processing XML, so there is no change in the tag of JavaServer Faces component to generate all necessary mechanism for the client side HTML and javascript. Also there's no change in the Pet Store 2.0 RSS bar code itself.

Using ROME

Using ROME is extremely easy. If you don't have to care what the format of the original feed(a bunch of versions of RSS and Atom), basically you can just do the following to fetch the feed:-

    FeedFetcherCache syndFeed = new HashMapFeedInfoCache();
    FeedFetcher feedFetcher = new HttpURLFeedFetcher(feedInfoCache);
    /* attach listener here if you need to change the behavior of
     * response from the server
     */
    SyndFeed syndFeed = feedFetcher.retrieveFeed(new URL(urlString));

then to process the feed:-

    syndFeed.getTitle()  // get the title of Feed title
    List <SyndEntry> entries = syndFeed.getEntries();
    for (SyndEntry entry : entries) {
        entry.getTitle();  // get the title of each entry in the feed
    }

The current RSS component generates the JSON from the feed and sends back to the client, then the client side javascript processes it and displays in a ticker. All of those back-end process is done in the managed bean's method which is called by the javascript via shale-remoting. There are some new features in this version of Pet Store 2.0, such as tagging. So please check that out! Posted by yuta ( 10月 02日 2006年, 03:00:58 午後 PDT ) Permalink 投稿されたコメント [0]

20060818 2006年 8月 18日 金曜日

Captcha in Petstore 2.0

Despite that there are some issues in captcha, such as in accessibility, it is widely used in many web applications to separate the human and computer activities. (For the detailed explanation about captcha, take a look at the wikipedia entry.) There are many implementations available for various languages to incorporate this mechanism into your web application. It's also very easy to do so with Java EE 5 and AWT and in fact, the latest release of Petstore 2.0 from Java BluePrints group has this functionality in fileupload component. It looks like this.

captcha usage



Parts

This mechanism consists of the following resources.

  1. Captcha generator servlet
    This servlet creates the captcha image with a random string and put that information of the string into HttpSession object for later reference.
    Creating the image dynamically is very simple. Although the code in Petstore 2.0 has a little more twist like drawing lines as a noise and scatter characters within the box, it essentially does:-

      BufferedImage bufferImg = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
      Graphics g = bufferImg.getGraphics();
      g.setColor(background-color);
      g.fillRect(0, 0, width, height);
      g.setFont(new Font("Arial", Font.BOLD | Font.ITALIC, 30));
      g.setColor(Color.GRAY);
      g.drawString(message, startPosition, endPosition);
    

    Also, after creating this image, it applies the filter to convert the color to the blue-ish one. This is for a developer who wants to add another kind of filter such as for a distortion.
    The class which generates the image and is used by the servlet can be seen here in java.net.

  2. Captcha validation filter
    This is a very simple filter which compares the user input(with session id) and the stored string in the HttpSession object. When matched, it just pass the request to the original requested resource, otherwise it generates a error message in JSON and sends back to the client.

Next Challenge

So what's next? Perhaps I should create a JavaServer Faces component for captcha. Creating the component itself is relatively easy. They are just <h:outputText>, <h:graphicImage>, and <h:outputText> with attributes to specify the size, color, etc. The problem is a transaction for a validation. Yes, we can do a dynamic validation on only the client side with AJAX, however the logic to validate has to be associated with the process of the actual form submission. Even if we did the client side dynamic validation, the "proof" of validation still must be stored somewhere on the server side, otherwise people can mimic the form submission. That means a developer needs to implement a logic to do something in his or her web application even with JSF component. Well, I guess I need to think about this a little more...

Posted by yuta ( 8月 18日 2006年, 11:35:58 午前 PDT ) Permalink 投稿されたコメント [1]

20060511 2006年 5月 11日 木曜日

Java PetStore 2.0 EA1
つい数分前に新しい PetStore 2.0 Early Access 1.0をリリースしました。
前のバージョンは EJBのショーケースとしてEコマース的なアプリケーションでしたが、今回は Web Tierのみ(もちろんDBとの連携は Persistence APIですが)の アプリケーションになっています。PetStore 2.0 はAJAXを多用し、Google Maps などとの Mash up も行っています。その他にも、オートコンプリート、 プログレスバー、RSSリーダー、PayPalとの接続など盛りだくさんの 機能が含まれています。また、NetBeans 5.5 のプロジェクトとしても 使えます。ちょっと変更が必要なのですが、 Geertjan Using the Pet Store 2.0 in NetBeans 5.5を書いてくれました。 このリリースで私たちが採用したプログラミングモデルには実験的なものも 含まれています。多くの方に使っていただいて、いろいろなフィードバックを 得られたらと思っています。
Posted by yuta ( 5月 11日 2006年, 07:38:03 午後 PDT ) Permalink 投稿されたコメント [0]

Java デベロッパ向け AJAX FAQ
Greg MurrayのFAQを日本語に訳しました。
ここです
もしなにか訳の間違い、わかりにくい点などありましたら このブログエントリにコメントをください。 Posted by yuta ( 5月 11日 2006年, 03:19:52 午後 PDT ) Permalink 投稿されたコメント [3]

20060502 2006年 5月 02日 火曜日

日本語版 The Aquarium

日本語版アクエリアムが立ち上がりました。
もちろん、水族館の話ではなく、glassfish、JavaEE5、AJAXなど 今話題になっている情報へのポータルブログです。基本的には 英語版の情報を日本語で伝えるのですが、日本独自のブログや 情報も載せていきたいと思っています。

Posted by yuta ( 5月 02日 2006年, 12:01:30 午後 PDT ) Permalink 投稿されたコメント [0]

20060414 2006年 4月 14日 金曜日

Java BluePrints AJAX コンポーネント
BluePrints グループで AJAX を使っている JavaServer Faces のコンポーネントを出しました。
→→→ Java BluePrints AJAX Components
今回のリリースでは、いくつかコンポーネントが追加され、また JSF1.1 用と JSF1.2 用の コンポーネントが分けられています。 これは主に JSF1.1 用コンポーネントを Java Studio Creator で使いやすくするためです。
どんなコンポーネントがあるのかは最初のリンクを見ていただければすぐわかります。 使い方は単に JSP ページ中で Faces のタグを書くだけですので簡単です。
ぜひ使ってみてください。 Have Fun!!

Posted by yuta ( 4月 14日 2006年, 11:28:17 午後 PDT ) Permalink 投稿されたコメント [0]

20060401 2006年 4月 01日 土曜日

Shale Remoting と JSF カスタムコンポーネント - その2
さて、じゃあ実際どうやるのかっていうと。。。まず Shale Remoting がやることは基本的に、内部の PhaseListener がリソースへのリクエストを managed bean の deferred メソッド(ところで deferred method ってなんていうんだろう? 遅延メソッド?)へ delegate するってことです。だからユーザはリクエストを実際のサーバ内部用にプロパゲートしたり とかいうコードを書く必要がなくなります。じゃあまずスタティックなリソースへのリクエストから:
これは超簡単。JSF のカスタムコンポーネントが複数集まったライブラリ jar の構造を思い浮かべてみてください。EJB jar みたいで、ejb-jar.xml の代わりに faces-config.xml と TLD ファイルが META-INF の下に入っているような感じです。スタティックなリソースは たぶん META-INF/resource-directory/yuta.js でしょう。これを取るには、コンポーネント中のレンダラーで↓みたくすればOK。

private static XhtmlHelper helper = new XhtmlHelper();

public void encodeEnd(FacesContext context, UIComponent component)
    throws IOException {
    
    ResponseWriter writer = context.getResponseWriter();

    //shale remoting resource retrieval
    helper.linkJavascript(context, component, writer,
        Mechanism.CLASS_RESOURCE, "/META-INF/r-dir/yuta.js");

出力された HTML を見てみると context root なんかが結合されたパスになってます。このリクエストが来ると Shale が自動的にリソースをとってくれます。
次はダイナミックリソース。
ダイナミックリソースというと、servlet が GET/POST リクエストでダイナミックな HTML を返すというのを思い浮かべますが、JSF のコンポーネントではおそらく Managed Bean のある特定のメソッドを使うのがいいと思います。 ダイナミックリソースの取得に Shale Remoting を使うにはレンダラ、faces-config.xml 、そしてリクエストを受け取る Managed Bean 三つのコーディングが必要です。

20060330 2006年 3月 30日 木曜日

Shale Remoting と JSF カスタムコンポーネント
Shale リモーティング - 最近のお気に入りです。
そもそもの始まりは、今 GlassFish上で AJAX 化した JSF カスタムコンポーネントを集めた ライブラリ jar を作っているんだけど、その中にあるクラスから同じライブラリ中 のリソース(JavaScript とかイメージとかいろいろ)を返す、consistent な方法は 何かっていう話をしてたわけです。 つまり、通常の web アプリ中に展開される場合は当然その中にあるリソースは WEB-INF の下を除いて URL でアクセス可能なのですが、スタンドアローンな jarファイル では URL でアクセスなんてできない。だから、カスタムコンポーネントの Renderer で HTML とかを出力する時にどのような URL を書き出したらいいかわからない ということになります。
これを解決する方法はいくつかあって、今までは PhaseListener を使ってねといろいろな場で言ってきました。↓こんな感じに↓

    public void afterPhase(PhaseEvent event) {
        String rootId = event.getFacesContext.getViewRoot().getViewId();
        if (rootId.endsWith("特定のJavaScriptファイルとかを指すID")) {
            handleResourceRequest(event, "/META-INF/コンポーネント名/ファイル名",
                "text/javascript");
        }
        ........
    }
    public void handleResourceRequest(PhaseEvent event, String resource, String contentType) {
        1). getClass().getResourceAsStream(resource);で実際のリソース取ってきて、
        2). event.getFacesContext().getExternalContext().getResponse();で
       HttpServletResponse 取って、
        3). リソース読み込んで response に setContentType()して、
            OutputStream に書き出す
     }
ただ、別のリソース を追加したりしたときの管理がけっこう面倒だったり、複数の人が別々のコンポーネント を作ってる時、当然 PhaseListener はそのコンポーネントの数だけ存在する場合が多く、 faces-config.xml に登録されたリスナはすべて起動されて結局同じ JavaScript を 複数回ロードしてしまうような結果をレンダーしてしまうとかいうけっこう厄介な問題が あります。
そうしたら、 Craig McClanahan 氏 が、「そりゃあんさん、Shale Remotingでんがな」とおっしゃって、試してみたところ、 簡単で美しい。好きになってしまいました。

うーん。。。導入部が長くなってしまいました。実際どうやるかは次のブログで。。。 Posted by yuta ( 3月 30日 2006年, 03:41:53 午後 PST ) Permalink 投稿されたコメント [0]


Valid HTML! Valid CSS!

This is a personal weblog, I do not speak for my employer.