001/*
002 * (C) Copyright 2015 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-2.1.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 *     Anahide Tchertchian
016 */
017package org.nuxeo.ecm.restapi.server.jaxrs.resource.wro;
018
019import java.io.IOException;
020import java.io.OutputStream;
021import java.lang.annotation.Annotation;
022import java.lang.reflect.Type;
023import java.net.URI;
024import java.net.URISyntaxException;
025
026import javax.servlet.ServletContext;
027import javax.servlet.ServletException;
028import javax.servlet.http.HttpServletRequest;
029import javax.servlet.http.HttpServletResponse;
030import javax.ws.rs.WebApplicationException;
031import javax.ws.rs.core.Context;
032import javax.ws.rs.core.MediaType;
033import javax.ws.rs.core.MultivaluedMap;
034import javax.ws.rs.core.UriInfo;
035import javax.ws.rs.ext.MessageBodyWriter;
036import javax.ws.rs.ext.Provider;
037
038import org.apache.commons.logging.Log;
039import org.apache.commons.logging.LogFactory;
040import org.nuxeo.ecm.restapi.server.jaxrs.resource.wro.ResourceBundleEndpoint.ResourceBundleDispatcher;
041
042/**
043 * Writer for a resource bundle, used to dispatch jax-rs call internally to the wro servlet.
044 *
045 * @since 7.3
046 */
047@Provider
048public class ResourceBundleWriter implements MessageBodyWriter<ResourceBundleDispatcher> {
049
050    private static final Log log = LogFactory.getLog(ResourceBundleEndpoint.class);
051
052    @Context
053    protected UriInfo uriInfo;
054
055    @Context
056    protected ServletContext servletContext;
057
058    @Context
059    protected HttpServletRequest request;
060
061    @Context
062    protected HttpServletResponse response;
063
064    @Override
065    public long getSize(ResourceBundleDispatcher arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4) {
066        return -1;
067    }
068
069    @Override
070    public boolean isWriteable(Class<?> arg0, Type arg1, Annotation[] arg2, MediaType arg3) {
071        return ResourceBundleDispatcher.class.isAssignableFrom(arg0);
072    }
073
074    @Override
075    public void writeTo(ResourceBundleDispatcher arg0, Class<?> arg1, Type arg2, Annotation[] arg3, MediaType arg4,
076            MultivaluedMap<String, Object> arg5, OutputStream arg6) throws IOException, WebApplicationException {
077        try {
078            URI uri = uriInfo.getRequestUri();
079            String path = uri.getPath();
080            // remove lead /nuxeo
081            path = path.replaceFirst(servletContext.getContextPath(), "");
082            // redirect to the wro servlet path
083            path = path.replaceFirst("/site/api/", "/wapi/");
084            URI dispatch = new URI(null, null, path, uri.getQuery(), uri.getFragment());
085            servletContext.getRequestDispatcher(dispatch.toString()).forward(request, response);
086        } catch (URISyntaxException | ServletException e) {
087            log.error("Error while forwarding to Wro servlet", e);
088        }
089    }
090
091}