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.webapp.helpers; 023 024import static org.jboss.seam.ScopeType.CONVERSATION; 025 026import java.io.IOException; 027import java.io.Serializable; 028 029import javax.faces.context.FacesContext; 030import javax.servlet.http.HttpServletResponse; 031 032import org.apache.commons.logging.Log; 033import org.apache.commons.logging.LogFactory; 034import org.jboss.seam.annotations.In; 035import org.jboss.seam.annotations.Name; 036import org.jboss.seam.annotations.web.RequestParameter; 037import org.jboss.seam.annotations.Scope; 038import org.jboss.seam.core.ConversationEntry; 039import org.jboss.seam.core.Manager; 040import org.nuxeo.ecm.core.api.Blob; 041import org.nuxeo.ecm.core.api.CoreSession; 042import org.nuxeo.ecm.core.api.DocumentModel; 043import org.nuxeo.ecm.core.api.DocumentNotFoundException; 044import org.nuxeo.ecm.core.api.DocumentRef; 045import org.nuxeo.ecm.core.api.IdRef; 046import org.nuxeo.ecm.core.api.PropertyException; 047import org.nuxeo.ecm.platform.ui.web.api.NavigationContext; 048 049@Name("logoHelper") 050@Scope(CONVERSATION) 051public class LogoHelper implements Serializable { 052 053 private static final long serialVersionUID = 876540986876L; 054 055 private static final String PAGE_NAME = "/showLogo.faces"; 056 057 private static final String DEFAULT_LOGO = "/img/default_logo.gif"; 058 059 private static final Log log = LogFactory.getLog(LogoHelper.class); 060 061 @In(value = "org.jboss.seam.core.manager") 062 public transient Manager conversationManager; 063 064 @In(create = true, required = false) 065 transient NavigationContext navigationContext; 066 067 @In(create = true, required = false) 068 transient CoreSession documentManager; 069 070 private String lastLogoHolderKey = ""; 071 072 private DocumentModel lastLogoHolder; 073 074 private String lastURL = ""; 075 076 private String lastMainConversation = ""; 077 078 public String getLogoURL() { 079 if (navigationContext == null || navigationContext.getCurrentServerLocation() == null) { 080 lastLogoHolderKey = ""; 081 lastURL = ""; 082 return DEFAULT_LOGO; 083 } 084 085 DocumentModel ws = navigationContext.getCurrentWorkspace(); 086 087 return getLogoURL(ws); 088 } 089 090 public String getDefaultLogoURL() { 091 return DEFAULT_LOGO; 092 } 093 094 public String getLogoURLFromDocRef(String docRef) { 095 if (documentManager == null) { 096 return DEFAULT_LOGO; 097 } 098 DocumentRef ref = new IdRef(docRef); 099 try { 100 DocumentModel doc = documentManager.getDocument(ref); 101 return getLogoURL(doc); 102 } catch (DocumentNotFoundException e) { 103 log.error(e, e); 104 return DEFAULT_LOGO; 105 } 106 } 107 108 public String getLogoURL(DocumentModel doc) { 109 if (doc == null) { 110 return DEFAULT_LOGO; 111 } 112 113 String key = doc.getCacheKey(); 114 if (key.equals(lastLogoHolderKey)) { 115 return lastURL; 116 } 117 118 Blob blob = getBlob(doc); 119 if (blob == null) { 120 return DEFAULT_LOGO; 121 } 122 lastURL = PAGE_NAME + "?key=" + key + "&docRef=" + doc.getRef().toString() + '&' 123 + getConversationPropagationSuffix(); 124 lastLogoHolderKey = doc.getCacheKey(); 125 lastLogoHolder = doc; 126 127 return lastURL; 128 } 129 130 private static Blob getBlob(DocumentModel doc) { 131 if (doc == null) { 132 return null; 133 } 134 if (doc.hasSchema("file")) { 135 try { 136 return (Blob) doc.getProperty("file", "content"); 137 } catch (PropertyException e) { 138 return null; 139 } 140 } else { 141 return null; 142 } 143 } 144 145 @RequestParameter 146 String key; 147 148 @RequestParameter 149 String docRef; 150 151 public String getLogo() { 152 Blob imgBlob = null; 153 154 // returns cached blob 155 if (key != null && key.equals(lastLogoHolderKey)) { 156 imgBlob = getBlob(lastLogoHolder); 157 } 158 159 // recompute blob 160 if (imgBlob == null) { 161 DocumentModel ob = navigationContext.getCurrentWorkspace(); 162 if (ob == null || !ob.getRef().toString().equals(docRef)) { 163 DocumentRef ref = new IdRef(docRef); 164 try { 165 ob = documentManager.getDocument(ref); 166 } catch (DocumentNotFoundException e) { 167 log.error(e, e); 168 } 169 } 170 imgBlob = getBlob(ob); 171 } 172 173 FacesContext context = FacesContext.getCurrentInstance(); 174 HttpServletResponse response = (HttpServletResponse) context.getExternalContext().getResponse(); 175 try { 176 if (imgBlob == null 177 || (imgBlob.getMimeType() != null && !imgBlob.getMimeType().toLowerCase().startsWith("image"))) { 178 response.setContentType("image/gif"); 179 response.sendRedirect(context.getExternalContext().getRequestContextPath() + DEFAULT_LOGO); 180 return null; 181 } else { 182 response.addHeader("Cache-Control", "max-age=600"); 183 response.addHeader("Cache-Control", "public"); 184 response.setContentType(imgBlob.getMimeType()); 185 response.getOutputStream().write(imgBlob.getByteArray()); 186 response.getOutputStream().close(); 187 response.flushBuffer(); 188 context.responseComplete(); 189 } 190 191 } catch (IOException e) { 192 log.error("error while sending logo: ", e); 193 } 194 195 return null; 196 } 197 198 private String getLastOrMainConversationId(String cId) { 199 if (!cId.startsWith("0NX")) { 200 cId = lastMainConversation; 201 } 202 203 if (lastMainConversation == null || lastMainConversation.equals("")) { 204 cId = "0NXMAIN"; 205 } 206 return cId; 207 } 208 209 private String getConversationPropagationSuffix() { 210 String suffix = ""; 211 212 if (!conversationManager.getCurrentConversationEntry().isNested()) { 213 String cId = conversationManager.getCurrentConversationId(); 214 // tmp hack to handle the case when the logo is rendered 215 // just after the page existed the conversation 216 cId = getLastOrMainConversationId(cId); 217 suffix += conversationManager.getConversationIdParameter() + '=' + cId; 218 /** 219 * if (conversationManager.isLongRunningConversation()) { suffix += '&' + 220 * conversationManager.getConversationIsLongRunningParameter() + "true"; lastMainConversation = cId; } 221 **/ 222 } else { 223 ConversationEntry conv = conversationManager.getCurrentConversationEntry(); 224 String convId = conv.getConversationIdStack().get(0); 225 convId = getLastOrMainConversationId(convId); 226 suffix += conversationManager.getConversationIdParameter() + '=' + convId; 227 /** 228 * suffix += '&' + conversationManager.getConversationIsLongRunningParameter() + "true"; 229 * lastMainConversation = convId; 230 **/ 231 } 232 233 return suffix; 234 } 235 236}