• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

jreleaser / jreleaser / #477

04 Apr 2025 05:53PM UTC coverage: 35.124% (-5.1%) from 40.183%
#477

push

github

aalmiray
fix(deploy): Add missing Forgejo messages

Related to #1842

18210 of 51845 relevant lines covered (35.12%)

0.35 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

0.0
/sdks/jreleaser-ftp-java-sdk/src/main/java/org/jreleaser/sdk/ftp/FtpUtils.java
1
/*
2
 * SPDX-License-Identifier: Apache-2.0
3
 *
4
 * Copyright 2020-2025 The JReleaser authors.
5
 *
6
 * Licensed under the Apache License, Version 2.0 (the "License");
7
 * you may not use this file except in compliance with the License.
8
 * You may obtain a copy of the License at
9
 *
10
 *     https://www.apache.org/licenses/LICENSE-2.0
11
 *
12
 * Unless required by applicable law or agreed to in writing, software
13
 * distributed under the License is distributed on an "AS IS" BASIS,
14
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15
 * See the License for the specific language governing permissions and
16
 * limitations under the License.
17
 */
18
package org.jreleaser.sdk.ftp;
19

20
import org.apache.commons.net.ProtocolCommandEvent;
21
import org.apache.commons.net.ProtocolCommandListener;
22
import org.apache.commons.net.ftp.FTPClient;
23
import org.apache.commons.net.ftp.FTPReply;
24
import org.jreleaser.bundle.RB;
25
import org.jreleaser.model.internal.JReleaserContext;
26
import org.jreleaser.model.internal.common.Ftp;
27
import org.jreleaser.model.internal.common.TimeoutAware;
28
import org.jreleaser.model.internal.download.FtpDownloader;
29
import org.jreleaser.model.internal.upload.FtpUploader;
30
import org.jreleaser.model.spi.download.DownloadException;
31
import org.jreleaser.model.spi.upload.UploadException;
32

33
import java.io.IOException;
34

35
import static org.jreleaser.util.StringUtils.isBlank;
36

37
/**
38
 * @author Andres Almiray
39
 * @since 1.1.0
40
 */
41
public class FtpUtils {
42
    private FtpUtils() {
43
        // noop
44
    }
45

46
    public static FTPClient open(JReleaserContext context, FtpDownloader downloader) throws DownloadException {
47
        if (context.isDryrun()) return null;
×
48

49
        try {
50
            return ftpClient(context, downloader);
×
51
        } catch (IOException e) {
×
52
            throw new DownloadException(RB.$("ERROR_unexpected_download_from", downloader.getName()), e);
×
53
        }
54
    }
55

56
    public static FTPClient open(JReleaserContext context, FtpUploader uploader) throws UploadException {
57
        if (context.isDryrun()) return null;
×
58

59
        try {
60
            return ftpClient(context, uploader);
×
61
        } catch (IOException e) {
×
62
            throw new UploadException(RB.$("ERROR_unexpected_upload_to", uploader.getName()), e);
×
63
        }
64
    }
65

66
    private static <T extends Ftp & TimeoutAware> FTPClient ftpClient(JReleaserContext context, T ftp) throws IOException {
67
        FTPClient client = new FTPClient();
×
68
        client.setConnectTimeout(ftp.getConnectTimeout() * 1000);
×
69
        client.setSoTimeout(ftp.getReadTimeout() * 1000);
×
70

71
        client.addProtocolCommandListener(new FtpCommandListener(context));
×
72

73
        client.connect(ftp.getHost(), ftp.getPort());
×
74
        int reply = client.getReplyCode();
×
75
        if (!FTPReply.isPositiveCompletion(reply)) {
×
76
            client.disconnect();
×
77
            throw new IOException(RB.$("ERROR_unexpected_error"));
×
78
        }
79

80
        String username = ftp.getUsername();
×
81
        if (isBlank(username)) {
×
82
            username = "anonymous";
×
83
        }
84

85
        client.login(username, ftp.getPassword());
×
86
        reply = client.getReplyCode();
×
87
        if (!FTPReply.isPositiveCompletion(reply)) {
×
88
            client.disconnect();
×
89
            throw new IOException(RB.$("ERROR_login"));
×
90
        }
91

92
        return client;
×
93
    }
94

95
    public static void close(FtpUploader uploader, FTPClient ftp) throws UploadException {
96
        try {
97
            if (null != ftp) ftp.disconnect();
×
98
        } catch (IOException e) {
×
99
            throw new UploadException(RB.$("ERROR_disconnect", uploader.getName()), e);
×
100
        }
×
101
    }
×
102

103
    public static void close(FtpDownloader downloader, FTPClient ftp) throws DownloadException {
104
        try {
105
            if (null != ftp) ftp.disconnect();
×
106
        } catch (IOException e) {
×
107
            throw new DownloadException(RB.$("ERROR_disconnect", downloader.getName()), e);
×
108
        }
×
109
    }
×
110

111
    private static class FtpCommandListener implements ProtocolCommandListener {
112
        private static final String LOGIN = "LOGIN";
113

114
        private final JReleaserContext context;
115

116
        public FtpCommandListener(JReleaserContext context) {
×
117
            this.context = context;
×
118
        }
×
119

120
        @Override
121
        public void protocolCommandSent(ProtocolCommandEvent event) {
122
            StringBuilder msg = new StringBuilder("> ");
×
123

124
            String cmd = event.getCommand();
×
125
            if ("PASS".equalsIgnoreCase(cmd) || "USER".equalsIgnoreCase(cmd)) {
×
126
                msg.append(cmd).append(" *******");
×
127
            } else {
128
                if (LOGIN.equalsIgnoreCase(cmd)) {
×
129
                    String m = event.getMessage();
×
130
                    m = m.substring(0, msg.indexOf(LOGIN) + LOGIN.length());
×
131
                    msg.append(m).append(" *******");
×
132
                } else {
×
133
                    msg.append(event.getMessage().trim());
×
134
                }
135
            }
136

137
            context.getLogger().debug(msg.toString());
×
138
        }
×
139

140
        @Override
141
        public void protocolReplyReceived(ProtocolCommandEvent event) {
142
            context.getLogger().debug("< " + event.getMessage().trim());
×
143
            if (FTPReply.isNegativeTransient(event.getReplyCode()) ||
×
144
                FTPReply.isNegativePermanent(event.getReplyCode())) {
×
145
                throw new IllegalStateException(event.getMessage());
×
146
            }
147
        }
×
148
    }
149
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc