001/*
002 * (C) Copyright 2006-2008 Nuxeo SA (http://nuxeo.com/) and others.
003 *
004 * Licensed under the Apache License, Version 2.0 (the "License");
005 * you may not use this file except in compliance with the License.
006 * You may obtain a copy of the License at
007 *
008 *     http://www.apache.org/licenses/LICENSE-2.0
009 *
010 * Unless required by applicable law or agreed to in writing, software
011 * distributed under the License is distributed on an "AS IS" BASIS,
012 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
013 * See the License for the specific language governing permissions and
014 * limitations under the License.
015 *
016 * Contributors:
017 *     arussel
018 */
019package org.nuxeo.ecm.platform.web.common.exceptionhandling.service;
020
021import java.util.ArrayList;
022import java.util.Enumeration;
023import java.util.List;
024
025import javax.servlet.http.HttpServletRequest;
026
027import org.apache.commons.logging.Log;
028import org.apache.commons.logging.LogFactory;
029
030/**
031 * @author arussel
032 */
033public class DefaultRequestDumper implements RequestDumper {
034
035    private static final Log log = LogFactory.getLog(DefaultRequestDumper.class);
036
037    protected List<String> attributes = new ArrayList<>();
038
039    @Override
040    public String getDump(HttpServletRequest request) {
041        StringBuilder builder = new StringBuilder();
042        builder.append("\nRequest Attributes:\n\n");
043        Enumeration<String> e = request.getAttributeNames();
044        while (e.hasMoreElements()) {
045            String name = e.nextElement();
046            if (attributes.contains(name)) {
047                continue;
048            }
049            builder.append(name);
050            builder.append(" : ");
051            try {
052                Object obj = request.getAttribute(name);
053                builder.append(obj.toString());
054            } catch (RuntimeException exc) {
055                // avoid errors when printing the error dump
056                log.error("ERROR TRYING TO GET THIS REQUEST ATTRIBUTE VALUE: " + name);
057                builder.append("ERROR TRYING TO GET THIS REQUEST ATTRIBUTE VALUE");
058            }
059            builder.append("\n");
060        }
061        return builder.toString();
062    }
063
064    @Override
065    public void setNotListedAttributes(List<String> attributes) {
066        this.attributes = attributes;
067    }
068
069}