001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.access.pattern;
015
016import java.util.Arrays;
017
018import ch.qos.logback.access.spi.IAccessEvent;
019import ch.qos.logback.core.util.OptionHelper;
020
021public class RequestParameterConverter extends AccessConverter {
022
023    String key;
024
025    @Override
026    public void start() {
027        key = getFirstOption();
028        if (OptionHelper.isNullOrEmpty(key)) {
029            addWarn("Missing key for the request parameter");
030        } else {
031            super.start();
032        }
033    }
034
035    @Override
036    public String convert(IAccessEvent accessEvent) {
037        if (!isStarted()) {
038            return "INACTIVE_REQUEST_PARAM_CONV";
039        }
040
041        String[] paramArray = accessEvent.getRequestParameter(key);
042        if (paramArray.length == 1) {
043            return paramArray[0];
044        } else {
045            // for an array string {"a", "b"} named 'sa', Array.toString(sa) returns the
046            // string
047            // "[a, b]".
048            return Arrays.toString(paramArray);
049        }
050    }
051
052}