Search in sources :

Example 1 with TYPE

use of javax.ws.rs.core.Link.TYPE in project trellis by trellis-ldp.

the class PostHandlerTest method testUnsupportedType.

@Test
void testUnsupportedType() throws IOException {
    when(mockResourceService.supportedInteractionModels()).thenReturn(emptySet());
    when(mockTrellisRequest.getLink()).thenReturn(fromUri(LDP.Container.getIRIString()).rel(TYPE).build());
    final PostHandler handler = buildPostHandler(RESOURCE_EMPTY, NEW_RESOURCE, null);
    try (final Response res = assertThrows(BadRequestException.class, () -> handler.initialize(mockParent, MISSING_RESOURCE), "No exception thrown when the IXN model isn't supported!").getResponse()) {
        assertEquals(BAD_REQUEST, res.getStatusInfo(), ERR_RESPONSE_CODE);
        assertTrue(res.getLinks().stream().anyMatch(link -> link.getUri().toString().equals(UnsupportedInteractionModel.getIRIString()) && link.getRel().equals(LDP.constrainedBy.getIRIString())), "Missing constraint Link header!");
    }
}
Also used : Response(javax.ws.rs.core.Response) Optional.of(java.util.Optional.of) Link.fromUri(javax.ws.rs.core.Link.fromUri) Triple(org.apache.commons.rdf.api.Triple) Metadata(org.trellisldp.api.Metadata) APPLICATION_OCTET_STREAM(javax.ws.rs.core.MediaType.APPLICATION_OCTET_STREAM) TURTLE(org.apache.commons.rdf.api.RDFSyntax.TURTLE) Arrays.asList(java.util.Arrays.asList) TEXT_TURTLE(org.trellisldp.common.RdfMediaType.TEXT_TURTLE) UnsupportedInteractionModel(org.trellisldp.vocabulary.Trellis.UnsupportedInteractionModel) BadRequestException(javax.ws.rs.BadRequestException) LDP(org.trellisldp.vocabulary.LDP) TYPE(javax.ws.rs.core.Link.TYPE) JSONLD(org.apache.commons.rdf.api.RDFSyntax.JSONLD) Dataset(org.apache.commons.rdf.api.Dataset) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) BinaryMetadata(org.trellisldp.api.BinaryMetadata) Collections.emptySet(java.util.Collections.emptySet) DC(org.trellisldp.vocabulary.DC) DefaultAuditService(org.trellisldp.audit.DefaultAuditService) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) IRI(org.apache.commons.rdf.api.IRI) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) URI.create(java.net.URI.create) Response(javax.ws.rs.core.Response) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) NTRIPLES(org.apache.commons.rdf.api.RDFSyntax.NTRIPLES) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) CREATED(javax.ws.rs.core.Response.Status.CREATED) InputStream(java.io.InputStream) BadRequestException(javax.ws.rs.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 2 with TYPE

use of javax.ws.rs.core.Link.TYPE in project trellis by trellis-ldp.

the class PutHandlerTest method testUnsupportedType.

@Test
void testUnsupportedType() {
    when(mockResourceService.supportedInteractionModels()).thenReturn(emptySet());
    when(mockTrellisRequest.getContentType()).thenReturn(TEXT_TURTLE);
    when(mockTrellisRequest.getLink()).thenReturn(fromUri(LDP.Resource.getIRIString()).rel(TYPE).build());
    final PutHandler handler = buildPutHandler(RESOURCE_TURTLE, null);
    final Response.ResponseBuilder builder = handler.initialize(mockParent, mockResource);
    try (final Response res = assertThrows(BadRequestException.class, () -> handler.setResource(builder), "No exception when the interaction model isn't supported!").getResponse()) {
        assertEquals(BAD_REQUEST, res.getStatusInfo(), ERR_RESPONSE_CODE);
        assertTrue(res.getLinks().stream().anyMatch(link -> link.getUri().toString().equals(UnsupportedInteractionModel.getIRIString()) && link.getRel().equals(LDP.constrainedBy.getIRIString())), "Missing constraint header!");
    }
}
Also used : Response(javax.ws.rs.core.Response) Optional.empty(java.util.Optional.empty) TrellisRuntimeException(org.trellisldp.api.TrellisRuntimeException) Optional.of(java.util.Optional.of) Link.fromUri(javax.ws.rs.core.Link.fromUri) Metadata(org.trellisldp.api.Metadata) TURTLE(org.apache.commons.rdf.api.RDFSyntax.TURTLE) TEXT_TURTLE(org.trellisldp.common.RdfMediaType.TEXT_TURTLE) ResourceService(org.trellisldp.api.ResourceService) UnsupportedInteractionModel(org.trellisldp.vocabulary.Trellis.UnsupportedInteractionModel) BadRequestException(javax.ws.rs.BadRequestException) LDP(org.trellisldp.vocabulary.LDP) TYPE(javax.ws.rs.core.Link.TYPE) Dataset(org.apache.commons.rdf.api.Dataset) BAD_REQUEST(javax.ws.rs.core.Response.Status.BAD_REQUEST) BinaryMetadata(org.trellisldp.api.BinaryMetadata) CompletableFuture.runAsync(java.util.concurrent.CompletableFuture.runAsync) Collections.emptySet(java.util.Collections.emptySet) HttpConstants(org.trellisldp.common.HttpConstants) DefaultAuditService(org.trellisldp.audit.DefaultAuditService) CONFLICT(javax.ws.rs.core.Response.Status.CONFLICT) NO_CONTENT(javax.ws.rs.core.Response.Status.NO_CONTENT) IOException(java.io.IOException) CompletionException(java.util.concurrent.CompletionException) UncheckedIOException(java.io.UncheckedIOException) Test(org.junit.jupiter.api.Test) Mockito(org.mockito.Mockito) IRI(org.apache.commons.rdf.api.IRI) CompletionStage(java.util.concurrent.CompletionStage) Stream(java.util.stream.Stream) Response(javax.ws.rs.core.Response) RDFSyntax(org.apache.commons.rdf.api.RDFSyntax) MementoService(org.trellisldp.api.MementoService) Executable(org.junit.jupiter.api.function.Executable) Assertions(org.junit.jupiter.api.Assertions) WebApplicationException(javax.ws.rs.WebApplicationException) TEXT_PLAIN(javax.ws.rs.core.MediaType.TEXT_PLAIN) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) CREATED(javax.ws.rs.core.Response.Status.CREATED) InputStream(java.io.InputStream) BadRequestException(javax.ws.rs.BadRequestException) Test(org.junit.jupiter.api.Test)

Example 3 with TYPE

use of javax.ws.rs.core.Link.TYPE in project trellis by trellis-ldp.

the class TrellisWebDAVRequestFilter method filter.

@Override
public void filter(final ContainerRequestContext ctx) {
    final IRI identifier = services.getResourceService().getResourceIdentifier(getBaseUrl(baseUrl, ctx.getUriInfo()), ctx.getUriInfo().getPath());
    if (PUT.equals(ctx.getMethod()) && createUncontained) {
        final Resource res = services.getResourceService().get(identifier).toCompletableFuture().join();
        final List<PathSegment> segments = ctx.getUriInfo().getPathSegments();
        if ((MISSING_RESOURCE.equals(res) || DELETED_RESOURCE.equals(res)) && segments.stream().map(PathSegment::getPath).anyMatch(s -> !s.isEmpty())) {
            ctx.setMethod(POST);
            ctx.setRequestUri(ctx.getUriInfo().getBaseUriBuilder().path(getAllButLastSegment(segments)).build());
            ctx.getHeaders().putSingle(SLUG, getLastSegment(segments));
        }
    } else if ("MKCOL".equals(ctx.getMethod())) {
        // Note: MKCOL is just a POST with Link: <ldp:BasicContainer>; rel=type and appropriate Slug header
        final List<PathSegment> segments = ctx.getUriInfo().getPathSegments();
        final String slug = getLastSegment(segments);
        if (slug.isEmpty()) {
            // cannot POST a new root resource
            ctx.abortWith(status(CONFLICT).build());
        } else {
            ctx.setRequestUri(ctx.getUriInfo().getBaseUriBuilder().path(getAllButLastSegment(segments)).build());
            ctx.setMethod(POST);
            ctx.getHeaders().putSingle(SLUG, getLastSegment(segments));
            ctx.getHeaders().putSingle(LINK, Link.fromUri(LDP.BasicContainer.getIRIString()).rel(TYPE).build().toString());
        }
    } else if (DELETE.equals(ctx.getMethod())) {
        recursiveDelete(services, HttpSession.from(ctx.getSecurityContext()), identifier, getBaseUrl(baseUrl, ctx.getUriInfo()));
    }
}
Also used : DELETE(javax.ws.rs.HttpMethod.DELETE) LINK(javax.ws.rs.core.HttpHeaders.LINK) Provider(javax.ws.rs.ext.Provider) ServiceBundler(org.trellisldp.common.ServiceBundler) ContainerRequestFilter(javax.ws.rs.container.ContainerRequestFilter) Resource(org.trellisldp.api.Resource) ContainerRequestContext(javax.ws.rs.container.ContainerRequestContext) SLUG(org.trellisldp.common.HttpConstants.SLUG) Inject(javax.inject.Inject) WebDAVUtils.getAllButLastSegment(org.trellisldp.webdav.impl.WebDAVUtils.getAllButLastSegment) CONFIG_HTTP_PUT_UNCONTAINED(org.trellisldp.common.HttpConstants.CONFIG_HTTP_PUT_UNCONTAINED) Response.status(javax.ws.rs.core.Response.status) LDP(org.trellisldp.vocabulary.LDP) TYPE(javax.ws.rs.core.Link.TYPE) DELETED_RESOURCE(org.trellisldp.api.Resource.SpecialResources.DELETED_RESOURCE) PathSegment(javax.ws.rs.core.PathSegment) CONFLICT(javax.ws.rs.core.Response.Status.CONFLICT) CONFIG_HTTP_BASE_URL(org.trellisldp.common.HttpConstants.CONFIG_HTTP_BASE_URL) ConfigProvider.getConfig(org.eclipse.microprofile.config.ConfigProvider.getConfig) Config(org.eclipse.microprofile.config.Config) PreMatching(javax.ws.rs.container.PreMatching) List(java.util.List) IRI(org.apache.commons.rdf.api.IRI) POST(javax.ws.rs.HttpMethod.POST) HttpSession(org.trellisldp.common.HttpSession) WebDAVUtils.getLastSegment(org.trellisldp.webdav.impl.WebDAVUtils.getLastSegment) UriInfo(javax.ws.rs.core.UriInfo) PUT(javax.ws.rs.HttpMethod.PUT) WebDAVUtils.recursiveDelete(org.trellisldp.webdav.impl.WebDAVUtils.recursiveDelete) MISSING_RESOURCE(org.trellisldp.api.Resource.SpecialResources.MISSING_RESOURCE) Link(javax.ws.rs.core.Link) IRI(org.apache.commons.rdf.api.IRI) Resource(org.trellisldp.api.Resource) List(java.util.List) PathSegment(javax.ws.rs.core.PathSegment)

Example 4 with TYPE

use of javax.ws.rs.core.Link.TYPE in project trellis by trellis-ldp.

the class MementoResource method getTimeMapBuilder.

/**
 * Create a response builder for a TimeMap response.
 *
 * @param mementos the mementos
 * @param baseUrl the base URL
 * @param req the LDP request
 * @return a response builder object
 */
public ResponseBuilder getTimeMapBuilder(final SortedSet<Instant> mementos, final TrellisRequest req, final String baseUrl) {
    final List<MediaType> acceptableTypes = req.getAcceptableMediaTypes();
    final String identifier = HttpUtils.buildResourceUrl(req, baseUrl);
    final List<Link> allLinks = getMementoLinks(identifier, mementos).collect(toList());
    final ResponseBuilder builder = ok().link(identifier, ORIGINAL + " " + TIMEGATE);
    builder.links(getMementoHeaders(identifier, mementos).map(this::filterLinkParams).toArray(Link[]::new)).link(Resource.getIRIString(), TYPE).link(RDFSource.getIRIString(), TYPE).header(ALLOW, join(",", GET, HEAD, OPTIONS));
    final RDFSyntax syntax = getSyntax(trellis.getIOService(), acceptableTypes, APPLICATION_LINK_FORMAT);
    if (syntax != null) {
        final IRI profile = getProfile(acceptableTypes, syntax);
        final IRI jsonldProfile = profile != null ? profile : compacted;
        return builder.type(syntax.mediaType()).entity((StreamingOutput) out -> trellis.getIOService().write(trellis.getTimemapGenerator().asRdf(identifier, allLinks), out, syntax, baseUrl, jsonldProfile));
    }
    return builder.type(APPLICATION_LINK_FORMAT).entity(allLinks.stream().map(this::filterLinkParams).map(Link::toString).collect(joining(",\n")) + "\n");
}
Also used : Stream.empty(java.util.stream.Stream.empty) DATETIME(org.trellisldp.common.HttpConstants.DATETIME) HttpUtils.getProfile(org.trellisldp.http.impl.HttpUtils.getProfile) SortedSet(java.util.SortedSet) GET(javax.ws.rs.HttpMethod.GET) ServiceBundler(org.trellisldp.common.ServiceBundler) UriBuilder.fromUri(javax.ws.rs.core.UriBuilder.fromUri) Resource(org.trellisldp.vocabulary.LDP.Resource) ALLOW(javax.ws.rs.core.HttpHeaders.ALLOW) ORIGINAL(org.trellisldp.common.HttpConstants.ORIGINAL) Collections.singletonList(java.util.Collections.singletonList) TIMEMAP(org.trellisldp.common.HttpConstants.TIMEMAP) HttpUtils.getSyntax(org.trellisldp.http.impl.HttpUtils.getSyntax) MediaType(javax.ws.rs.core.MediaType) String.join(java.lang.String.join) FROM(org.trellisldp.common.HttpConstants.FROM) UNTIL(org.trellisldp.common.HttpConstants.UNTIL) RDFSource(org.trellisldp.vocabulary.LDP.RDFSource) Arrays.asList(java.util.Arrays.asList) OPTIONS(javax.ws.rs.HttpMethod.OPTIONS) Response.status(javax.ws.rs.core.Response.status) Stream.concat(java.util.stream.Stream.concat) TYPE(javax.ws.rs.core.Link.TYPE) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) JSONLD.compacted(org.trellisldp.vocabulary.JSONLD.compacted) SECONDS(java.time.temporal.ChronoUnit.SECONDS) TrellisRequest(org.trellisldp.common.TrellisRequest) ZonedDateTime.ofInstant(java.time.ZonedDateTime.ofInstant) Iterator(java.util.Iterator) Collections.emptyList(java.util.Collections.emptyList) Stream.of(java.util.stream.Stream.of) APPLICATION_LINK_FORMAT(org.trellisldp.common.HttpConstants.APPLICATION_LINK_FORMAT) StreamingOutput(javax.ws.rs.core.StreamingOutput) FOUND(javax.ws.rs.core.Response.Status.FOUND) Instant(java.time.Instant) Collectors.joining(java.util.stream.Collectors.joining) Collectors.toList(java.util.stream.Collectors.toList) HEAD(javax.ws.rs.HttpMethod.HEAD) List(java.util.List) IRI(org.apache.commons.rdf.api.IRI) TIMEGATE(org.trellisldp.common.HttpConstants.TIMEGATE) Stream(java.util.stream.Stream) RDFSyntax(org.apache.commons.rdf.api.RDFSyntax) Response.ok(javax.ws.rs.core.Response.ok) UTC(java.time.ZoneOffset.UTC) RFC_1123_DATE_TIME(java.time.format.DateTimeFormatter.RFC_1123_DATE_TIME) Link(javax.ws.rs.core.Link) MEMENTO(org.trellisldp.common.HttpConstants.MEMENTO) IRI(org.apache.commons.rdf.api.IRI) MediaType(javax.ws.rs.core.MediaType) RDFSyntax(org.apache.commons.rdf.api.RDFSyntax) ResponseBuilder(javax.ws.rs.core.Response.ResponseBuilder) Link(javax.ws.rs.core.Link)

Aggregations

TYPE (javax.ws.rs.core.Link.TYPE)4 IRI (org.apache.commons.rdf.api.IRI)4 Stream (java.util.stream.Stream)3 IOException (java.io.IOException)2 InputStream (java.io.InputStream)2 Arrays.asList (java.util.Arrays.asList)2 Collections.emptySet (java.util.Collections.emptySet)2 List (java.util.List)2 Optional.of (java.util.Optional.of)2 CompletionException (java.util.concurrent.CompletionException)2 CompletionStage (java.util.concurrent.CompletionStage)2 BadRequestException (javax.ws.rs.BadRequestException)2 Link (javax.ws.rs.core.Link)2 Link.fromUri (javax.ws.rs.core.Link.fromUri)2 TEXT_PLAIN (javax.ws.rs.core.MediaType.TEXT_PLAIN)2 Response (javax.ws.rs.core.Response)2 BAD_REQUEST (javax.ws.rs.core.Response.Status.BAD_REQUEST)2 CONFLICT (javax.ws.rs.core.Response.Status.CONFLICT)2 CREATED (javax.ws.rs.core.Response.Status.CREATED)2 Response.status (javax.ws.rs.core.Response.status)2