001 /* 002 * Copyright 2010 the original author or authors. 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, either express or implied. 013 * See the License for the specific language governing permissions and 014 * limitations under the License. 015 */ 016 package com.hs.mail.smtp; 017 018 import java.io.File; 019 import java.io.PrintStream; 020 import java.net.InetAddress; 021 import java.util.Random; 022 023 import com.hs.mail.container.config.Config; 024 import com.hs.mail.container.server.socket.TcpTransport; 025 import com.hs.mail.smtp.message.MailAddress; 026 import com.hs.mail.smtp.message.SmtpMessage; 027 028 /** 029 * 030 * @author Won Chul Doh 031 * @since May 30, 2010 032 * 033 */ 034 public class SmtpSession { 035 036 /** 037 * Static Random instance used to generate SMTP ids 038 */ 039 private final static Random random = new Random(); 040 041 private TcpTransport transport; 042 private InetAddress clientAddress; 043 private String clientDomain; 044 private String protocol; 045 private String smtpID; 046 private long authID = -1; 047 private boolean debug = false; 048 private PrintStream out; // debug output stream 049 private SmtpMessage message; 050 051 public SmtpSession(TcpTransport trans) { 052 this.transport = trans; 053 this.clientAddress = trans.getRemoteAddress(); 054 this.smtpID = random.nextInt(1024) + ""; 055 } 056 057 public void setDebug(boolean debug) { 058 this.debug = debug; 059 } 060 061 public synchronized void setDebugOut(PrintStream out) { 062 this.out = out; 063 } 064 065 public synchronized PrintStream getDebugOut() { 066 if (out == null) 067 return System.out; 068 else 069 return out; 070 } 071 072 public TcpTransport getTransport() { 073 return transport; 074 } 075 076 public InetAddress getClientAddress() { 077 return clientAddress; 078 } 079 080 public String getRemoteIP() { 081 return clientAddress.getHostAddress(); 082 } 083 084 public String getRemoteHost() { 085 return clientAddress.getHostName(); 086 } 087 088 public String getClientDomain() { 089 return clientDomain; 090 } 091 092 public void setClientDomain(String domain) { 093 this.clientDomain = domain; 094 } 095 096 public String getProtocol() { 097 return protocol; 098 } 099 100 public void setProtocol(String protocol) { 101 this.protocol = protocol; 102 } 103 104 public String getSessionID() { 105 return smtpID; 106 } 107 108 public long getAuthID() { 109 return authID; 110 } 111 112 public void setAuthID(long authID) { 113 this.authID = authID; 114 } 115 116 public SmtpMessage getMessage() { 117 return message; 118 } 119 120 public void setMessage(SmtpMessage message) { 121 this.message = message; 122 } 123 124 public void createSmtpMessage(MailAddress from) { 125 this.message = new SmtpMessage(from); 126 } 127 128 public void writeResponse(String response) { 129 transport.println(response); 130 if (debug) 131 getDebugOut().println(response); 132 } 133 134 }