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 }