-
Notifications
You must be signed in to change notification settings - Fork 20
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
First draft of a simple auth-aware HttpClient service #40
Merged
Merged
Changes from 4 commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
3dde92c
First draft of a simple auth-aware HttpClient service
ajs6f bdb030f
Small improvements
ajs6f da190bf
Fix checkstyle problems
ajs6f dd75cca
Making checkstyle problems fail the build
ajs6f 4b5ce97
Corrections to Karaf feature file and blueprint.xml
ajs6f File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -7,3 +7,5 @@ | |
workbench.xmi | ||
target | ||
build | ||
.DS_Store | ||
bin/ |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
apply plugin: 'osgi' | ||
|
||
description = 'Islandora CLAW HTTP Client' | ||
|
||
dependencies { | ||
compile group: 'org.apache.httpcomponents', name: 'httpclient', version: '4.5.3' | ||
|
||
testCompile group: 'junit', name: 'junit', version:'4.12' | ||
} | ||
|
||
jar { | ||
manifest { | ||
description project.description | ||
docURL project.docURL | ||
vendor project.vendor | ||
license project.license | ||
|
||
instruction 'Import-Package', 'org.apache.http.client,' + | ||
defaultOsgiImports | ||
instruction 'Export-Package', 'ca.islandora.alpaca.http.client' | ||
} | ||
} | ||
|
||
artifacts { | ||
archives (file('build/cfg/main/ca.islandora.alpaca.http.client.cfg')) { | ||
classifier 'configuration' | ||
type 'cfg' | ||
} | ||
} |
3 changes: 3 additions & 0 deletions
3
islandora-http-client/src/main/cfg/ca.islandora.alpaca.http.client.cfg
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,3 @@ | ||
# The static token value to be used for authentication by the HttpClient available as an OSGi service for | ||
# other services to use against the Fedora repository | ||
token.value=islandora |
85 changes: 85 additions & 0 deletions
85
...p-client/src/main/java/ca/islandora/alpaca/http/client/StaticTokenRequestInterceptor.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,85 @@ | ||
/* | ||
* Licensed to Islandora Foundation under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* The Islandora Foundation licenses this file to you under the MIT License. | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/MIT | ||
* | ||
* 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 ca.islandora.alpaca.http.client; | ||
|
||
import static java.util.Objects.requireNonNull; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.HttpRequestInterceptor; | ||
import org.apache.http.client.HttpClient; | ||
import org.apache.http.impl.client.HttpClientBuilder; | ||
import org.apache.http.message.BasicHeader; | ||
import org.apache.http.protocol.HttpContext; | ||
|
||
/** | ||
* Adds a single authentication header to any request that does not | ||
* already have at least one authentication header. | ||
* | ||
* @author ajs6f | ||
* | ||
*/ | ||
public class StaticTokenRequestInterceptor implements HttpRequestInterceptor { | ||
|
||
public static final String AUTH_HEADER = "Authorization"; | ||
|
||
private Header header; | ||
|
||
/** | ||
* Default constructor | ||
*/ | ||
public StaticTokenRequestInterceptor() { | ||
} | ||
|
||
/** | ||
* @param token the authentication token to use | ||
*/ | ||
public StaticTokenRequestInterceptor(final String token) { | ||
this.header = makeHeader(token); | ||
} | ||
|
||
/** | ||
* @param token the authentication token to use | ||
*/ | ||
public void setToken(final String token) { | ||
this.header = makeHeader(token); | ||
} | ||
|
||
private static Header makeHeader(final String token) { | ||
return new BasicHeader(AUTH_HEADER, "Bearer " + requireNonNull(token, "Token must not be null!")); | ||
} | ||
|
||
@Override | ||
public void process(final HttpRequest request, final HttpContext context) { | ||
// we do not inject if auth headers present | ||
if (request.getFirstHeader(AUTH_HEADER) == null) { | ||
request.addHeader(header); | ||
} | ||
} | ||
|
||
/** | ||
* Convenience factory method. | ||
* | ||
* @param interceptor | ||
* @return a default-configuration {@link HttpClient} that is wrapped with this interceptor | ||
*/ | ||
public static HttpClient defaultClient(final StaticTokenRequestInterceptor interceptor) { | ||
return HttpClientBuilder.create().addInterceptorFirst(interceptor).build(); | ||
} | ||
} |
26 changes: 26 additions & 0 deletions
26
islandora-http-client/src/main/resources/OSGI-INF/blueprint/blueprint.xml
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,26 @@ | ||
<?xml version="1.0" encoding="UTF-8"?> | ||
<blueprint xmlns="http://www.osgi.org/xmlns/blueprint/v1.0.0" | ||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" | ||
xmlns:cm="http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0" | ||
xsi:schemaLocation=" | ||
http://aries.apache.org/blueprint/xmlns/blueprint-cm/v1.1.0 http://aries.apache.org/schemas/blueprint-cm/blueprint-cm-1.1.0.xsd | ||
http://www.osgi.org/xmlns/blueprint/v1.0.0 http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd"> | ||
|
||
<!-- OSGI blueprint property placeholder --> | ||
<cm:property-placeholder id="properties" persistent-id="ca.islandora.alpaca.http.client" update-strategy="reload" > | ||
<cm:default-properties> | ||
<cm:property name="token.value" value="islandora"/> | ||
</cm:default-properties> | ||
</cm:property-placeholder> | ||
|
||
<service id="httpClient" interface="org.apache.http.client.HttpClient" filter="(osgi.jndi.service.name=claw/httpClient)"> | ||
<bean class="ca.islandora.alpaca.http.client.StaticTokenRequestInterceptor" factory-method=“defaultClient”> | ||
<argument> | ||
<bean id="interceptor" class="ca.islandora.alpaca.http.client.StaticTokenRequestInterceptor"> | ||
<property name="token" value="${token.value}"/> | ||
</bean> | ||
</argument> | ||
</bean> | ||
</service> | ||
|
||
</blueprint> |
56 changes: 56 additions & 0 deletions
56
...ient/src/test/java/ca/islandora/alpaca/http/client/StaticTokenRequestInterceptorTest.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,56 @@ | ||
/* | ||
* Licensed to Islandora Foundation under one or more contributor license | ||
* agreements. See the NOTICE file distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* The Islandora Foundation licenses this file to you under the MIT License. | ||
* You may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* https://opensource.org/licenses/MIT | ||
* | ||
* 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 ca.islandora.alpaca.http.client; | ||
|
||
import static ca.islandora.alpaca.http.client.StaticTokenRequestInterceptor.AUTH_HEADER; | ||
|
||
import org.apache.http.Header; | ||
import org.apache.http.HttpRequest; | ||
import org.apache.http.client.methods.HttpGet; | ||
import org.junit.Assert; | ||
import org.junit.Test; | ||
|
||
/** | ||
* @author ajs6f | ||
* | ||
*/ | ||
public class StaticTokenRequestInterceptorTest extends Assert { | ||
|
||
@Test | ||
public void shouldInjectHeaderWhenNoAuthHeadersPresent() { | ||
final StaticTokenRequestInterceptor testInterceptor = new StaticTokenRequestInterceptor("testToken"); | ||
final HttpRequest request = new HttpGet(); | ||
testInterceptor.process(request, null); | ||
final Header[] authHeaders = request.getHeaders(AUTH_HEADER); | ||
assertEquals("Should only be one auth header!", 1, authHeaders.length); | ||
assertEquals("Wrong value for header!", "Bearer testToken", authHeaders[0].getValue()); | ||
} | ||
|
||
@Test | ||
public void shouldNotInjectHeaderWhenAuthHeadersPresent() { | ||
final StaticTokenRequestInterceptor testInterceptor = new StaticTokenRequestInterceptor(); | ||
testInterceptor.setToken("testToken"); | ||
final HttpRequest request = new HttpGet(); | ||
request.addHeader(AUTH_HEADER, "fake header"); | ||
testInterceptor.process(request, null); | ||
final Header[] authHeaders = request.getHeaders(AUTH_HEADER); | ||
assertEquals("Should only be one auth header!", 1, authHeaders.length); | ||
assertEquals("Wrong value for header!", "fake header", authHeaders[0].getValue()); | ||
} | ||
} |
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 |
---|---|---|
@@ -1,7 +1,9 @@ | ||
include ':islandora-karaf' | ||
include ':islandora-indexing-triplestore' | ||
include ':islandora-connector-broadcast' | ||
include ':islandora-http-client' | ||
|
||
project(':islandora-karaf').projectDir = "$rootDir/karaf" as File | ||
project(':islandora-indexing-triplestore').projectDir = "$rootDir/islandora-indexing-triplestore" as File | ||
project(':islandora-connector-broadcast').projectDir = "$rootDir/islandora-connector-broadcast" as File | ||
project(':islandora-http-client').projectDir = "$rootDir/islandora-http-client" as File |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Does this work? I've never tried the
filter
property when exporting a service. My only experience is with the form:There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Probably I was just being absent minded. This is why we need more i-tests... I will fix this to the style you are suggesting, which I'm sure is correct.