    001/*
    002 * Licensed to the Apache Software Foundation (ASF) under one or more
    003 * contributor license agreements.  See the NOTICE file distributed with
    004 * this work for additional information regarding copyright ownership.
    005 * The ASF licenses this file to You under the Apache License, Version 2.0
    006 * (the "License"); you may not use this file except in compliance with
    007 * the License.  You may obtain a copy of the License at
    008 *
    009 *      https://www.apache.org/licenses/LICENSE-2.0
    010 *
    011 * Unless required by applicable law or agreed to in writing, software
    012 * distributed under the License is distributed on an "AS IS" BASIS,
    013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    014 * See the License for the specific language governing permissions and
    015 * limitations under the License.
    016 */
    017package org.apache.commons.net;
    018
    019import java.io.ObjectInputStream;
    020import java.io.ObjectOutputStream;
    021import java.io.Serializable;
    022
    023import org.apache.commons.net.util.ListenerList;
    024
    025/**
    026 * ProtocolCommandSupport is a convenience class for managing a list of ProtocolCommandListeners and firing ProtocolCommandEvents. You can simply delegate
    027 * ProtocolCommandEvent firing and listener registering/unregistering tasks to this class.
    028 *
    029 * @see ProtocolCommandEvent
    030 * @see ProtocolCommandListener
    031 */
    032public class ProtocolCommandSupport implements Serializable {
    033
    034    /**
    035     * Serialization is unnecessary for this class. Reject attempts to do so until such time as the Serializable attribute can be dropped.
    036     */
    037    private static final long serialVersionUID = -8017692739988399978L;
    038
    039    /**
    040     * The source to use for all generated ProtocolCommandEvents.
    041     */
    042    private final Object source;
    043
    044    /**
    045     * The ProtocolCommandListener.
    046     */
    047    private final ListenerList<ProtocolCommandListener> listeners;
    048
    049    /**
    050     * Creates a ProtocolCommandSupport instance using the indicated source as the source of ProtocolCommandEvents.
    051     *
    052     * @param source The source to use for all generated ProtocolCommandEvents.
    053     */
    054    public ProtocolCommandSupport(final Object source) {
    055        this.listeners = new ListenerList<>();
    056        this.source = source;
    057    }
    058
    059    /**
    060     * Adds a ProtocolCommandListener.
    061     *
    062     * @param listener The ProtocolCommandListener to add.
    063     */
    064    public void addProtocolCommandListener(final ProtocolCommandListener listener) {
    065        listeners.addListener(listener);
    066    }
    067
    068    /**
    069     * Fires a ProtocolCommandEvent signaling the sending of a command to all registered listeners, invoking their
    070     * {@link org.apache.commons.net.ProtocolCommandListener#protocolCommandSent protocolCommandSent()} methods.
    071     *
    072     * @param command The string representation of the command type sent, not including the arguments (e.g., "STAT" or "GET").
    073     * @param message The entire command string verbatim as sent to the server, including all arguments.
    074     */
    075    public void fireCommandSent(final String command, final String message) {
    076        if (!listeners.isEmpty()) {
    077            final ProtocolCommandEvent event = new ProtocolCommandEvent(source, command, message);
    078            listeners.forEach(listener -> listener.protocolCommandSent(event));
    079        }
    080    }
    081
    082    /**
    083     * Fires a ProtocolCommandEvent signaling the reception of a command reply to all registered listeners, invoking their
    084     * {@link org.apache.commons.net.ProtocolCommandListener#protocolReplyReceived protocolReplyReceived()} methods.
    085     *
    086     * @param replyCode The integer code indicating the nature of the reply. This will be the protocol integer value for protocols that use integer reply codes,
    087     *                  or the reply class constant corresponding to the reply for protocols like POP3 that use strings like OK rather than integer codes (i.e.,
    088     *                  POP3Repy.OK).
    089     * @param message   The entire reply as received from the server.
    090     */
    091    public void fireReplyReceived(final int replyCode, final String message) {
    092        if (!listeners.isEmpty()) {
    093            final ProtocolCommandEvent event = new ProtocolCommandEvent(source, replyCode, message);
    094            listeners.forEach(listener -> listener.protocolReplyReceived(event));
    095        }
    096    }
    097
    098    /**
    099     * Gets the number of ProtocolCommandListeners currently registered.
    100     *
    101     * @return The number of ProtocolCommandListeners currently registered.
    102     */
    103    public int getListenerCount() {
    104        return listeners.getListenerCount();
    105    }
    106
    107    /**
    108     * Throws UnsupportedOperationException.
    109     *
    110     * @param ignored Ignored.
    111     */
    112    private void readObject(final ObjectInputStream ignored) {
    113        throw new UnsupportedOperationException("Serialization is not supported");
    114    }
    115
    116    /**
    117     * Removes a ProtocolCommandListener.
    118     *
    119     * @param listener The ProtocolCommandListener to remove.
    120     */
    121    public void removeProtocolCommandListener(final ProtocolCommandListener listener) {
    122        listeners.removeListener(listener);
    123    }
    124
    125    /**
    126     * Always throws {@link UnsupportedOperationException}.
    127     *
    128     * @param ignored ignored.
    129     * @throws UnsupportedOperationException Always thrown.
    130     */
    131    private void writeObject(final ObjectOutputStream ignored) {
    132        throw new UnsupportedOperationException("Serialization is not supported");
    133    }
    134}