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