1 /**
2 * Licensed to the Apache Software Foundation (ASF) under one
3 * or more contributor license agreements.  See the NOTICE file
4 * distributed with this work for additional information
5 * regarding copyright ownership.  The ASF licenses this file
6 * to you under the Apache License, Version 2.0 (the
7 * "License"); you may not use this file except in compliance
8 * with the License.  You may obtain a copy of the License at
9 *
10 *     http://www.apache.org/licenses/LICENSE-2.0
11 *
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
17 */
18 
19 package org.apache.hadoop.yarn.webapp;
20 
21 import com.google.common.collect.Lists;
22 import com.google.inject.servlet.RequestScoped;
23 
24 import java.util.Iterator;
25 import java.util.List;
26 
27 import org.apache.hadoop.classification.InterfaceAudience;
28 
29 /**
30  * A class to help passing around request scoped info
31  */
32 @InterfaceAudience.LimitedPrivate({"YARN", "MapReduce"})
33 @RequestScoped
34 public class ResponseInfo implements Iterable<ResponseInfo.Item> {
35 
36   public static class Item {
37     public final String key;
38     public final String url;
39     public final Object value;
40     public final boolean isRaw;
41 
Item(String key, String url, Object value, boolean isRaw)42     Item(String key, String url, Object value, boolean isRaw) {
43       this.key = key;
44       this.url = url;
45       this.value = value;
46       this.isRaw = isRaw;
47     }
48 
of(String key, Object value, boolean isRaw)49     public static Item of(String key, Object value, boolean isRaw) {
50       return new Item(key, null, value, isRaw);
51     }
52 
of(String key, String url, Object value)53     public static Item of(String key, String url, Object value) {
54       return new Item(key, url, value, false);
55     }
56   }
57 
58   final List<Item> items = Lists.newArrayList();
59   String about = "Info";
60 
61   // Do NOT add any constructors here, unless...
62 
$about(String about)63   public static ResponseInfo $about(String about) {
64     ResponseInfo info = new ResponseInfo();
65     info.about = about;
66     return info;
67   }
68 
about(String about)69   public ResponseInfo about(String about) {
70     this.about = about;
71     return this;
72   }
73 
about()74   public String about() {
75     return about;
76   }
77 
_(String key, Object value)78   public ResponseInfo _(String key, Object value) {
79     items.add(Item.of(key, value, false));
80     return this;
81   }
82 
_(String key, String url, Object anchor)83   public ResponseInfo _(String key, String url, Object anchor) {
84     if (url == null) {
85       items.add(Item.of(key, anchor, false));
86     } else {
87       items.add(Item.of(key, url, anchor));
88     }
89     return this;
90   }
91 
92   //Value is raw HTML and shouldn't be escaped
_r(String key, Object value)93   public ResponseInfo _r(String key, Object value) {
94     items.add(Item.of(key, value, true));
95     return this;
96   }
97 
clear()98   public void clear() {
99     items.clear();
100   }
101 
102   @Override
iterator()103   public Iterator<Item> iterator() {
104     return items.iterator();
105   }
106 }
107