Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public static synchronized AlarmService getAlarmService() {
settings.getReferenceSettings().getAlarmServiceSettings().getID());
MessageBus messageBus = ProtocolComponentFactory.getInstance().getMessageBus(settings,
securityManager);
ContributorMediator contributorMediator = new SimpleContributorMediator(messageBus, settings, null, null);
ContributorMediator contributorMediator = new SimpleContributorMediator(messageBus, settings, null);
AlarmDAOFactory alarmDAOFactory = new AlarmDAOFactory();
AlarmStore store = alarmDAOFactory.getAlarmServiceDAOInstance(
settings.getReferenceSettings().getAlarmServiceSettings().getAlarmServiceDatabase());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ public static synchronized AuditTrailService getAuditTrailService() {
if (auditTrailService == null) {
ContributorMediator mediator = new SimpleContributorMediator(
ProtocolComponentFactory.getInstance().getMessageBus(settings, securityManager),
settings, null, ProtocolComponentFactory.getInstance().getFileExchange(settings));
settings, null);

PutFileClient putClient = ModifyComponentFactory.getInstance().retrievePutClient(settings,
securityManager, "audit-trail-preserver");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@
import org.bitrepository.protocol.messagebus.MessageBus;
import org.bitrepository.protocol.messagebus.MessageBusManager;
import org.bitrepository.protocol.security.SecurityManager;
import org.bitrepository.settings.referencesettings.ProtocolType;

import javax.jms.JMSException;
import java.io.File;
Expand Down Expand Up @@ -335,19 +336,26 @@ protected void deleteFileAfterwards(URL url) {
* @return The URL for the file.
*/
protected URL getURLOrUploadFile() {
FileExchange fileExchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
if (cmdHandler.hasOption(Constants.FILE_ARG)) {
File f = findTheFile();
FileExchange fileexchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
return fileexchange.putFile(f);
return fileExchange.putFile(f);
} else {
String urlArg = cmdHandler.getOptionValue(Constants.URL_ARG);

try {
final URL url = new URL(urlArg);
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();

if (responseCode > 399) {
throw new Exception("Http URL Connection ResponseCode: " + responseCode);
ProtocolType protocolType = ProtocolType.fromValue(url.getProtocol().toUpperCase(Locale.ROOT));
if (protocolType != ProtocolType.FILE) {
// Test if URL can actually be opened to exit early
// - otherwise checksum pillars will still receive and store checksum.
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
int responseCode = connection.getResponseCode();

if (responseCode > 399) {
throw new Exception("Http URL Connection ResponseCode: " + responseCode);
}
}

return url;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -144,8 +144,8 @@ private void downloadFile() {
} else {
outputFile = new File(cmdHandler.getOptionValue(Constants.FILE_ID_ARG));
}
FileExchange fileexchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
fileexchange.getFile(outputFile, fileUrl.toExternalForm());
FileExchange fileExchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
fileExchange.getFile(outputFile, fileUrl.toExternalForm());
}

/**
Expand All @@ -156,8 +156,8 @@ private void downloadFile() {
*/
private URL extractUrl(String fileID) {
try {
FileExchange fileexchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
return fileexchange.getURL(fileID);
FileExchange fileExchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
return fileExchange.getURL(fileID);
} catch (MalformedURLException e) {
throw new IllegalStateException("Could not make an URL for the file '" + fileID + "'.", e);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@
import org.bitrepository.modify.ModifyComponentFactory;
import org.bitrepository.modify.putfile.PutFileClient;
import org.bitrepository.protocol.FileExchange;
import org.bitrepository.protocol.ProtocolComponentFactory;
import org.bitrepository.protocol.utils.FileExchangeResolver;

import java.io.IOException;
import java.net.URL;
Expand Down Expand Up @@ -172,7 +172,7 @@ private OperationEvent putTheFile() {
final URL url = getURLOrUploadFile();

String fileID = retrieveFileID();
FileExchange fileExchange = ProtocolComponentFactory.getInstance().getFileExchange(settings);
FileExchange fileExchange = FileExchangeResolver.getBasicFileExchangeFromURL(url);

output.debug("Initiating the PutFile conversation.");
ChecksumDataForFileTYPE validationChecksum = getValidationChecksum();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ protected void createOptionsForCmdArgumentHandler() {
cmdHandler.addOption(fileOption);

Option urlOption = new Option(URL_ARG, HAS_ARGUMENT,
"The URL for the file to be retrieved. Is required, unless the actual file is given.");
"The URL for the file to replace with. Is required, unless the actual file is given.");
urlOption.setRequired(ARGUMENT_IS_NOT_REQUIRED);
cmdHandler.addOption(urlOption);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,28 +43,24 @@ public interface FileExchange {
*
* @param in The stream from the file to upload.
* @param url The URL where the stream is to be put.
* @throws IOException If any problems occurs during the transportation of
* the data.
* @throws IOException If any problems occurs during the transportation of the data.
*/
void putFile(InputStream in, URL url) throws IOException;

/**
* Places a given file onto the file exchange
*
* @param file The file to be put into the file exchange.
* @return The url for the file, when it has been placed onto the
* file exchange.
* @return The url for the file, when it has been placed onto the file exchange.
*/
URL putFile(File file);

/**
* Retrieves the data from a given url and puts it onto a given
* output-stream.
* Retrieves the data from a given url and puts it onto a given output-stream.
*
* @param out The output stream to put the data.
* @param url The url for where the data should be retrieved.
* @throws IOException If any problems occurs during the retrieval of the
* data.
* @throws IOException If any problems occurs during the retrieval of the data.
*/
void getFile(OutputStream out, URL url) throws IOException;

Expand All @@ -73,36 +69,32 @@ public interface FileExchange {
*
* @param url The URL to retrieve.
* @return The InputStream for the data at the given URL.
* @throws IOException If any problems occurs during the transportation of
* the data.
* @throws IOException If any problems occurs during the transportation of the data.
*/
InputStream getFile(URL url) throws IOException;

/**
* Method for downloading a file at a given address.
*
* @param outputFile The file where the data at the address should be
* placed.
* @param outputFile The file where the data at the address should be placed.
* @param fileAddress The address where the data should be downloaded from.
*/
void getFile(File outputFile, String fileAddress);

/**
* Creates the URL based on a filename.
* Creates the file-exchange URL based on a filename.
*
* @param filename The name of the piece of data to transfer (in the form
* of a file).
* @param filename The name of the piece of data to transfer (in the form of a file).
* @return The URL containing the filename.
* @throws MalformedURLException If the filename prevents the creation of
* a valid URL.
* @throws MalformedURLException If the filename prevents the creation of a valid URL.
*/
URL getURL(String filename) throws MalformedURLException;

/**
* Removes a file from the given URL.
*
* @param url The URL where the file should be removed from.
* @throws IOException If issues occurs while removing the file.
* @throws IOException If issues occur while removing the file.
* @throws URISyntaxException If the URL is not valid.
*/
void deleteFile(URL url) throws IOException, URISyntaxException;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,10 @@
*/
package org.bitrepository.protocol;

import org.bitrepository.common.ArgumentValidator;
import org.bitrepository.common.utils.FileUtils;
import org.bitrepository.common.utils.StreamUtils;
import org.bitrepository.settings.referencesettings.FileExchangeSettings;

import java.io.BufferedOutputStream;
import java.io.File;
Expand All @@ -38,10 +40,10 @@
* File exchange used for exchanging files on a local filesystem
*/
public class LocalFileExchange implements FileExchange {
private final File storageDir;
private final FileExchangeSettings settings;

public LocalFileExchange(String storageDir) {
this.storageDir = new File(storageDir);
public LocalFileExchange(FileExchangeSettings settings) {
this.settings = settings;
}

@Override
Expand All @@ -58,7 +60,7 @@ public void putFile(InputStream in, URL url) throws IOException {
@Override
public URL putFile(File dataFile) {
try {
File dest = new File(storageDir, new File(dataFile.toString()).getName());
File dest = new File(settings.getPath(), dataFile.getName());
FileUtils.copyFile(dataFile, dest);
return dest.toURI().toURL();
} catch (MalformedURLException e) {
Expand Down Expand Up @@ -98,7 +100,9 @@ public void getFile(File outputFile, String fileAddress) {

@Override
public URL getURL(String filename) throws MalformedURLException {
File dest = new File(storageDir, new File(filename).getName());
ArgumentValidator.checkNotNull(settings,
"The ReferenceSettings are missing the settings for the file exchange.");
File dest = new File(settings.getPath(), new File(filename).getName());
URL url;
try {
url = dest.toURI().toURL();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,20 +25,16 @@
package org.bitrepository.protocol;

import org.bitrepository.common.settings.Settings;
import org.bitrepository.protocol.http.HttpFileExchange;
import org.bitrepository.protocol.http.HttpsFileExchange;
import org.bitrepository.protocol.messagebus.MessageBus;
import org.bitrepository.protocol.messagebus.MessageBusManager;
import org.bitrepository.protocol.security.SecurityManager;
import org.bitrepository.settings.referencesettings.ProtocolType;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.bitrepository.protocol.utils.FileExchangeResolver;
import org.bitrepository.settings.referencesettings.FileExchangeSettings;

/**
* Provides access to the different component in the org.bitrepository.org.bitrepository.protocol module (Spring/IOC wannabe)
* Provides access to the different components in the org.bitrepository.protocol module (Spring/IOC wannabe)
*/
public final class ProtocolComponentFactory {
private static final Logger log = LoggerFactory.getLogger(ProtocolComponentFactory.class);
private static ProtocolComponentFactory instance;

public static synchronized ProtocolComponentFactory getInstance() {
Expand Down Expand Up @@ -72,26 +68,19 @@ public MessageBus getMessageBus(Settings settings, SecurityManager securityManag
}

/**
* Gets you an <code>FileExchange</code> instance for data communication. Is instantiated based on the
* Gets you a <code>FileExchange</code> instance for data communication. Is instantiated based on the
* configurations.
*
* @param settings The settings for the file exchange.
* @return The FileExchange according to the configuration.
*/
public FileExchange getFileExchange(Settings settings) {
if (fileExchange == null) {
if ((settings.getReferenceSettings().getFileExchangeSettings() != null)) {
ProtocolType protocolType = settings.getReferenceSettings().getFileExchangeSettings().getProtocolType();
if (protocolType == ProtocolType.HTTP) {
fileExchange = new HttpFileExchange(settings);
} else if (protocolType == ProtocolType.HTTPS) {
fileExchange = new HttpsFileExchange(settings);
} else if (protocolType == ProtocolType.FILE) {
fileExchange = new LocalFileExchange(
settings.getReferenceSettings().getFileExchangeSettings().getPath());
}
FileExchangeSettings fileExchangeSettings = settings.getReferenceSettings().getFileExchangeSettings();
if (fileExchangeSettings != null) {
fileExchange = FileExchangeResolver.getFileExchange(fileExchangeSettings);
} else {
fileExchange = new HttpFileExchange(settings);
throw new IllegalStateException("No FileExchangeSettings found in configuration");
}
}
return fileExchange;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ public class ActiveMQMessageBus implements MessageBus {
* The session for sending messages. Should not be the same as the consumer session,
* as sessions are not thread safe. This also means the session should be used in a synchronized manor.
* TODO Switch to use a session pool/producer poll to allow multithreaded message sending, see
* https://sbforge.org/jira/browse/BITMAG-357.
* https://sbforge.org/jira/browse/BITMAG-357.
*/
private final Session producerSession;

Expand Down Expand Up @@ -164,9 +164,7 @@ public class ActiveMQMessageBus implements MessageBus {
* @param settings The settings to use.
* @param securityManager The security manager to use for message authentication.
*/
public ActiveMQMessageBus(
Settings settings,
SecurityManager securityManager) {
public ActiveMQMessageBus(Settings settings, SecurityManager securityManager) {
configuration = settings.getMessageBusConfiguration();
log.info("Initializing ActiveMQMessageBus: '{}'", configuration);
this.securityManager = securityManager;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,6 @@
import org.apache.http.impl.conn.ChunkyManagedHttpClientConnectionFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.bitrepository.common.ArgumentValidator;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.common.utils.StreamUtils;
import org.bitrepository.protocol.CoordinationLayerException;
import org.bitrepository.protocol.FileExchange;
Expand Down Expand Up @@ -63,9 +62,9 @@ public class HttpFileExchange implements FileExchange {
private static final int HTTP_ERROR_CODE_BARRIER = 300;
protected int HTTP_BUFFER_SIZE = 1024 * 1024;
protected static final int HTTP_CHUNK_SIZE = 64 * 1024;
protected final Settings settings;
protected final FileExchangeSettings settings;

public HttpFileExchange(Settings settings) {
public HttpFileExchange(FileExchangeSettings settings) {
this.settings = settings;
}

Expand Down Expand Up @@ -123,14 +122,12 @@ public void getFile(File outputFile, String fileAddress) {
}

/**
* Retrieves the data from a given url and puts it onto a given
* output-stream. It has to be a 'HTTP' url, since the data is retrieved
* through a HTTP-request.
* Retrieves the data from a given url and puts it onto a given output-stream.
* It has to be a 'HTTP' url, since the data is retrieved through an HTTP-request.
*
* @param out The output stream to put the data.
* @param url The url for where the data should be retrieved.
* @throws IOException If any problems occurs during the retrieval of the
* data.
* @throws IOException If any problems occurs during the retrieval of the data.
*/
protected void performDownload(OutputStream out, URL url) throws IOException {
if (out == null || url == null) {
Expand Down Expand Up @@ -171,7 +168,6 @@ private void performUpload(InputStream in, URL url) throws IOException {
ArgumentValidator.checkNotNull(in, "InputStream in");
ArgumentValidator.checkNotNull(url, "URL url");


try (CloseableHttpClient httpClient = getHttpClient()) {
HttpPut httpPut = new HttpPut(url.toExternalForm());
InputStreamEntity reqEntity = new LargeChunkedInputStreamEntity(in);
Expand All @@ -181,8 +177,7 @@ private void performUpload(InputStream in, URL url) throws IOException {

// HTTP code >= 300 means error!
if (response.getStatusLine().getStatusCode() >= HTTP_ERROR_CODE_BARRIER) {
throw new IOException(
"Could not upload file to URL '" + url.toExternalForm() + "'. got status code '" +
throw new IOException("Could not upload file to URL '" + url.toExternalForm() + "'. got status code '" +
response.getStatusLine() + "'");
}
log.debug("Uploaded data-stream to url '{}' and received the response line '{}'",
Expand All @@ -193,13 +188,12 @@ private void performUpload(InputStream in, URL url) throws IOException {
@Override
public URL getURL(String filename) throws MalformedURLException {
ArgumentValidator.checkNotNullOrEmpty(filename, "String fileName");
ArgumentValidator.checkNotNull(settings.getReferenceSettings().getFileExchangeSettings(),
ArgumentValidator.checkNotNull(settings,
"The ReferenceSettings are missing the settings for the file exchange.");
FileExchangeSettings feSettings = settings.getReferenceSettings().getFileExchangeSettings();
String urlEncodedFilename = URLEncoder.encode(filename, StandardCharsets.UTF_8);

return new URL(feSettings.getProtocolType().value(), feSettings.getServerName(), feSettings.getPort().intValue(),
feSettings.getPath() + "/" + urlEncodedFilename);
return new URL(settings.getProtocolType().value(), settings.getServerName(), settings.getPort().intValue(),
settings.getPath() + "/" + urlEncodedFilename);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@
import org.apache.http.impl.conn.ChunkyManagedHttpClientConnectionFactory;
import org.apache.http.impl.conn.PoolingHttpClientConnectionManager;
import org.apache.http.impl.conn.SystemDefaultDnsResolver;
import org.bitrepository.common.settings.Settings;
import org.bitrepository.protocol.CoordinationLayerException;
import org.bitrepository.settings.referencesettings.FileExchangeSettings;

import javax.net.ssl.HostnameVerifier;
import javax.net.ssl.HttpsURLConnection;
Expand All @@ -51,7 +51,7 @@
public class HttpsFileExchange extends HttpFileExchange {
private final HostnameVerifier hostnameVerifier;

public HttpsFileExchange(Settings settings) {
public HttpsFileExchange(FileExchangeSettings settings) {
super(settings);
hostnameVerifier = NoopHostnameVerifier.INSTANCE;
}
Expand Down
Loading