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