/****************************************************************
 * Licensed to the Apache Software Foundation (ASF) under one   *
 * or more contributor license agreements.  See the NOTICE file *
 * distributed with this work for additional information        *
 * regarding copyright ownership.  The ASF licenses this file   *
 * to you 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.james.pop3server;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

import org.apache.james.protocols.api.Response;

/**
 * Contains an SMTP result
 */
public class POP3Response implements Response {

    // POP3 response prefixes
    /** OK response. Requested content will follow */
    public final static String OK_RESPONSE = "+OK";

    /**
     * Error response. Requested content will not be provided. This prefix is
     * followed by a more detailed error message.
     */
    public final static String ERR_RESPONSE = "-ERR";

    private String retCode = null;
    private List<CharSequence> lines = null;
    private String rawLine = null;
    private boolean endSession = false;

    /**
     * Construct a new POP3Response. The given code and description can not be
     * null, if null an IllegalArgumentException get thrown
     * 
     * @param code
     *            the returnCode
     * @param description
     *            the description
     */
    public POP3Response(String code, CharSequence description) {
        if (code == null)
            throw new IllegalArgumentException("POP3Response code can not be null");

        this.setRetCode(code);
        if (description == null) {
            description = "";
        }
        this.rawLine = code + " " + description;

        this.appendLine(description);

    }

    public POP3Response(String code) {
        this(code, null);
    }

    /**
     * Append the responseLine to the SMTPResponse
     * 
     * @param line
     *            the responseLine to append
     */
    public void appendLine(CharSequence line) {
        if (lines == null) {
            lines = new LinkedList<CharSequence>();
        }
        lines.add(line);
    }

    public String getRetCode() {
        return retCode;
    }

    public void setRetCode(String retCode) {
        this.retCode = retCode;
    }

    /**
     * Return a List of all responseLines stored in this POP3Response
     * 
     * @return all responseLines
     */
    public List<CharSequence> getLines() {
        List<CharSequence> responseList = new ArrayList<CharSequence>();
        for (int i = 0; i < lines.size(); i++) {
            if (i == 0) {
                responseList.add(getRetCode() + " " +lines.get(i));
            } else {
                responseList.add(lines.get(i));
            }
        }
        return responseList;
    }

    /**
     * Return the raw representation of the Stored POP3Response
     * 
     * @return rawLine the raw POP3Response
     */
    public String getRawLine() {
        return rawLine;
    }

    /**
     * Return true if the session is ended
     * 
     * @return true if session is ended
     */
    public boolean isEndSession() {
        return endSession;
    }

    /**
     * Set to true to end the session
     * 
     * @param endSession
     */
    public void setEndSession(boolean endSession) {
        this.endSession = endSession;
    }

    /**
     * @see java.lang.Object#toString()
     */
    public String toString() {
        return getRetCode() + " " + getLines();
    }
}

@@ -1253,37 +1253,8 @@
 st;%0A
-import java.util.LinkedList;%0A
 impo
@@ -1311,16 +1311,24 @@
 ols.api.
+Abstract
 Response
@@ -1349,20 +1349,20 @@
 ains an 
-SMTP
+POP3
  result%0A
@@ -1395,19 +1395,24 @@
 nse 
-implement
+extend
 s 
+Abstract
 Resp
@@ -1759,164 +1759,8 @@
 %22;%0A%0A
-    private String retCode = null;%0A    private List%3CCharSequence%3E lines = null;%0A    private String rawLine = null;%0A    private boolean endSession = false;%0A%0A
     
@@ -2107,292 +2107,20 @@
     
-if (code == null)%0A            throw new IllegalArgumentException(%22POP3Response code can not be null%22);%0A%0A        this.setRetCode(code);%0A        if (description == null) %7B%0A            description = %22%22;%0A        %7D%0A        this.rawLine = code + %22 %22 + description;%0A%0A        this.appendLine(
+super(code, 
 desc
@@ -2129,17 +2129,16 @@
 ption);%0A
-%0A
     %7D%0A%0A 
@@ -2212,465 +2212,8 @@
  %7D%0A%0A
-    /**%0A     * Append the responseLine to the SMTPResponse%0A     * %0A     * @param line%0A     *            the responseLine to append%0A     */%0A    public void appendLine(CharSequence line) %7B%0A        if (lines == null) %7B%0A            lines = new LinkedList%3CCharSequence%3E();%0A        %7D%0A        lines.add(line);%0A    %7D%0A%0A    public String getRetCode() %7B%0A        return retCode;%0A    %7D%0A%0A    public void setRetCode(String retCode) %7B%0A        this.retCode = retCode;%0A    %7D%0A
 %0A   
@@ -2728,704 +2728,6 @@
  %7D%0A%0A
-    /**%0A     * Return the raw representation of the Stored POP3Response%0A     * %0A     * @return rawLine the raw POP3Response%0A     */%0A    public String getRawLine() %7B%0A        return rawLine;%0A    %7D%0A%0A    /**%0A     * Return true if the session is ended%0A     * %0A     * @return true if session is ended%0A     */%0A    public boolean isEndSession() %7B%0A        return endSession;%0A    %7D%0A%0A    /**%0A     * Set to true to end the session%0A     * %0A     * @param endSession%0A     */%0A    public void setEndSession(boolean endSession) %7B%0A        this.endSession = endSession;%0A    %7D%0A%0A    /**%0A     * @see java.lang.Object#toString()%0A     */%0A    public String toString() %7B%0A        return getRetCode() + %22 %22 + getLines();%0A    %7D%0A
 %7D%0A

Add Abstract implementation of Response