001    // Copyright 2007, 2008 The Apache Software Foundation
002    //
003    // Licensed under the Apache License, Version 2.0 (the "License");
004    // you may not use this file except in compliance with the License.
005    // You may obtain a copy of the License at
006    //
007    //     http://www.apache.org/licenses/LICENSE-2.0
008    //
009    // Unless required by applicable law or agreed to in writing, software
010    // distributed under the License is distributed on an "AS IS" BASIS,
011    // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012    // See the License for the specific language governing permissions and
013    // limitations under the License.
014    
015    package org.apache.tapestry5.internal.renderers;
016    
017    import org.apache.tapestry5.MarkupWriter;
018    import org.apache.tapestry5.internal.InternalConstants;
019    import org.apache.tapestry5.ioc.annotations.Primary;
020    import org.apache.tapestry5.services.Context;
021    import org.apache.tapestry5.services.ObjectRenderer;
022    import org.apache.tapestry5.services.Request;
023    
024    import java.util.List;
025    
026    public class RequestRenderer implements ObjectRenderer<Request>
027    {
028        private final Context context;
029    
030        private final ObjectRenderer masterObjectRenderer;
031    
032        public RequestRenderer(@Primary ObjectRenderer masterObjectRenderer, Context context)
033        {
034            this.masterObjectRenderer = masterObjectRenderer;
035            this.context = context;
036        }
037    
038        public void render(Request request, MarkupWriter writer)
039        {
040            writer.element("dl");
041    
042            dt(writer, "Context Path");
043    
044            writer.element("dd");
045    
046            String contextPath = request.getContextPath();
047    
048            if (contextPath.equals(""))
049            {
050                writer.element("em");
051                writer.write("none (deployed as root)");
052                writer.end();
053            }
054            else
055            {
056                writer.write(contextPath);
057            }
058            writer.end(); // dd
059    
060            dt(writer, "Request Path");
061            dd(writer, request.getPath());
062    
063            dt(writer, "Locale");
064            dd(writer, request.getLocale().toString());
065    
066            dt(writer, "Secure");
067            dd(writer, Boolean.toString(request.isSecure()));
068    
069            dt(writer, "Server Name");
070            dd(writer, request.getServerName());
071    
072            writer.end();
073    
074            parameters(request, writer);
075            headers(request, writer);
076            context(writer);
077        }
078    
079        private void context(MarkupWriter writer)
080        {
081            List<String> attributeNames = context.getAttributeNames();
082    
083            if (attributeNames.isEmpty()) return;
084    
085            section(writer, "Context Attributes");
086    
087            writer.element("dl");
088    
089            for (String name : attributeNames)
090            {
091                dt(writer, name);
092    
093                writer.element("dd");
094    
095                masterObjectRenderer.render(context.getAttribute(name), writer);
096    
097                writer.end(); // dd
098            }
099    
100            writer.end(); // dl
101        }
102    
103        private void parameters(Request request, MarkupWriter writer)
104        {
105            List<String> parameterNames = request.getParameterNames();
106    
107            if (parameterNames.isEmpty())
108                return;
109    
110            section(writer, "Query Parameters");
111    
112            writer.element("dl");
113    
114            for (String name : parameterNames)
115            {
116                String[] values = request.getParameters(name);
117    
118                dt(writer, name);
119    
120                writer.element("dd");
121    
122                if (values.length > 1)
123                {
124                    writer.element("ul");
125    
126                    for (String value : values)
127                    {
128                        writer.element("li");
129                        writer.write(value);
130                        writer.end();
131                    }
132    
133                    writer.end(); // ul
134                }
135                else
136                {
137                    writer.write(values[0]);
138                }
139    
140                writer.end(); // dd
141            }
142    
143            writer.end(); // dl
144        }
145    
146        private void dt(MarkupWriter writer, String name)
147        {
148            writer.element("dt");
149            writer.write(name);
150            writer.end();
151        }
152    
153        private void dd(MarkupWriter writer, String name)
154        {
155            writer.element("dd");
156            writer.write(name);
157            writer.end();
158        }
159    
160        private void section(MarkupWriter writer, String name)
161        {
162            writer.element("div", "class", InternalConstants.OBJECT_RENDER_DIV_SECTION);
163            writer.write(name);
164            writer.end();
165        }
166    
167        private void headers(Request request, MarkupWriter writer)
168        {
169            section(writer, "Headers");
170    
171            writer.element("dl");
172    
173            for (String name : request.getHeaderNames())
174            {
175                dt(writer, name);
176                dd(writer, request.getHeader(name));
177            }
178    
179            writer.end(); // dl
180    
181        }
182    
183    }