Skip to content

Commit

Permalink
Improve URI creation from baseUrl
Browse files Browse the repository at this point in the history
Resolves #383
  • Loading branch information
acoburn authored Apr 3, 2019
1 parent 053c4c9 commit 624f2bf
Show file tree
Hide file tree
Showing 3 changed files with 64 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@
import static javax.ws.rs.core.Response.Status.GONE;
import static javax.ws.rs.core.Response.Status.NO_CONTENT;
import static javax.ws.rs.core.Response.status;
import static javax.ws.rs.core.UriBuilder.fromUri;
import static javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING;
import static org.apache.commons.lang3.StringUtils.isBlank;
import static org.apache.jena.util.SplitIRI.localnameXML;
Expand Down Expand Up @@ -247,7 +248,7 @@ public void getProperties(@Suspended final AsyncResponse response, @Context fina
throws ParserConfigurationException {
final TrellisRequest req = new TrellisRequest(request, uriInfo, headers);
final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + req.getPath());
final String location = getBaseUrl(req) + req.getPath();
final String location = fromUri(getBaseUrl(req)).path(req.getPath()).build().toString();
final Document doc = getDocument();
services.getResourceService().get(identifier)
.thenApply(this::checkResource)
Expand Down Expand Up @@ -279,7 +280,7 @@ public void updateProperties(@Suspended final AsyncResponse response,
final TrellisRequest req = new TrellisRequest(request, uriInfo, headers, security);
final IRI identifier = rdf.createIRI(TRELLIS_DATA_PREFIX + req.getPath());
final String baseUrl = getBaseUrl(req);
final String location = baseUrl + req.getPath();
final String location = fromUri(baseUrl).path(req.getPath()).build().toString();
final Session session = ofNullable(req.getPrincipalName()).map(services.getAgentService()::asAgent)
.map(HttpSession::new).orElseGet(HttpSession::new);
services.getResourceService().get(identifier)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,7 @@ public ResponseBuilder getTimeMapBuilder(final SortedSet<Instant> mementos, fina
final String baseUrl) {

final List<MediaType> acceptableTypes = req.getAcceptableMediaTypes();
final String identifier = getBaseUrl(baseUrl, req) + req.getPath();
final String identifier = fromUri(getBaseUrl(baseUrl, req)).path(req.getPath()).build().toString();
final List<Link> links = getMementoLinks(identifier, mementos).collect(toList());

final ResponseBuilder builder = ok().link(identifier, ORIGINAL + " " + TIMEGATE);
Expand Down Expand Up @@ -146,7 +146,7 @@ public void write(final OutputStream out) throws IOException {
*/
public ResponseBuilder getTimeGateBuilder(final SortedSet<Instant> mementos, final TrellisRequest req,
final String baseUrl) {
final String identifier = getBaseUrl(baseUrl, req) + req.getPath();
final String identifier = fromUri(getBaseUrl(baseUrl, req)).path(req.getPath()).build().toString();
return status(FOUND)
.location(fromUri(identifier + "?version=" + req.getDatetime().getInstant().getEpochSecond()).build())
.link(identifier, ORIGINAL + " " + TIMEGATE)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
/*
* 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.trellisldp.http;

import static java.util.Arrays.asList;
import static org.mockito.MockitoAnnotations.initMocks;

import javax.ws.rs.core.Application;

import org.glassfish.jersey.server.ResourceConfig;

/**
* @author acoburn
*/
public class TrellisHttpResourceBaseUrlTest extends AbstractTrellisHttpResourceTest {

// The absence of trailing slash is intentional here; it tests configuration overrides
// that lack a trailing slash.
private static final String URL = "http://example.com";

@Override
protected String getBaseUrl() {
return URL + "/";
}

@Override
public Application configure() {

// Junit runner doesn't seem to work very well with JerseyTest
initMocks(this);

final String baseUri = getBaseUri().toString();
final String origin = baseUri.substring(0, baseUri.length() - 1);

final ResourceConfig config = new ResourceConfig();
config.register(new TrellisHttpResource(mockBundler, URL));
config.register(new TestAuthenticationFilter("testUser", "group"));
config.register(new WebAcFilter(mockAccessControlService));
config.register(new AgentAuthorizationFilter(mockAgentService));
config.register(new CacheControlFilter());
config.register(new WebSubHeaderFilter(HUB));
config.register(new TrellisHttpFilter());
config.register(new CrossOriginResourceSharingFilter(asList(origin), asList("PATCH", "POST", "PUT"),
asList("Link", "Content-Type", "Accept-Datetime", "Accept"),
asList("Link", "Content-Type", "Memento-Datetime"), true, 100));
return config;
}
}

0 comments on commit 624f2bf

Please sign in to comment.