OutputProcessors.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.extensions.hal.crawler;

  21. import java.util.List;

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

  23. import com.google.common.collect.Iterables;

  24. import io.wcm.caravan.hal.resource.HalResource;
  25. import io.wcm.caravan.hal.resource.Link;
  26. import io.wcm.caravan.io.http.request.CaravanHttpRequest;
  27. import io.wcm.caravan.pipeline.JsonPipelineOutput;

  28. /**
  29.  * Default output processors
  30.  */
  31. @ProviderType
  32. public final class OutputProcessors {

  33.   private OutputProcessors() {
  34.     // nothing to do
  35.   }

  36.   /**
  37.    * Creates a flat Collection of HAL links for each processed request.
  38.    * @return Output processor creating flat links collection
  39.    */
  40.   public static OutputProcessor report() {
  41.     return new OutputProcessor() {

  42.       @Override
  43.       public JsonPipelineOutput process(JsonPipelineOutput previousStepOutput, List<JsonPipelineOutput> loadedLinkOutputs) {

  44.         // create output HAL
  45.         String url = getUrl(previousStepOutput);
  46.         HalResource hal = new HalResource(url);

  47.         // add URL with relation to links
  48.         CaravanHttpRequest request = getRequest(previousStepOutput);
  49.         if (request != null && request.getHeaders().containsKey(HalCrawler.HEADER_CRAWLER_RELATION)) {
  50.           String relation = Iterables.getFirst(request.getHeaders().get(HalCrawler.HEADER_CRAWLER_RELATION), null);
  51.           hal.addLinks(relation, new Link(url));
  52.         }

  53.         // add child links
  54.         loadedLinkOutputs.stream()
  55.             .map(loadedLinkOutput -> new HalResource(loadedLinkOutput.getPayload()))
  56.             .flatMap(loadedLinkHal -> loadedLinkHal.getLinks().entries().stream())
  57.             .filter(entry -> !"self".equals(entry.getKey()))
  58.             .forEach(entry -> hal.addLinks(entry.getKey(), entry.getValue()));

  59.         // output
  60.         return previousStepOutput.withPayload(hal.getModel());
  61.       }

  62.       @Override
  63.       public String getId() {
  64.         return "REPORT";
  65.       }

  66.     };
  67.   }

  68.   private static String getUrl(JsonPipelineOutput previousStepOutput) {
  69.     CaravanHttpRequest request = getRequest(previousStepOutput);
  70.     return request == null ? null : request.getUrl();
  71.   }

  72.   private static CaravanHttpRequest getRequest(JsonPipelineOutput previousStepOutput) {

  73.     List<CaravanHttpRequest> requests = previousStepOutput.getRequests();
  74.     if (requests.isEmpty()) {
  75.       return null;
  76.     }
  77.     return requests.get(requests.size() - 1);

  78.   }

  79. }