kwin commented on a change in pull request #19:
URL: 
https://github.com/apache/sling-org-apache-sling-jcr-resource/pull/19#discussion_r785062268



##########
File path: 
src/main/java/org/apache/sling/jcr/resource/internal/helper/jcr/BinaryDownloadUriProvider.java
##########
@@ -0,0 +1,141 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You 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.apache.sling.jcr.resource.internal.helper.jcr;
+
+import java.net.URI;
+
+import javax.jcr.Binary;
+import javax.jcr.ItemNotFoundException;
+import javax.jcr.Node;
+import javax.jcr.Property;
+import javax.jcr.RepositoryException;
+import javax.jcr.ValueFormatException;
+
+import org.apache.jackrabbit.api.binary.BinaryDownload;
+import org.apache.jackrabbit.api.binary.BinaryDownloadOptions;
+import 
org.apache.jackrabbit.api.binary.BinaryDownloadOptions.BinaryDownloadOptionsBuilder;
+import org.apache.sling.api.resource.Resource;
+import org.apache.sling.api.resource.external.URIProvider;
+import org.jetbrains.annotations.NotNull;
+import org.osgi.service.component.annotations.Activate;
+import org.osgi.service.component.annotations.Component;
+import org.osgi.service.component.annotations.ConfigurationPolicy;
+import org.osgi.service.metatype.annotations.AttributeDefinition;
+import org.osgi.service.metatype.annotations.Designate;
+import org.osgi.service.metatype.annotations.ObjectClassDefinition;
+
+/**
+ * Provides URIs for direct binary read-access based on the Jackrabbit API 
{@link BinaryDownload}.
+ * 
+ * @see <a 
href="https://jackrabbit.apache.org/oak/docs/features/direct-binary-access.html";>Oak
 Direct Binary Access</a>
+ *
+ */
+@Component(service = URIProvider.class, configurationPolicy = 
ConfigurationPolicy.REQUIRE)
+@Designate(ocd = BinaryDownloadUriProvider.Configuration.class)
+public class BinaryDownloadUriProvider implements URIProvider {
+
+    enum ContentDisposition {
+        INLINE,
+        ATTACHMENT
+    }
+
+    @ObjectClassDefinition(
+            name = "Apache Sling Binary Download URI Provider",
+            description = "Provides URIs for resources containing a primary 
JCR binary property backed by a blob store with external access")
+    public static @interface Configuration {
+        @AttributeDefinition(
+                name = "Content-Disposition",
+                description = "The content-disposition header to send when the 
binary is delivered via HTTP")
+        ContentDisposition contentDisposition();
+    }
+
+    private final boolean isContentDispositionAttachment;
+
+    @Activate
+    public BinaryDownloadUriProvider(Configuration configuration) {
+        this(configuration.contentDisposition() == 
ContentDisposition.ATTACHMENT);
+    }
+ 
+    BinaryDownloadUriProvider(boolean isContentDispositionAttachment) {
+        this.isContentDispositionAttachment = isContentDispositionAttachment;
+    }
+
+    @Override
+    public @NotNull URI toURI(@NotNull Resource resource, @NotNull Scope 
scope, @NotNull Operation operation) {
+        if (!isRelevantScopeAndOperation(scope, operation)) {
+            throw new IllegalArgumentException("This provider does only 
provide URIs for read operations in scope public or external, but not for scope 
" + scope + " and operation " + operation);
+        }
+        Node node = resource.adaptTo(Node.class);
+        if (node == null) {
+            throw new IllegalArgumentException("This provider only provide 
URIs for node-based resources");
+        }
+        try {
+            // get main property (probably containing binary data)
+            Property primaryProperty = 
JcrNodeResource.getPrimaryProperty(node);

Review comment:
       This is a specific implementation for JCR backed resources only. I don't 
see a reason, why this should not reuse code from JcrNodeResource.




-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to