    001// Copyright 2007, 2008, 2012 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
    015package org.apache.tapestry5.internal.renderers;
    016
    017import org.apache.tapestry5.MarkupWriter;
    018import org.apache.tapestry5.SymbolConstants;
    019import org.apache.tapestry5.ioc.annotations.Primary;
    020import org.apache.tapestry5.ioc.annotations.Symbol;
    021import org.apache.tapestry5.ioc.internal.util.CollectionFactory;
    022import org.apache.tapestry5.ioc.internal.util.InternalUtils;
    023import org.apache.tapestry5.services.Context;
    024import org.apache.tapestry5.services.ObjectRenderer;
    025import org.apache.tapestry5.services.Request;
    026
    027import java.util.List;
    028
    029public class RequestRenderer implements ObjectRenderer<Request>
    030{
    031    private final Context context;
    032
    033    private final String contextPath;
    034
    035    private final ObjectRenderer masterObjectRenderer;
    036
    037    public RequestRenderer(@Primary ObjectRenderer masterObjectRenderer, Context context, @Symbol(SymbolConstants.CONTEXT_PATH) String contextPath)
    038    {
    039        this.masterObjectRenderer = masterObjectRenderer;
    040        this.context = context;
    041        this.contextPath = contextPath;
    042    }
    043
    044    public void render(Request request, MarkupWriter writer)
    045    {
    046        coreProperties(request, writer);
    047        parameters(request, writer);
    048        headers(request, writer);
    049        attributes(request, writer);
    050        context(writer);
    051    }
    052
    053    private void coreProperties(Request request, MarkupWriter writer)
    054    {
    055        writer.element("dl", "class", "dl-horizontal");
    056
    057        dt(writer, "Context Path");
    058
    059        writer.element("dd");
    060
    061        if (contextPath.equals(""))
    062        {
    063            writer.element("em");
    064            writer.write("none (deployed as root)");
    065            writer.end();
    066        } else
    067        {
    068            writer.write(contextPath);
    069        }
    070
    071        writer.end(); // dd
    072
    073        dt(writer, "Path", request.getPath());
    074
    075        dt(writer, "Locale", request.getLocale().toString());
    076
    077        dt(writer, "Server Name", request.getServerName());
    078
    079
    080        List<String> flags = CollectionFactory.newList();
    081        if (request.isSecure())
    082        {
    083            flags.add("secure");
    084        }
    085
    086        if (request.isXHR())
    087        {
    088            flags.add("XHR");
    089        }
    090
    091        if (request.isRequestedSessionIdValid())
    092        {
    093            flags.add("requested session id valid");
    094        }
    095
    096        if (request.isSessionInvalidated())
    097        {
    098            flags.add("session invalidated");
    099        }
    100
    101        if (!flags.isEmpty())
    102        {
    103            dt(writer, "Flags", InternalUtils.join(flags));
    104        }
    105
    106        dt(writer, "Ports (local/server)",
    107                String.format("%d / %d", request.getLocalPort(), request.getServerPort()));
    108
    109        dt(writer, "Method", request.getMethod());
    110
    111        writer.end();
    112    }
    113
    114    private void context(MarkupWriter writer)
    115    {
    116        List<String> attributeNames = context.getAttributeNames();
    117
    118        if (attributeNames.isEmpty()) return;
    119
    120        section(writer, "Context Attributes");
    121
    122        writer.element("dl");
    123
    124        for (String name : attributeNames)
    125        {
    126            dt(writer, name);
    127
    128            writer.element("dd");
    129
    130            masterObjectRenderer.render(context.getAttribute(name), writer);
    131
    132            writer.end(); // dd
    133        }
    134
    135        writer.end(); // dl
    136    }
    137
    138    private void parameters(Request request, MarkupWriter writer)
    139    {
    140        List<String> parameterNames = request.getParameterNames();
    141
    142        if (parameterNames.isEmpty())
    143            return;
    144
    145        section(writer, "Query Parameters");
    146
    147        writer.element("dl");
    148
    149        for (String name : parameterNames)
    150        {
    151            String[] values = request.getParameters(name);
    152
    153            dt(writer, name);
    154
    155            writer.element("dd");
    156
    157            if (values.length > 1)
    158            {
    159                writer.element("ul");
    160
    161                for (String value : values)
    162                {
    163                    writer.element("li");
    164                    writer.write(value);
    165                    writer.end();
    166                }
    167
    168                writer.end(); // ul
    169            } else
    170            {
    171                writer.write(values[0]);
    172            }
    173
    174            writer.end(); // dd
    175        }
    176
    177        writer.end(); // dl
    178    }
    179
    180    private void dt(MarkupWriter writer, String name, String value)
    181    {
    182        if (value != null)
    183        {
    184            dt(writer, name);
    185            dd(writer, value);
    186        }
    187    }
    188
    189    private void dt(MarkupWriter writer, String name)
    190    {
    191        writer.element("dt");
    192        writer.write(name);
    193        writer.end();
    194    }
    195
    196    private void dd(MarkupWriter writer, String name)
    197    {
    198        writer.element("dd");
    199        writer.write(name);
    200        writer.end();
    201    }
    202
    203    private void section(MarkupWriter writer, String name)
    204    {
    205        writer.element("h3");
    206        writer.write(name);
    207        writer.end();
    208    }
    209
    210    private void headers(Request request, MarkupWriter writer)
    211    {
    212        section(writer, "Headers");
    213
    214        writer.element("dl", "class", "dl-horizontal");
    215
    216        for (String name : request.getHeaderNames())
    217        {
    218            dt(writer, name, request.getHeader(name));
    219        }
    220
    221        writer.end(); // dl
    222
    223    }
    224
    225    private void attributes(Request request, MarkupWriter writer)
    226    {
    227        List<String> attributeNames = request.getAttributeNames();
    228
    229        if (attributeNames.isEmpty())
    230        {
    231            return;
    232        }
    233
    234        section(writer, "Attributes");
    235
    236        writer.element("dl");
    237
    238        for (String name : attributeNames)
    239        {
    240            dt(writer, name, String.valueOf(request.getAttribute(name)));
    241        }
    242
    243        writer.end(); // dl
    244    }
    245
    246}