📄 ftpwriter.java
字号:
// $Id: FtpWriter.java 306760 2005-10-06 11:42:47 +0530 (Thu, 06 Oct 2005) rana_b $
/*
* Copyright 2004 The Apache Software Foundation
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.ftpserver;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.OutputStreamWriter;
import java.io.StringReader;
import java.io.Writer;
import java.net.InetAddress;
import java.net.Socket;
import org.apache.commons.logging.Log;
import org.apache.ftpserver.ftplet.FileSystemView;
import org.apache.ftpserver.ftplet.FtpRequest;
import org.apache.ftpserver.ftplet.FtpResponse;
import org.apache.ftpserver.ftplet.FtpStatistics;
import org.apache.ftpserver.interfaces.ConnectionObserver;
import org.apache.ftpserver.interfaces.IFtpConfig;
import org.apache.ftpserver.interfaces.IMessageResource;
import org.apache.ftpserver.util.DateUtils;
import org.apache.ftpserver.util.IoUtils;
/**
* FTP response object. The server uses this to send server messages
*
* @author <a href="mailto:rana_b@yahoo.com">Rana Bhattacharyya</a>
*/
public
class FtpWriter implements FtpResponse {
private final static String CRLF = "\r\n";
///////////////////////// All Server Vatiables /////////////////////////
public static final String SERVER_IP = "server.ip";
public static final String SERVER_PORT = "server.port";
public static final String REQUEST_LINE = "request.line";
public static final String REQUEST_CMD = "request.cmd";
public static final String REQUEST_ARG = "request.arg";
public static final String STAT_START_TIME = "stat.start.time";
public static final String STAT_CON_TOTAL = "stat.con.total";
public static final String STAT_CON_CURR = "stat.con.curr";
public static final String STAT_LOGIN_TOTAL = "stat.login.total";
public static final String STAT_LOGIN_CURR = "stat.login.curr";
public static final String STAT_LOGIN_ANON_TOTAL = "stat.login.anon.total";
public static final String STAT_LOGIN_ANON_CURR = "stat.login.anon.curr";
public static final String STAT_FILE_UPLOAD_COUNT = "stat.file.upload.count";
public static final String STAT_FILE_UPLOAD_BYTES = "stat.file.upload.bytes";
public static final String STAT_FILE_DOWNLOAD_COUNT = "stat.file.download.count";
public static final String STAT_FILE_DOWNLOAD_BYTES = "stat.file.download.bytes";
public static final String STAT_FILE_DELETE_COUNT = "stat.file.delete.count";
public static final String STAT_DIR_CREATE_COUNT = "stat.dir.create.count";
public static final String STAT_DIR_DELETE_COUNT = "stat.dir.delete.count";
public static final String OUTPUT_CODE = "output.code";
public static final String OUTPUT_MSG = "output.msg";
public static final String CLIENT_IP = "client.ip";
public static final String CLIENT_CON_TIME = "client.con.time";
public static final String CLIENT_LOGIN_NAME = "client.login.name";
public static final String CLIENT_LOGIN_TIME = "client.login.time";
public static final String CLIENT_ACCESS_TIME = "client.access.time";
public static final String CLIENT_HOME = "client.home";
public static final String CLIENT_DIR = "client.dir";
/////////////////////////////////////////////////////////////////////////////
private Log m_log;
private Writer m_writer;
private ConnectionObserver m_observer;
private IFtpConfig m_fconfig;
private FtpRequest m_request;
/**
* Set the control socket.
*/
public void setControlSocket(Socket soc) throws IOException {
m_writer = new OutputStreamWriter(soc.getOutputStream(), "GB2312");//UTF-8 GB2312
}
/**
* Set ftp config.
*/
public void setFtpConfig(IFtpConfig fconfig) {
m_fconfig = fconfig;
m_log = m_fconfig.getLogFactory().getInstance(getClass());
}
/**
* Set ftp request.
*/
public void setFtpRequest(FtpRequest request) {
m_request = request;
}
/**
* Get the observer object to get what the server response.
*/
public void setObserver(ConnectionObserver observer) {
m_observer = observer;
}
/**
* Spy print. Monitor server response.
*/
private void spyResponse(String str) {
ConnectionObserver observer = m_observer;
if(observer != null) {
observer.response(str);
}
}
/**
* Generate and send ftp server response.
*/
public void send(int code, String subId, String basicMsg) throws IOException {
IMessageResource resource = m_fconfig.getMessageResource();
String lang = m_request.getLanguage();
String msg = resource.getMessage(code, subId, lang);
if(msg == null) {
m_log.error("Message not found : " + code + ',' + subId + ',' + lang);
msg = "";
}
msg = replaceVariables(code, basicMsg, msg);
write(code, msg);
}
/**
* Send the ftp server reply code to client.
*/
public void write(int code) throws IOException {
write(code, null);
}
/**
* Send the ftp code and message to client
*/
public void write(int code, String msg) throws IOException {
if(msg == null) {
msg = "";
}
msg = processNewLine(code, msg);
spyResponse(msg);
m_writer.write(msg);
m_writer.flush();
}
/**
* Close writer.
*/
public void close() {
IoUtils.close(m_writer);
}
/**
* Process ftp response new line character.
*/
private String processNewLine(int code, String msg) {
// no newline
if(msg.indexOf('\n') == -1) {
return code + " " + msg + CRLF;
}
StringBuffer buff = new StringBuffer(256);
try {
BufferedReader sr = new BufferedReader(new StringReader(msg));
buff.append(code);
buff.append('-');
String line = sr.readLine();
for(;;) {
String nextLine = sr.readLine();
if(nextLine != null) {
buff.append(line);
buff.append(CRLF);
}
else {
buff.append(code);
buff.append(' ');
buff.append(line);
buff.append(CRLF);
break;
}
line = nextLine;
}
sr.close();
}
catch(IOException ex) {
}
return buff.toString();
}
/**
* Replace server variables.
*/
private String replaceVariables(int code, String basicMsg, String str) {
int startIndex = 0;
int openIndex = str.indexOf('{', startIndex);
if (openIndex == -1) {
return str;
}
int closeIndex = str.indexOf('}', startIndex);
if( (closeIndex == -1) || (openIndex > closeIndex) ) {
return str;
}
StringBuffer sb = new StringBuffer(128);
sb.append(str.substring(startIndex, openIndex));
while(true) {
String varName = str.substring(openIndex+1, closeIndex);
sb.append( getVariableValue(code, basicMsg, varName) );
startIndex = closeIndex + 1;
openIndex = str.indexOf('{', startIndex);
if (openIndex == -1) {
sb.append(str.substring(startIndex));
break;
}
closeIndex = str.indexOf('}', startIndex);
if( (closeIndex == -1) || (openIndex > closeIndex) ) {
sb.append(str.substring(startIndex));
break;
}
sb.append(str.substring(startIndex, openIndex));
}
return sb.toString();
}
/**
* Get the variable value.
*/
public String getVariableValue(int code, String basicMsg, String varName) {
String varVal = null;
// all output variables
if(varName.startsWith("output.")) {
varVal = getOutputVariableValue(code, basicMsg, varName);
}
// all server variables
else if(varName.startsWith("server.")) {
varVal = getServerVariableValue(varName);
}
// all request variables
else if(varName.startsWith("request.")) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -