111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert/*
211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * Copyright 2008 the original author or authors.
311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert *
411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * Licensed under the Apache License, Version 2.0 (the "License");
511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * you may not use this file except in compliance with the License.
611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * You may obtain a copy of the License at
711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert *
811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert *      http://www.apache.org/licenses/LICENSE-2.0
911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert *
1011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * Unless required by applicable law or agreed to in writing, software
1111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * distributed under the License is distributed on an "AS IS" BASIS,
1211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * See the License for the specific language governing permissions and
1411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * limitations under the License.
1511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert */
1611cd02dfb91661c65134cac258cf5924270e9d2Dan Albertpackage org.mockftpserver.fake.command;
1711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
1811cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.core.command.Command;
1911cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.core.command.ReplyCodes;
2011cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.core.session.Session;
2111cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.core.session.SessionKeys;
2211cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.core.util.IoUtil;
2311cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.fake.filesystem.FileEntry;
2411cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.fake.filesystem.FileSystemEntry;
2511cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport org.mockftpserver.fake.filesystem.FileSystemException;
2611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
2711cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport java.io.ByteArrayOutputStream;
2811cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport java.io.IOException;
2911cd02dfb91661c65134cac258cf5924270e9d2Dan Albertimport java.io.InputStream;
3011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
3111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert/**
3211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * CommandHandler for the RETR command. Handler logic:
3311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <ol>
3411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>If the user has not logged in, then reply with 530 and terminate</li>
3511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>If the required pathname parameter is missing, then reply with 501 and terminate</li>
3611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>If the pathname parameter does not specify a valid, existing filename, then reply with 550 and terminate</li>
3711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>If the current user does not have read access to the file at the specified path or execute permission to its directory, then reply with 550 and terminate</li>
3811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>Send an initial reply of 150</li>
3911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>Send the contents of the named file across the data connection</li>
4011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>If there is an error reading the file, then reply with 550 and terminate</li>
4111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * <li>Send a final reply with 226</li>
4211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * </ol>
4311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert *
4411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * @author Chris Mair
4511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert * @version $Revision$ - $Date$
4611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert */
4711cd02dfb91661c65134cac258cf5924270e9d2Dan Albertpublic class RetrCommandHandler extends AbstractFakeCommandHandler {
4811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
4911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert    protected void handle(Command command, Session session) {
5011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        verifyLoggedIn(session);
5111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        this.replyCodeForFileSystemException = ReplyCodes.READ_FILE_ERROR;
5211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
5311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        String path = getRealPath(session, command.getRequiredParameter(0));
5411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        FileSystemEntry entry = getFileSystem().getEntry(path);
5511cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        verifyFileSystemCondition(entry != null, path, "filesystem.doesNotExist");
5611cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        verifyFileSystemCondition(!entry.isDirectory(), path, "filesystem.isNotAFile");
5711cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        FileEntry fileEntry = (FileEntry) entry;
5811cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
5911cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        // User must have read permission to the file
6011cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        verifyReadPermission(session, path);
6111cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
6211cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        // User must have execute permission to the parent directory
6311cd02dfb91661c65134cac258cf5924270e9d2Dan Albert        verifyExecutePermission(session, getFileSystem().getParent(path));
6411cd02dfb91661c65134cac258cf5924270e9d2Dan Albert
65        sendReply(session, ReplyCodes.TRANSFER_DATA_INITIAL_OK);
66        InputStream input = fileEntry.createInputStream();
67        session.openDataConnection();
68        byte[] bytes = null;
69        try {
70            bytes = IoUtil.readBytes(input);
71        }
72        catch (IOException e) {
73            LOG.error("Error reading from file [" + fileEntry.getPath() + "]", e);
74            throw new FileSystemException(fileEntry.getPath(), null, e);
75        }
76        finally {
77            try {
78                input.close();
79            }
80            catch (IOException e) {
81                LOG.error("Error closing InputStream for file [" + fileEntry.getPath() + "]", e);
82            }
83        }
84
85        if (isAsciiMode(session)) {
86            bytes = convertLfToCrLf(bytes);
87        }
88        session.sendData(bytes, bytes.length);
89        session.closeDataConnection();
90        sendReply(session, ReplyCodes.TRANSFER_DATA_FINAL_OK);
91    }
92
93    /**
94     * Within the specified byte array, replace all LF (\n) that are NOT preceded by a CR (\r) into CRLF (\r\n).
95     *
96     * @param bytes - the bytes to be converted
97     * @return the result of converting LF to CRLF
98     */
99    protected byte[] convertLfToCrLf(byte[] bytes) {
100        ByteArrayOutputStream out = new ByteArrayOutputStream();
101        char lastChar = ' ';
102        for (int i = 0; i < bytes.length; i++) {
103            char ch = (char) bytes[i];
104            if (ch == '\n' && lastChar != '\r') {
105                out.write('\r');
106                out.write('\n');
107            } else {
108                out.write(bytes[i]);
109            }
110            lastChar = ch;
111        }
112        return out.toByteArray();
113    }
114
115    private boolean isAsciiMode(Session session) {
116        // Defaults to true
117        return session.getAttribute(SessionKeys.ASCII_TYPE) != Boolean.FALSE;
118    }
119
120}