use of javax.ws.rs.HttpMethod.HEAD in project servicetalk by apple.
the class DefaultContainerResponseWriter method sendResponse.
private void sendResponse(final long contentLength, @Nullable final Publisher<Buffer> content, final ContainerResponse containerResponse) {
final HttpResponseStatus status = getStatus(containerResponse);
final StreamingHttpResponse response;
if (content != null && !isHeadRequest()) {
final HttpExecutionStrategy executionStrategy = getResponseExecutionStrategy(request);
// TODO(scott): use request factory methods that accept a payload body to avoid overhead of payloadBody.
final Publisher<Buffer> payloadBody = (executionStrategy != null && executionStrategy.isSendOffloaded() ? content.subscribeOn(serviceCtx.executionContext().executor(), IoThreadFactory.IoThread::currentThreadIsIoThread) : content).beforeCancel(// Cleanup internal state if server cancels response body
this::cancelResponse);
response = responseFactory.newResponse(status).version(protocolVersion).payloadBody(payloadBody);
} else {
response = responseFactory.newResponse(status).version(protocolVersion);
}
final HttpHeaders headers = response.headers();
// If we use HTTP/2 protocol all headers MUST be in lower case
final boolean isH2 = response.version().major() == 2;
containerResponse.getHeaders().forEach((k, vs) -> vs.forEach(v -> {
headers.add(isH2 ? k.toLowerCase() : k, v == null ? emptyAsciiString() : asCharSequence(v));
}));
if (!headers.contains(CONTENT_LENGTH)) {
if (contentLength == UNKNOWN_RESPONSE_LENGTH) {
// We can omit Transfer-Encoding for HEAD per https://tools.ietf.org/html/rfc7231#section-4.3.2
if (!isHeadRequest() && !HTTP_1_0.equals(protocolVersion)) {
headers.set(TRANSFER_ENCODING, CHUNKED);
}
} else {
headers.set(CONTENT_LENGTH, contentLength == 0 ? ZERO : Long.toString(contentLength));
headers.removeIgnoreCase(TRANSFER_ENCODING, CHUNKED);
}
}
responseSubscriber.onSuccess(response);
}
use of javax.ws.rs.HttpMethod.HEAD 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");
}
use of javax.ws.rs.HttpMethod.HEAD in project trellis by trellis-ldp.
the class GetHandler method getLdpNr.
private CompletionStage<ResponseBuilder> getLdpNr(final ResponseBuilder builder) {
final EntityTag etag = generateEtag(getResource().getRevision());
checkCache(getResource().getModified(), etag);
final IRI dsid = getResource().getBinaryMetadata().map(BinaryMetadata::getIdentifier).orElse(null);
// Add standard headers
builder.header(ACCEPT_RANGES, "bytes").tag(etag);
addAllowHeaders(builder);
// Short circuit HEAD requests
if (HEAD.equals(getRequest().getMethod())) {
return completedFuture(builder);
}
// Stream the binary content
return getBinaryStream(dsid, getRequest()).thenApply(in -> (StreamingOutput) out -> copy(in, out)).thenApply(builder::entity);
}
use of javax.ws.rs.HttpMethod.HEAD in project trellis by trellis-ldp.
the class GetHandler method getLdpRs.
private ResponseBuilder getLdpRs(final ResponseBuilder builder, final RDFSyntax syntax, final IRI profile) {
final Prefer prefer = getRequest().getPrefer();
// Check for a cache hit
if (!RDFSyntax.RDFA.equals(syntax)) {
final EntityTag etag = generateEtag(getResource().getRevision(), weakEtags);
checkCache(getResource().getModified(), etag);
builder.tag(etag);
}
addAllowHeaders(builder);
if (prefer != null) {
builder.header(PREFERENCE_APPLIED, PREFER_RETURN + "=" + prefer.getPreference().orElse(PREFER_REPRESENTATION));
if (prefer.getPreference().filter(PREFER_MINIMAL::equals).isPresent()) {
return builder.status(NO_CONTENT);
}
}
// Short circuit HEAD requests
if (HEAD.equals(getRequest().getMethod())) {
return builder;
}
// Stream the rdf content
return builder.entity((StreamingOutput) out -> {
try (final Stream<Quad> stream = getResource().stream(getPreferredGraphs(prefer))) {
getServices().getIOService().write(stream.map(Quad::asTriple).map(unskolemizeTriples(getServices().getResourceService(), getBaseUrl())), out, syntax, getIdentifier(), getJsonLdProfile(profile, syntax));
}
});
}
Aggregations