
io.syndesis.connector.ftp.FtpVerifierExtension Maven / Gradle / Ivy
/*
* Copyright (C) 2016 Red Hat, Inc.
*
* Licensed 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 io.syndesis.connector.ftp;
import java.io.IOException;
import java.util.Map;
import org.apache.camel.CamelContext;
import org.apache.camel.component.extension.verifier.DefaultComponentVerifierExtension;
import org.apache.camel.component.extension.verifier.ResultBuilder;
import org.apache.camel.component.extension.verifier.ResultErrorBuilder;
import org.apache.camel.component.extension.verifier.ResultErrorHelper;
import org.apache.commons.net.ftp.FTPClient;
import org.apache.commons.net.ftp.FTPReply;
public class FtpVerifierExtension extends DefaultComponentVerifierExtension {
protected FtpVerifierExtension(String defaultScheme, CamelContext context) {
super(defaultScheme, context);
}
// *********************************
// Parameters validation
//
@Override
protected Result verifyParameters(Map parameters) {
ResultBuilder builder = ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.PARAMETERS)
.error(ResultErrorHelper.requiresOption("host", parameters))
.error(ResultErrorHelper.requiresOption("port", parameters));
if (builder.build().getErrors().isEmpty()) {
verifyCredentials(builder, parameters);
}
return builder.build();
}
// *********************************
// Connectivity validation
// *********************************
@Override
protected Result verifyConnectivity(Map parameters) {
return ResultBuilder.withStatusAndScope(Result.Status.OK, Scope.CONNECTIVITY)
.error(parameters, this::verifyCredentials).build();
}
@SuppressWarnings("PMD.NPathComplexity")
private void verifyCredentials(ResultBuilder builder, Map parameters) {
final String host = (String) parameters.get("host");
final int port = Integer.parseInt((String) parameters.get("port"));
final String userName = (parameters.get("username") == null) ? "anonymous"
: (String) parameters.get("username");
final String password = (parameters.get("password") == null) || "anonymous".equals(userName) ? ""
: (String) parameters.get("password");
int reply;
FTPClient ftp = new FTPClient();
String illegalParametersMessage = "Unable to connect to the FTP server";
boolean hasValidParameters = false;
try {
ftp.connect(host, port);
reply = ftp.getReplyCode();
hasValidParameters = FTPReply.isPositiveCompletion(reply);
} catch (IOException e) {
illegalParametersMessage = e.getMessage();
}
if (!hasValidParameters) {
builder.error(
ResultErrorBuilder.withCodeAndDescription(VerificationError.StandardCode.ILLEGAL_PARAMETER_VALUE,
illegalParametersMessage).parameterKey("host").parameterKey("port").build());
} else {
boolean isAuthenticated = false;
String authentionErrorMessage = "Authentication failed";
try {
isAuthenticated = ftp.login(userName, password);
} catch (IOException ioe) {
authentionErrorMessage = ioe.getMessage();
}
if (!isAuthenticated) {
builder.error(ResultErrorBuilder
.withCodeAndDescription(VerificationError.StandardCode.AUTHENTICATION, authentionErrorMessage)
.parameterKey("username").parameterKey("password").build());
} else {
try {
ftp.logout();
ftp.disconnect();
} catch (IOException ignored) {
// ignore
}
}
}
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy