001/*
002 * Copyright (c) 2009 The openGion Project.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND,
013 * either express or implied. See the License for the specific language
014 * governing permissions and limitations under the License.
015 */
016package org.opengion.hayabusa.html;
017
018/**
019 * CreateToken インターフェース の実装Abstractクラスです。
020 * これを,共通のスーパークラスとして 各種トークン作成に使います。
021 *
022 * 各種トークンに対応したサブクラス上でgenerateURLをオーバーライドして下さい。
023 *
024 * @og.group 画面表示
025 * @og.rev 5.8.2.1 (2014/12/13) 新規作成
026 *
027 * @version  4.0
028 * @author       Kazuhiko Hasegawa
029 * @since    JDK5.0,
030 */
031public abstract class AbstractCreateToken implements CreateToken {
032
033        /**
034         * デフォルトコンストラクター
035         *
036         * @og.rev 6.4.2.0 (2016/01/29) PMD refactoring. Each class should declare at least one constructor.
037         */
038        protected AbstractCreateToken() { super(); }            // これも、自動的に呼ばれるが、空のメソッドを作成すると警告されるので、明示的にしておきます。
039
040        /**
041         * トークン付のURLを返します。
042         * 通常はこのメソッドをオーバーライドします。
043         *
044         * @og.rev 6.1.1.0 (2015/01/17) 可変長配列にメソッドを統合。
045         *
046         * @param       inURL   トークンを付加するURL
047         * @param   time        有効期限(millis)
048         * @param       user    ユーザ
049         * @param       param   パラメータ配列(可変長引数)
050         *
051         * @return      トークン付のURL
052         */
053        public String generateURL( final String inURL, final long time, final String user, final String... param ) {
054                return inURL;
055        }
056
057        /**
058         * Aタグの文字列を解析して、hrefの最後にトークンを付加します。
059         * URLチェックよりも後ろにトークンが付く形になるので併用は可能です。
060         *
061         * @og.rev 6.1.1.0 (2015/01/17) 可変長配列にメソッドを統合。
062         *
063         * @param   tag         Aタグ文字列
064         * @param   time        有効期限(millis)
065         * @param       user    ユーザ
066         * @param       param   パラメータ配列(可変長引数)
067         *
068         * @return  トークンが付加されたAタグ文字列
069         */
070        public String embedToken( final String tag, final long time, final String user, final String... param  ) {
071                String rtn = tag;
072                final int hrefStr = rtn.indexOf( "href=\"" );
073                if( hrefStr >= 0 ) {
074                        final int hrefEnd = rtn.indexOf( "\"",hrefStr + 6 );
075                        if( hrefEnd >= 0 ) {
076                                String href = rtn.substring( hrefStr + 6, hrefEnd );
077                                href = generateURL( href, time, user, param );
078                                rtn = rtn.substring( 0,  hrefStr ) + "href=\"" + href + rtn.substring( hrefEnd );
079                        }
080                }
081                return rtn;
082        }
083}