001 /****************************************************************
002 * Licensed to the Apache Software Foundation (ASF) under one *
003 * or more contributor license agreements. See the NOTICE file *
004 * distributed with this work for additional information *
005 * regarding copyright ownership. The ASF licenses this file *
006 * to you under the Apache License, Version 2.0 (the *
007 * "License"); you may not use this file except in compliance *
008 * with the License. You may obtain a copy of the License at *
009 * *
010 * http://www.apache.org/licenses/LICENSE-2.0 *
011 * *
012 * Unless required by applicable law or agreed to in writing, *
013 * software distributed under the License is distributed on an *
014 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY *
015 * KIND, either express or implied. See the License for the *
016 * specific language governing permissions and limitations *
017 * under the License. *
018 ****************************************************************/
019
020
021 package org.apache.james.protocols.smtp.core;
022
023 import java.util.Date;
024
025 import org.apache.james.protocols.api.Response;
026 import org.apache.james.protocols.api.handler.ConnectHandler;
027 import org.apache.james.protocols.smtp.SMTPResponse;
028 import org.apache.james.protocols.smtp.SMTPRetCode;
029 import org.apache.james.protocols.smtp.SMTPSession;
030 import org.apache.mailet.base.RFC822DateFormat;
031
032 /**
033 * This ConnectHandler print the greeting on connecting
034 */
035 public class WelcomeMessageHandler implements ConnectHandler<SMTPSession> {
036
037 /**
038 * Static RFC822DateFormat used to generate date headers
039 */
040 private final static RFC822DateFormat rfc822DateFormat = new RFC822DateFormat();
041
042 /**
043 * @see org.apache.james.protocols.api.handler.ConnectHandler#onConnect(org.apache.james.protocols.api.ProtocolSession)
044 */
045 public Response onConnect(SMTPSession session) {
046 String smtpGreeting = session.getSMTPGreeting();
047
048 SMTPResponse welcomeResponse;
049 // if no greeting was configured use a default
050 if (smtpGreeting == null) {
051 // Initially greet the connector
052 // Format is: Sat, 24 Jan 1998 13:16:09 -0500
053 welcomeResponse = new SMTPResponse(SMTPRetCode.SERVICE_READY,
054 new StringBuilder(256)
055 .append(session.getHelloName())
056 .append(" SMTP Server (")
057 .append(getProductName())
058 .append(") ready ")
059 .append(rfc822DateFormat.format(new Date())));
060 } else {
061 welcomeResponse = new SMTPResponse(SMTPRetCode.SERVICE_READY,smtpGreeting);
062 }
063 return welcomeResponse;
064 }
065
066 protected String getProductName() {
067 return "JAMES SMTP Server";
068 }
069
070 }