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