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
Support POST/PUT requests with a Content-Location header instead of a…
… request body
- Loading branch information
Showing
22 changed files
with
444 additions
and
8 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
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
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
31 changes: 31 additions & 0 deletions
31
fcrepo-http-commons/src/main/java/org/fcrepo/http/commons/domain/ContentLocation.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,31 @@ | ||
/** | ||
* Copyright 2014 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.http.commons.domain; | ||
|
||
import java.lang.annotation.ElementType; | ||
import java.lang.annotation.Retention; | ||
import java.lang.annotation.RetentionPolicy; | ||
import java.lang.annotation.Target; | ||
|
||
/** | ||
* Also check the Content-Location header | ||
* | ||
* @author cbeer | ||
*/ | ||
@Target({ElementType.PARAMETER}) | ||
@Retention(RetentionPolicy.RUNTIME) | ||
public @interface ContentLocation { | ||
} |
95 changes: 95 additions & 0 deletions
95
...ommons/src/main/java/org/fcrepo/http/commons/domain/ContentLocationMessageBodyReader.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,95 @@ | ||
/** | ||
* Copyright 2014 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.http.commons.domain; | ||
|
||
import com.google.common.annotations.VisibleForTesting; | ||
import com.google.common.base.Predicate; | ||
import com.google.common.collect.Iterables; | ||
import org.fcrepo.kernel.services.ExternalContentService; | ||
import org.springframework.beans.factory.annotation.Autowired; | ||
import org.springframework.stereotype.Component; | ||
|
||
import javax.ws.rs.WebApplicationException; | ||
import javax.ws.rs.core.MediaType; | ||
import javax.ws.rs.core.MultivaluedMap; | ||
import javax.ws.rs.ext.MessageBodyReader; | ||
import javax.ws.rs.ext.Provider; | ||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.lang.annotation.Annotation; | ||
import java.lang.reflect.Type; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
import java.util.Arrays; | ||
|
||
import static javax.ws.rs.core.Response.Status.BAD_REQUEST; | ||
|
||
/** | ||
* Provide an InputStream either from the POST/PUT body, or by resolving a Content-Location URI | ||
* @author cabeer | ||
*/ | ||
@Provider | ||
@Component | ||
public class ContentLocationMessageBodyReader implements MessageBodyReader<InputStream> { | ||
|
||
public static final Predicate<Annotation> HAS_CONTENT_LOCATION_PREDICATE = new Predicate<Annotation>() { | ||
@Override | ||
public boolean apply(final Annotation annotation) { | ||
return annotation.annotationType().equals(ContentLocation.class); | ||
} | ||
}; | ||
/** | ||
* The fcrepo node service | ||
*/ | ||
@Autowired | ||
private ExternalContentService contentService; | ||
|
||
@Override | ||
public boolean isReadable(final Class<?> type, | ||
final Type genericType, | ||
final Annotation[] annotations, | ||
final MediaType mediaType) { | ||
return InputStream.class.isAssignableFrom(type) && | ||
Iterables.any(Arrays.asList(annotations), HAS_CONTENT_LOCATION_PREDICATE); | ||
} | ||
|
||
@Override | ||
public InputStream readFrom(final Class<InputStream> type, | ||
final Type genericType, | ||
final Annotation[] annotations, | ||
final MediaType mediaType, | ||
final MultivaluedMap<String, String> httpHeaders, | ||
final InputStream entityStream) throws IOException, WebApplicationException { | ||
|
||
if (httpHeaders.containsKey("Content-Location")) { | ||
final String location = httpHeaders.getFirst("Content-Location"); | ||
|
||
try { | ||
return contentService.retrieveExternalContent(new URI(location)); | ||
} catch (URISyntaxException e) { | ||
throw new WebApplicationException(e, BAD_REQUEST); | ||
} | ||
|
||
} else { | ||
return entityStream; | ||
} | ||
} | ||
|
||
@VisibleForTesting | ||
protected void setContentService(final ExternalContentService externalContentService) { | ||
this.contentService = externalContentService; | ||
} | ||
} |
72 changes: 72 additions & 0 deletions
72
...ns/src/test/java/org/fcrepo/http/commons/domain/ContentLocationMessageBodyReaderTest.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,72 @@ | ||
/** | ||
* Copyright 2014 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.http.commons.domain; | ||
|
||
import com.sun.jersey.core.header.InBoundHeaders; | ||
import org.fcrepo.kernel.services.ExternalContentService; | ||
import org.junit.Before; | ||
import org.junit.Test; | ||
import org.mockito.Mock; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.net.URI; | ||
import java.net.URISyntaxException; | ||
|
||
import static org.junit.Assert.assertEquals; | ||
import static org.mockito.Mockito.when; | ||
import static org.mockito.MockitoAnnotations.initMocks; | ||
|
||
/** | ||
* @author cabeer | ||
*/ | ||
public class ContentLocationMessageBodyReaderTest { | ||
|
||
|
||
private ContentLocationMessageBodyReader testObj; | ||
|
||
@Mock | ||
private InputStream mockInputStream; | ||
|
||
@Mock | ||
private ExternalContentService mockContentService; | ||
|
||
@Before | ||
public void setUp() throws URISyntaxException, IOException { | ||
initMocks(this); | ||
testObj = new ContentLocationMessageBodyReader(); | ||
testObj.setContentService(mockContentService); | ||
} | ||
|
||
@Test | ||
public void testReadFromURI() throws Exception { | ||
final InBoundHeaders headers = new InBoundHeaders(); | ||
headers.putSingle("Content-Location", "http://localhost:8080/xyz"); | ||
when(mockContentService.retrieveExternalContent(new URI("http://localhost:8080/xyz"))) | ||
.thenReturn(mockInputStream); | ||
final InputStream actual = testObj.readFrom(InputStream.class, null, null, null, headers, null); | ||
assertEquals(mockInputStream, actual); | ||
} | ||
|
||
@Test | ||
public void testReadFromRequestBody() throws Exception { | ||
|
||
final InBoundHeaders headers = new InBoundHeaders(); | ||
final InputStream actual = testObj.readFrom(InputStream.class, null, null, null, headers, mockInputStream); | ||
assertEquals(mockInputStream, actual); | ||
|
||
} | ||
} |
Oops, something went wrong.