001/*
002 * (C) Copyright 2006-2007 Nuxeo SAS (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 *     Nuxeo - initial API and implementation
016 *
017 * $Id: JOOoConvertPluginImpl.java 18651 2007-05-13 20:28:53Z sfermigier $
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.restlet.Filter;
025import org.restlet.Restlet;
026import org.restlet.data.MediaType;
027import org.restlet.data.Request;
028import org.restlet.data.Response;
029import org.restlet.data.Status;
030
031/**
032 * Restlet Filter that ensure thread safety for seam unaware restlet.
033 *
034 * @author <a href="mailto:ldoguin@nuxeo.com">Laurent Doguin</a>
035 */
036public class ThreadSafeRestletFilter extends Filter {
037
038    private static final Log log = LogFactory.getLog(ThreadSafeRestletFilter.class);
039
040    @Override
041    protected void beforeHandle(Request request, Response response) {
042    }
043
044    @Override
045    protected void afterHandle(Request request, Response response) {
046    }
047
048    @Override
049    protected void doHandle(Request request, Response response) {
050        if (getNext() != null) {
051            try {
052                // get a new instance of the restlet each time it is called.
053                Restlet next = getNext().getClass().newInstance();
054                next.handle(request, response);
055            } catch (ReflectiveOperationException e) {
056                log.error("Restlet handling error", e);
057                response.setEntity("Error while getting a new Restlet instance: " + e.getMessage(),
058                        MediaType.TEXT_PLAIN);
059            }
060        } else {
061            response.setStatus(Status.CLIENT_ERROR_NOT_FOUND);
062        }
063    }
064
065}