CacheControlUtils.java

  1. /*
  2.  * #%L
  3.  * wcm.io
  4.  * %%
  5.  * Copyright (C) 2014 wcm.io
  6.  * %%
  7.  * Licensed under the Apache License, Version 2.0 (the "License");
  8.  * you may not use this file except in compliance with the License.
  9.  * You may obtain a copy of the License at
  10.  *
  11.  *      http://www.apache.org/licenses/LICENSE-2.0
  12.  *
  13.  * Unless required by applicable law or agreed to in writing, software
  14.  * distributed under the License is distributed on an "AS IS" BASIS,
  15.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  16.  * See the License for the specific language governing permissions and
  17.  * limitations under the License.
  18.  * #L%
  19.  */
  20. package io.wcm.caravan.pipeline.cache;

  21. import java.util.ArrayList;
  22. import java.util.Arrays;
  23. import java.util.List;

  24. import org.osgi.annotation.versioning.ProviderType;

  25. import io.wcm.caravan.pipeline.JsonPipeline;
  26. import io.wcm.caravan.pipeline.JsonPipelineOutput;
  27. import rx.Observable;
  28. import rx.functions.Func1;
  29. import rx.observers.SafeSubscriber;

  30. /**
  31.  * Cache control utilities aid to manage pipeline output cache control meta data.
  32.  */
  33. @ProviderType
  34. public final class CacheControlUtils {

  35.   private CacheControlUtils() {
  36.     // static methods only
  37.   }

  38.   /**
  39.    * @param pipelineOutputs Pipeline outputs with {@code max-age} values
  40.    * @return the lowest max-age value of all the given pipeline outputs
  41.    */
  42.   public static int getLowestMaxAge(Iterable<JsonPipelineOutput> pipelineOutputs) {

  43.     return Observable
  44.         .from(pipelineOutputs)
  45.         .filter(output -> output != null)
  46.         .map(output -> output.getMaxAge())
  47.         .reduce(Math::min)
  48.         .toBlocking().single();
  49.   }

  50.   /**
  51.    * @param pipelineOutputs  Pipeline outputs with {@code max-age} values
  52.    * @return the lowest max-age value of all the given pipeline outputs
  53.    */
  54.   public static int getLowestMaxAge(JsonPipelineOutput... pipelineOutputs) {
  55.     return getLowestMaxAge(Arrays.asList(pipelineOutputs));
  56.   }

  57.   /**
  58.    * Aggregates multiple resources fetched with different {@link JsonPipeline} instances, into a single
  59.    * {@link JsonPipelineOutput} and ensures sure that the max-age Cache-Control-Header is set to the minimum value of
  60.    * all aggregated responses.
  61.    * @param pipelines an observable that emits MULTIPLE {@link JsonPipeline}s
  62.    * @param zipFunc a lambda that is given the list of all {@link JsonPipelineOutput}s when they have been retrieved
  63.    * @return a new observable that emits the aggregated JsonPipelineOutput with the correct max-age
  64.    */
  65.   public static Observable<JsonPipelineOutput> zipWithLowestMaxAge(
  66.       Observable<JsonPipeline> pipelines,
  67.       Func1<List<JsonPipelineOutput>, JsonPipelineOutput> zipFunc) {

  68.     Observable<Observable<JsonPipelineOutput>> outputs = pipelines.map(pipeline -> pipeline.getOutput());

  69.     return zipWithLowestMaxAgeInternal(outputs, zipFunc);
  70.   }

  71.   /**
  72.    * Aggregates multiple resources fetched with different {@link JsonPipeline} instances, into a single
  73.    * {@link JsonPipelineOutput} and ensures sure that the max-age Cache-Control-Header is set to the minimum value of
  74.    * all aggregated responses.
  75.    * @param pipelines the {@link JsonPipeline}s to zip
  76.    * @param zipFunc a lambda that is given the list of all {@link JsonPipelineOutput}s when they have been retrieved
  77.    * @return a new observable that emits the aggregated JsonPipelineOutput with the correct max-age
  78.    */
  79.   public static Observable<JsonPipelineOutput> zipWithLowestMaxAge(
  80.       Iterable<JsonPipeline> pipelines,
  81.       Func1<List<JsonPipelineOutput>, JsonPipelineOutput> zipFunc) {

  82.     Observable<Observable<JsonPipelineOutput>> outputs = Observable.from(pipelines).map(pipeline -> pipeline.getOutput());

  83.     return zipWithLowestMaxAgeInternal(outputs, zipFunc);
  84.   }


  85.   private static Observable<JsonPipelineOutput> zipWithLowestMaxAgeInternal(
  86.       Observable<Observable<JsonPipelineOutput>> multipleOutputs,
  87.       Func1<List<JsonPipelineOutput>, JsonPipelineOutput> zipFunc) {

  88.     // this method could just return the zippingObservable created below, but this lead to an odd bug when this
  89.     // method was called with an empty "multipleOutputs" observable. In that case, the OperatorZip called
  90.     // #onCompleted twice (is this an implementation bug in rx.java?), which was causing weird behaviour
  91.     // if #defaultIfEmpty was used on the zipped result (the default value was emitted twice)

  92.     // that behaviour can be avoided by adding a SafeSubscriber layer

  93.     return Observable.create(subscriber -> {

  94.       Observable<JsonPipelineOutput> zippingObservable = Observable.zip(multipleOutputs, (arrayOfOutputs) -> {

  95.         // collect the JsonPipelineOutput instances in a generic list (casting is required since FuncN only gives as a Object[])
  96.         List<JsonPipelineOutput> listOfOutputs = new ArrayList<>(arrayOfOutputs.length);
  97.         for (Object o : arrayOfOutputs) {
  98.           listOfOutputs.add((JsonPipelineOutput)o);
  99.         }

  100.         // calculate the zipping function to produce the aggregated resposne from all JsonPipelineOutputs
  101.         JsonPipelineOutput zippedOutput = zipFunc.call(listOfOutputs);

  102.         // then update the max age of the overall output with the lowest max-age values of all outputs in the list
  103.         int lowestMaxAge = getLowestMaxAge(listOfOutputs);
  104.         return zippedOutput.withMaxAge(lowestMaxAge);

  105.       });

  106.       zippingObservable.subscribe(new SafeSubscriber<JsonPipelineOutput>(subscriber));
  107.     });
  108.   }

  109. }