Skip to content

Commit

Permalink
Encode URIs using UriBuilder
Browse files Browse the repository at this point in the history
Resolves #1127
  • Loading branch information
acoburn committed Oct 28, 2020
1 parent a2f5b48 commit 8872ec6
Show file tree
Hide file tree
Showing 5 changed files with 20 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import javax.inject.Inject;

import org.apache.commons.rdf.api.IRI;
import org.apache.jena.util.URIref;
import org.eclipse.microprofile.config.Config;
import org.eclipse.microprofile.config.ConfigProvider;
import org.slf4j.Logger;
Expand Down Expand Up @@ -147,7 +148,7 @@ private File getFileFromIdentifier(final IRI identifier) {
final String iriString = identifier.getIRIString();
if (!iriString.startsWith("file:"))
throw new IllegalArgumentException("Could not create File object from IRI: " + identifier);
final String schemeSpecificPart = URI.create(iriString).getSchemeSpecificPart();
final String schemeSpecificPart = URI.create(URIref.encode(iriString)).getSchemeSpecificPart();
return new File(basePath, trimStart(schemeSpecificPart, "/"));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.trellisldp.test;

import static java.net.URI.create;
import static javax.ws.rs.client.Entity.entity;
import static javax.ws.rs.core.HttpHeaders.AUTHORIZATION;
import static javax.ws.rs.core.HttpHeaders.LINK;
Expand All @@ -34,6 +33,7 @@

import javax.ws.rs.client.Client;
import javax.ws.rs.core.Response;
import javax.ws.rs.core.UriBuilder;

import org.junit.jupiter.api.DisplayName;
import org.junit.jupiter.api.Test;
Expand Down Expand Up @@ -333,7 +333,7 @@ public void setUp() {
final String publicContainerAcl = getLinks(res).stream().filter(link -> link.getRel().equals(acl))
.map(link -> link.getUri().toString()).findFirst().orElse("");
assertEquals(getPublicContainer() + EXT_ACL,
create(getPublicContainer()).resolve(publicContainerAcl).toString(),
UriBuilder.fromUri(getPublicContainer()).build().resolve(publicContainerAcl).toString(),
"Check ACL location for 'public'");
}

Expand Down Expand Up @@ -367,7 +367,7 @@ public void setUp() {
final String protectedContainerAcl = getLinks(res).stream().filter(link -> link.getRel().equals(acl))
.map(link -> link.getUri().toString()).findFirst().orElse("");
assertEquals(getProtectedContainer() + EXT_ACL,
create(protectedContainer).resolve(protectedContainerAcl).toString(),
UriBuilder.fromUri(protectedContainer).build().resolve(protectedContainerAcl).toString(),
"Check 'protected' ACL URL");
}

Expand Down Expand Up @@ -403,7 +403,8 @@ public void setUp() {
final String privateContainerAcl = getLinks(res).stream().filter(link -> link.getRel().equals(acl))
.map(link -> link.getUri().toString()).findFirst().orElse("");
assertEquals(getPrivateContainer() + EXT_ACL,
create(privateContainer).resolve(privateContainerAcl).toString(), "Check 'private' ACL URL");
UriBuilder.fromUri(privateContainer).build().resolve(privateContainerAcl).toString(),
"Check 'private' ACL URL");
}

final String privateAcl = prefixAcl
Expand Down Expand Up @@ -452,7 +453,8 @@ public void setUp() {
final String groupContainerAcl = getLinks(res).stream().filter(link -> link.getRel().equals(acl))
.map(link -> link.getUri().toString()).findFirst().orElse("");
assertEquals(getGroupContainer() + EXT_ACL,
create(groupContainer).resolve(groupContainerAcl).toString(), "Check 'group' ACL URL");
UriBuilder.fromUri(groupContainer).build().resolve(groupContainerAcl).toString(),
"Check 'group' ACL URL");
}

final String groupAcl = prefixAcl
Expand Down Expand Up @@ -486,7 +488,8 @@ public void setUp() {
final String defaultContainerAcl = getLinks(res).stream().filter(link -> link.getRel().equals(acl))
.map(link -> link.getUri().toString()).findFirst().orElse("");
assertEquals(getDefaultContainer() + EXT_ACL,
create(defaultContainer).resolve(defaultContainerAcl).toString(), "Check 'default' ACL URL");
UriBuilder.fromUri(defaultContainer).build().resolve(defaultContainerAcl).toString(),
"Check 'default' ACL URL");
}

final String defaultAcl = "PREFIX foaf: <http://xmlns.com/foaf/0.1/>\n"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@
package org.trellisldp.http.impl;

import static java.lang.String.join;
import static java.net.URI.create;
import static java.util.Date.from;
import static java.util.concurrent.CompletableFuture.completedFuture;
import static java.util.stream.Collectors.joining;
Expand Down Expand Up @@ -81,6 +80,7 @@
import javax.ws.rs.core.Link;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.StreamingOutput;
import javax.ws.rs.core.UriBuilder;

import org.apache.commons.rdf.api.IRI;
import org.apache.commons.rdf.api.Quad;
Expand Down Expand Up @@ -410,10 +410,12 @@ private void addLdpHeaders(final ResponseBuilder builder, final IRI model) {

private void handleTrailingSlashRedirection(final Resource resource) {
if (getRequest().hasTrailingSlash() && !isContainer(resource.getInteractionModel())) {
throw new RedirectionException(303, create(getBaseUrl() + normalizePath(getRequest().getPath())));
throw new RedirectionException(303,
UriBuilder.fromUri(getBaseUrl()).path(normalizePath(getRequest().getPath())).build());
} else if (!getRequest().hasTrailingSlash() && !getRequest().getPath().isEmpty()
&& isContainer(resource.getInteractionModel())) {
throw new RedirectionException(303, create(getBaseUrl() + normalizePath(getRequest().getPath()) + "/"));
throw new RedirectionException(303,
UriBuilder.fromUri(getBaseUrl()).path(normalizePath(getRequest().getPath()) + "/").build());
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.trellisldp.http.impl;

import static java.net.URI.create;
import static javax.ws.rs.HttpMethod.DELETE;
import static javax.ws.rs.HttpMethod.GET;
import static javax.ws.rs.HttpMethod.HEAD;
Expand Down Expand Up @@ -50,6 +49,7 @@
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriBuilder;

import org.apache.commons.rdf.api.Dataset;
import org.apache.commons.rdf.api.IRI;
Expand Down Expand Up @@ -213,7 +213,7 @@ private CompletionStage<ResponseBuilder> handleResourceCreation(final Dataset mu
.thenCompose(future -> emitEvent(internalId, AS.Create, ldpType))
.thenApply(future -> {
ldpResourceTypes(ldpType).map(IRI::getIRIString).forEach(type -> builder.link(type, Link.TYPE));
return builder.location(create(getIdentifier()));
return builder.location(UriBuilder.fromUri(getIdentifier()).build());
});
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@
*/
package org.trellisldp.http.impl;

import static java.net.URI.create;
import static javax.ws.rs.core.HttpHeaders.IF_MATCH;
import static javax.ws.rs.core.HttpHeaders.IF_UNMODIFIED_SINCE;
import static javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM;
Expand Down Expand Up @@ -49,6 +48,7 @@
import javax.ws.rs.core.Link;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response.ResponseBuilder;
import javax.ws.rs.core.UriBuilder;

import org.apache.commons.rdf.api.Dataset;
import org.apache.commons.rdf.api.IRI;
Expand Down Expand Up @@ -297,7 +297,7 @@ private void checkConstraints(final Dataset dataset, final IRI ldpType, final RD

private ResponseBuilder decorateResponse(final ResponseBuilder builder) {
if (getResource() == null) {
return builder.status(CREATED).contentLocation(create(getIdentifier()));
return builder.status(CREATED).contentLocation(UriBuilder.fromUri(getIdentifier()).build());
}
return builder;
}
Expand Down

0 comments on commit 8872ec6

Please sign in to comment.