View Javadoc
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.impl;
21  
22  import io.wcm.caravan.io.http.request.CaravanHttpRequest;
23  import io.wcm.caravan.pipeline.JsonPipelineOutput;
24  
25  import java.util.Iterator;
26  import java.util.List;
27  
28  import com.fasterxml.jackson.databind.JsonNode;
29  
30  /**
31   * Implementation of JsonPipelineOutput
32   */
33  public class JsonPipelineOutputImpl implements JsonPipelineOutput {
34  
35    private final JsonPipelineMetadata metadata;
36    private final JsonNode payload;
37    private final List<CaravanHttpRequest> requests;
38  
39    /**
40     * Creates pipeline output with the given payload
41     * @param payload
42     */
43    public JsonPipelineOutputImpl(JsonNode payload, List<CaravanHttpRequest> requests) {
44      this.metadata = new JsonPipelineMetadata(200);
45      this.payload = payload;
46      this.requests = requests;
47    }
48  
49    JsonPipelineOutputImpl(JsonPipelineMetadata metadata, JsonNode payload, List<CaravanHttpRequest> requests) {
50      this.metadata = metadata;
51      this.payload = payload;
52      this.requests = requests;
53    }
54  
55    JsonPipelineOutputImpl deepCopy() {
56      JsonPipelineOutputImpl copy = new JsonPipelineOutputImpl(payload, requests);
57      copy.getMetadata().setMaxAge(getMaxAge());
58      copy.getMetadata().setStatusCode(getStatusCode());
59      return copy;
60    }
61  
62    @Override
63    public JsonNode getPayload() {
64      return payload;
65    }
66  
67    public JsonPipelineMetadata getMetadata() {
68      return metadata;
69    }
70  
71    @Override
72    public JsonPipelineOutput withPayload(JsonNode newPayload) {
73      return new JsonPipelineOutputImpl(metadata, newPayload, requests);
74    }
75  
76    @Override
77    public int getStatusCode() {
78      return metadata.getStatusCode();
79    }
80  
81    @Override
82    public JsonPipelineOutput withStatusCode(int statusCode) {
83      JsonPipelineOutputImpl copy = deepCopy();
84      copy.getMetadata().setStatusCode(statusCode);
85      return copy;
86    }
87  
88    @Override
89    public int getMaxAge() {
90      return metadata.getMaxAge();
91    }
92  
93    @Override
94    public JsonPipelineOutput withMaxAge(int expirySeconds) {
95      JsonPipelineOutputImpl copy = deepCopy();
96      copy.getMetadata().setMaxAge(expirySeconds);
97      return copy;
98    }
99  
100   @Override
101   public String getCorrelationId() {
102     Iterator<CaravanHttpRequest> requestIterator = requests.iterator();
103     while (requestIterator.hasNext()) {
104       String correlationId = requestIterator.next().getCorrelationId();
105       if (correlationId != null) {
106         return correlationId;
107       }
108     }
109     return null;
110   }
111 
112   @Override
113   public List<CaravanHttpRequest> getRequests() {
114     return requests;
115   }
116 }