Skip to content
This repository has been archived by the owner on May 10, 2022. It is now read-only.

Commit

Permalink
Bug 443238 - Add anchor to Link on LDP-NR creation
Browse files Browse the repository at this point in the history
Change-Id: Ic91adc099cdc1f588def9ebfef85d52b01d71667
Signed-off-by: Samuel Padgett <spadgett@us.ibm.com>
  • Loading branch information
Samuel Padgett committed Sep 3, 2014
1 parent 11e2367 commit c58f732
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 45 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,19 +4,19 @@
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* Contributors:
*
*
* Frank Budinsky - initial API and implementation
* Steve Speicher - initial API and implementation
* Samuel Padgett - initial API and implementation
* Samuel Padgett - make jsonld-java dependency optional
* Steve Speicher - updates for recent LDP spec changes
* Steve Speicher - make root URI configurable
* Steve Speicher - make root URI configurable
* Samuel Padgett - remove membership and containment triples on delete and update dcterms:modified
* Samuel Padgett - add ETag and Link headers with correct types on GET requests
* Samuel Padgett - fix NPEx creating root container on first launch
Expand All @@ -28,6 +28,8 @@
* Samuel Padgett - add support for LDP Non-RDF Source
* Samuel Padgett - support Prefer header
* Samuel Padgett - support read-only properties and rel="describedby"
* Samuel Padgett - add an anchor parameter to the Link response header
* on LDP-NR creation
*******************************************************************************/
package org.eclipse.lyo.ldp.server.jena;

Expand All @@ -41,6 +43,7 @@
import javax.ws.rs.HttpMethod;
import javax.ws.rs.WebApplicationException;
import javax.ws.rs.core.HttpHeaders;
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.Response.ResponseBuilder;
Expand Down Expand Up @@ -130,15 +133,15 @@ public void setConfigParameters()
}
fResourceURIPrefix = appendURISegment(fURI, prefix);
}

/* (non-Javadoc)
* @see org.eclipse.lyo.ldp.server.impl.ILDPContainer#query(java.io.OutputStream, java.lang.String, java.lang.String)
*/
public void query(OutputStream outStream, String queryString, String resultsFormat)
{
fGraphStore.query(outStream, queryString, resultsFormat);
}

/* (non-Javadoc)
* @see org.eclipse.lyo.ldp.server.impl.ILDPContainer#post(java.io.InputStream, java.lang.String, java.lang.String)
*/
Expand All @@ -160,7 +163,7 @@ public String post(InputStream stream, String contentType, String user, String n
fGraphStore.end();
}
}

/* (non-Javadoc)
* @see org.eclipse.lyo.ldp.server.impl.ILDPContainer#putCreate(java.lang.String, java.io.InputStream, java.lang.String, java.lang.String)
*/
Expand All @@ -179,7 +182,7 @@ public boolean putCreate(String resourceURI, InputStream stream, String contentT
"Can not create a resource for URI that has already been used for a deleted resource at: "+resourceURI).build());
}
fGraphStore.createCompanionGraph(resourceURI, JenaLDPResourceManager.mintConfigURI(resourceURI));
createResource(resourceURI, false, stream, contentType, user);
createResource(resourceURI, false, stream, contentType, user);
create = true;
} else {
updateResource(stream, contentType, user, requestHeaders);
Expand All @@ -188,10 +191,10 @@ public boolean putCreate(String resourceURI, InputStream stream, String contentT
} finally {
fGraphStore.end();
}

return create;
}

/* (non-Javadoc)
* @see org.eclipse.lyo.ldp.server.impl.ILDPContainer#patch(java.lang.String, java.io.InputStream, java.lang.String, java.lang.String)
*/
Expand All @@ -209,7 +212,7 @@ public void patch(String resourceURI, InputStream stream, String contentType, St
fGraphStore.end();
}
}

/**
* Create resource and add membership triples
* @param resourceURI The NEW resource being added (including any query params, etc)
Expand Down Expand Up @@ -252,13 +255,13 @@ protected String createResource(String resourceURI, boolean addToContainer, Inpu

fGraphStore.putGraph(resourceURI, model);

return resourceURI;
return resourceURI;
}

/**
* Adds containment triples and, if a direct container, membership triples
* for this resource.
*
*
* @param resourceURI the URI of the resource to add
* @param resourceModel the RDF content or null for non-RDF source (needed for the ldp:isMemberOfRelation)
* @param time the current time (for dcterms:modified properties)
Expand All @@ -267,7 +270,7 @@ protected void addToContainer(String resourceURI, Model resourceModel, Calendar
final String containerURI = getContainerURIForResource(resourceURI);
final Model containerModel = fGraphStore.getGraph(containerURI);
final Resource containerResource = containerModel.getResource(containerURI);

// TODO: Push direct container specific logic down to JeanLDPDirectContainer.

// Can't do it yet since this might not be the container for the resource. PATCH and PUT to create
Expand Down Expand Up @@ -298,24 +301,24 @@ protected void addToContainer(String resourceURI, Model resourceModel, Calendar
containerResource.removeAll(DCTerms.modified);
containerResource.addLiteral(DCTerms.modified, containerModel.createTypedLiteral(time));
}

protected void patchResource(String resourceURI, String baseURI, InputStream stream, String contentType, String user)
{
Model model = readModel(baseURI, stream, contentType);
Resource subject = model.getResource(resourceURI);

// Update dcterms:modified

Calendar time = Calendar.getInstance();
subject.removeAll(DCTerms.modified);
subject.addLiteral(DCTerms.modified, model.createTypedLiteral(time));

// TODO: Process patch contents

/*fGraphStore.putGraph(resourceURI, model);
model.close(); */
}

/* (non-Javadoc)
* @see org.eclipse.lyo.ldp.server.impl.ILDPContainer#delete(java.lang.String)
*/
Expand All @@ -324,7 +327,7 @@ public void delete()
// TODO: Need to determine if we need any other Container-specific
super.delete();
}

@Override
protected Model amendResponseGraph(Model container, MultivaluedMap<String, String> preferences)
{
Expand All @@ -335,15 +338,15 @@ protected Model amendResponseGraph(Model container, MultivaluedMap<String, Strin
if (!includeContainment(preferences)) {
result.getResource(fURI).removeAll(LDP.contains);
}

return result;
}

protected boolean isReturnRepresentationPreferenceApplied(MultivaluedMap<String, String> preferences) {
// Return true if any recognized include or omit preferences are in the request.
final List<String> include = preferences.get(LDPConstants.PREFER_INCLUDE);
final List<String> omit = preferences.get(LDPConstants.PREFER_OMIT);

if (include != null
&& (include.contains(LDPConstants.PREFER_MINIMAL_CONTAINER) || include.contains(LDPConstants.PREFER_CONTAINMENT))) {
return true;
Expand All @@ -366,7 +369,7 @@ public static Property getMemberRelation(Model containerGraph, Resource containe
Statement stmt = containerResource.getProperty(LDP.hasMemberRelation);
return stmt != null ? containerGraph.getProperty(stmt.getObject().asResource().getURI()) : LDP.member;
}

public static Property getIsMemberOfRelation(Model containerGraph, Resource containerResource)
{
Statement stmt = containerResource.getProperty(LDP.isMemberOfRelation);
Expand All @@ -391,7 +394,7 @@ public Set<String> getAllowedMethods() {

return allow;
}

@Override
protected Set<String> getReadOnlyProperties() {
Set<String> readOnly = super.getReadOnlyProperties();
Expand All @@ -405,7 +408,7 @@ public Response postNonRDFSource(InputStream content, String stripCharset, Strin
fGraphStore.writeLock();
try {
String uri = fGraphStore.mintURI(fURI, fResourceURIPrefix, slug);

// Config graph for internal metadata (e.g., tracking resource deletion)
String configURI = JenaLDPResourceManager.mintConfigURI(uri);
Model configModel = fGraphStore.createCompanionGraph(uri, configURI);
Expand Down Expand Up @@ -438,10 +441,15 @@ public Response postNonRDFSource(InputStream content, String stripCharset, Strin

fGraphStore.commit();

return build(Response
.status(Status.CREATED)
.header(HttpHeaders.LOCATION, uri)
.link(associatedURI, LDPConstants.LINK_REL_DESCRIBEDBY));
return build(
Response
.status(Status.CREATED)
.header(HttpHeaders.LOCATION, uri)
.links(Link.fromUri(associatedURI)
.rel(LDPConstants.LINK_REL_DESCRIBEDBY)
.param(LDPConstants.LINK_PARAM_ANCHOR, uri)
.build())
);
} finally {
fGraphStore.end();
}
Expand All @@ -450,19 +458,19 @@ public Response postNonRDFSource(InputStream content, String stripCharset, Strin
protected boolean includeContainment(MultivaluedMap<String, String> preferences) {
final List<String> include = preferences.get(LDPConstants.PREFER_INCLUDE);
final List<String> omit = preferences.get(LDPConstants.PREFER_OMIT);

final boolean omitContainment = (omit != null && omit.contains(LDPConstants.PREFER_CONTAINMENT));
if (include != null) {
if (include.contains(LDPConstants.PREFER_CONTAINMENT)) {
return true;
}

if (include.contains(LDPConstants.PREFER_MINIMAL_CONTAINER) ||
include.contains(LDPConstants.DEPRECATED_PREFER_EMPTY_CONTAINER)) {
return false;
}
}

return !omitContainment;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* and Eclipse Distribution License v. 1.0 which accompanies this distribution.
*
*
* The Eclipse Public License is available at http://www.eclipse.org/legal/epl-v10.html
* and the Eclipse Distribution License is available at
* http://www.eclipse.org/org/documents/edl-v10.php.
*
*
* Contributors:
*
*
* Frank Budinsky - initial API and implementation
* Steve Speicher - initial API and implementation
* Samuel Padgett - initial API and implementation
Expand All @@ -20,24 +20,25 @@
* Samuel Padgett - add Accept-Patch header constants
* Samuel Padgett - add Allow header to GET responses
* Samuel Padgett - support Prefer header
* Samuel Padgett - add constant for Link header anchor parameters
*******************************************************************************/
package org.eclipse.lyo.ldp.server;

import javax.ws.rs.core.MediaType;

public class LDPConstants
{

protected static final String NS = "http://www.w3.org/ns/ldp#";

/**
* The namespace of the vocabulary as a string
*/
public static String getNSURI()
{
return NS;
}

// Content types
public static final String CT_APPLICATION_JSON = MediaType.APPLICATION_JSON;
public static final String CT_APPLICATION_LD_JSON = "application/ld+json";
Expand All @@ -49,7 +50,7 @@ public static String getNSURI()
public static final String CT_TEXT_HTML = MediaType.TEXT_HTML;
public static final String CT_TEXT_TURTLE = "text/turtle";
public static final String CT_TEXT_TRIG = "text/trig";

// HTTP Headers
public static final String HDR_ALLOW = "Allow";
public static final String HDR_ACCEPT_PATCH = "Accept-Patch";
Expand All @@ -59,11 +60,12 @@ public static String getNSURI()
public static final String HDR_SLUG = "Slug";
public static final String HDR_PREFER = "Prefer";
public static final String HDR_PREFERENCE_APPLIED = "Preference-Applied";

// Link relations
public static final String LINK_PARAM_ANCHOR = "anchor";
public static final String LINK_REL_TYPE = "type";
public static final String LINK_REL_DESCRIBEDBY = "describedby";

// RDF Property names both namespace and local
public static final String PROP_LNAME_MEMBERSHIP_RESOURCE = "membershipResource";
public static final String PROP_MEMBERSHIP_RESOURCE = nsName(PROP_LNAME_MEMBERSHIP_RESOURCE);
Expand All @@ -81,7 +83,7 @@ public static String getNSURI()
public static final String PROP_NEXTPAGE = nsName(PROP_LNAME_NEXTPAGE);
public static final String PROP_LNAME_CONTAINER_SORT_PREDICATE = "containerSortPredicate";
public static final String PROP_CONTAINER_SORT_PREDICATE = nsName(PROP_LNAME_CONTAINER_SORT_PREDICATE);

// RDF Classes both namespace and local
public static final String CLASS_LNAME_PAGE = "Page";
public static final String CLASS_PAGE = nsName(CLASS_LNAME_PAGE);
Expand All @@ -99,10 +101,10 @@ public static String getNSURI()
public static final String CLASS_RDFSOURCE = nsName(CLASS_LNAME_RDFSOURCE);
public static final String CLASS_LNAME_NONRDFSOURCE = "NonRDFSource";
public static final String CLASS_NONRDFSOURCE = nsName(CLASS_LNAME_NONRDFSOURCE);

// Only container types that resource can be for, doesn't include #Container
public static final String [] CONTAINER_TYPES = {CLASS_BASIC_CONTAINER, CLASS_DIRECT_CONTAINER, CLASS_INDIRECT_CONTAINER};

// Prefer header preferences
public static final String PREFER_RETURN_REPRESENTATION = "return=representation";
public static final String PREFER_INCLUDE = "include";
Expand All @@ -117,7 +119,7 @@ public static String getNSURI()
* {@link #PREFER_MINIMAL_CONTAINER}.
*/
public static final String DEPRECATED_PREFER_EMPTY_CONTAINER = nsName("PreferEmptyContainer");

public static String nsName(String local) {
return NS + local;
}
Expand Down

0 comments on commit c58f732

Please sign in to comment.