001/* 002 * (C) Copyright 2011 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.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.platform.ui.web.component.seam; 018 019import java.io.IOException; 020import java.net.URI; 021import java.util.HashMap; 022 023import javax.servlet.http.HttpServletRequest; 024 025import org.nuxeo.common.utils.URIUtils; 026import org.nuxeo.ecm.platform.ui.web.tag.fn.DocumentModelFunctions; 027import org.nuxeo.ecm.platform.web.common.vh.VirtualHostHelper; 028 029import com.lowagie.text.BadElementException; 030import com.lowagie.text.DocListener; 031import com.lowagie.text.Image; 032import com.lowagie.text.html.simpleparser.ChainedProperties; 033import com.lowagie.text.html.simpleparser.ImageProvider; 034 035/** 036 * Nuxeo image provider handling base url and authentication propagation when resolving resources on server. 037 * 038 * @since 5.4.2 039 */ 040public class NuxeoITextImageProvider implements ImageProvider { 041 042 protected final HttpServletRequest request; 043 044 public NuxeoITextImageProvider(HttpServletRequest request) { 045 super(); 046 this.request = request; 047 } 048 049 @Override 050 public Image getImage(String src, HashMap h, ChainedProperties cprops, DocListener doc) { 051 if (!src.startsWith("http")) { 052 // add base url 053 String base = VirtualHostHelper.getServerURL(request, false); 054 if (base != null && base.endsWith("/")) { 055 base = base.substring(0, base.length() - 1); 056 } 057 if (base != null) { 058 src = base + src; 059 } 060 } 061 // pass jsession id for authentication propagation 062 String uriPath = URIUtils.getURIPath(src); 063 src = uriPath + ";jsessionid=" + DocumentModelFunctions.extractJSessionId(request); 064 URI uri = URI.create(src); 065 String uriQuery = uri.getQuery(); 066 if (uriQuery != null && uriQuery.length() > 0) { 067 src = src + '?' + uriQuery; 068 } 069 try { 070 return Image.getInstance(src); 071 } catch (IOException e) { 072 throw new RuntimeException(e); 073 } catch (BadElementException e) { 074 throw new RuntimeException(e); 075 } 076 } 077}