Mercurial Hosting > nabble
comparison src/nabble/view/web/template/RequestNamespace.java @ 0:7ecd1a4ef557
add content
author | Franklin Schmidt <fschmidt@gmail.com> |
---|---|
date | Thu, 21 Mar 2019 19:15:52 -0600 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:7ecd1a4ef557 |
---|---|
1 package nabble.view.web.template; | |
2 | |
3 import fschmidt.util.java.HtmlUtils; | |
4 import fschmidt.util.servlet.ServletUtils; | |
5 import nabble.model.Node; | |
6 import nabble.model.Site; | |
7 import nabble.model.ViewCount; | |
8 import nabble.naml.compiler.Command; | |
9 import nabble.naml.compiler.CommandSpec; | |
10 import nabble.naml.compiler.IPrintWriter; | |
11 import nabble.naml.compiler.Interpreter; | |
12 import nabble.naml.compiler.Namespace; | |
13 import nabble.naml.compiler.ScopedInterpreter; | |
14 import nabble.naml.namespaces.StringList; | |
15 import nabble.naml.namespaces.TemplateException; | |
16 import nabble.view.lib.Jtp; | |
17 | |
18 import javax.servlet.ServletException; | |
19 import javax.servlet.http.HttpServletRequest; | |
20 import java.io.IOException; | |
21 import java.util.ArrayList; | |
22 import java.util.Arrays; | |
23 import java.util.Collections; | |
24 import java.util.HashMap; | |
25 import java.util.List; | |
26 import java.util.Map; | |
27 | |
28 | |
29 @Namespace ( | |
30 name = "http_request", | |
31 global = true | |
32 ) | |
33 public class RequestNamespace { | |
34 public final HttpServletRequest request; | |
35 | |
36 public RequestNamespace(HttpServletRequest request) { | |
37 this.request = request; | |
38 } | |
39 | |
40 public static final CommandSpec NAME = new CommandSpec.Builder() | |
41 .dotParameter("name") | |
42 .build() | |
43 ; | |
44 | |
45 public static final CommandSpec get_parameter = NAME; | |
46 | |
47 @Command public void get_parameter(IPrintWriter out,Interpreter interp) { | |
48 String name = interp.getArgString("name"); | |
49 out.print( request.getParameter(name) ); | |
50 } | |
51 | |
52 public static final CommandSpec get_request_header = NAME; | |
53 | |
54 @Command public void get_request_header(IPrintWriter out,Interpreter interp) { | |
55 String name = interp.getArgString("name"); | |
56 out.print( request.getHeader(name) ); | |
57 } | |
58 | |
59 public static final CommandSpec get_cookie_value = NAME; | |
60 | |
61 @Command public void get_cookie_value(IPrintWriter out,Interpreter interp) { | |
62 String name = interp.getArgString("name"); | |
63 out.print( ServletUtils.getCookieValue(request, name) ); | |
64 } | |
65 | |
66 @Command public void request_method(IPrintWriter out,Interpreter interp) { | |
67 out.print( request.getMethod() ); | |
68 } | |
69 | |
70 @Command public void servlet_path(IPrintWriter out,Interpreter interp) { | |
71 out.print( request.getServletPath() ); | |
72 } | |
73 | |
74 @Command public void server_name(IPrintWriter out,Interpreter interp) { | |
75 out.print( request.getServerName() ); | |
76 } | |
77 | |
78 @Command public void current_url(IPrintWriter out,Interpreter interp) { | |
79 out.print( interp.encode( ServletUtils.getCurrentURL(request) ) ); | |
80 } | |
81 | |
82 @Command public void current_path(IPrintWriter out,Interpreter interp) { | |
83 out.print( interp.encode( Jtp.getCurrentPath(request) ) ); | |
84 } | |
85 | |
86 // overridden in ServletNamespace | |
87 public static final CommandSpec get_node_from_request_parameter = CommandSpec.DO; | |
88 | |
89 @Command public void get_node_from_request_parameter(IPrintWriter out,ScopedInterpreter<NodeNamespace> interp) | |
90 throws IOException, ServletException, TemplateException | |
91 { | |
92 Node node = NabbleNamespace.current().site().getNode( Jtp.getLong(request, "node") ); | |
93 if( node != null ) | |
94 out.print( interp.getArg(new NodeNamespace(node),"do") ); | |
95 } | |
96 | |
97 public static final CommandSpec get_parameter_values = CommandSpec.DO() | |
98 .parameters("name") | |
99 .build() | |
100 ; | |
101 | |
102 @Command public void get_parameter_values(IPrintWriter out,ScopedInterpreter<ParameterValueList> interp) | |
103 throws IOException, ServletException | |
104 { | |
105 String name = interp.getArgString("name"); | |
106 String[] values = request.getParameterValues(name); | |
107 List<String> list = values == null ? Collections.<String>emptyList() : Arrays.asList(values); | |
108 Object block = interp.getArg(new ParameterValueList(list),"do"); | |
109 out.print(block); | |
110 } | |
111 | |
112 public static final CommandSpec get_parameters_pipe_separated = CommandSpec.DO() | |
113 .parameters("name") | |
114 .build() | |
115 ; | |
116 | |
117 @Command public void get_parameters_pipe_separated(IPrintWriter out,ScopedInterpreter<ParameterValueList> interp) { | |
118 String name = interp.getArgString("name"); | |
119 String values = request.getParameter(name); | |
120 List<String> list = values == null ? Collections.<String>emptyList() : Arrays.asList(values.split("\\|")); | |
121 for (int i = 0; i < list.size(); i++) { | |
122 list.set(i, HtmlUtils.urlDecode(list.get(i))); | |
123 } | |
124 Object block = interp.getArg(new ParameterValueList(list),"do"); | |
125 out.print(block); | |
126 } | |
127 | |
128 @Namespace ( | |
129 name = "parameter_value_list", | |
130 global = true | |
131 ) | |
132 public static final class ParameterValueList extends StringList { | |
133 | |
134 ParameterValueList(List<String> values) { | |
135 super(values); | |
136 } | |
137 | |
138 public List<String> values() { | |
139 return elements; | |
140 } | |
141 | |
142 @Command public void current_parameter_value(IPrintWriter out,Interpreter interp) { | |
143 out.print(get()); | |
144 } | |
145 | |
146 | |
147 // hack for now | |
148 | |
149 private Map<String,Integer> views = null; | |
150 | |
151 public static final CommandSpec node_views = new CommandSpec.Builder() | |
152 .parameters("node_id") | |
153 .build() | |
154 ; | |
155 | |
156 @Command public void node_views(IPrintWriter out,Interpreter interp) { | |
157 if( views == null ) { | |
158 Site site = NabbleNamespace.current().site(); | |
159 List<Long> ids = new ArrayList<Long>(); | |
160 for( String value : elements ) { | |
161 ids.add(Long.parseLong(value)); | |
162 } | |
163 views = new HashMap<String,Integer>(); | |
164 for( Map.Entry<Long,Integer> entry : ViewCount.getCounts(site,ids).entrySet() ) { | |
165 views.put( Long.toString(entry.getKey()), entry.getValue() ); | |
166 } | |
167 } | |
168 String nodeId = interp.getArgString("node_id"); | |
169 out.print( views.get(nodeId) ); | |
170 } | |
171 | |
172 } | |
173 | |
174 | |
175 | |
176 | |
177 | |
178 @Command public void request_base_url(IPrintWriter out,Interpreter interp) { | |
179 out.print( interp.encode( Jtp.getBaseUrl(request) ) ); | |
180 } | |
181 } |