001/*
002 * (C) Copyright 2010 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 *     Florent Guillaume
018 */
019
020package org.nuxeo.ecm.platform.ui.web.restAPI;
021
022import org.apache.commons.logging.Log;
023import org.apache.commons.logging.LogFactory;
024import org.nuxeo.runtime.api.Framework;
025import org.restlet.data.Form;
026import org.restlet.data.Method;
027import org.restlet.data.Request;
028import org.restlet.data.Response;
029
030/**
031 * Small restlet logging in the commons logging log.
032 * <p>
033 * Very useful when running functional tests for instance, to separate cleanly what happens in the logs.
034 */
035public class SystemLogRestlet extends BaseStatelessNuxeoRestlet {
036
037    private static final Log log = LogFactory.getLog(SystemLogRestlet.class);
038
039    public static final String LEVEL = "level";
040
041    public static final String MESSAGE = "message";
042
043    public static final String TOKEN = "token";
044
045    public static final String TOKEN_PROP = "org.nuxeo.systemlog.token";
046
047    @Override
048    protected void doHandleStatelessRequest(Request req, Response res) {
049        if (Method.HEAD.equals(req.getMethod())) {
050            // selenium does a HEAD first, then a GET
051            return;
052        }
053        Form form = req.getResourceRef().getQueryAsForm();
054        String level = form.getFirstValue(LEVEL);
055        String message = form.getFirstValue(MESSAGE);
056        String token = form.getFirstValue(TOKEN);
057        String tokenProp = Framework.getProperty(TOKEN_PROP);
058        if (tokenProp == null || !tokenProp.equals(token)) {
059            log.debug(String.format("Provided token '%s' does not match %s", token, TOKEN_PROP));
060        } else if ("error".equalsIgnoreCase(level)) {
061            log.error(message);
062        } else if ("warn".equalsIgnoreCase(level)) {
063            log.warn(message);
064        } else if ("info".equalsIgnoreCase(level)) {
065            log.info(message);
066        } else if ("debug".equalsIgnoreCase(level)) {
067            log.debug(message);
068        } else {
069            log.trace(message);
070        }
071    }
072
073}