Skip to content

Commit

Permalink
Merge pull request #2 from Buuhuu/feature/SLING-7357
Browse files Browse the repository at this point in the history
SLING-7357: set content type to forward distrib http headers
  • Loading branch information
tteofili authored Jan 9, 2018
2 parents 36c7bf8 + 4d8c725 commit 959ea05
Show file tree
Hide file tree
Showing 13 changed files with 66 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@
import org.apache.felix.scr.annotations.Reference;
import org.apache.felix.scr.annotations.ReferenceCardinality;
import org.apache.felix.scr.annotations.ReferencePolicy;
import org.apache.http.HttpHeaders;
import org.apache.jackrabbit.vault.packaging.Packaging;
import org.apache.sling.api.resource.ResourceResolverFactory;
import org.apache.sling.commons.osgi.PropertiesUtil;
Expand Down Expand Up @@ -246,8 +247,10 @@ protected SimpleDistributionAgent createAgent(String agentName, BundleContext co
Map<String, String> priorityQueues = PropertiesUtil.toMap(config.get(PRIORITY_QUEUES), new String[0]);
priorityQueues = SettingsUtils.removeEmptyEntries(priorityQueues);

Map<String, String> headers = new HashMap<String, String>(1);
headers.put(HttpHeaders.CONTENT_TYPE, packageBuilder.getContentType());
Integer timeout = PropertiesUtil.toInteger(HTTP, 10) * 1000;
HttpConfiguration httpConfiguration = new HttpConfiguration(timeout);
HttpConfiguration httpConfiguration = new HttpConfiguration(timeout, headers);

DistributionPackageExporter packageExporter = new LocalDistributionPackageExporter(packageBuilder);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -59,6 +59,11 @@ public String getType() {
return wrapped.getType();
}

@Override
public String getContentType() {
return wrapped.getContentType();
}

@Nonnull
@Override
public DistributionPackage createPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,12 @@ public interface DistributionPackageBuilder {
*/
String getType();

/**
* returns the content type of packages created and consumed by the package builder.
* @return the content type of the package
*/
String getContentType();

/**
* creates a {@link DistributionPackage} for a specific {@link org.apache.sling.distribution.DistributionRequest}
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -50,15 +50,21 @@ public abstract class AbstractDistributionPackageBuilder implements Distribution
private final Logger log = LoggerFactory.getLogger(getClass());

private final String type;
private final String contentType;

AbstractDistributionPackageBuilder(String type) {
AbstractDistributionPackageBuilder(String type, String contentType) {
this.type = type;
this.contentType = contentType;
}

public String getType() {
return type;
}

public String getContentType() {
return this.contentType;
}

@Nonnull
public DistributionPackage createPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request)
throws DistributionException {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ public FileDistributionPackageBuilder(String type,
String tempFilesFolder,
String digestAlgorithm, String[] nodeFilters,
String[] propertyFilters) {
super(type);
super(type, distributionContentSerializer.getContentType());
this.distributionContentSerializer = distributionContentSerializer;
this.nodeFilters = VltUtils.parseFilters(nodeFilters);
this.propertyFilters = VltUtils.parseFilters(propertyFilters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ public ResourceDistributionPackageBuilder(String type,
boolean useOffHeapMemory,
String digestAlgorithm, String[] nodeFilters,
String[] propertyFilters) {
super(type);
super(type, distributionContentSerializer.getContentType());
this.distributionContentSerializer = distributionContentSerializer;
this.nodeFilters = VltUtils.parseFilters(nodeFilters);
this.propertyFilters = VltUtils.parseFilters(propertyFilters);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,12 @@ void exportToStream(ResourceResolver resourceResolver, DistributionExportOptions
*/
String getName();

/**
* retrieve the mime type of the exported content of this content serializer
* @implNote the default implementation returns application/octet-stream.
*/
String getContentType();

/**
* whether or not this {@link DistributionContentSerializer} can build package filters for including / excluding
* certain resources / attributes directly from a {@link org.apache.sling.distribution.DistributionRequest}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,10 @@ public String getType() {
return packageBuilder.getType();
}

public String getContentType() {
return contentSerializer.getContentType();
}

@Nonnull
public DistributionPackage createPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
return packageBuilder.createPackage(resourceResolver, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,11 @@ public String getName() {
return name;
}

@Override
public String getContentType() {
return "application/octet-stream";
}

@Override
public boolean isRequestFiltering() {
return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -288,6 +288,10 @@ public String getType() {
return packageBuilder.getType();
}

public String getContentType() {
return packageBuilder.getContentType();
}

@Nonnull
public DistributionPackage createPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull DistributionRequest request) throws DistributionException {
return packageBuilder.createPackage(resourceResolver, request);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
* under the License.
*/

@Version("0.1.0")
@Version("1.0.0")
package org.apache.sling.distribution.serialization;

import aQute.bnd.annotation.Version;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,22 +18,34 @@
*/
package org.apache.sling.distribution.transport.impl;

import java.util.Collections;
import java.util.Map;

/**
* HTTP related configuration for {@link SimpleHttpDistributionTransport}
*/
public class HttpConfiguration {

private final Integer connectTimeout;
private final Integer socketTimeout;
private final Map<String, String> headers;

public HttpConfiguration(Integer timeout) {
this.socketTimeout = timeout;
this.connectTimeout = timeout;
this(timeout, timeout);
}

public HttpConfiguration(Integer connectTimeout, Integer socketTimeout) {
this(connectTimeout, socketTimeout, Collections.<String, String>emptyMap());
}

public HttpConfiguration(Integer timeout, Map<String, String> headers) {
this(timeout, timeout, headers);
}

public HttpConfiguration(Integer connectTimeout, Integer socketTimeout, Map<String, String> headers) {
this.connectTimeout = connectTimeout;
this.socketTimeout = socketTimeout;
this.headers = headers;
}

public Integer getConnectTimeout() {
Expand All @@ -43,4 +55,8 @@ public Integer getConnectTimeout() {
public Integer getSocketTimeout() {
return socketTimeout;
}

public Map<String, String> getHeaders() {
return headers;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ public void deliverPackage(@Nonnull ResourceResolver resourceResolver, @Nonnull
}
}

for (Map.Entry<String, String> header : httpConfiguration.getHeaders().entrySet()) {
req.addHeader(header.getKey(), header.getValue());
}

InputStream inputStream = null;
try {
inputStream = DistributionPackageUtils.createStreamWithHeader(distributionPackage);
Expand Down

0 comments on commit 959ea05

Please sign in to comment.