Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
Minor refactoring to address sonar-related issues including:
- Throws declaration should not be redundant (RuntimeException unneeded in function signature: FcrepoTransactionManager) - Class Data Abstraction Coupling (FcrepoClient constructor logic moved into FcrepoHttpClientBuilder class) - Class Fan Out Complexity (HttpRequestBase instance creation moved to HttpMethods class) - Generic exceptions should never be thrown (FcrepoProducer now throws a TransactionSystemException) - Public types should be documented (processor.* classes now have full documentation) - String literals should not be duplicated (FcrepoClient debug messages are now more specific) - FcrepoTransactionManager thrown exceptions are now more specific Resolves: https://jira.duraspace.org/browse/FCREPO-1397
- Loading branch information
Showing
14 changed files
with
171 additions
and
96 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
88 changes: 88 additions & 0 deletions
88
src/main/java/org/fcrepo/camel/FcrepoHttpClientBuilder.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
/** | ||
* Copyright 2015 DuraSpace, 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 org.fcrepo.camel; | ||
|
||
import static org.apache.commons.lang3.StringUtils.isBlank; | ||
import static org.slf4j.LoggerFactory.getLogger; | ||
|
||
import org.apache.http.HttpHost; | ||
import org.apache.http.auth.AuthScope; | ||
import org.apache.http.auth.UsernamePasswordCredentials; | ||
import org.apache.http.client.CredentialsProvider; | ||
import org.apache.http.impl.client.BasicCredentialsProvider; | ||
import org.apache.http.impl.client.CloseableHttpClient; | ||
import org.apache.http.impl.client.HttpClients; | ||
import org.slf4j.Logger; | ||
|
||
/** | ||
* A utility class for building an httpclient for interacting with a Fedora repository | ||
* | ||
* @author Aaron Coburn | ||
* @since March 9, 2015 | ||
*/ | ||
public class FcrepoHttpClientBuilder { | ||
|
||
private String username; | ||
|
||
private String password; | ||
|
||
private String host; | ||
|
||
private static final Logger LOGGER = getLogger(FcrepoHttpClientBuilder.class); | ||
|
||
/** | ||
* Create a FcrepoHttpClientBuilder object with which it is possible to create | ||
* an HttpClient object | ||
* | ||
* @param username an optional username for authentication | ||
* @param password an optional password for authentication | ||
* @param host an optional realm for authentication | ||
*/ | ||
public FcrepoHttpClientBuilder(final String username, final String password, final String host) { | ||
this.username = username; | ||
this.password = password; | ||
this.host = host; | ||
} | ||
|
||
/** | ||
* Build an HttpClient | ||
* | ||
* @return an HttpClient | ||
*/ | ||
public CloseableHttpClient build() { | ||
|
||
if (isBlank(username) || isBlank(password)) { | ||
return HttpClients.createDefault(); | ||
} else { | ||
LOGGER.debug("Accessing fcrepo with user credentials"); | ||
|
||
final CredentialsProvider credsProvider = new BasicCredentialsProvider(); | ||
AuthScope scope = null; | ||
|
||
if (isBlank(host)) { | ||
scope = new AuthScope(AuthScope.ANY); | ||
} else { | ||
scope = new AuthScope(new HttpHost(host)); | ||
} | ||
credsProvider.setCredentials( | ||
scope, | ||
new UsernamePasswordCredentials(username, password)); | ||
return HttpClients.custom() | ||
.setDefaultCredentialsProvider(credsProvider) | ||
.build(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.