Skip to content

Commit

Permalink
Add header by implementing a core call for beans of type UriAwareHttp…
Browse files Browse the repository at this point in the history
  • Loading branch information
whikloj authored and Andrew Woods committed Nov 5, 2015
1 parent f34efc8 commit 6d7f196
Show file tree
Hide file tree
Showing 2 changed files with 106 additions and 5 deletions.
86 changes: 86 additions & 0 deletions src/main/java/org/fcrepo/auth/webac/LinkHeaderProvider.java
@@ -0,0 +1,86 @@
/**
* 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.auth.webac;

import static com.hp.hpl.jena.rdf.model.ModelFactory.createDefaultModel;
import static org.fcrepo.auth.webac.URIConstants.WEBAC_ACCESS_CONTROL_VALUE;
import static org.slf4j.LoggerFactory.getLogger;

import javax.jcr.Session;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.UriInfo;

import org.fcrepo.http.commons.api.UriAwareHttpHeaderFactory;
import org.fcrepo.http.commons.session.SessionFactory;
import org.fcrepo.kernel.api.identifiers.IdentifierConverter;
import org.fcrepo.kernel.api.models.FedoraResource;
import org.fcrepo.kernel.api.services.NodeService;
import org.fcrepo.kernel.modeshape.rdf.impl.DefaultIdentifierTranslator;
import org.fcrepo.kernel.modeshape.rdf.impl.PropertiesRdfContext;

import org.slf4j.Logger;

import com.google.common.collect.ArrayListMultimap;
import com.google.common.collect.ListMultimap;
import com.google.common.collect.Multimap;
import com.hp.hpl.jena.rdf.model.Model;
import com.hp.hpl.jena.rdf.model.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

/**
* Insert WebAC Link headers to responses
*
* @author whikloj
* @since 2015-10-30
*/
@Component
public class LinkHeaderProvider implements UriAwareHttpHeaderFactory {

private static final Logger LOGGER = getLogger(LinkHeaderProvider.class);

@Autowired
private SessionFactory sessionFactory;

@Autowired
private NodeService nodeService;

@Override
public Multimap<String, String> createHttpHeadersForResource(final UriInfo uriInfo, final FedoraResource resource) {

final Session internalSession = sessionFactory.getInternalSession();
final IdentifierConverter<Resource, FedoraResource> translator =
new DefaultIdentifierTranslator(internalSession);
final Model model = createDefaultModel();
final ListMultimap<String, String> headers = ArrayListMultimap.create();

LOGGER.debug("Adding WebAC Link Header for Resource: {}", resource);

nodeService.find(internalSession, resource.getPath()).getTriples(translator, PropertiesRdfContext.class)
.filter(t -> model.asStatement(t).getPredicate().hasURI(WEBAC_ACCESS_CONTROL_VALUE))
.filter(t -> t.getObject().isURI())
.forEachRemaining(t -> {
headers.put("Link", Link.fromUri(uriInfo.getBaseUriBuilder()
.path(translator.convert(model.asStatement(t).getObject().asResource()).getPath())
.toString()).rel("acl").build().toString());
});

return headers;
}


}
Expand Up @@ -16,13 +16,20 @@
package org.fcrepo.integration.auth.webac;

import static javax.ws.rs.core.Response.Status.CREATED;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertTrue;
import static org.fcrepo.auth.webac.URIConstants.WEBAC_ACCESS_CONTROL_VALUE;
import static org.fcrepo.auth.webac.WebACRolesProvider.ROOT_AUTHORIZATION_PROPERTY;
import static org.fcrepo.kernel.api.RdfLexicon.DC_NAMESPACE;
import static org.junit.Assert.assertEquals;

import java.io.IOException;
import java.io.InputStream;
import java.util.Arrays;
import java.util.Optional;

import javax.ws.rs.core.Link;

import org.fcrepo.integration.http.api.AbstractResourceIT;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpResponse;
Expand All @@ -32,10 +39,10 @@
import org.apache.http.client.methods.HttpPatch;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.client.methods.HttpPut;
import org.apache.http.message.AbstractHttpMessage;
import org.apache.http.Header;
import org.apache.http.entity.InputStreamEntity;
import org.apache.http.entity.StringEntity;
import org.apache.http.message.AbstractHttpMessage;
import org.fcrepo.integration.http.api.AbstractResourceIT;
import org.junit.Ignore;
import org.junit.Test;
import org.slf4j.Logger;
Expand Down Expand Up @@ -156,14 +163,22 @@ public void scenario1() throws IOException {
final String testObj = ingestObj("/rest/webacl_box1");
final String acl1 = ingestAcl("fedoraAdmin", "/acls/01/acl.ttl", "/acls/01/authorization.ttl");
linkToAcl(testObj, acl1);
final String aclLink = Link.fromUri(acl1).rel("acl").build().toString();

logger.debug("Anonymous can't read");
final HttpGet request = getObjMethod(testObj.replace(serverAddress, ""));
assertEquals(HttpStatus.SC_FORBIDDEN, getStatus(request));

logger.debug("Can username 'smith123' read " + testObj);
setAuth(request, "smith123");
assertEquals(HttpStatus.SC_OK, getStatus(request));
try (final CloseableHttpResponse response = execute(request)) {
assertEquals(HttpStatus.SC_OK, getStatus(response));

final Optional<String> header = Arrays.asList(response.getHeaders("Link")).stream().map(Header::getValue)
.filter(aclLink::equals).findFirst();
assertTrue("Missing Link header", header.isPresent());
}

}

@Test
Expand Down

0 comments on commit 6d7f196

Please sign in to comment.