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     */
016    package org.opengion.plugin.token;
017    
018    import java.io.BufferedReader;
019    import java.io.IOException;
020    import java.net.URLDecoder;
021    import java.net.URLEncoder;
022    
023    import org.opengion.fukurou.util.LogWriter;
024    import org.opengion.fukurou.util.StringUtil;
025    import org.opengion.fukurou.util.URLConnect;
026    import org.opengion.hayabusa.common.HybsSystem;
027    import org.opengion.hayabusa.html.AbstractCreateToken;
028    
029    import aspera.tokengen.ASTokenGen;
030    /**
031     * Asperaのト?クンを付加するためのプラグインです?
032     * 
033     * 毎回tokengenをcallするため利用する場合?若干時間がかかります?
034     * 
035     * 
036     * 以下をシス?リソースで設定可能で?
037     * AS_NODE_USER         : ノ?ドユーザ。?期?はnode_user
038     * AS_NODE_PASS         : ノ?ドユーザのパスワード?初期値はnode_pass
039     * AS_NODE_URL          : ノ?ド?アドレス。?期?はhttp://devdemo.asperasoft.com:9092
040     * 
041     * tokenを利用した認証につ?はAsperaの開発?イ?
042     * https://developer.asperasoft.com/Home
043     *  Home
044     *  >  GENERAL REFERENCE
045     *  >  Authentication and authorization in FASP transfers
046     *  >  Authorization
047     * を参照してください?
048     * 
049     * Home
050     * >  WEB APIs
051     * >  Node
052     * >  v1
053     * >  Rest API - Transfer Operations
054     * でNodeAPIにつ?の解説があります?
055     *
056     * @og.group 画面表示
057     * @og.rev 5.8.2.2 (2014/12/19)
058     *
059     * @version  5.0
060     * @author       Takahashi Masakazu
061     * @since    JDK5.0,
062     */
063    public class CreateToken_Aspera extends AbstractCreateToken {
064            //* こ?プログラ??VERSION??を設定します?       {@value} */
065            private static final String VERSION = "5.8.2.1 (2014/12/13)" ;
066            
067            private static final String AS_NODE_USER         = StringUtil.nval(HybsSystem.sys( "AS_TOKEN_USER " ),"node_user" );
068            private static final String AS_NODE_PASS         = StringUtil.nval(HybsSystem.sys( "AS_TOKEN_USER " ),"node_pass" );
069            private static final String AS_NODE_URL  = StringUtil.nval(HybsSystem.sys( "AS_NODE_URL " ),"http://devdemo.asperasoft.com:9092" );
070            
071            private static String AS_NODE_API = AS_NODE_URL + "/files/download_setup";
072            private URLConnect conn                 = null;
073            
074    //      private ASTokenGen generator; // static化するとセキュリ?上危険な可能性がある?で?ておく(transferPathの削除が?来な?ぽ??
075            
076            /**
077             * ?ォルトコンストラクター?
078             *
079             */
080            public CreateToken_Aspera(){
081    //      ト?クンを作?する場?
082    //              generator = new ASTokenGen( AS_NODE_USER, null, Direction.DOWNLOAD);
083    //              generator.setAsTokenGenPath( AS_TOKENGEN_PATH );
084    //              generator.setEncryptionKey( AS_TOKEN_ENCR );
085                    
086                    conn = new URLConnect(AS_NODE_API ,AS_NODE_USER +":"+AS_NODE_PASS);
087                    conn.setRequestProperty( "Content-Type,Accept", "application/json; charset=UTF8,application/json" );
088            }
089    
090    
091            /**
092             * アスペラのト?クンを付けたURLを返します?
093             *
094             *
095             * @return  URL
096             */
097            @Override
098            public String  generateURL( final String inURL, final long time, final String user, final String[] param ){
099    //              tokenGeneratorでのト?クン作?方?
100    //              generator.addTransferPath( inURL ); 
101    //      generator.setLifeSeconds(3600);
102    //        ASToken token = generator.generateASToken();
103    //      if( token == null ){
104    //              System.out.println("Aspera token is null : " + inURL);
105    //              return inURL;
106    //      }
107    //              token.toString();
108            
109            // AsperaNodeAPIを利用してト?クン付JSONを取得す?
110            // ユーザはト?クン用の共通ユーザを利用してconnectする?
111                    StringBuffer rtnMessage = new StringBuffer("");
112            try{
113                    String encURL = URLEncoder.encode( inURL, "UTF-8" );
114                    //String encURL = inURL;
115                conn.setPostData( "{ \"transfer_requests\" : [ { \"transfer_request\" : { \"paths\" : [ { \"source\" : \""+encURL+"\" } ] } } ] }" );
116    
117                            conn.connect();
118                            
119                            BufferedReader reader = conn.getReader();
120                String s;
121                while ((s = reader.readLine()) != null) {
122                    rtnMessage.append( URLDecoder.decode( s, "UTF-8" ) );
123                }
124                reader.close();
125    
126            }
127            catch( IOException ex ) {
128                    System.out.println(ex);
129                            String errMsg = "Daemon_URLConnect:??タ取得中にエラーが発生しました? + HybsSystem.CR
130                                                    + " inURL=[" + inURL + "]" + HybsSystem.CR
131                                                    + ex;
132                            LogWriter.log( errMsg );
133                            rtnMessage = new StringBuffer(inURL);
134                    }
135            catch( Exception ex){
136                    System.out.println(ex);
137                            String errMsg = "Daemon_URLConnect:??タ取得中にエラーが発生しました? + HybsSystem.CR
138                                                    + " inURL=[" + inURL + "]" + HybsSystem.CR
139                                                    + ex;
140                            LogWriter.log( errMsg );
141                            rtnMessage = new StringBuffer(inURL);
142            }
143                    finally {
144                            if( conn != null ) { conn.disconnect(); }
145                    }
146            
147                    // エラー時も無視してそ?ままJSON形式を返却する?
148                    // aspera用のJavaScripptを画面に読み込ませておく?あり?
149                    
150                    
151                    return "javascript:fileControls.selectFolder(" + rtnMessage.toString().replace( "\"", """ ) + ")";
152            }       
153    
154    }