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.fukurou.transfer;
017    
018    import java.io.BufferedOutputStream;
019    import java.io.File;
020    import java.io.FileOutputStream;
021    import java.io.IOException;
022    import java.io.InputStream;
023    import java.io.OutputStream;
024    
025    import org.opengion.fukurou.db.Transaction;
026    import org.opengion.fukurou.util.Closer;
027    import org.opengion.fukurou.util.StringUtil;
028    import org.opengion.fukurou.util.URLConnect;
029    
030    /**
031     * 伝é?要求に対してファイルを取得し、ローカルサーバã?に保存しますã?
032     *
033     * こã?実行方法ã?、読取方法がHTTP受信(ファイルä¸?¦§)(HTTP_FILELIST)のみ使用することができますã?
034     * HTTP受信(ファイルä¸?¦§)により取得されたファイルä¸?¦§のå�?ƒ•ァイルに対して、URL接続を行いã€?
035     * 実行対象でæŒ?®šされた保存å?にファイルを保存しますã?
036     *
037     * @og.group 伝é?シスãƒ?ƒ 
038     *
039     * @version  5.0
040     * @author   Hiroki.Nakamura
041     * @since    JDK1.6
042     */
043    public class TransferExec_FILEGET implements TransferExec {
044    
045            // リモートコントロールサーブレãƒ?ƒˆ
046            protected static final String REMOTE_SERVLET = "servlet/remoteControl";
047    
048            /**
049             * ファイルに書込みしますã?
050             *
051             * @param vals 伝é?ãƒ??タ(配å?)
052             * @param config 伝é?設定オブジェクãƒ?
053             * @param tran トランザクションオブジェクãƒ?
054             */
055            @Override
056            public void execute( final String[] vals, final TransferConfig config, final Transaction tran ) {
057                    if( vals == null || vals.length == 0 ) { return; }
058    
059                    String kbRead = config.getKbRead();
060                    if( !"HTTP_FILELIST".equals( kbRead ) ) {
061                            String errMsg = "実行方æ³?ファイル取å¾?FILEGET))を利用する場合ã?"
062                                                            + "読取方法ã?HTTP受信(ファイルä¸?¦§)(HTTP_FILELIST)を指定して下さã�??"
063                                                            + "KBREAD=[" + kbRead + "]";
064                            throw new RuntimeException( errMsg );
065                    }
066    
067                    String[] readObjArr = StringUtil.csv2Array( config.getReadObj(), ' ' );
068                    if( readObjArr[0] == null || readObjArr[0].length() == 0 ) {
069                            String errMsg = "受信å…?Ÿº準ディレクトリが取得できませんã€?READOBJ=" + config.getReadObj() + "]";
070                            throw new RuntimeException( errMsg );
071                    }
072                    File remoteFileDir = new File( readObjArr[0] );
073    
074                    String hostPort = readObjArr[1];
075                    if( hostPort == null || hostPort.length() == 0 ) {
076                            String errMsg = "受信ホスト名が取得できませんã€?READOBJ=" + config.getReadObj() + "]";
077                            throw new RuntimeException( errMsg );
078                    }
079    
080                    String saveBasePath = new File( config.getExecObj() ).getAbsolutePath();
081    
082                    for( String val : vals ) {
083                            String saveFileName = null;
084                            if( remoteFileDir.isDirectory() ) {
085                                    // 読取å?がディレクトリの場合ã?、保存基準ディレクトリに相対パス名を付加して保å­?
086                                    saveFileName = saveBasePath + val.replace( remoteFileDir.getAbsolutePath(), "" );
087                            }
088                            else {
089                                    // 読取å?がファイルの場合ã?、保存基準ディレクトリ?‹ファイル名で保å­?
090                                    String fileName = new File( val ).getName();
091                                    saveFileName = saveBasePath + File.separatorChar + fileName;
092                            }
093    
094                            File saveFile = new File( saveFileName );
095                            File parent = saveFile.getParentFile();
096                            if( !parent.exists() && !parent.mkdirs() ) {
097                                    String errMsg = "保存ディレクトリの作æ?に失敗しました。file=[" + saveFileName + "]";
098                                    throw new RuntimeException( errMsg );
099                            }
100    
101                            URLConnect conn = null;
102                            InputStream is = null;
103                            OutputStream os = null;
104                            try {
105                                    String url = hostPort + REMOTE_SERVLET + "?file=" + StringUtil.urlEncode( val );
106    
107                                    conn = new URLConnect( url, TransferConfig.HTTP_AUTH_USER_PASS );
108                                    if( config.getProxyHost() != null && config.getProxyHost().length() > 0 ) {
109                                            conn.setProxy( config.getProxyHost(),config.getProxyPort() );
110                                    }
111    
112                                    conn.setCharset( "UTF-8" );
113                                    conn.connect();
114                                    is = conn.getInputStream();
115    
116                                    os = new BufferedOutputStream( new FileOutputStream( saveFileName ) );
117                                    byte buf[] = new byte[4096];
118                                    int len = 0;
119                                    while( ( len = is.read( buf ) ) != -1 ) {
120                                            os.write( buf, 0 ,len );
121                                    }
122                                    os.flush();
123                            }
124                            catch( IOException ex ) {
125                                    String errMsg = "ファイル取得時にエラーが発生しました。file=[" + val + "]";
126                                    throw new RuntimeException( errMsg, ex );
127                            }
128                            finally {
129                                    Closer.ioClose( os );
130                                    Closer.ioClose( is );
131    
132                                    if( conn != null ) { conn.disconnect(); }
133                            }
134                    }
135            }
136    }