<commit_before>/*
 * jETeL/CloverETL - Java based ETL application framework.
 * Copyright (c) Javlin, a.s. (<EMAIL>)
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jetel.component.fileoperation.pool;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.jetel.component.fileoperation.PrimitiveS3OperationHandler;
import org.jetel.component.fileoperation.URIUtils;
import org.jetel.util.protocols.Validable;
import org.jets3t.service.Constants;
import org.jets3t.service.Jets3tProperties;
import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.security.AWSCredentials;

/**
 * @author krivanekm (<EMAIL>)
 *         (c) Javlin, a.s. (www.cloveretl.com)
 *
 * @created 18. 3. 2015
 */
public class PooledS3Connection extends AbstractPoolableConnection implements Validable {

	/*
	 * Jets3tProperties configuration keys
	 */
	private static final String S3SERVICE_S3_ENDPOINT = "s3service.s3-endpoint";
	private static final String S3SERVICE_S3_ENDPOINT_HTTP_PORT = "s3service.s3-endpoint-http-port";
	private static final String S3SERVICE_S3_ENDPOINT_HTTPS_PORT = "s3service.s3-endpoint-https-port";

	/**
	 * Shared instance, do not modify!
	 */
	private static final Jets3tProperties DEFAULTS = Jets3tProperties.getInstance(Constants.JETS3T_PROPERTIES_FILENAME);
	
	/**
	 * Shared instance for all connections to the same authority,
	 * should be thread-safe.
	 */
	private S3Service service;
	
	/**
	 * s3://access_key_id:secret_access_key@hostname:port/
	 * 
	 * - hostname does not include bucket name
	 */
	private URI baseUri;

	/**
	 * @param authority
	 */
	protected PooledS3Connection(S3Authority authority) {
		super(authority);
		
		URI uri = authority.getUri();
		StringBuilder sb = new StringBuilder();
		sb.append(uri.getScheme()).append("://");
		sb.append(uri.getRawAuthority()).append('/'); // do not decode escape sequences
		this.baseUri = URI.create(sb.toString());
	}
	
	@Override
	public boolean isOpen() {
		return (service != null) && !service.isShutdown();
	}
	
	public void init() throws IOException {
		this.service = createService((S3Authority) authority);
		validate();
	}
	
	@Override
	public void validate() throws IOException {
		try {
			// validate connection
			service.listAllBuckets();
		} catch (S3ServiceException e) {
			throw new IOException("Connection validation failed", PrimitiveS3OperationHandler.getIOException(e));
		}
	}<commit_msg>FIX: CLO-1512 - S3: Empty key error message improved.<commit_after>/*
 * jETeL/CloverETL - Java based ETL application framework.
 * Copyright (c) Javlin, a.s. (<EMAIL>)
 *  
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 */
package org.jetel.component.fileoperation.pool;

import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.URI;

import org.jetel.component.fileoperation.PrimitiveS3OperationHandler;
import org.jetel.component.fileoperation.URIUtils;
import org.jetel.util.protocols.Validable;
import org.jets3t.service.Constants;
import org.jets3t.service.Jets3tProperties;
import org.jets3t.service.S3Service;
import org.jets3t.service.S3ServiceException;
import org.jets3t.service.ServiceException;
import org.jets3t.service.impl.rest.httpclient.RestS3Service;
import org.jets3t.service.security.AWSCredentials;

/**
 * @author krivanekm (<EMAIL>)
 *         (c) Javlin, a.s. (www.cloveretl.com)
 *
 * @created 18. 3. 2015
 */
public class PooledS3Connection extends AbstractPoolableConnection implements Validable {

	/*
	 * Jets3tProperties configuration keys
	 */
	private static final String S3SERVICE_S3_ENDPOINT = "s3service.s3-endpoint";
	private static final String S3SERVICE_S3_ENDPOINT_HTTP_PORT = "s3service.s3-endpoint-http-port";
	private static final String S3SERVICE_S3_ENDPOINT_HTTPS_PORT = "s3service.s3-endpoint-https-port";

	/**
	 * Shared instance, do not modify!
	 */
	private static final Jets3tProperties DEFAULTS = Jets3tProperties.getInstance(Constants.JETS3T_PROPERTIES_FILENAME);
	
	/**
	 * Shared instance for all connections to the same authority,
	 * should be thread-safe.
	 */
	private S3Service service;
	
	/**
	 * s3://access_key_id:secret_access_key@hostname:port/
	 * 
	 * - hostname does not include bucket name
	 */
	private URI baseUri;

	/**
	 * @param authority
	 */
	protected PooledS3Connection(S3Authority authority) {
		super(authority);
		
		URI uri = authority.getUri();
		StringBuilder sb = new StringBuilder();
		sb.append(uri.getScheme()).append("://");
		sb.append(uri.getRawAuthority()).append('/'); // do not decode escape sequences
		this.baseUri = URI.create(sb.toString());
	}
	
	@Override
	public boolean isOpen() {
		return (service != null) && !service.isShutdown();
	}
	
	public void init() throws IOException {
		this.service = createService((S3Authority) authority);
		validate();
	}
	
	@Override
	public void validate() throws IOException {
		try {
			// validate connection
			service.listAllBuckets();
		} catch (S3ServiceException e) {
			if (e.getCause() instanceof IllegalArgumentException) {
				if ("Empty key".equals(e.getCause().getMessage())) {
					throw new IOException("S3 URL does not contain valid keys. Please supply access key and secret key in the following format: s3://<AccessKey:SecretKey>@s3.amazonaws.com/<bucket>", PrimitiveS3OperationHandler.getIOException(e));
				}
			}
			throw new IOException("Connection validation failed", PrimitiveS3OperationHandler.getIOException(e));
		}
	}
{'fraction_non_alphanumeric': 0.10214824299331342, 'fraction_numerical': 0.018068003983496943, 'mean_word_length': 4.420200462606014, 'pattern_counts': {'":': 2, '<': 9, '<?xml version=': 0, '>': 9, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1698379', 'n_tokens_mistral': 2235, 'n_tokens_neox': 2059, 'n_words': 618}