
io.syndesis.connector.sheets.GoogleSheetsConnector 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.sheets;
import java.io.ByteArrayInputStream;
import java.util.Base64;
import java.util.Map;
import java.util.Optional;
import com.google.api.client.http.javanet.NetHttpTransport;
import com.google.api.services.sheets.v4.Sheets;
import io.syndesis.integration.component.proxy.ComponentDefinition;
import io.syndesis.integration.component.proxy.ComponentProxyComponent;
import org.apache.camel.Component;
import org.apache.camel.component.google.sheets.BatchGoogleSheetsClientFactory;
import org.apache.camel.component.google.sheets.GoogleSheetsClientFactory;
import org.apache.camel.component.google.sheets.GoogleSheetsComponent;
import org.apache.camel.component.google.sheets.stream.GoogleSheetsStreamComponent;
import org.apache.camel.util.ObjectHelper;
/**
* @author Christoph Deppisch
*/
public class GoogleSheetsConnector extends ComponentProxyComponent {
public static final String BEGIN_CERT = "-----BEGIN CERTIFICATE-----";
public static final String END_CERT = "-----END CERTIFICATE-----";
private String rootUrl;
private String serverCertificate;
private boolean validateCertificates = true;
public GoogleSheetsConnector(String componentId, String componentScheme) {
super(componentId, componentScheme);
}
@SuppressWarnings("PMD.SignatureDeclareThrowsException")
@Override
protected Optional createDelegateComponent(ComponentDefinition definition, Map options) throws Exception {
NetHttpTransport.Builder transport = new NetHttpTransport.Builder();
if (validateCertificates && ObjectHelper.isNotEmpty(serverCertificate)) {
byte [] decoded = Base64.getDecoder().decode(serverCertificate.replaceAll(BEGIN_CERT, "")
.replaceAll(END_CERT, ""));
transport.trustCertificatesFromStream(new ByteArrayInputStream(decoded));
} else {
transport.doNotValidateCertificate();
}
final GoogleSheetsClientFactory clientFactory = new BatchGoogleSheetsClientFactory(transport.build()) {
@Override
protected void configure(Sheets.Builder clientBuilder) {
clientBuilder.setRootUrl(rootUrl);
}
};
switch (getComponentScheme()) {
case "google-sheets-stream": {
GoogleSheetsStreamComponent component = new GoogleSheetsStreamComponent();
component.setClientFactory(clientFactory);
return Optional.of(component);
}
case "google-sheets": {
GoogleSheetsComponent component = new GoogleSheetsComponent();
component.setClientFactory(clientFactory);
return Optional.of(component);
}
default:
throw new IllegalArgumentException("Invalid component scheme for google sheets connector: " + getComponentScheme());
}
}
public String getRootUrl() {
return rootUrl;
}
/**
* Specifies the rootUrl.
*
* @param rootUrl
*/
public void setRootUrl(String rootUrl) {
this.rootUrl = rootUrl;
}
public String getServerCertificate() {
return serverCertificate;
}
/**
* Specifies the serverCertificate.
*
* @param serverCertificate
*/
public void setServerCertificate(String serverCertificate) {
this.serverCertificate = serverCertificate;
}
public boolean isValidateCertificates() {
return validateCertificates;
}
/**
* Specifies the validateCertificates.
*
* @param validateCertificates
*/
public void setValidateCertificates(boolean validateCertificates) {
this.validateCertificates = validateCertificates;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy