001/*
002 * (C) Copyright 2010 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     Florent Guillaume
016 */
017
018package org.nuxeo.ecm.platform.ui.web.restAPI;
019
020import org.apache.commons.logging.Log;
021import org.apache.commons.logging.LogFactory;
022import org.nuxeo.runtime.api.Framework;
023import org.restlet.data.Form;
024import org.restlet.data.Method;
025import org.restlet.data.Request;
026import org.restlet.data.Response;
027
028/**
029 * Small restlet logging in the commons logging log.
030 * <p>
031 * Very useful when running functional tests for instance, to separate cleanly what happens in the logs.
032 */
033public class SystemLogRestlet extends BaseStatelessNuxeoRestlet {
034
035    private static final Log log = LogFactory.getLog(SystemLogRestlet.class);
036
037    public static final String LEVEL = "level";
038
039    public static final String MESSAGE = "message";
040
041    public static final String TOKEN = "token";
042
043    public static final String TOKEN_PROP = "org.nuxeo.systemlog.token";
044
045    @Override
046    protected void doHandleStatelessRequest(Request req, Response res) {
047        if (Method.HEAD.equals(req.getMethod())) {
048            // selenium does a HEAD first, then a GET
049            return;
050        }
051        Form form = req.getResourceRef().getQueryAsForm();
052        String level = form.getFirstValue(LEVEL);
053        String message = form.getFirstValue(MESSAGE);
054        String token = form.getFirstValue(TOKEN);
055        String tokenProp = Framework.getProperty(TOKEN_PROP);
056        if (tokenProp == null || !tokenProp.equals(token)) {
057            log.debug(String.format("Provided token '%s' does not match %s", token, TOKEN_PROP));
058        } else if ("error".equalsIgnoreCase(level)) {
059            log.error(message);
060        } else if ("warn".equalsIgnoreCase(level)) {
061            log.warn(message);
062        } else if ("info".equalsIgnoreCase(level)) {
063            log.info(message);
064        } else if ("debug".equalsIgnoreCase(level)) {
065            log.debug(message);
066        } else {
067            log.trace(message);
068        }
069    }
070
071}